blob: 3e3ea827ba5cb771802478bcb7121d203cd4f284 [file] [log] [blame]
[email protected]0b887622014-09-03 02:31:031#!/usr/bin/env python
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.
5
6import argparse
7import re
8import sys
9
10from collections import defaultdict
11
12import git_common as git
13
[email protected]d629fb42014-10-01 09:40:1014
[email protected]0b887622014-09-03 02:31:0315FOOTER_PATTERN = re.compile(r'^\s*([\w-]+): (.*)$')
16CHROME_COMMIT_POSITION_PATTERN = re.compile(r'^([\w/-]+)@{#(\d+)}$')
17GIT_SVN_ID_PATTERN = re.compile('^([^\s@]+)@(\d+)')
18
[email protected]d629fb42014-10-01 09:40:1019
[email protected]0b887622014-09-03 02:31:0320def normalize_name(header):
21 return '-'.join([ word.title() for word in header.strip().split('-') ])
22
23
24def parse_footer(line):
25 match = FOOTER_PATTERN.match(line)
26 if match:
27 return (match.group(1), match.group(2))
28 else:
29 return None
30
31
32def parse_footers(message):
33 """Parses a git commit message into a multimap of footers."""
34 footer_lines = []
35 for line in reversed(message.splitlines()):
36 if line == '' or line.isspace():
37 break
38 footer_lines.append(line)
39
40 footers = map(parse_footer, footer_lines)
41 if not all(footers):
42 return defaultdict(list)
43
44 footer_map = defaultdict(list)
45 for (k, v) in footers:
46 footer_map[normalize_name(k)].append(v.strip())
47
48 return footer_map
49
50
[email protected]f0e41522015-06-10 19:52:0151def get_footer_svn_id(branch=None):
52 if not branch:
53 branch = git.root()
54 svn_id = None
55 message = git.run('log', '-1', '--format=%B', branch)
56 footers = parse_footers(message)
57 git_svn_id = get_unique(footers, 'git-svn-id')
58 if git_svn_id:
59 match = GIT_SVN_ID_PATTERN.match(git_svn_id)
60 if match:
61 svn_id = match.group(1)
62 return svn_id
63
64
[email protected]0b887622014-09-03 02:31:0365def get_unique(footers, key):
66 key = normalize_name(key)
67 values = footers[key]
68 assert len(values) <= 1, 'Multiple %s footers' % key
69 if values:
70 return values[0]
71 else:
72 return None
73
74
75def get_position(footers):
[email protected]74c44f62014-09-09 22:35:0376 """Get the commit position from the footers multimap using a heuristic.
[email protected]0b887622014-09-03 02:31:0377
78 Returns:
79 A tuple of the branch and the position on that branch. For example,
80
81 Cr-Commit-Position: refs/heads/master@{#292272}
82
83 would give the return value ('refs/heads/master', 292272). If
84 Cr-Commit-Position is not defined, we try to infer the ref and position
85 from git-svn-id. The position number can be None if it was not inferrable.
86 """
87
88 position = get_unique(footers, 'Cr-Commit-Position')
89 if position:
90 match = CHROME_COMMIT_POSITION_PATTERN.match(position)
91 assert match, 'Invalid Cr-Commit-Position value: %s' % position
92 return (match.group(1), match.group(2))
93
94 svn_commit = get_unique(footers, 'git-svn-id')
95 if svn_commit:
96 match = GIT_SVN_ID_PATTERN.match(svn_commit)
97 assert match, 'Invalid git-svn-id value: %s' % svn_commit
[email protected]4593f472014-10-13 21:25:4398 # V8 has different semantics than Chromium.
99 if re.match(r'.*https?://v8\.googlecode\.com/svn/trunk',
100 match.group(1)):
101 return ('refs/heads/candidates', match.group(2))
102 if re.match(r'.*https?://v8\.googlecode\.com/svn/branches/bleeding_edge',
103 match.group(1)):
104 return ('refs/heads/master', match.group(2))
105
[email protected]74c44f62014-09-09 22:35:03106 # Assume that any trunk svn revision will match the commit-position
107 # semantics.
[email protected]0a17dab2014-09-09 23:07:36108 if re.match('.*/trunk.*$', match.group(1)):
[email protected]0b887622014-09-03 02:31:03109 return ('refs/heads/master', match.group(2))
[email protected]74c44f62014-09-09 22:35:03110
111 # But for now only support faking branch-heads for chrome.
[email protected]0b887622014-09-03 02:31:03112 branch_match = re.match('.*/chrome/branches/([\w/-]+)/src$', match.group(1))
113 if branch_match:
114 # svn commit numbers do not map to branches.
115 return ('refs/branch-heads/%s' % branch_match.group(1), None)
116
117 raise ValueError('Unable to infer commit position from footers')
118
119
120def main(args):
121 parser = argparse.ArgumentParser(
122 formatter_class=argparse.ArgumentDefaultsHelpFormatter
123 )
124 parser.add_argument('ref')
125
126 g = parser.add_mutually_exclusive_group()
127 g.add_argument('--key', metavar='KEY',
128 help='Get all values for the given footer name, one per '
129 'line (case insensitive)')
130 g.add_argument('--position', action='store_true')
131 g.add_argument('--position-ref', action='store_true')
132 g.add_argument('--position-num', action='store_true')
133
134
135 opts = parser.parse_args(args)
136
137 message = git.run('log', '-1', '--format=%B', opts.ref)
138 footers = parse_footers(message)
139
140 if opts.key:
141 for v in footers.get(normalize_name(opts.key), []):
142 print v
143 elif opts.position:
144 pos = get_position(footers)
145 print '%s@{#%s}' % (pos[0], pos[1] or '?')
146 elif opts.position_ref:
147 print get_position(footers)[0]
148 elif opts.position_num:
149 pos = get_position(footers)
150 assert pos[1], 'No valid position for commit'
151 print pos[1]
152 else:
153 for k in footers.keys():
154 for v in footers[k]:
155 print '%s: %s' % (k, v)
[email protected]013731e2015-02-26 18:28:43156 return 0
[email protected]0b887622014-09-03 02:31:03157
158
159if __name__ == '__main__':
[email protected]013731e2015-02-26 18:28:43160 try:
161 sys.exit(main(sys.argv[1:]))
162 except KeyboardInterrupt:
163 sys.stderr.write('interrupted\n')
164 sys.exit(1)