哪个提交有这个 blob? [英] Which commit has this blob?

查看:30
本文介绍了哪个提交有这个 blob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个 blob 的哈希值,有没有办法获取在其树中包含该 blob 的提交列表?

Given the hash of a blob, is there a way to get a list of commits that have this blob in their tree?

推荐答案

以下两个脚本都将 blob 的 SHA1 作为第一个参数,在它之后,可选地,任何git log 就会明白.例如.--all 搜索所有分支而不是当前分支,或者 -g 搜索 reflog,或者任何你喜欢的东西.

Both of the following scripts take the blob’s SHA1 as the first argument, and after it, optionally, any arguments that git log will understand. E.g. --all to search in all branches instead of just the current one, or -g to search in the reflog, or whatever else you fancy.

这里是一个 shell 脚本——短小精悍,但速度很慢:

Here it is as a shell script – short and sweet, but slow:

#!/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 的优化版本,仍然很短,但速度要快得多:

And an optimised version in Perl, still quite short but much faster:

#!/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: $!
";

        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 ...>]
"
    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: $!
";
chomp $obj_name;

open my $log, '-|', git => log => @ARGV, '--pretty=format:%T %h %s'
    or die "Couldn't open pipe to git-log: $!
";

while ( <$log> ) {
    chomp;
    my ( $tree, $commit, $subject ) = split " ", $_, 3;
    print "$commit $subject
" if check_tree( $tree );
}

这篇关于哪个提交有这个 blob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆