public
Last active

git-find-blob: pass a blob SHA1 and find commits which contain it

  • Download Gist
git-find-blob.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#!/usr/bin/perl
use 5.008;
use strict;
use Memoize;
 
# by Aristotle Pagaltzis <http://stackoverflow.com/users/9410/aristotle-pagaltzis>
# taken from thread http://stackoverflow.com/questions/223678/git-which-commit-has-this-blob
# on 6 june 2010
 
my $usage =
"usage: git-find-blob <blob> [<git-log arguments ...>]
 
pass the blob SHA1 as the first parameter
and then any number of arguments to git log
 
";
die $usage unless @ARGV;
 
my $obj_name = shift;
 
sub check_tree {
my ( $tree ) = @_;
my @subtree;
 
{
open my $ls_tree, '-|', git => 'ls-tree' => $tree
or die "Couldn't open pipe to git-ls-tree: $!\n";
 
while ( <$ls_tree> ) {
/\A[0-7]{6} (\S+) (\S+)/
or die "unexpected git-ls-tree output";
return 1 if $2 eq $obj_name;
push @subtree, $2 if $1 eq 'tree';
}
}
 
check_tree( $_ ) && return 1 for @subtree;
 
return;
}
 
memoize 'check_tree';
 
open my $log, '-|', git => log => @ARGV, '--pretty=format:%T %h %s'
or die "Couldn't open pipe to git-log: $!\n";
 
while ( <$log> ) {
chomp;
my ( $tree, $commit, $subject ) = split " ", $_, 3;
print "$commit $subject\n" if check_tree( $tree );
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.