0

I have the SHA-1 object hash of a blob object in a (local) git repository. I need to find all uses in the form of commit-id, pathname and filename, e.g. generating all possible git show commands that would print the contents my blob:

git show <commit-id1>:foo/bar/baz.txt
git show <commit-id1>:README.txt
git show <commit-id2>:foo/quux.txt
1
  • 1
    I think in order to get this information, you need to loop through all commits, and then (recursively) through all trees and check if a blob id matches your hash. I don’t think there is any other way due to how the Git object tree is built.
    – poke
    Commented Oct 19, 2015 at 10:46

1 Answer 1

1

I can use git log to find all commits I care about, and then use git ls-tree -r to find all the blobs in a commit, and then use Perl to keep only the blobs I'm interested in:

for COMMITID in `git log --pretty=format:%H`; do
  git ls-tree -r "$COMMITID" | perl -we '
      use integer; use strict; my $commitid = $ARGV[0]; my $f;
      die if !open($f, "<", "blobid.lst");
      my %h = map { s@\s.*@@s; $_ ? ($_=>1) : () } <$f>;
      while (<STDIN>) {
        die "bad: $_\n" if !s@^\S+\sblob\s([0-9a-f]{40})\t@@;
        my $blobid=$1;
        chomp;
        print "git show \x27$commitid:$_\x27\n" if $h{$blobid} }' -- "$COMMITID"
done

The list of the blob IDs I'm interested in are stored in the file blobid.lst, one ID per line.

Not the answer you're looking for? Browse other questions tagged or ask your own question.