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-]+): (.*)$') |
| 17 | CHROME_COMMIT_POSITION_PATTERN = re.compile(r'^([\w/-]+)@{#(\d+)}$') |
| 18 | GIT_SVN_ID_PATTERN = re.compile('^([^\s@]+)@(\d+)') |
| 19 | |
agable@chromium.org | d629fb4 | 2014-10-01 09:40:10 +0000 | [diff] [blame] | 20 | |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 21 | def normalize_name(header): |
| 22 | return '-'.join([ word.title() for word in header.strip().split('-') ]) |
| 23 | |
| 24 | |
| 25 | def parse_footer(line): |
| 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.""" |
| 35 | footer_lines = [] |
| 36 | for line in reversed(message.splitlines()): |
| 37 | if line == '' or line.isspace(): |
| 38 | break |
| 39 | footer_lines.append(line) |
| 40 | |
| 41 | footers = map(parse_footer, footer_lines) |
| 42 | if not all(footers): |
| 43 | return defaultdict(list) |
| 44 | |
| 45 | footer_map = defaultdict(list) |
| 46 | for (k, v) in footers: |
| 47 | footer_map[normalize_name(k)].append(v.strip()) |
| 48 | |
| 49 | return footer_map |
| 50 | |
| 51 | |
mmoss@chromium.org | f0e4152 | 2015-06-10 19:52:01 +0000 | [diff] [blame] | 52 | def get_footer_svn_id(branch=None): |
| 53 | if not branch: |
| 54 | branch = git.root() |
| 55 | svn_id = None |
| 56 | message = git.run('log', '-1', '--format=%B', branch) |
| 57 | footers = parse_footers(message) |
| 58 | git_svn_id = get_unique(footers, 'git-svn-id') |
| 59 | if git_svn_id: |
| 60 | match = GIT_SVN_ID_PATTERN.match(git_svn_id) |
| 61 | if match: |
| 62 | svn_id = match.group(1) |
| 63 | return svn_id |
| 64 | |
| 65 | |
tandrii@chromium.org | 3c3c034 | 2016-03-04 11:59:28 +0000 | [diff] [blame] | 66 | def get_footer_change_id(message): |
| 67 | """Returns a list of Gerrit's ChangeId from given commit message.""" |
| 68 | return parse_footers(message).get(normalize_name('Change-Id'), []) |
| 69 | |
| 70 | |
| 71 | def add_footer_change_id(message, change_id): |
| 72 | """Returns message with Change-ID footer in it. |
| 73 | |
| 74 | Assumes that Change-Id is not yet in footers, which is then |
| 75 | inserted after any of these footers: Bug|Issue|Test|Feature. |
| 76 | """ |
| 77 | assert 0 == len(get_footer_change_id(message)) |
| 78 | change_id_line = 'Change-Id: %s' % change_id |
| 79 | # This code does the same as parse_footers, but keeps track of line |
| 80 | # numbers so that ChangeId is inserted in the right place. |
| 81 | lines = message.splitlines() |
| 82 | footer_lines = [] |
| 83 | for line in reversed(lines): |
| 84 | if line == '' or line.isspace(): |
| 85 | break |
| 86 | footer_lines.append(line) |
tandrii@chromium.org | 9fc50db | 2016-03-17 12:38:55 +0000 | [diff] [blame] | 87 | else: |
| 88 | # The whole description was consisting of footers, |
| 89 | # which means those aren't footers. |
| 90 | footer_lines = [] |
tandrii@chromium.org | 3c3c034 | 2016-03-04 11:59:28 +0000 | [diff] [blame] | 91 | # footers order is from end to start of the message. |
| 92 | footers = map(parse_footer, footer_lines) |
tandrii@chromium.org | 9fc50db | 2016-03-17 12:38:55 +0000 | [diff] [blame] | 93 | if not footers or not all(footers): |
tandrii@chromium.org | 3c3c034 | 2016-03-04 11:59:28 +0000 | [diff] [blame] | 94 | lines.append('') |
| 95 | lines.append(change_id_line) |
| 96 | else: |
| 97 | after = set(map(normalize_name, ['Bug', 'Issue', 'Test', 'Feature'])) |
| 98 | for i, (key, _) in enumerate(footers): |
| 99 | if normalize_name(key) in after: |
| 100 | insert_at = len(lines) - i |
| 101 | break |
| 102 | else: |
| 103 | insert_at = len(lines) - len(footers) |
| 104 | lines.insert(insert_at, change_id_line) |
| 105 | return '\n'.join(lines) |
| 106 | |
| 107 | |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 108 | def get_unique(footers, key): |
| 109 | key = normalize_name(key) |
| 110 | values = footers[key] |
| 111 | assert len(values) <= 1, 'Multiple %s footers' % key |
| 112 | if values: |
| 113 | return values[0] |
| 114 | else: |
| 115 | return None |
| 116 | |
| 117 | |
| 118 | def get_position(footers): |
iannucci@chromium.org | 74c44f6 | 2014-09-09 22:35:03 +0000 | [diff] [blame] | 119 | """Get the commit position from the footers multimap using a heuristic. |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 120 | |
| 121 | Returns: |
| 122 | A tuple of the branch and the position on that branch. For example, |
| 123 | |
| 124 | Cr-Commit-Position: refs/heads/master@{#292272} |
| 125 | |
| 126 | would give the return value ('refs/heads/master', 292272). If |
| 127 | Cr-Commit-Position is not defined, we try to infer the ref and position |
| 128 | from git-svn-id. The position number can be None if it was not inferrable. |
| 129 | """ |
| 130 | |
| 131 | position = get_unique(footers, 'Cr-Commit-Position') |
| 132 | if position: |
| 133 | match = CHROME_COMMIT_POSITION_PATTERN.match(position) |
| 134 | assert match, 'Invalid Cr-Commit-Position value: %s' % position |
| 135 | return (match.group(1), match.group(2)) |
| 136 | |
| 137 | svn_commit = get_unique(footers, 'git-svn-id') |
| 138 | if svn_commit: |
| 139 | match = GIT_SVN_ID_PATTERN.match(svn_commit) |
| 140 | assert match, 'Invalid git-svn-id value: %s' % svn_commit |
hinoka@chromium.org | 4593f47 | 2014-10-13 21:25:43 +0000 | [diff] [blame] | 141 | # V8 has different semantics than Chromium. |
| 142 | if re.match(r'.*https?://v8\.googlecode\.com/svn/trunk', |
| 143 | match.group(1)): |
| 144 | return ('refs/heads/candidates', match.group(2)) |
| 145 | if re.match(r'.*https?://v8\.googlecode\.com/svn/branches/bleeding_edge', |
| 146 | match.group(1)): |
| 147 | return ('refs/heads/master', match.group(2)) |
| 148 | |
iannucci@chromium.org | 74c44f6 | 2014-09-09 22:35:03 +0000 | [diff] [blame] | 149 | # Assume that any trunk svn revision will match the commit-position |
| 150 | # semantics. |
iannucci@chromium.org | 0a17dab | 2014-09-09 23:07:36 +0000 | [diff] [blame] | 151 | if re.match('.*/trunk.*$', match.group(1)): |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 152 | return ('refs/heads/master', match.group(2)) |
iannucci@chromium.org | 74c44f6 | 2014-09-09 22:35:03 +0000 | [diff] [blame] | 153 | |
| 154 | # But for now only support faking branch-heads for chrome. |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 155 | branch_match = re.match('.*/chrome/branches/([\w/-]+)/src$', match.group(1)) |
| 156 | if branch_match: |
| 157 | # svn commit numbers do not map to branches. |
| 158 | return ('refs/branch-heads/%s' % branch_match.group(1), None) |
| 159 | |
| 160 | raise ValueError('Unable to infer commit position from footers') |
| 161 | |
| 162 | |
| 163 | def main(args): |
| 164 | parser = argparse.ArgumentParser( |
| 165 | formatter_class=argparse.ArgumentDefaultsHelpFormatter |
| 166 | ) |
martiniss@chromium.org | 456ca7f | 2016-05-23 21:33:28 +0000 | [diff] [blame] | 167 | parser.add_argument('ref', nargs='?', help="Git ref to retrieve footers from." |
| 168 | " Omit to parse stdin.") |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 169 | |
| 170 | g = parser.add_mutually_exclusive_group() |
| 171 | g.add_argument('--key', metavar='KEY', |
| 172 | help='Get all values for the given footer name, one per ' |
| 173 | 'line (case insensitive)') |
| 174 | g.add_argument('--position', action='store_true') |
| 175 | g.add_argument('--position-ref', action='store_true') |
| 176 | g.add_argument('--position-num', action='store_true') |
martiniss@chromium.org | 456ca7f | 2016-05-23 21:33:28 +0000 | [diff] [blame] | 177 | 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] | 178 | |
| 179 | |
| 180 | opts = parser.parse_args(args) |
| 181 | |
martiniss@chromium.org | 456ca7f | 2016-05-23 21:33:28 +0000 | [diff] [blame] | 182 | if opts.ref: |
| 183 | message = git.run('log', '-1', '--format=%B', opts.ref) |
| 184 | else: |
| 185 | message = '\n'.join(l for l in sys.stdin) |
| 186 | |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 187 | footers = parse_footers(message) |
| 188 | |
| 189 | if opts.key: |
| 190 | for v in footers.get(normalize_name(opts.key), []): |
| 191 | print v |
| 192 | elif opts.position: |
| 193 | pos = get_position(footers) |
| 194 | print '%s@{#%s}' % (pos[0], pos[1] or '?') |
| 195 | elif opts.position_ref: |
| 196 | print get_position(footers)[0] |
| 197 | elif opts.position_num: |
| 198 | pos = get_position(footers) |
| 199 | assert pos[1], 'No valid position for commit' |
| 200 | print pos[1] |
martiniss@chromium.org | 456ca7f | 2016-05-23 21:33:28 +0000 | [diff] [blame] | 201 | elif opts.json: |
| 202 | with open(opts.json, 'w') as f: |
| 203 | json.dump(footers, f) |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 204 | else: |
| 205 | for k in footers.keys(): |
| 206 | for v in footers[k]: |
| 207 | print '%s: %s' % (k, v) |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 208 | return 0 |
luqui@chromium.org | 0b88762 | 2014-09-03 02:31:03 +0000 | [diff] [blame] | 209 | |
| 210 | |
| 211 | if __name__ == '__main__': |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 212 | try: |
| 213 | sys.exit(main(sys.argv[1:])) |
| 214 | except KeyboardInterrupt: |
| 215 | sys.stderr.write('interrupted\n') |
| 216 | sys.exit(1) |