给定blob的散列,有没有办法获得树中包含此blob的提交列表?
发布于 2008-10-21 23:11:23
以下两个脚本都将blob的SHA1作为第一个参数,然后可选地将blob可以理解的任何参数作为第一个参数。例如,使用--all
在所有分支中搜索,而不仅仅是在当前分支中搜索,或者使用-g
在reflog中搜索,或者其他任何你喜欢的。
这是一个shell脚本-简短而甜蜜,但速度很慢:
#!/bin/sh
obj_name="$1"
shift
git log "$@" --pretty=tformat:'%T %h %s' \
| while read tree commit subject ; do
if git ls-tree -r $tree | grep -q "$obj_name" ; then
echo $commit "$subject"
fi
done
Perl的一个优化版本,仍然很短,但速度快得多:
#!/usr/bin/perl
use 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 );
}
发布于 2015-09-16 22:32:14
不幸的是,脚本对我来说有点慢,所以我不得不优化一下。幸运的是,我不仅有散列,还有文件的路径。
git log --all --pretty=format:%H -- <path> | xargs -I% sh -c "git ls-tree % -- <path> | grep -q <hash> && echo %"
发布于 2021-03-17 03:55:49
对于人类来说,最有用的命令可能是
git whatchanged --all --find-object=<blob hash>
这将跨--all
分支显示任何使用该散列添加或删除文件的提交,以及路径是什么。
git$ git whatchanged --all --find-object=b3bb59f06644
commit 8ef93124645f89c45c9ec3edd3b268b38154061a
⋮
diff: do not show submodule with untracked files as "-dirty"
⋮
:100644 100644 b3bb59f06644 8f6227c993a5 M submodule.c
commit 7091499bc0a9bccd81a1c864de7b5f87a366480e
⋮
Revert "submodules: fix of regression on fetching of non-init subsub-repo"
⋮
:100644 100644 eef5204e641e b3bb59f06644 M submodule.c
请注意,git whatchanged
已经在其输出行中包含了前后的blob散列。
https://stackoverflow.com/questions/223678
复制相似问题