3

Given the hash of a blob object (that may have been taken in a former revision), how is it possible to find

  • the history and
  • the path of the corresponding file in the current revision?

Some details as for the use case:

We use a git repository to store files that are part of document generation.

The files change over time (hence the repo ;) ). The documents are kept and refer to the files in the repo.

For consistency, the saved documents will reference the file version at the timepoint where the document was created.

So I thought keeping the object hash should be adequate, as this precisely identifies which file version was used.

However, I now face following problem : I want to propose to upgrade the document to newer versions of the files. So I have to

  • find out whether there are new versions
  • because the document generation occurs with command line tools, I need the current location of the file in the filesystem (may have been renamed or moved).

I still couldn't find any easy way to achieve this. Is there any, or should I reconsider my way of keeping track of the composited files?

1 Answer 1

2

To get the history based on an object hash you can you this bash script:
Pass in the hash as the command line arg, and you will get the commit sha.

#!/bin/sh

blob=$1

git rev-list --all |
while read commit; do
    if git ls-tree -r $commit | grep -q $blob; then
        echo $commit
    fi
done

When you have the commit sha, you can run

git log [sha] --name-only

This will give u the file name. And finally:

git log --follow -- [filename]

This will only work if the current version file path is the same as it was at the time of the snapshot. But will show all historical file changes (assuming git mv was used )

If this is not the case, it can still be done by using git ls-tree to walk all commits, and if a tree node is reached traversing it to its file leaf nodes. However, you will then manually need to figure out the issues between path A) a/b/foo.txt and path B) c/d/foo.txt

Is it the same foo.txt or different?

In my experience it is best to have a good file naming convention and storing the commit sha not the file blob sha. It is much easier to get to a file blob sha from a commit then the other way.

Sign up to request clarification or add additional context in comments.

1 Comment

I expanded on your solution here and made a script that can find the commit that contains a given file: gist.github.com/themattman/20ec6da84304740972e057c22b15c0ee

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.