Chromium Code Reviews| Index: git_footers.py |
| diff --git a/git_footers.py b/git_footers.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..12299f7d7a2a07ecae00520c35b4f4786fc3312c |
| --- /dev/null |
| +++ b/git_footers.py |
| @@ -0,0 +1,70 @@ |
| +#!/usr/bin/env python |
| +# Copyright 2014 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import argparse |
| +import re |
| +import sys |
| + |
| +from collections import defaultdict |
| + |
| +import git_common as git |
| + |
| +FOOTER_PATTERN = re.compile(r'^\s*([\w-]+): (.*)$') |
| + |
| +def normalize_name(header): |
| + return '-'.join([ word.title() for word in header.strip().split('-') ]) |
| + |
| + |
| +def parse_footer(line): |
| + match = FOOTER_PATTERN.match(line) |
| + if match: |
| + return (match.group(1), match.group(2)) |
| + else: |
| + return None |
| + |
| + |
| +def parse_footers(log): |
|
iannucci
2014/08/30 00:24:19
this isn't really a log, it's a commit message. na
|
| + log_lines = [] |
| + for line in reversed(log.splitlines()): |
| + if line == '' or line.isspace(): |
| + break |
| + log_lines.append(line) |
| + |
| + footers = map(parse_footer, log_lines) |
| + if not all(footers): |
| + return defaultdict(list) |
| + |
| + footer_map = defaultdict(list) |
| + for (k, v) in footers: |
| + footer_map[normalize_name(k)].append(v.strip()) |
| + |
| + return footer_map |
| + |
| + |
| +def main(args): |
| + parser = argparse.ArgumentParser( |
| + formatter_class=argparse.ArgumentDefaultsHelpFormatter |
| + ) |
| + parser.add_argument('ref') |
| + parser.add_argument('--key', metavar='KEY', |
| + help='Get all values for the given header, one per line ' |
| + '(case insensitive)') |
|
iannucci
2014/08/30 00:24:19
could we also add a special --position, --position
|
| + |
| + opts = parser.parse_args(args) |
| + |
| + log = git.run('log', '-1', opts.ref) |
|
iannucci
2014/08/30 00:24:19
suggest formatting this using --format=%B
|
| + footers = parse_footers(log) |
| + |
| + if opts.key: |
| + for v in footers.get(normalize_name(opts.key), []): |
| + print v |
| + else: |
| + for k in footers.keys(): |
| + for v in footers[k]: |
| + print '%s: %s' % (k, v) |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main(sys.argv[1:])) |