blob: 2ea535dc124a429101345fa74826f584caeda9e7 [file] [log] [blame]
Brian O'Connor436ff312016-10-11 16:19:17 -07001#!/usr/bin/env python
2
3import os
4import re
5import sys
6
7from git import Git
8
9g = Git(os.getcwd())
10
11commitMsgs = {}
12
13def getCommits(srcRef, dstRef):
14 commits = {}
15
16 commit = ''
17 changeId = ''
18 commitHash = ''
19 for line in g.log('--format=fuller', '%s..%s' % (srcRef, dstRef)).split('\n'):
20 if len(commit) > 0 and line.find('commit') == 0:
21 commits[changeId] = commitHash
22 commitMsgs[commitHash] = commit
23 commit = ''
24 size = 0
25 match = re.search(r'commit ([0-9a-f]+)', line)
26 if match:
27 commitHash = match.group(1)
28 else:
29 raise Exception('Bad commit hash in line:\n%s' % line)
30 elif line.find('Change-Id:') >= 0:
31 match = re.search(r'Change-Id: (I[0-9a-f]+)', line)
32 if match:
33 changeId = match.group(1)
34 else:
35 raise Exception('Bad Change-Id in line:\n%s' % line)
36 commit += line + '\n'
37
38 return commits
39
40
41if __name__ == '__main__':
42 try:
43 sourceRef, targetRef = sys.argv[1:3]
44 except ValueError:
45 print 'branch-compare <source ref> <target ref>'
46
47 targetCommits = getCommits(sourceRef, targetRef)
48 sourceCommits = getCommits(targetRef, sourceRef)
49
50 missingCommitsFromTarget = set(sourceCommits.keys()) - set(targetCommits.keys())
51
52 for id in missingCommitsFromTarget:
53 hash = sourceCommits[id]
54 print 'git cherry-pick', hash
55 print commitMsgs[hash]