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). If |
| 68 Cr-Commit-Position is not defined, we try to infer the ref and position |
| 69 from git-svn-id. The position number can be None if it was not inferrable. |
| 70 """ |
| 71 |
| 72 position = get_unique(footers, 'Cr-Commit-Position') |
| 73 if position: |
| 74 match = CHROME_COMMIT_POSITION_PATTERN.match(position) |
| 75 assert match, 'Invalid Cr-Commit-Position value: %s' % position |
| 76 return (match.group(1), match.group(2)) |
| 77 |
| 78 svn_commit = get_unique(footers, 'git-svn-id') |
| 79 if svn_commit: |
| 80 match = GIT_SVN_ID_PATTERN.match(svn_commit) |
| 81 assert match, 'Invalid git-svn-id value: %s' % svn_commit |
| 82 if re.match('.*/chrome/trunk/src$', match.group(1)): |
| 83 return ('refs/heads/master', match.group(2)) |
| 84 branch_match = re.match('.*/chrome/branches/([\w/-]+)/src$', match.group(1)) |
| 85 if branch_match: |
| 86 # svn commit numbers do not map to branches. |
| 87 return ('refs/branch-heads/%s' % branch_match.group(1), None) |
| 88 |
| 89 raise ValueError('Unable to infer commit position from footers') |
| 90 |
| 91 |
| 92 def main(args): |
| 93 parser = argparse.ArgumentParser( |
| 94 formatter_class=argparse.ArgumentDefaultsHelpFormatter |
| 95 ) |
| 96 parser.add_argument('ref') |
| 97 |
| 98 g = parser.add_mutually_exclusive_group() |
| 99 g.add_argument('--key', metavar='KEY', |
| 100 help='Get all values for the given footer name, one per ' |
| 101 'line (case insensitive)') |
| 102 g.add_argument('--position', action='store_true') |
| 103 g.add_argument('--position-ref', action='store_true') |
| 104 g.add_argument('--position-num', action='store_true') |
| 105 |
| 106 |
| 107 opts = parser.parse_args(args) |
| 108 |
| 109 message = git.run('log', '-1', '--format=%B', opts.ref) |
| 110 footers = parse_footers(message) |
| 111 |
| 112 if opts.key: |
| 113 for v in footers.get(normalize_name(opts.key), []): |
| 114 print v |
| 115 elif opts.position: |
| 116 pos = get_position(footers) |
| 117 print '%s@{#%s}' % (pos[0], pos[1] or '?') |
| 118 elif opts.position_ref: |
| 119 print get_position(footers)[0] |
| 120 elif opts.position_num: |
| 121 pos = get_position(footers) |
| 122 assert pos[1], 'No valid position for commit' |
| 123 print pos[1] |
| 124 else: |
| 125 for k in footers.keys(): |
| 126 for v in footers[k]: |
| 127 print '%s: %s' % (k, v) |
| 128 |
| 129 |
| 130 if __name__ == '__main__': |
| 131 sys.exit(main(sys.argv[1:])) |
OLD | NEW |