Created
January 17, 2013 16:06
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use:
find_closest_sha1.py gitdir working-unversioned-dir