gitgit-notes

How to get the tree hash for a given directory name?


I'd like to attach a note to a tree object. However, in order to do so I first need to know the tree object's hash. For a given directory name that's part of my repository, how do I get its belonging tree object's hash in order to attach a note to it?

From reading this answer I understand I could use

git cat-file -p master^{tree}

to list the root tree's contents, but I'd still have to grep the output for the directory name, and follow nested tree objects recursively to get the tree object's hash for a directory deeper in the hierarchy.

Basically, I'm looking for the implementation of a fictional get-tree-hash.sh script. If called like

get-tree-hash.sh path/to/directory/in/my/git/repo

it should output

The hash for the "repo" tree inside "path/to/directory/in/my/git" is:
92a68a2f5560fa7080393b633e2afd1d5271deef

Solution

  • You can do:

    git rev-parse HEAD:path/to/directory/in/my/git
    

    To print only the hash. So you don't need cut or awk to extract it.