通过git存储库中对应的blob哈希查找文件? [英] Finding a file by its corresponding blob's hash in a git repository?

查看:231
本文介绍了通过git存储库中对应的blob哈希查找文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个文件可能已经在git存储库中,并且即使在同一提交中,它也可能位于多个路径名下.

Suppose I have a file that may already be in a git repository, and it might reside under multiple pathnames, even in the same commit.

如何查找所有包含带有与该文件对应的哈希值的blob的提交,并列出这些提交以及每个文件所在的路径名?

How do I find all commits containing a blob with a hash corresponding to that file, and list those commits along with the pathname(s) under which the file resides in each?

是否有一种按哈希查找文件的技术还可以搜索索引和工作目录?

Is there a find-file-by-hash technique that also searches the index and working directory?

推荐答案

确定,以展开接受的答案&hellip ;

OK, to expand on the accepted answer…

关于查找所有带有路径名的提交,那么被接受的答案中的脚本唯一不为您做的就是打印路径名.但是请不要担心-修改起来很容易.

As to finding all commits with pathnames, then the only thing the script in the accepted answer does not do for you is printing the pathname. But fear not—it's easy to modify.

如果转到附近的Git存储库并运行git ls-tree -r HEAD,您将看到此命令转储具有SHA-1名称和普通"文件名.答案中的脚本只是在grep此输出中查找SHA-1名称,而忽略其余名称.

If you go to a nearby Git repository and run git ls-tree -r HEAD you'll see that this command dumps the whole tree hierarchy referenced by the named commit (HEAD in our case)—with both SHA-1 names and "normal" filenames. The script from the answer just greps this output to find the SHA-1 name and ignores the rest.

因此我们可以将其修改为:

So we can modify it to read:

#!/bin/sh
obj_name="$1"
shift
git log "$@" --pretty=format:'%T %h %s' \
| while read tree commit subject ; do
    git ls-tree -r "$commit" | while read _ _ sha name; do \
      if [ "$sha" == "$obj_name" ]; then
        echo "$sha\t$name"
        break
      fi
    done
  done

…并且它现在还将打印与目标Blob相关联的文件的名称以及提交名称.

…and it will now also print the name of the file associaled with the target blob along with the commit name.

这篇关于通过git存储库中对应的blob哈希查找文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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