0

So, I have a set of files copied from a git repository a long time ago. Unfortunately no metadata exists on those files and also .git directory was not copied back then. Therefore we don't know which commit of the said repository contained the versions of those files that we have.

I need to find an automated way to compare the files I have with the state of the files on each commit in the history and shows me the set of commits that my files match.

sorush-r
  • 10,490
  • 17
  • 89
  • 173
  • https://stackoverflow.com/a/49059971/7976758 , https://stackoverflow.com/a/73474235/7976758 – phd Jul 13 '23 at 12:59

1 Answers1

1

You mean like.... exact same content? You can get the object ID of one file you want to track down... then you can go through history of all commits and check each one with git ls-tree -r <some-commit-id> | grep the-object-id-i-am-looking-for. The ones that show up, well, you have the commit and the file name in that commit.

So... something like:

cat <some-file> | git hash-object --stdin

That should give you the object ID you are looking for. Let's call it THE_ID.

THE_ID=$( cat <some-file> | git hash-object --stdin )
git log --pretty=%h --all | while read commit; do
    git ls-tree -r $commit | grep $THE_ID > /dev/null || continue
    # if we land here, then there was something that matched the object id in the commit
    echo commit $commit
    git ls-tree -r $commit | grep $THE_ID # yeah, twice the same thing... perhaps there's a simpler way to do it... but this whould work
    echo # an empty line
done
eftshift0
  • 26,375
  • 3
  • 36
  • 60