Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get SVN diff and summarise added/removed/changed function definitions
#!/bin/env python
"""get svn diff and compare added functions to removed functions,
command line args passed through to svn diff, e.g. use -r nnn:nnn to filter a specific revision."""
import sys, subprocess, re
from collections import defaultdict
svn_diff = subprocess.Popen(['svn', 'diff'] + sys.argv[1:], stdout=subprocess.PIPE).communicate()[0]
defn1 = "\W((?:def|class)\s+\w+)[:\(]" # python def and class
defn2 = "\W(function(?:\s+\w+)?)\(" # javascript function
is_defn = re.compile('(?:%s)|(?:%s)' % (defn1, defn2))
new, old = defaultdict(list), defaultdict(list)
for line in svn_diff.split("\n"):
if line[:3] in ('---', '+++'):
continue
for defn in is_defn.findall(line):
defn = defn[0] or defn[1] # two groupings in the pattern, pick whichever one matched
if line[0] == '-':
old[defn].append(line[1:].strip())
if line[0] == '+':
new[defn].append(line[1:].strip())
added, removed, changed = [], [], []
for defn in new.keys():
if defn in old:
# convert to a single string so they stay together when sorted
changes = "".join(["\t%s\n"%line for line in old[defn]]
+ ["\t %s\n"%(line.replace(defn, '-'*(len(defn)-3)+'> ')) for line in new[defn]])
changed.append(changes[1:-1]) # strip off initial tab & trailing new line
else:
added.extend(new[defn])
for defn in old.keys():
if not defn in new:
removed.extend(old[defn])
for title, lines in (("Added:", added), ("Changed:", changed), ("Removed:", removed)):
if lines:
print title
print "".join('\t%s\n'%line for line in sorted(lines))
else:
print title, 'none.'
@faxiubite

This comment has been minimized.

Copy link

@faxiubite faxiubite commented Feb 7, 2014

Could you please add some pictures, so we can get the code clearly and easily,
I want get the function like "beyond compare", when I input the two path, I can get the diff code like beyond compare,
Could you please give me some suggestion?
thank you,

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