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 | |
| 16 def normalize_name(header): | |
| 17 return '-'.join([ word.title() for word in header.strip().split('-') ]) | |
| 18 | |
| 19 | |
| 20 def parse_footer(line): | |
| 21 match = FOOTER_PATTERN.match(line) | |
| 22 if match: | |
| 23 return (match.group(1), match.group(2)) | |
| 24 else: | |
| 25 return None | |
| 26 | |
| 27 | |
| 28 def parse_footers(log): | |
|
iannucci
2014/08/30 00:24:19
this isn't really a log, it's a commit message. na
| |
| 29 log_lines = [] | |
| 30 for line in reversed(log.splitlines()): | |
| 31 if line == '' or line.isspace(): | |
| 32 break | |
| 33 log_lines.append(line) | |
| 34 | |
| 35 footers = map(parse_footer, log_lines) | |
| 36 if not all(footers): | |
| 37 return defaultdict(list) | |
| 38 | |
| 39 footer_map = defaultdict(list) | |
| 40 for (k, v) in footers: | |
| 41 footer_map[normalize_name(k)].append(v.strip()) | |
| 42 | |
| 43 return footer_map | |
| 44 | |
| 45 | |
| 46 def main(args): | |
| 47 parser = argparse.ArgumentParser( | |
| 48 formatter_class=argparse.ArgumentDefaultsHelpFormatter | |
| 49 ) | |
| 50 parser.add_argument('ref') | |
| 51 parser.add_argument('--key', metavar='KEY', | |
| 52 help='Get all values for the given header, one per line ' | |
| 53 '(case insensitive)') | |
|
iannucci
2014/08/30 00:24:19
could we also add a special --position, --position
| |
| 54 | |
| 55 opts = parser.parse_args(args) | |
| 56 | |
| 57 log = git.run('log', '-1', opts.ref) | |
|
iannucci
2014/08/30 00:24:19
suggest formatting this using --format=%B
| |
| 58 footers = parse_footers(log) | |
| 59 | |
| 60 if opts.key: | |
| 61 for v in footers.get(normalize_name(opts.key), []): | |
| 62 print v | |
| 63 else: | |
| 64 for k in footers.keys(): | |
| 65 for v in footers[k]: | |
| 66 print '%s: %s' % (k, v) | |
| 67 | |
| 68 | |
| 69 if __name__ == '__main__': | |
| 70 sys.exit(main(sys.argv[1:])) | |
| OLD | NEW |