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