Skip to content

Instantly share code, notes, and snippets.

@schwern
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schwern/0b3cb8769124407f7a48 to your computer and use it in GitHub Desktop.
Save schwern/0b3cb8769124407f7a48 to your computer and use it in GitHub Desktop.
Find the nth unpushed reference in the current branch.
#!/usr/bin/env perl
use strict;
use warnings;
sub current_branch {
my $branch=`git symbolic-ref --short HEAD`;
chomp $branch;
die "Cannot figure out the current branch.\n" unless $branch;
return $branch;
}
sub tracking_branch {
my $branch = shift;
my $remote=`git config branch.$branch.remote`;
chomp $remote;
die "$branch is not tracking a remote.\n" unless $remote;
my $remote_merge=`git config branch.$branch.merge | xargs git name-rev --name-only`;
chomp $remote_merge;
die "$branch has a remote ($remote) but is not set up to merge.\n" unless $remote_merge;
return "$remote/$remote_merge";
}
my $number = shift;
my $remote_branch = tracking_branch(current_branch());
system "git log --pretty=format:%H $remote_branch..HEAD | tail -n$number | head -n1";
print "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment