[email protected] | 0b88762 | 2014-09-03 02:31:03 | [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 | |
[email protected] | d629fb4 | 2014-10-01 09:40:10 | [diff] [blame] | 14 | |
[email protected] | 0b88762 | 2014-09-03 02:31:03 | [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 | |
[email protected] | d629fb4 | 2014-10-01 09:40:10 | [diff] [blame] | 19 | |
[email protected] | 0b88762 | 2014-09-03 02:31:03 | [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 | |
[email protected] | f0e4152 | 2015-06-10 19:52:01 | [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 | |
[email protected] | 0b88762 | 2014-09-03 02:31:03 | [diff] [blame] | 65 | def 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 | |
| 75 | def get_position(footers): |
[email protected] | 74c44f6 | 2014-09-09 22:35:03 | [diff] [blame] | 76 | """Get the commit position from the footers multimap using a heuristic. |
[email protected] | 0b88762 | 2014-09-03 02:31:03 | [diff] [blame] | 77 | |
| 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] | 4593f47 | 2014-10-13 21:25:43 | [diff] [blame] | 98 | # 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] | 74c44f6 | 2014-09-09 22:35:03 | [diff] [blame] | 106 | # Assume that any trunk svn revision will match the commit-position |
| 107 | # semantics. |
[email protected] | 0a17dab | 2014-09-09 23:07:36 | [diff] [blame] | 108 | if re.match('.*/trunk.*$', match.group(1)): |
[email protected] | 0b88762 | 2014-09-03 02:31:03 | [diff] [blame] | 109 | return ('refs/heads/master', match.group(2)) |
[email protected] | 74c44f6 | 2014-09-09 22:35:03 | [diff] [blame] | 110 | |
| 111 | # But for now only support faking branch-heads for chrome. |
[email protected] | 0b88762 | 2014-09-03 02:31:03 | [diff] [blame] | 112 | 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 | |
| 120 | def 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] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 156 | return 0 |
[email protected] | 0b88762 | 2014-09-03 02:31:03 | [diff] [blame] | 157 | |
| 158 | |
| 159 | if __name__ == '__main__': |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 160 | try: |
| 161 | sys.exit(main(sys.argv[1:])) |
| 162 | except KeyboardInterrupt: |
| 163 | sys.stderr.write('interrupted\n') |
| 164 | sys.exit(1) |