Skip to content

Instantly share code, notes, and snippets.

@trash-80
Forked from Ronnie76er/git-create-review.py
Created December 19, 2013 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trash-80/8042276 to your computer and use it in GitHub Desktop.
Save trash-80/8042276 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#
# git-create-review.py
# based on script created here: http://mojodna.net/2009/02/24/my-work-git-workflow.html
# especially for the command to turn a git diff into a svn diff
import subprocess
from subprocess import Popen, PIPE, STDOUT
import sys
findRevCmd = 'git svn find-rev %s'
#Gets the diff between two revisions and then changes it into an svn diff
gitDiffCmd = """
git diff --no-prefix %s^..%s -- %s |
sed -e "/^diff --git [^[:space:]]*/{h;}" \\
-e "s/^diff --git [^[:space:]]*/Index:/" \\
-e "s/^index.*/===================================================================/" \\
-e "/^new file .*/d" \\
-e "/^--- \/dev\/null/{g;}" \\
-e "s/^diff --git [^[:space:]]*/-+-/" \\
-e "s/^+++ .*/&\\t(revision %s)/" -e "s/^--- .*/&\\t(revision %s)/" -e "s/^-+- .*/&\\t(revision 0)/" \\
-e "s/^-+- [[:space:]]*/--- /"
"""
def getRevNumber(hash):
'''Given a git hash, find the svn revision number'''
cmd = findRevCmd % hash
p = Popen(cmd, shell=True, stdout=PIPE)
return p.communicate()[0].strip()
def getFileHashes(fileName, grepString):
'''Returns a list of the hashes associated with this file by the grep.
returns: a list of hashes. If the git log returns an error, then this will return None'''
cmd1 = 'git log --grep=%s --oneline -i --reverse %s' % (grepString, fileName)
cmd2 = 'awk \'{print $1;}\''
p1 = Popen(cmd1, shell=True, stdout=PIPE, stderr=subprocess.PIPE)
p2 = Popen(cmd2, shell=True, stdin=p1.stdout, stdout=PIPE)
p1.wait()
# If return code is not 0, return none
if(p1.returncode != 0):
return None
hashes = p2.communicate()[0].split('\n')
hashes.remove('')
return hashes
def getFirstAndLastHash(fileName, grepString):
'''Returns the first and last hash for a grep in a change
fileName - the file to find the hashes for
grepString - string to grep for in the commit comments
returns: an array with 0 set as the first hash and 1 the last hash'''
allHashes = getFileHashes(fileName, grepString)
if(allHashes == None):
return None
firstLastHashes = [allHashes[0], allHashes[len(allHashes) - 1]]
return firstLastHashes
searchString = sys.argv[1]
#get a list of files by the grep statement
cmd = 'git log --grep=%s --pretty="format:" -i --name-only' % searchString
p = Popen(cmd, shell=True, stdout=PIPE)
affectedFiles = p.communicate()[0]
theFiles = affectedFiles.split('\n')
finalFileList = set()
#add to a set to ensure no repeats.
for thing in theFiles:
if(thing != ''):
finalFileList.add(thing)
#for each file, get the svn diff for it, and output the result
for file in finalFileList:
hashes = getFirstAndLastHash(file, searchString)
if(hashes == None):
continue
prevRev = getRevNumber("%s^" % hashes[0])
rev = getRevNumber(hashes[1])
diffCommand = gitDiffCmd % (hashes[0], hashes[1], file, rev, prevRev)
finalDiff = Popen(diffCommand, shell=True, stdout=PIPE)
print finalDiff.communicate()[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment