Created
June 6, 2010 18:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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,