如何获取给定目录名的树形哈希?

ssc*_*rth 7 git git-notes

我想在树对象上附上一个注释.但是,为了做到这一点,我首先需要知道树对象的哈希值.对于作为我的存储库一部分的给定目录名,如何获取其所属的树对象的哈希以便为其附加注释?

从阅读这个答案我知道我可以使用

git cat-file -p master^{tree}
Run Code Online (Sandbox Code Playgroud)

列出根树的内容,但我仍然需要grep目录名的输出,并递归地遵循嵌套的树对象来获取树对象的层次结构中较深层目录的哈希值.

基本上,我正在寻找虚构get-tree-hash.sh脚本的实现.如果被称为

get-tree-hash.sh path/to/directory/in/my/git/repo
Run Code Online (Sandbox Code Playgroud)

它应该输出

The hash for the "repo" tree inside "path/to/directory/in/my/git" is:
92a68a2f5560fa7080393b633e2afd1d5271deef
Run Code Online (Sandbox Code Playgroud)

mat*_*ore 8

你可以做:

git rev-parse HEAD:path/to/directory/in/my/git
Run Code Online (Sandbox Code Playgroud)

仅打印哈希.所以你不需要cutawk提取它.


ssc*_*rth 5

我自己弄清楚了,

git ls-tree HEAD -- path/to/directory/in/my/git | cut -d' ' -f3 | cut -f1
Run Code Online (Sandbox Code Playgroud)

做我想要的.

  • `... | awk'{print $ 3}'`有点不那么笨拙 (2认同)