Skip to content

Instantly share code, notes, and snippets.

@elek
Created December 5, 2016 12:46
Show Gist options
  • Save elek/be10120a75049517e8a1369797a4b673 to your computer and use it in GitHub Desktop.
Save elek/be10120a75049517e8a1369797a4b673 to your computer and use it in GitHub Desktop.
"""Git branch comparator
Compares two git branches based on jira issue conventions. (eg. HADOOP-123). Prints out the commits which exist only on the first branch.
Usage:
compare.py --repo=REPO <source> <destination>
"""
from git import Repo
import re
from docopt import docopt
def issue_match(commit):
return (commit, re.search("([A-Z]+-[0-9]+)", commit.summary))
def find_match(x):
return x[1].group(1)
def accept_true(x):
return x[1]
if __name__ == '__main__':
arguments = docopt(__doc__, version='Branch Comparator')
r = Repo(arguments['--repo'])
branchA = r.commit(arguments['<source>'])
branchB = r.commit(arguments['<destination>'])
merge_base = r.merge_base(branchA, branchB)[0]
historyA = map(find_match, filter(accept_true, map(issue_match, r.iter_commits(merge_base.hexsha + ".." + branchA.hexsha))))
historyB = map(find_match, filter(accept_true, map(issue_match, r.iter_commits(merge_base.hexsha + ".." + branchB.hexsha))))
issue_to_commit = dict(map((lambda x: (x[1].group(1), x[0].summary)), filter(accept_true, map(issue_match, r.iter_commits(merge_base.hexsha + ".." + branchA.hexsha)))))
firstOnly = [val for val in historyA if val not in historyB]
print("Numer of issues", len(firstOnly))
print("\n".join([issue_to_commit[issue_id] for issue_id in firstOnly]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment