Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find commit with the smallest diff

Tags:

git

I got sent a bunch of files originating from the same git repo I work with, but they were developed against an older commit. How to find out which commit they used? Something like least lines of diff.

like image 451
Reactormonk Avatar asked Dec 05 '12 19:12

Reactormonk


1 Answers

How to find out which commit they used? Something like least lines of diff.

Well, you can do exactly that: find the commit with the smallest diff against your target directory. Just run a loop over all the commits in your repository, and for each one compute a diff against your target directory, and remember the one with the smallest diff.

Let's assume you have your repository in ./repo and the files in question in ./target.

#!/bin/sh

cd repo
HEAD=$(git rev-parse HEAD)
git log --pretty='%H' | while read rev; do
    git checkout -q $rev
    lines=$(diff -ruN -x .git . ../target | wc -l)

    if ! [[ "$minlines" ]] || [[ $lines -lt $minlines ]]; then
        minlines=$lines
        commit=$rev
        echo "$lines $rev"
    fi

    [[ $lines -eq 0 ]] && break
done | tail -1

git checkout $HEAD

This will take a while for a repository with a long history, but it works. It will print out the size of the diff followed by the commit id for the commit with the smallest diff.

If you interrupt this script while it's running you'll need to check out an appropriate branch head (e.g., git checkout master).

like image 102
larsks Avatar answered Sep 20 '22 15:09

larsks