2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
10 from collections import defaultdict
12 import git_common as git
15 FOOTER_PATTERN = re.compile(r'^\s*([\w-]+): (.*)$')
16 CHROME_COMMIT_POSITION_PATTERN = re.compile(r'^([\w/-]+)@{#(\d+)}$')
17 GIT_SVN_ID_PATTERN = re.compile('^([^\s@]+)@(\d+)')
20 def normalize_name(header):
21 return '-'.join([ word.title() for word in header.strip().split('-') ])
24 def parse_footer(line):
25 match = FOOTER_PATTERN.match(line)
27 return (match.group(1), match.group(2))
32 def parse_footers(message):
33 """Parses a git commit message into a multimap of footers."""
35 for line in reversed(message.splitlines()):
36 if line == '' or line.isspace():
38 footer_lines.append(line)
40 footers = map(parse_footer, footer_lines)
42 return defaultdict(list)
44 footer_map = defaultdict(list)
45 for (k, v) in footers:
46 footer_map[normalize_name(k)].append(v.strip())
51 def get_unique(footers, key):
52 key = normalize_name(key)
54 assert len(values) <= 1, 'Multiple %s footers' % key
61 def get_position(footers):
62 """Get the commit position from the footers multimap using a heuristic.
65 A tuple of the branch and the position on that branch. For example,
67 Cr-Commit-Position: refs/heads/master@{#292272}
69 would give the return value ('refs/heads/master', 292272). If
70 Cr-Commit-Position is not defined, we try to infer the ref and position
71 from git-svn-id. The position number can be None if it was not inferrable.
74 position = get_unique(footers, 'Cr-Commit-Position')
76 match = CHROME_COMMIT_POSITION_PATTERN.match(position)
77 assert match, 'Invalid Cr-Commit-Position value: %s' % position
78 return (match.group(1), match.group(2))
80 svn_commit = get_unique(footers, 'git-svn-id')
82 match = GIT_SVN_ID_PATTERN.match(svn_commit)
83 assert match, 'Invalid git-svn-id value: %s' % svn_commit
84 # V8 has different semantics than Chromium.
85 if re.match(r'.*https?://v8\.googlecode\.com/svn/trunk',
87 return ('refs/heads/candidates', match.group(2))
88 if re.match(r'.*https?://v8\.googlecode\.com/svn/branches/bleeding_edge',
90 return ('refs/heads/master', match.group(2))
92 # Assume that any trunk svn revision will match the commit-position
94 if re.match('.*/trunk.*$', match.group(1)):
95 return ('refs/heads/master', match.group(2))
97 # But for now only support faking branch-heads for chrome.
98 branch_match = re.match('.*/chrome/branches/([\w/-]+)/src$', match.group(1))
100 # svn commit numbers do not map to branches.
101 return ('refs/branch-heads/%s' % branch_match.group(1), None)
103 raise ValueError('Unable to infer commit position from footers')
107 parser = argparse.ArgumentParser(
108 formatter_class=argparse.ArgumentDefaultsHelpFormatter
110 parser.add_argument('ref')
112 g = parser.add_mutually_exclusive_group()
113 g.add_argument('--key', metavar='KEY',
114 help='Get all values for the given footer name, one per '
115 'line (case insensitive)')
116 g.add_argument('--position', action='store_true')
117 g.add_argument('--position-ref', action='store_true')
118 g.add_argument('--position-num', action='store_true')
121 opts = parser.parse_args(args)
123 message = git.run('log', '-1', '--format=%B', opts.ref)
124 footers = parse_footers(message)
127 for v in footers.get(normalize_name(opts.key), []):
130 pos = get_position(footers)
131 print '%s@{#%s}' % (pos[0], pos[1] or '?')
132 elif opts.position_ref:
133 print get_position(footers)[0]
134 elif opts.position_num:
135 pos = get_position(footers)
136 assert pos[1], 'No valid position for commit'
139 for k in footers.keys():
141 print '%s: %s' % (k, v)
144 if __name__ == '__main__':
145 sys.exit(main(sys.argv[1:]))