【问题标题】:How to find all uses of a blob in a git repo如何在 git repo 中查找 blob 的所有用途
【发布时间】:2015-10-19 10:17:26
【问题描述】:

我在(本地)git 存储库中有一个 blob 对象的 SHA-1 对象哈希。我需要以提交 ID、路径名和文件名的形式查找所有用途,例如生成所有可能的 git show 命令来打印我的 blob 的内容:

git show <commit-id1>:foo/bar/baz.txt
git show <commit-id1>:README.txt
git show <commit-id2>:foo/quux.txt

【问题讨论】:

  • 我认为为了获取这些信息,您需要遍历所有提交,然后(递归地)遍历所有树并检查 blob id 是否与您的哈希匹配。由于 Git 对象树的构建方式,我认为没有其他方法。

标签: git


【解决方案1】:

我可以使用git log 查找我关心的所有提交,然后使用git ls-tree -r 查找提交中的所有 blob,然后使用 Perl 仅保留我感兴趣的 blob:

for COMMITID in `git log --pretty=format:%H`; do
  git ls-tree -r "$COMMITID" | perl -we '
      use integer; use strict; my $commitid = $ARGV[0]; my $f;
      die if !open($f, "<", "blobid.lst");
      my %h = map { s@\s.*@@s; $_ ? ($_=>1) : () } <$f>;
      while (<STDIN>) {
        die "bad: $_\n" if !s@^\S+\sblob\s([0-9a-f]{40})\t@@;
        my $blobid=$1;
        chomp;
        print "git show \x27$commitid:$_\x27\n" if $h{$blobid} }' -- "$COMMITID"
done

我感兴趣的 blob ID 列表存储在文件blobid.lst 中,每行一个 ID。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 2013-09-16
    • 2020-05-21
    • 2017-05-07
    • 2018-11-12
    • 1970-01-01
    相关资源
    最近更新 更多