Skip to content

Instantly share code, notes, and snippets.

@jdillick
Created January 17, 2013 16:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdillick/4557055 to your computer and use it in GitHub Desktop.
Save jdillick/4557055 to your computer and use it in GitHub Desktop.
Found this little gem of python for comparing a git repo to an un-versioned copy of the code. I returns the closest commit hash from the git repository corresponding to your code. This works great when you don't know what version of the code you have running.
#!/usr/bin/env python
import subprocess
import shlex
import sys
import os
import operator
gitdir,workdir=map(os.path.realpath,sys.argv[1:3])
os.chdir(gitdir)
proc=subprocess.Popen(shlex.split('git rev-list --all'),stdout=subprocess.PIPE)
shas,err=proc.communicate()
shas=shas.split()
head=shas[0]
data={}
for sha1 in shas:
subprocess.Popen(shlex.split('git checkout {s}'.format(s=sha1)),
stderr=open('/dev/null')).wait()
proc=subprocess.Popen(shlex.split('diff {g} {w}'.format(g=gitdir,w=workdir)),
stdout=subprocess.PIPE)
out,err=proc.communicate()
distance=len(out)
data[sha1]=distance
answer=min(data.items(),key=operator.itemgetter(1))[0]
print('closest match: {s}'.format(s=answer))
subprocess.Popen(shlex.split('git checkout {h}'.format(h=head)),
stderr=open('/dev/null')).wait()
@jdillick
Copy link
Author

To use:
find_closest_sha1.py gitdir working-unversioned-dir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment