Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding first commit of a file in git

Tags:

git

The situation: I have a tar.gz of a release from a github project but would like to work out which commit this was taken from. It doesn't appear to have been tagged or is it obvious from commit message themselves.

So I can calculate sha1 of the files, but would like to work out which commit these belong to?

Calling git wizards!

like image 239
bee Avatar asked Feb 10 '12 17:02

bee


1 Answers

Since the git-stored hash doesn't just include the file contents (and, in theory, hash collisions happen anyhow), in order to be really sure you've got the right version of the file you need to compare the contents.

for rev in $(git log --format=%H -- /path/to/file); do
   git diff --quiet $x:/path/to/file my-current-file;
   if [[ $? -eq 0 ]]; then
      echo $x;
   fi
done

In English: iterate over the revisions that changed the file, in reverse order. For each such revision, diff the version of the file there with the outside-the-tree file. If the two files are identical, print the revision hash.

If you want to do this for the whole tarball, you can do the same but diff the whole tree instead of a single file (and omit the file path as an argument to git log) - use whatever tolerant diff options you like.

like image 74
Borealid Avatar answered Oct 02 '22 18:10

Borealid