blob: 61546b3d30fac322c93d9736e74b142e1c34bfdb [file] [log] [blame]
Edward Lemurd6186f92019-08-12 17:56:58 +00001#!/usr/bin/env vpython
luqui@chromium.org0b887622014-09-03 02:31:03 +00002# 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
Raul Tambre80ee78e2019-05-06 22:41:05 +00006from __future__ import print_function
7
luqui@chromium.org0b887622014-09-03 02:31:03 +00008import argparse
martiniss@chromium.org456ca7f2016-05-23 21:33:28 +00009import json
luqui@chromium.org0b887622014-09-03 02:31:03 +000010import re
11import sys
12
13from collections import defaultdict
14
15import git_common as git
16
agable@chromium.orgd629fb42014-10-01 09:40:10 +000017
Andrii Shyshkalov80cae422017-04-27 01:01:42 +020018FOOTER_PATTERN = re.compile(r'^\s*([\w-]+): *(.*)$')
Andrii Shyshkalov49fe9222016-12-15 11:05:06 +010019CHROME_COMMIT_POSITION_PATTERN = re.compile(r'^([\w/\-\.]+)@{#(\d+)}$')
Aaron Gabled9a67562018-01-03 15:56:08 -080020FOOTER_KEY_BLACKLIST = set(['http', 'https'])
luqui@chromium.org0b887622014-09-03 02:31:03 +000021
agable@chromium.orgd629fb42014-10-01 09:40:10 +000022
luqui@chromium.org0b887622014-09-03 02:31:03 +000023def normalize_name(header):
24 return '-'.join([ word.title() for word in header.strip().split('-') ])
25
26
27def parse_footer(line):
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +000028 """Returns footer's (key, value) if footer is valid, else None."""
luqui@chromium.org0b887622014-09-03 02:31:03 +000029 match = FOOTER_PATTERN.match(line)
Aaron Gabled9a67562018-01-03 15:56:08 -080030 if match and match.group(1) not in FOOTER_KEY_BLACKLIST:
luqui@chromium.org0b887622014-09-03 02:31:03 +000031 return (match.group(1), match.group(2))
Aaron Gabled9a67562018-01-03 15:56:08 -080032 return None
luqui@chromium.org0b887622014-09-03 02:31:03 +000033
34
35def parse_footers(message):
36 """Parses a git commit message into a multimap of footers."""
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +000037 _, _, parsed_footers = split_footers(message)
38 footer_map = defaultdict(list)
39 if parsed_footers:
40 # Read footers from bottom to top, because latter takes precedense,
41 # and we want it to be first in the multimap value.
42 for (k, v) in reversed(parsed_footers):
43 footer_map[normalize_name(k)].append(v.strip())
44 return footer_map
45
46
Andrii Shyshkalov04b51d62017-05-11 13:21:30 +020047def matches_footer_key(line, key):
48 """Returns whether line is a valid footer whose key matches a given one.
49
50 Keys are compared in normalized form.
51 """
52 r = parse_footer(line)
53 if r is None:
Andrii Shyshkalov1a91c602017-05-11 14:35:56 +020054 return False
Andrii Shyshkalov04b51d62017-05-11 13:21:30 +020055 return normalize_name(r[0]) == normalize_name(key)
56
57
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +000058def split_footers(message):
59 """Returns (non_footer_lines, footer_lines, parsed footers).
60
61 Guarantees that:
Aaron Gable4be31872018-01-03 16:30:46 -080062 (non_footer_lines + footer_lines) ~= message.splitlines(), with at
63 most one new newline, if the last paragraph is text followed by footers.
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +000064 parsed_footers is parse_footer applied on each line of footer_lines.
Andrii Shyshkalov04b51d62017-05-11 13:21:30 +020065 There could be fewer parsed_footers than footer lines if some lines in
66 last paragraph are malformed.
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +000067 """
68 message_lines = list(message.splitlines())
luqui@chromium.org0b887622014-09-03 02:31:03 +000069 footer_lines = []
Aaron Gable4be31872018-01-03 16:30:46 -080070 maybe_footer_lines = []
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +000071 for line in reversed(message_lines):
luqui@chromium.org0b887622014-09-03 02:31:03 +000072 if line == '' or line.isspace():
73 break
Aaron Gable4be31872018-01-03 16:30:46 -080074 elif parse_footer(line):
75 footer_lines.extend(maybe_footer_lines)
76 maybe_footer_lines = []
77 footer_lines.append(line)
78 else:
79 # We only want to include malformed lines if they are preceeded by
80 # well-formed lines. So keep them in holding until we see a well-formed
81 # line (case above).
82 maybe_footer_lines.append(line)
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +000083 else:
84 # The whole description was consisting of footers,
85 # which means those aren't footers.
86 footer_lines = []
luqui@chromium.org0b887622014-09-03 02:31:03 +000087
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +000088 footer_lines.reverse()
Andrii Shyshkalov04b51d62017-05-11 13:21:30 +020089 footers = filter(None, map(parse_footer, footer_lines))
90 if not footers:
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +000091 return message_lines, [], []
Aaron Gable4be31872018-01-03 16:30:46 -080092 if maybe_footer_lines:
93 # If some malformed lines were left over, add a newline to split them
94 # from the well-formed ones.
95 return message_lines[:-len(footer_lines)] + [''], footer_lines, footers
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +000096 return message_lines[:-len(footer_lines)], footer_lines, footers
luqui@chromium.org0b887622014-09-03 02:31:03 +000097
98
tandrii@chromium.org3c3c0342016-03-04 11:59:28 +000099def get_footer_change_id(message):
100 """Returns a list of Gerrit's ChangeId from given commit message."""
101 return parse_footers(message).get(normalize_name('Change-Id'), [])
102
103
104def add_footer_change_id(message, change_id):
105 """Returns message with Change-ID footer in it.
106
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +0000107 Assumes that Change-Id is not yet in footers, which is then inserted at
108 earliest footer line which is after all of these footers:
109 Bug|Issue|Test|Feature.
tandrii@chromium.org3c3c0342016-03-04 11:59:28 +0000110 """
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +0000111 assert 'Change-Id' not in parse_footers(message)
112 return add_footer(message, 'Change-Id', change_id,
113 after_keys=['Bug', 'Issue', 'Test', 'Feature'])
114
Andrii Shyshkalov18975322017-01-25 16:44:13 +0100115
Aaron Gablec06db442017-04-26 17:29:49 -0700116def add_footer(message, key, value, after_keys=None, before_keys=None):
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +0000117 """Returns a message with given footer appended.
118
Aaron Gablec06db442017-04-26 17:29:49 -0700119 If after_keys and before_keys are both None (default), appends footer last.
120 If after_keys is provided and matches footers already present, inserts footer
121 as *early* as possible while still appearing after all provided keys, even
122 if doing so conflicts with before_keys.
123 If before_keys is provided, inserts footer as late as possible while still
124 appearing before all provided keys.
125
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +0000126 For example, given
127 message='Header.\n\nAdded: 2016\nBug: 123\nVerified-By: CQ'
128 after_keys=['Bug', 'Issue']
129 the new footer will be inserted between Bug and Verified-By existing footers.
130 """
131 assert key == normalize_name(key), 'Use normalized key'
132 new_footer = '%s: %s' % (key, value)
133
Aaron Gablec06db442017-04-26 17:29:49 -0700134 top_lines, footer_lines, _ = split_footers(message)
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +0000135 if not footer_lines:
136 if not top_lines or top_lines[-1] != '':
137 top_lines.append('')
138 footer_lines = [new_footer]
tandrii@chromium.org9fc50db2016-03-17 12:38:55 +0000139 else:
Aaron Gablec06db442017-04-26 17:29:49 -0700140 after_keys = set(map(normalize_name, after_keys or []))
141 after_indices = [
142 footer_lines.index(x) for x in footer_lines for k in after_keys
Andrii Shyshkalov04b51d62017-05-11 13:21:30 +0200143 if matches_footer_key(x, k)]
Aaron Gablec06db442017-04-26 17:29:49 -0700144 before_keys = set(map(normalize_name, before_keys or []))
145 before_indices = [
146 footer_lines.index(x) for x in footer_lines for k in before_keys
Andrii Shyshkalov04b51d62017-05-11 13:21:30 +0200147 if matches_footer_key(x, k)]
Aaron Gablec06db442017-04-26 17:29:49 -0700148 if after_indices:
149 # after_keys takes precedence, even if there's a conflict.
150 insert_idx = max(after_indices) + 1
151 elif before_indices:
152 insert_idx = min(before_indices)
tandrii@chromium.org3c3c0342016-03-04 11:59:28 +0000153 else:
Aaron Gablec06db442017-04-26 17:29:49 -0700154 insert_idx = len(footer_lines)
155 footer_lines.insert(insert_idx, new_footer)
tandrii@chromium.orgf2aa52b2016-06-03 12:58:20 +0000156 return '\n'.join(top_lines + footer_lines)
tandrii@chromium.org3c3c0342016-03-04 11:59:28 +0000157
158
Aaron Gableb584c4f2017-04-26 16:28:08 -0700159def remove_footer(message, key):
160 """Returns a message with all instances of given footer removed."""
161 key = normalize_name(key)
162 top_lines, footer_lines, _ = split_footers(message)
163 if not footer_lines:
164 return message
Aaron Gableb08ba652017-07-12 15:30:02 -0700165 new_footer_lines = []
166 for line in footer_lines:
167 try:
168 f = normalize_name(parse_footer(line)[0])
169 if f != key:
170 new_footer_lines.append(line)
171 except TypeError:
172 # If the footer doesn't parse (i.e. is malformed), just let it carry over.
173 new_footer_lines.append(line)
Aaron Gableb584c4f2017-04-26 16:28:08 -0700174 return '\n'.join(top_lines + new_footer_lines)
175
176
luqui@chromium.org0b887622014-09-03 02:31:03 +0000177def get_unique(footers, key):
178 key = normalize_name(key)
179 values = footers[key]
180 assert len(values) <= 1, 'Multiple %s footers' % key
181 if values:
182 return values[0]
183 else:
184 return None
185
186
187def get_position(footers):
iannucci@chromium.org74c44f62014-09-09 22:35:03 +0000188 """Get the commit position from the footers multimap using a heuristic.
luqui@chromium.org0b887622014-09-03 02:31:03 +0000189
190 Returns:
191 A tuple of the branch and the position on that branch. For example,
192
193 Cr-Commit-Position: refs/heads/master@{#292272}
194
agable814b1ca2016-12-21 13:05:59 -0800195 would give the return value ('refs/heads/master', 292272).
luqui@chromium.org0b887622014-09-03 02:31:03 +0000196 """
197
198 position = get_unique(footers, 'Cr-Commit-Position')
199 if position:
200 match = CHROME_COMMIT_POSITION_PATTERN.match(position)
201 assert match, 'Invalid Cr-Commit-Position value: %s' % position
202 return (match.group(1), match.group(2))
203
luqui@chromium.org0b887622014-09-03 02:31:03 +0000204 raise ValueError('Unable to infer commit position from footers')
205
206
207def main(args):
208 parser = argparse.ArgumentParser(
209 formatter_class=argparse.ArgumentDefaultsHelpFormatter
210 )
Andrii Shyshkalov22a9cf52017-07-13 14:23:58 +0200211 parser.add_argument('ref', nargs='?', help='Git ref to retrieve footers from.'
212 ' Omit to parse stdin.')
luqui@chromium.org0b887622014-09-03 02:31:03 +0000213
214 g = parser.add_mutually_exclusive_group()
215 g.add_argument('--key', metavar='KEY',
216 help='Get all values for the given footer name, one per '
217 'line (case insensitive)')
218 g.add_argument('--position', action='store_true')
219 g.add_argument('--position-ref', action='store_true')
220 g.add_argument('--position-num', action='store_true')
Andrii Shyshkalov22a9cf52017-07-13 14:23:58 +0200221 g.add_argument('--json', help='filename to dump JSON serialized footers to.')
luqui@chromium.org0b887622014-09-03 02:31:03 +0000222
luqui@chromium.org0b887622014-09-03 02:31:03 +0000223 opts = parser.parse_args(args)
224
martiniss@chromium.org456ca7f2016-05-23 21:33:28 +0000225 if opts.ref:
226 message = git.run('log', '-1', '--format=%B', opts.ref)
227 else:
Andrii Shyshkalov22a9cf52017-07-13 14:23:58 +0200228 message = sys.stdin.read()
martiniss@chromium.org456ca7f2016-05-23 21:33:28 +0000229
luqui@chromium.org0b887622014-09-03 02:31:03 +0000230 footers = parse_footers(message)
231
232 if opts.key:
233 for v in footers.get(normalize_name(opts.key), []):
Raul Tambre80ee78e2019-05-06 22:41:05 +0000234 print(v)
luqui@chromium.org0b887622014-09-03 02:31:03 +0000235 elif opts.position:
236 pos = get_position(footers)
Raul Tambre80ee78e2019-05-06 22:41:05 +0000237 print('%s@{#%s}' % (pos[0], pos[1] or '?'))
luqui@chromium.org0b887622014-09-03 02:31:03 +0000238 elif opts.position_ref:
Raul Tambre80ee78e2019-05-06 22:41:05 +0000239 print(get_position(footers)[0])
luqui@chromium.org0b887622014-09-03 02:31:03 +0000240 elif opts.position_num:
241 pos = get_position(footers)
242 assert pos[1], 'No valid position for commit'
Raul Tambre80ee78e2019-05-06 22:41:05 +0000243 print(pos[1])
martiniss@chromium.org456ca7f2016-05-23 21:33:28 +0000244 elif opts.json:
245 with open(opts.json, 'w') as f:
246 json.dump(footers, f)
luqui@chromium.org0b887622014-09-03 02:31:03 +0000247 else:
248 for k in footers.keys():
249 for v in footers[k]:
Raul Tambre80ee78e2019-05-06 22:41:05 +0000250 print('%s: %s' % (k, v))
sbc@chromium.org013731e2015-02-26 18:28:43 +0000251 return 0
luqui@chromium.org0b887622014-09-03 02:31:03 +0000252
253
254if __name__ == '__main__':
sbc@chromium.org013731e2015-02-26 18:28:43 +0000255 try:
256 sys.exit(main(sys.argv[1:]))
257 except KeyboardInterrupt:
258 sys.stderr.write('interrupted\n')
259 sys.exit(1)