繁星点点滴滴
以下两个脚本都将BLOB的SHA 1作为第一个参数,在此之后,可以选择以下任何参数git log会理解的。例如:--all在所有分支中搜索,而不是仅搜索当前分支,或-g在翻车里搜索,或者其他你想要的东西。在这里,它是一个shell脚本-短而甜蜜,但缓慢:#!/bin/shobj_name="$1"shift
git log "$@" --pretty=format:'%T %h %s' \| while read tree commit subject ; do
if git ls-tree -r $tree | grep -q "$obj_name" ; then
echo $commit "$subject"
fidone还有一个优化的Perl版本,它仍然很短,但速度要快得多:#!/usr/bin/perluse 5.008;use strict;use Memoize;my $obj_name;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';die "usage: git-find-blob <blob> [<git-log arguments ...>]\n"
if not @ARGV;my $obj_short = shift @ARGV;$obj_name = do {
local $ENV{'OBJ_NAME'} = $obj_short;
`git rev-parse --verify \$OBJ_NAME`;} or die "Couldn't parse $obj_short: $!\n";chomp $obj_name;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 );}