Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How can I find a commit that most closely matches a directory?

Someone took a version (unknown to me) of Moodle, applied many changes within a directory, and released it (tree here).

How can I determine which commit of the original project was most likely edited to form this tree?

this would allow me to form a branch at the appropriate commit with this patch. Surely it came from either the 1.8 or 1.9 branches, probably from a release tag, but diffing between particular commits doesn't help me much.

Postmortem Update: knittl's answer got me as close as I'm going to get. I first added my patch repo as the remote "foreign" (no commits in common, that's OK), then did diffs in loops with a couple format options. The first used the --shortstat format:

for REV in $(git rev-list v1.9.0^..v1.9.5); do      git diff --shortstat "$REV" f7f7ad53c8839b8ea4e7 -- mod/assignment >> ~/rdiffs.txt;      echo "$REV" >> ~/rdiffs.txt;  done; 

The second just counted the line changes in a unified diff with no context:

for REV in $(git rev-list v1.9.0^..v1.9.5); do      git diff -U0 "$REV" f7f7ad53c8839b8ea4e7 -- mod/assignment | wc -l >> ~/rdiffs2.txt;     echo "$REV" >> ~/rdiffs2.txt;  done; 

There were thousands of commits to dig through, but this one seems to be the closest match.

like image 954
Steve Clay Avatar asked Jun 17 '11 15:06

Steve Clay


People also ask

How do I find a specific commit in git?

If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .

How can I see the diff of a commit?

To see the diff for a particular COMMIT hash, where COMMIT is the hash of the commit: git diff COMMIT~ COMMIT will show you the difference between that COMMIT 's ancestor and the COMMIT .

How do you get details of a commit?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.

How can I see the code of a previous commit?

All that you have to do is go on to the file that you committed on and go to the history for it, then select the earliest commit with the <> icon to view the code at that time.


1 Answers

you could write a script, which diffs the given tree against a revision range in your repository.

assume we first fetch the changed tree (without history) into our own repository:

git remote add foreign git://… git fetch foreign 

we then output the diffstat (in short form) for each revision we want to match against:

for REV in $(git rev-list 1.8^..1.9); do    git diff --shortstat foreign/master $REV; done 

look for the commit with the smallest amount of changes (or use some sorting mechanism)

like image 62
knittl Avatar answered Sep 20 '22 21:09

knittl