citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

git-find-blob (1106B)


      1 #!/usr/bin/env perl
      2 use 5.008;
      3 use strict;
      4 use Memoize;
      5 
      6 my $obj_name;
      7 
      8 sub check_tree {
      9     my ( $tree ) = @_;
     10     my @subtree;
     11 
     12     {
     13         open my $ls_tree, '-|', git => 'ls-tree' => $tree
     14             or die "Couldn't open pipe to git-ls-tree: $!\n";
     15 
     16         while ( <$ls_tree> ) {
     17             /\A[0-7]{6} (\S+) (\S+)/
     18                 or die "unexpected git-ls-tree output";
     19             return 1 if $2 eq $obj_name;
     20             push @subtree, $2 if $1 eq 'tree';
     21         }
     22     }
     23 
     24     check_tree( $_ ) && return 1 for @subtree;
     25 
     26     return;
     27 }
     28 
     29 memoize 'check_tree';
     30 
     31 die "usage: git-find-blob <blob> [<git-log arguments ...>]\n"
     32     if not @ARGV;
     33 
     34 my $obj_short = shift @ARGV;
     35 $obj_name = do {
     36     local $ENV{'OBJ_NAME'} = $obj_short;
     37      `git rev-parse --verify \$OBJ_NAME`;
     38 } or die "Couldn't parse $obj_short: $!\n";
     39 chomp $obj_name;
     40 
     41 open my $log, '-|', git => log => @ARGV, '--pretty=format:%T %h %s'
     42     or die "Couldn't open pipe to git-log: $!\n";
     43 
     44 while ( <$log> ) {
     45     chomp;
     46     my ( $tree, $commit, $subject ) = split " ", $_, 3;
     47     print "$commit $subject\n" if check_tree( $tree );
     48 }