OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import argparse | 6 import argparse |
| 7 import json |
7 import re | 8 import re |
8 import sys | 9 import sys |
9 | 10 |
10 from collections import defaultdict | 11 from collections import defaultdict |
11 | 12 |
12 import git_common as git | 13 import git_common as git |
13 | 14 |
14 | 15 |
15 FOOTER_PATTERN = re.compile(r'^\s*([\w-]+): (.*)$') | 16 FOOTER_PATTERN = re.compile(r'^\s*([\w-]+): (.*)$') |
16 CHROME_COMMIT_POSITION_PATTERN = re.compile(r'^([\w/-]+)@{#(\d+)}$') | 17 CHROME_COMMIT_POSITION_PATTERN = re.compile(r'^([\w/-]+)@{#(\d+)}$') |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 # svn commit numbers do not map to branches. | 157 # svn commit numbers do not map to branches. |
157 return ('refs/branch-heads/%s' % branch_match.group(1), None) | 158 return ('refs/branch-heads/%s' % branch_match.group(1), None) |
158 | 159 |
159 raise ValueError('Unable to infer commit position from footers') | 160 raise ValueError('Unable to infer commit position from footers') |
160 | 161 |
161 | 162 |
162 def main(args): | 163 def main(args): |
163 parser = argparse.ArgumentParser( | 164 parser = argparse.ArgumentParser( |
164 formatter_class=argparse.ArgumentDefaultsHelpFormatter | 165 formatter_class=argparse.ArgumentDefaultsHelpFormatter |
165 ) | 166 ) |
166 parser.add_argument('ref') | 167 parser.add_argument('ref', nargs='?', help="Git ref to retrieve footers from." |
| 168 " Omit to parse stdin.") |
167 | 169 |
168 g = parser.add_mutually_exclusive_group() | 170 g = parser.add_mutually_exclusive_group() |
169 g.add_argument('--key', metavar='KEY', | 171 g.add_argument('--key', metavar='KEY', |
170 help='Get all values for the given footer name, one per ' | 172 help='Get all values for the given footer name, one per ' |
171 'line (case insensitive)') | 173 'line (case insensitive)') |
172 g.add_argument('--position', action='store_true') | 174 g.add_argument('--position', action='store_true') |
173 g.add_argument('--position-ref', action='store_true') | 175 g.add_argument('--position-ref', action='store_true') |
174 g.add_argument('--position-num', action='store_true') | 176 g.add_argument('--position-num', action='store_true') |
| 177 g.add_argument('--json', help="filename to dump JSON serialized headers to.") |
175 | 178 |
176 | 179 |
177 opts = parser.parse_args(args) | 180 opts = parser.parse_args(args) |
178 | 181 |
179 message = git.run('log', '-1', '--format=%B', opts.ref) | 182 if opts.ref: |
| 183 message = git.run('log', '-1', '--format=%B', opts.ref) |
| 184 else: |
| 185 message = '\n'.join(l for l in sys.stdin) |
| 186 |
180 footers = parse_footers(message) | 187 footers = parse_footers(message) |
181 | 188 |
182 if opts.key: | 189 if opts.key: |
183 for v in footers.get(normalize_name(opts.key), []): | 190 for v in footers.get(normalize_name(opts.key), []): |
184 print v | 191 print v |
185 elif opts.position: | 192 elif opts.position: |
186 pos = get_position(footers) | 193 pos = get_position(footers) |
187 print '%s@{#%s}' % (pos[0], pos[1] or '?') | 194 print '%s@{#%s}' % (pos[0], pos[1] or '?') |
188 elif opts.position_ref: | 195 elif opts.position_ref: |
189 print get_position(footers)[0] | 196 print get_position(footers)[0] |
190 elif opts.position_num: | 197 elif opts.position_num: |
191 pos = get_position(footers) | 198 pos = get_position(footers) |
192 assert pos[1], 'No valid position for commit' | 199 assert pos[1], 'No valid position for commit' |
193 print pos[1] | 200 print pos[1] |
| 201 elif opts.json: |
| 202 with open(opts.json, 'w') as f: |
| 203 json.dump(footers, f) |
194 else: | 204 else: |
195 for k in footers.keys(): | 205 for k in footers.keys(): |
196 for v in footers[k]: | 206 for v in footers[k]: |
197 print '%s: %s' % (k, v) | 207 print '%s: %s' % (k, v) |
198 return 0 | 208 return 0 |
199 | 209 |
200 | 210 |
201 if __name__ == '__main__': | 211 if __name__ == '__main__': |
202 try: | 212 try: |
203 sys.exit(main(sys.argv[1:])) | 213 sys.exit(main(sys.argv[1:])) |
204 except KeyboardInterrupt: | 214 except KeyboardInterrupt: |
205 sys.stderr.write('interrupted\n') | 215 sys.stderr.write('interrupted\n') |
206 sys.exit(1) | 216 sys.exit(1) |
OLD | NEW |