Home > Blockchain >  Given a blob hash, can I find the tree(s) which point to it?
Given a blob hash, can I find the tree(s) which point to it?

Time:09-13

XY problem:

I ran git show <filepath> to see the current version of a file. But I'm a bit confused by the output:

commit <abc1234...> (HEAD -> <branch>)
Author: <Fname Lname> <[email protected]>
Date:   <Date>

    <Commit message>

diff --git a/<filepath> b/<filepath>
index bcd4567..dedbeef 100644
--- a/<filepath>
    b/<filepath>

It's telling me that git show is showing me a diff of something, between bcd4567 and dedbeef. I want to know what these hashes are referring to. I know that I can use git cat-file -t <hash> to see the type of object these hashes are referring to (they're blobs) and I can use git cat-file -p <hash> to see the object themselves. So I know what file they are referring to. What I don't know is which version of the file they're referring to.

So there are two questions here. The direct question is:

  1. What does the line index bcd4567..dedbeef 100644 refer to in the git show output? What versions of a file is it showing me? Is it showing the difference between the work tree and the index, or something else?

And more generally:

  1. Given a situation where I have an unknown hash and want to know where it comes from, how can I discover this? Is there a command that will allow me to find e.g. all tree objects which point to a given hash, or to discover all the names (branch, tag, etc.) that a hash has been given?

CodePudding user response:

git show <filepath> is equivalent to git show HEAD <filepath>. It prints the changes between filepath of HEAD's parent and filepath of HEAD.

In index bcd4567..dedbeef 100644, bcd4567 is the blob hash of filepath in HEAD's parent and dedbeef is the blob hash of filepath in HEAD. You could add --full-index to show full hash.

If you know a blob hash, you could try git log --all --find-object=<blobhash> to find which commits introduce or remove(not sure) the blob.

To find all the tree objects that reference the blob, you could first list all the tree objects,

git cat-file --batch-all-objects --batch-check | grep tree

and then inspect them one by one to find out which references the blob directly.

git ls-tree $treehash | grep $blobhash

CodePudding user response:

Given the way you have written the git show command, without providing anything else other than the path of the file, then the 2 commits involved are HEAD~ and HEAD. So if you check the object IDs for that file in each revision (with git ls-tree, for example) you should see that one commit has bcd4567 and the other dedbeef.

  • Related