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

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

问题描述

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

解决方案

以下两个脚本都将blob的SHA1作为第一个参数,并且在它之后(可选)包含 git log 会理解。例如。 - 全部搜索所有分支,而不仅仅是当前的分支,或 -g 在reflog中搜索,或者其他你喜欢的东西。



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

 #!/ bin / sh 
obj_name =$ 1
shift
git log$ @--pretty = format:'%T%h%s'\
|同时读取树提交主题;如果git ls-tree -r $ tree |做
grep -q$ obj_name;然后
echo $ commit$ subject
fi
done

在Perl中的优化版本,仍然很短但速度更快:


$ b

 <!c $ c>#!/ usr / bin / perl 
使用5.008;
使用strict;
使用Memoize;

my $ obj_name;

sub check_tree {
my($ tree)= @_;
my @subtree;

{
打开我的$ ls_tree,' - |',git => 'ls-tree'=> $ tree
或死无法打开管道到git-ls-tree:$!\\\
; ($ lt; $ ls_tree>){
/ \ A [0-7] {6}(\ S +)(\ S +)/
或者死亡的

意外的git-ls-tree输出;
返回1如果$ 2 eq $ obj_name;
推送@subtree,$ 2如果$ 1 eq'tree';
}
}

check_tree($ _)&&为@subtree返回1;

return;
}

记住'check_tree';
$ b $ die用法:git-find-blob< blob> [< git-log arguments ...>] \\\

如果不是@ARGV;

my $ obj_short = shift @ARGV;
$ obj_name = do {
local $ ENV {'OBJ_NAME'} = $ obj_short;
`git rev-parse --verify \ $ OBJ_NAME`;
}或死无法解析$ obj_short:$!\\\
;
chomp $ obj_name;

打开我的$ log,' - |',git => log => @ARGV,'--pretty = format:%T%h%s'
或死无法打开管道到git-log:$!\\\
; $(< $ log>){
chomp;

;
my($ tree,$ commit,$ subject)= split,$ _,3;
打印$ commit $ subject\\\
if check_tree($ tree);
}


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

解决方案

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.

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

#!/bin/sh
obj_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"
    fi
done

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: $!\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 );
}

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

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