luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 1 | #!/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 | |
| 6 | import argparse |
martiniss@chromium.org | 456ca7f | 2016-05-23 21:33:28 +0000 | [diff] [blame] | 7 | import json |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 8 | import re |
| 9 | import sys |
| 10 | |
| 11 | from collections import defaultdict |
| 12 | |
| 13 | import git_common as git |
| 14 | |
agable@chromium.org | d629fb4 | 2014-10-01 09:40:10 +0000 | [diff] [blame] | 15 | |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 16 | FOOTER_PATTERN = re.compile(r'^\s*([\w-]+): (.*)$') |
Andrii Shyshkalov | 49fe922 | 2016-12-15 11:05:06 +0100 | [diff] [blame] | 17 | CHROME_COMMIT_POSITION_PATTERN = re.compile(r'^([\w/\-\.]+)@{#(\d+)}$') |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 18 | |
agable@chromium.org | d629fb4 | 2014-10-01 09:40:10 +0000 | [diff] [blame] | 19 | |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 20 | def normalize_name(header): |
| 21 | return '-'.join([ word.title() for word in header.strip().split('-') ]) |
| 22 | |
| 23 | |
| 24 | def parse_footer(line): |
tandrii@chromium.org | f2aa52b | 2016-06-03 12:58:20 +0000 | [diff] [blame] | 25 | """Returns footer's (key, value) if footer is valid, else None.""" |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 26 | match = FOOTER_PATTERN.match(line) |
| 27 | if match: |
| 28 | return (match.group(1), match.group(2)) |
| 29 | else: |
| 30 | return None |
| 31 | |
| 32 | |
| 33 | def parse_footers(message): |
| 34 | """Parses a git commit message into a multimap of footers.""" |
tandrii@chromium.org | f2aa52b | 2016-06-03 12:58:20 +0000 | [diff] [blame] | 35 | _, _, parsed_footers = split_footers(message) |
| 36 | footer_map = defaultdict(list) |
| 37 | if parsed_footers: |
| 38 | # Read footers from bottom to top, because latter takes precedense, |
| 39 | # and we want it to be first in the multimap value. |
| 40 | for (k, v) in reversed(parsed_footers): |
| 41 | footer_map[normalize_name(k)].append(v.strip()) |
| 42 | return footer_map |
| 43 | |
| 44 | |
| 45 | def split_footers(message): |
| 46 | """Returns (non_footer_lines, footer_lines, parsed footers). |
| 47 | |
| 48 | Guarantees that: |
| 49 | (non_footer_lines + footer_lines) == message.splitlines(). |
| 50 | parsed_footers is parse_footer applied on each line of footer_lines. |
| 51 | """ |
| 52 | message_lines = list(message.splitlines()) |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 53 | footer_lines = [] |
tandrii@chromium.org | f2aa52b | 2016-06-03 12:58:20 +0000 | [diff] [blame] | 54 | for line in reversed(message_lines): |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 55 | if line == '' or line.isspace(): |
| 56 | break |
| 57 | footer_lines.append(line) |
tandrii@chromium.org | f2aa52b | 2016-06-03 12:58:20 +0000 | [diff] [blame] | 58 | else: |
| 59 | # The whole description was consisting of footers, |
| 60 | # which means those aren't footers. |
| 61 | footer_lines = [] |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 62 | |
tandrii@chromium.org | f2aa52b | 2016-06-03 12:58:20 +0000 | [diff] [blame] | 63 | footer_lines.reverse() |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 64 | footers = map(parse_footer, footer_lines) |
tandrii@chromium.org | f2aa52b | 2016-06-03 12:58:20 +0000 | [diff] [blame] | 65 | if not footer_lines or not all(footers): |
| 66 | return message_lines, [], [] |
| 67 | return message_lines[:-len(footer_lines)], footer_lines, footers |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 68 | |
| 69 | |
tandrii@chromium.org | 3c3c034 | 2016-03-04 11:59:28 +0000 | [diff] [blame] | 70 | def get_footer_change_id(message): |
| 71 | """Returns a list of Gerrit's ChangeId from given commit message.""" |
| 72 | return parse_footers(message).get(normalize_name('Change-Id'), []) |
| 73 | |
| 74 | |
| 75 | def add_footer_change_id(message, change_id): |
| 76 | """Returns message with Change-ID footer in it. |
| 77 | |
tandrii@chromium.org | f2aa52b | 2016-06-03 12:58:20 +0000 | [diff] [blame] | 78 | Assumes that Change-Id is not yet in footers, which is then inserted at |
| 79 | earliest footer line which is after all of these footers: |
| 80 | Bug|Issue|Test|Feature. |
tandrii@chromium.org | 3c3c034 | 2016-03-04 11:59:28 +0000 | [diff] [blame] | 81 | """ |
tandrii@chromium.org | f2aa52b | 2016-06-03 12:58:20 +0000 | [diff] [blame] | 82 | assert 'Change-Id' not in parse_footers(message) |
| 83 | return add_footer(message, 'Change-Id', change_id, |
| 84 | after_keys=['Bug', 'Issue', 'Test', 'Feature']) |
| 85 | |
| 86 | def add_footer(message, key, value, after_keys=None): |
| 87 | """Returns a message with given footer appended. |
| 88 | |
| 89 | If after_keys is None (default), appends footer last. |
| 90 | Otherwise, after_keys must be iterable of footer keys, then the new footer |
| 91 | would be inserted at the topmost position such there would be no footer lines |
| 92 | after it with key matching one of after_keys. |
| 93 | For example, given |
| 94 | message='Header.\n\nAdded: 2016\nBug: 123\nVerified-By: CQ' |
| 95 | after_keys=['Bug', 'Issue'] |
| 96 | the new footer will be inserted between Bug and Verified-By existing footers. |
| 97 | """ |
| 98 | assert key == normalize_name(key), 'Use normalized key' |
| 99 | new_footer = '%s: %s' % (key, value) |
| 100 | |
| 101 | top_lines, footer_lines, parsed_footers = split_footers(message) |
| 102 | if not footer_lines: |
| 103 | if not top_lines or top_lines[-1] != '': |
| 104 | top_lines.append('') |
| 105 | footer_lines = [new_footer] |
| 106 | elif not after_keys: |
| 107 | footer_lines.append(new_footer) |
tandrii@chromium.org | 9fc50db | 2016-03-17 12:38:55 +0000 | [diff] [blame] | 108 | else: |
tandrii@chromium.org | f2aa52b | 2016-06-03 12:58:20 +0000 | [diff] [blame] | 109 | after_keys = set(map(normalize_name, after_keys)) |
| 110 | # Iterate from last to first footer till we find the footer keys above. |
| 111 | for i, (key, _) in reversed(list(enumerate(parsed_footers))): |
| 112 | if normalize_name(key) in after_keys: |
| 113 | footer_lines.insert(i + 1, new_footer) |
tandrii@chromium.org | 3c3c034 | 2016-03-04 11:59:28 +0000 | [diff] [blame] | 114 | break |
| 115 | else: |
tandrii@chromium.org | f2aa52b | 2016-06-03 12:58:20 +0000 | [diff] [blame] | 116 | footer_lines.insert(0, new_footer) |
| 117 | return '\n'.join(top_lines + footer_lines) |
tandrii@chromium.org | 3c3c034 | 2016-03-04 11:59:28 +0000 | [diff] [blame] | 118 | |
| 119 | |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 120 | def get_unique(footers, key): |
| 121 | key = normalize_name(key) |
| 122 | values = footers[key] |
| 123 | assert len(values) <= 1, 'Multiple %s footers' % key |
| 124 | if values: |
| 125 | return values[0] |
| 126 | else: |
| 127 | return None |
| 128 | |
| 129 | |
| 130 | def get_position(footers): |
iannucci@chromium.org | 74c44f6 | 2014-09-09 22:35:03 +0000 | [diff] [blame] | 131 | """Get the commit position from the footers multimap using a heuristic. |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 132 | |
| 133 | Returns: |
| 134 | A tuple of the branch and the position on that branch. For example, |
| 135 | |
| 136 | Cr-Commit-Position: refs/heads/master@{#292272} |
| 137 | |
agable | 814b1ca | 2016-12-21 13:05:59 -0800 | [diff] [blame] | 138 | would give the return value ('refs/heads/master', 292272). |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 139 | """ |
| 140 | |
| 141 | position = get_unique(footers, 'Cr-Commit-Position') |
| 142 | if position: |
| 143 | match = CHROME_COMMIT_POSITION_PATTERN.match(position) |
| 144 | assert match, 'Invalid Cr-Commit-Position value: %s' % position |
| 145 | return (match.group(1), match.group(2)) |
| 146 | |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 147 | raise ValueError('Unable to infer commit position from footers') |
| 148 | |
| 149 | |
| 150 | def main(args): |
| 151 | parser = argparse.ArgumentParser( |
| 152 | formatter_class=argparse.ArgumentDefaultsHelpFormatter |
| 153 | ) |
martiniss@chromium.org | 456ca7f | 2016-05-23 21:33:28 +0000 | [diff] [blame] | 154 | parser.add_argument('ref', nargs='?', help="Git ref to retrieve footers from." |
| 155 | " Omit to parse stdin.") |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 156 | |
| 157 | g = parser.add_mutually_exclusive_group() |
| 158 | g.add_argument('--key', metavar='KEY', |
| 159 | help='Get all values for the given footer name, one per ' |
| 160 | 'line (case insensitive)') |
| 161 | g.add_argument('--position', action='store_true') |
| 162 | g.add_argument('--position-ref', action='store_true') |
| 163 | g.add_argument('--position-num', action='store_true') |
martiniss@chromium.org | 456ca7f | 2016-05-23 21:33:28 +0000 | [diff] [blame] | 164 | g.add_argument('--json', help="filename to dump JSON serialized headers to.") |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 165 | |
| 166 | |
| 167 | opts = parser.parse_args(args) |
| 168 | |
martiniss@chromium.org | 456ca7f | 2016-05-23 21:33:28 +0000 | [diff] [blame] | 169 | if opts.ref: |
| 170 | message = git.run('log', '-1', '--format=%B', opts.ref) |
| 171 | else: |
| 172 | message = '\n'.join(l for l in sys.stdin) |
| 173 | |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 174 | footers = parse_footers(message) |
| 175 | |
| 176 | if opts.key: |
| 177 | for v in footers.get(normalize_name(opts.key), []): |
| 178 | print v |
| 179 | elif opts.position: |
| 180 | pos = get_position(footers) |
| 181 | print '%s@{#%s}' % (pos[0], pos[1] or '?') |
| 182 | elif opts.position_ref: |
| 183 | print get_position(footers)[0] |
| 184 | elif opts.position_num: |
| 185 | pos = get_position(footers) |
| 186 | assert pos[1], 'No valid position for commit' |
| 187 | print pos[1] |
martiniss@chromium.org | 456ca7f | 2016-05-23 21:33:28 +0000 | [diff] [blame] | 188 | elif opts.json: |
| 189 | with open(opts.json, 'w') as f: |
| 190 | json.dump(footers, f) |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 191 | else: |
| 192 | for k in footers.keys(): |
| 193 | for v in footers[k]: |
| 194 | print '%s: %s' % (k, v) |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 195 | return 0 |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 196 | |
| 197 | |
| 198 | if __name__ == '__main__': |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 199 | try: |
| 200 | sys.exit(main(sys.argv[1:])) |
| 201 | except KeyboardInterrupt: |
| 202 | sys.stderr.write('interrupted\n') |
| 203 | sys.exit(1) |