Skip to content

Instantly share code, notes, and snippets.

@DaveMessina
Created June 6, 2010 18:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save DaveMessina/427769 to your computer and use it in GitHub Desktop.
Save DaveMessina/427769 to your computer and use it in GitHub Desktop.
git-find-blob: pass a blob SHA1 and find commits which contain it
#!/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 );
}
@melo
Copy link

melo commented Mar 12, 2018

Hi @DaveMessina,

in case you need it, I updated this script to print the filename that uses the blob you are looking for.

You can find my version here: https://gist.github.com/melo/7cb46d7b4f80983a3fc5ef211c26a193

Bye,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment