-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgit-find-blob
executable file
·68 lines (49 loc) · 1.62 KB
/
git-find-blob
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/perl
# From http://stackoverflow.com/questions/223678/git-which-commit-has-this-blob
use strict;
use warnings;
use Memoize;
sub usage {
warn @_, "\n" if @_;
(my $ME = $0) =~ s,.*/,,;
die <<EOUSAGE;
Usage: $ME <blob> [<git-log arguments ...>]
Options:
-h, --help Show this help
Finds which commits have the given blob.
EOUSAGE
}
sub check_tree {
my ($tree, $obj_name) = @_;
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($_, $obj_name) && return 1 for @subtree;
return;
}
memoize 'check_tree';
usage if not @ARGV or $ARGV[0] eq '-h' or $ARGV[0] eq '--help';
my $blob_arg = shift;
open my $rev_parse, '-|', git => 'rev-parse' => '--verify', $blob_arg
or die "Couldn't open pipe to git-rev-parse: $!\n";
chomp(my $obj_name = <$rev_parse> || '');
die "git rev-parse --verify $blob_arg failed\n"
unless close($rev_parse);
die "git rev-parse returned unexpected output: $obj_name\n"
unless $obj_name =~ /^[0-9a-f]{40}$/;
print "(full blob is $obj_name)\n" if $obj_name ne $blob_arg;
open my $log, '-|', 'git', 'log', '--all', @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, $obj_name);
}