Skip to content

Instantly share code, notes, and snippets.

@guyromm
Created September 26, 2012 07:06
Show Gist options
  • Select an option

  • Save guyromm/3786520 to your computer and use it in GitHub Desktop.

Select an option

Save guyromm/3786520 to your computer and use it in GitHub Desktop.
git helper for diffs between revisions that have files shuffled between folders (not through git mv)
#!/usr/bin/python
# this script tries to guess file moves between revisions according to matches in the files basename.
# i.e - if the file was moved between folders but did not change its name - it'l pop up in our matches
# if there are multiple files changing location with the same basename false positives will appear.
import re,os,json,sys,commands
branchfrom = sys.argv[1] #'staging'
branchto = sys.argv[2] #'maxim.d-435'
def incr(lst,it):
if it not in lst: lst[it]=0
lst[it]+=1
print '########## GONE: ##########'
st,goneraw = commands.getstatusoutput('git diff %s..%s | grep -B1 \'+++ /dev/null\''%(branchfrom,branchto)) ; assert st==0
#goneraw = open('gone.txt','r').read()
gonelst = [it.split('\n')[0].replace('--- a/','') for it in goneraw.split('--\n')]
agg_gone = {}
for it in gonelst: incr(agg_gone,os.path.basename(it))
print gonelst
print '########## GONE BN: ##########'
print json.dumps(agg_gone,indent=True)
print '########## NEW: ##########'
#goneraw = open('appeared.txt','r').read()
st,goneraw = commands.getstatusoutput('git diff %s..%s | grep -A1 \'\-\-\- /dev/null\''%(branchfrom,branchto)) ; assert st==0
newlst = [it.split('\n')[1].replace('+++ b/','') for it in goneraw.split('--\n')]
agg_new = {}
for it in newlst: incr(agg_new,os.path.basename(it))
print newlst
print '########## NEW BN: ##########'
print json.dumps(agg_new,indent=True)
print '########## NEW NEW: ##########'
print set(newlst)-set(gonelst)
print '########## GONE GONE: ##########'
print set(gonelst)-set(newlst)
print '########## NEW NEW BN: ##########'
print set(agg_new) - set(agg_gone)
print '########## GONE GONE BN: ##########'
print set(agg_gone) - set(agg_new)
print '########## MOVE SUGGESTIONS: ##########'
for rawnewfn in newlst:
for rawgonefn in gonelst:
if os.path.basename(rawgonefn)==os.path.basename(rawnewfn):
#print rawgonefn + ' => '+rawnewfn
print 'git difftool -t meld -y %s:%s %s:%s'%(branchfrom,rawgonefn,branchto,rawnewfn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment