Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-notes

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
like image 748
sschuberth Avatar asked Mar 23 '16 14:03

sschuberth


2 Answers

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.

like image 114
matvore Avatar answered Nov 12 '22 21:11

matvore


Just figured it out myself,

git ls-tree HEAD -- path/to/directory/in/my/git | cut -d' ' -f3 | cut -f1

does what I want.

like image 6
sschuberth Avatar answered Nov 12 '22 19:11

sschuberth