Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 14 FOOTER_PATTERN = re.compile(r'^\s*([\w-]+): (.*)$') | |
| 15 CHROME_COMMIT_POSITION_PATTERN = re.compile(r'^([\w/-]+)@{#(\d+)}$') | |
| 16 GIT_SVN_ID_PATTERN = re.compile('^([^\s@]+)@(\d+)') | |
| 17 | |
| 18 def normalize_name(header): | |
| 19 return '-'.join([ word.title() for word in header.strip().split('-') ]) | |
| 20 | |
| 21 | |
| 22 def parse_footer(line): | |
| 23 match = FOOTER_PATTERN.match(line) | |
| 24 if match: | |
| 25 return (match.group(1), match.group(2)) | |
| 26 else: | |
| 27 return None | |
| 28 | |
| 29 | |
| 30 def parse_footers(message): | |
| 31 """Parses a git commit message into a multimap of footers.""" | |
| 32 footer_lines = [] | |
| 33 for line in reversed(message.splitlines()): | |
| 34 if line == '' or line.isspace(): | |
| 35 break | |
| 36 footer_lines.append(line) | |
| 37 | |
| 38 footers = map(parse_footer, footer_lines) | |
| 39 if not all(footers): | |
| 40 return defaultdict(list) | |
| 41 | |
| 42 footer_map = defaultdict(list) | |
| 43 for (k, v) in footers: | |
| 44 footer_map[normalize_name(k)].append(v.strip()) | |
| 45 | |
| 46 return footer_map | |
| 47 | |
| 48 | |
| 49 def get_unique(footers, key): | |
| 50 key = normalize_name(key) | |
| 51 values = footers[key] | |
| 52 assert len(values) <= 1, 'Multiple %s footers' % key | |
| 53 if values: | |
| 54 return values[0] | |
| 55 else: | |
| 56 return None | |
| 57 | |
| 58 | |
| 59 def get_position(footers): | |
| 60 """Get the chrome commit position from a footer multimap using a heuristic. | |
| 61 | |
| 62 Returns: | |
| 63 A tuple of the branch and the position on that branch. For example, | |
| 64 | |
| 65 Cr-Commit-Position: refs/heads/master@{#292272} | |
| 66 | |
| 67 would give the return value ('refs/heads/master', 292272). The position | |
| 68 number can be None if that doesn't make sense (e.g. a branch). | |
|
iannucci
2014/09/03 00:21:44
mention that this falls back to git-svn-id
also..
luqui
2014/09/03 02:28:48
Done.
| |
| 69 """ | |
| 70 | |
| 71 position = get_unique(footers, 'Cr-Commit-Position') | |
| 72 if position: | |
| 73 match = CHROME_COMMIT_POSITION_PATTERN.match(position) | |
| 74 assert match, 'Invalid Cr-Commit-Position value: %s' % position | |
| 75 return (match.group(1), match.group(2)) | |
| 76 | |
| 77 svn_commit = get_unique(footers, 'Git-Svn-Id') | |
|
iannucci
2014/09/03 00:21:44
note that it's always git-svn-id, though you're lo
luqui
2014/09/03 02:28:49
Made it git-svn-id for greppability.
| |
| 78 if svn_commit: | |
| 79 match = GIT_SVN_ID_PATTERN.match(svn_commit) | |
| 80 assert match, 'Invalid Git-Svn-Id value: %s' % svn_commit | |
| 81 if re.match('.*/chrome/trunk/src$', match.group(1)): | |
| 82 return ('refs/heads/master', match.group(2)) | |
| 83 branch_match = re.match('.*/chrome/branches/([\w/-]+)/src$', match.group(1)) | |
| 84 if branch_match: | |
| 85 return ('refs/branch-heads/%s' % branch_match.group(1), None) | |
| 86 | |
| 87 raise ValueError('Unable to infer commit position from footers') | |
| 88 | |
| 89 | |
| 90 def main(args): | |
| 91 parser = argparse.ArgumentParser( | |
| 92 formatter_class=argparse.ArgumentDefaultsHelpFormatter | |
| 93 ) | |
| 94 parser.add_argument('ref') | |
| 95 | |
| 96 g = parser.add_mutually_exclusive_group() | |
| 97 g.add_argument('--key', metavar='KEY', | |
| 98 help='Get all values for the given footer name, one per ' | |
| 99 'line (case insensitive)') | |
| 100 g.add_argument('--position', action='store_true') | |
| 101 g.add_argument('--position-ref', action='store_true') | |
| 102 g.add_argument('--position-num', action='store_true') | |
|
iannucci
2014/09/03 00:21:44
use an exclusive group for all of these
luqui
2014/09/03 02:28:48
Uh... they already are?
| |
| 103 | |
| 104 | |
| 105 opts = parser.parse_args(args) | |
| 106 | |
| 107 message = git.run('log', '-1', '--format=%B', opts.ref) | |
| 108 footers = parse_footers(message) | |
| 109 | |
| 110 if opts.key: | |
| 111 for v in footers.get(normalize_name(opts.key), []): | |
| 112 print v | |
| 113 elif opts.position: | |
| 114 pos = get_position(footers) | |
| 115 if pos[1] is not None: | |
| 116 print '%s@{#%s}' % (pos[0], pos[1]) | |
| 117 else: | |
| 118 print pos[0] | |
|
iannucci
2014/09/03 00:21:44
I would actually print it as
refs/branch-heads/
luqui
2014/09/03 02:28:48
Printing as refs/branch-heads/12345@{#?}
| |
| 119 elif opts.position_ref: | |
| 120 print get_position(footers)[0] | |
| 121 elif opts.position_num: | |
| 122 pos = get_position(footers) | |
| 123 assert pos[1], 'No valid position for commit' | |
| 124 print pos[1] | |
| 125 else: | |
| 126 for k in footers.keys(): | |
| 127 for v in footers[k]: | |
| 128 print '%s: %s' % (k, v) | |
| 129 | |
| 130 | |
| 131 if __name__ == '__main__': | |
| 132 sys.exit(main(sys.argv[1:])) | |
| OLD | NEW |