首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >哪个提交有这个blob?

哪个提交有这个blob?
EN

Stack Overflow用户
提问于 2008-10-21 22:00:45
回答 5查看 53.4K关注 0票数 172

给定blob的散列,有没有办法获得树中包含此blob的提交列表?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2008-10-21 23:11:23

以下两个脚本都将blob的SHA1作为第一个参数,然后可选地将blob可以理解的任何参数作为第一个参数。例如,使用--all在所有分支中搜索,而不仅仅是在当前分支中搜索,或者使用-g在reflog中搜索,或者其他任何你喜欢的。

这是一个shell脚本-简短而甜蜜,但速度很慢:

代码语言:javascript
复制
#!/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的一个优化版本,仍然很短,但速度快得多:

代码语言:javascript
复制
#!/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 );
}
票数 114
EN

Stack Overflow用户

发布于 2015-09-16 22:32:14

不幸的是,脚本对我来说有点慢,所以我不得不优化一下。幸运的是,我不仅有散列,还有文件的路径。

代码语言:javascript
复制
git log --all --pretty=format:%H -- <path> | xargs -I% sh -c "git ls-tree % -- <path> | grep -q <hash> && echo %"
票数 28
EN

Stack Overflow用户

发布于 2021-03-17 03:55:49

对于人类来说,最有用的命令可能是

代码语言:javascript
复制
git whatchanged --all --find-object=<blob hash>

这将跨--all分支显示任何使用该散列添加或删除文件的提交,以及路径是什么。

代码语言:javascript
复制
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散列。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/223678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档