OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 # Copyright (C) 2008 Evan Martin <martine@danga.com> | 6 # Copyright (C) 2008 Evan Martin <martine@danga.com> |
7 | 7 |
8 """A git-command for integrating reviews on Rietveld and Gerrit.""" | 8 """A git-command for integrating reviews on Rietveld and Gerrit.""" |
9 | 9 |
10 from __future__ import print_function | 10 from __future__ import print_function |
(...skipping 3428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3439 if color in line.upper(): | 3439 if color in line.upper(): |
3440 # Extract whitespaces first and the leading '-'. | 3440 # Extract whitespaces first and the leading '-'. |
3441 indent = len(line) - len(line.lstrip(' ')) + 1 | 3441 indent = len(line) - len(line.lstrip(' ')) + 1 |
3442 return line[:indent] + getattr(Fore, color) + line[indent:] + Fore.RESET | 3442 return line[:indent] + getattr(Fore, color) + line[indent:] + Fore.RESET |
3443 return line | 3443 return line |
3444 | 3444 |
3445 lines = CMDstatus.__doc__.splitlines() | 3445 lines = CMDstatus.__doc__.splitlines() |
3446 CMDstatus.__doc__ = '\n'.join(colorize_line(l) for l in lines) | 3446 CMDstatus.__doc__ = '\n'.join(colorize_line(l) for l in lines) |
3447 | 3447 |
3448 | 3448 |
| 3449 def write_json(path, contents): |
| 3450 with open(path, 'w') as f: |
| 3451 json.dump(contents, f) |
| 3452 |
| 3453 |
3449 @subcommand.usage('[issue_number]') | 3454 @subcommand.usage('[issue_number]') |
3450 def CMDissue(parser, args): | 3455 def CMDissue(parser, args): |
3451 """Sets or displays the current code review issue number. | 3456 """Sets or displays the current code review issue number. |
3452 | 3457 |
3453 Pass issue number 0 to clear the current issue. | 3458 Pass issue number 0 to clear the current issue. |
3454 """ | 3459 """ |
3455 parser.add_option('-r', '--reverse', action='store_true', | 3460 parser.add_option('-r', '--reverse', action='store_true', |
3456 help='Lookup the branch(es) for the specified issues. If ' | 3461 help='Lookup the branch(es) for the specified issues. If ' |
3457 'no issues are specified, all branches with mapped ' | 3462 'no issues are specified, all branches with mapped ' |
3458 'issues will be listed.') | 3463 'issues will be listed.') |
| 3464 parser.add_option('--json', help='Path to JSON output file.') |
3459 _add_codereview_select_options(parser) | 3465 _add_codereview_select_options(parser) |
3460 options, args = parser.parse_args(args) | 3466 options, args = parser.parse_args(args) |
3461 _process_codereview_select_options(parser, options) | 3467 _process_codereview_select_options(parser, options) |
3462 | 3468 |
3463 if options.reverse: | 3469 if options.reverse: |
3464 branches = RunGit(['for-each-ref', 'refs/heads', | 3470 branches = RunGit(['for-each-ref', 'refs/heads', |
3465 '--format=%(refname:short)']).splitlines() | 3471 '--format=%(refname:short)']).splitlines() |
3466 | 3472 |
3467 # Reverse issue lookup. | 3473 # Reverse issue lookup. |
3468 issue_branch_map = {} | 3474 issue_branch_map = {} |
3469 for branch in branches: | 3475 for branch in branches: |
3470 cl = Changelist(branchref=branch) | 3476 cl = Changelist(branchref=branch) |
3471 issue_branch_map.setdefault(cl.GetIssue(), []).append(branch) | 3477 issue_branch_map.setdefault(cl.GetIssue(), []).append(branch) |
3472 if not args: | 3478 if not args: |
3473 args = sorted(issue_branch_map.iterkeys()) | 3479 args = sorted(issue_branch_map.iterkeys()) |
| 3480 result = {} |
3474 for issue in args: | 3481 for issue in args: |
3475 if not issue: | 3482 if not issue: |
3476 continue | 3483 continue |
| 3484 result[int(issue)] = issue_branch_map.get(int(issue)) |
3477 print('Branch for issue number %s: %s' % ( | 3485 print('Branch for issue number %s: %s' % ( |
3478 issue, ', '.join(issue_branch_map.get(int(issue)) or ('None',)))) | 3486 issue, ', '.join(issue_branch_map.get(int(issue)) or ('None',)))) |
| 3487 if options.json: |
| 3488 write_json(options.json, result) |
3479 else: | 3489 else: |
3480 cl = Changelist(codereview=options.forced_codereview) | 3490 cl = Changelist(codereview=options.forced_codereview) |
3481 if len(args) > 0: | 3491 if len(args) > 0: |
3482 try: | 3492 try: |
3483 issue = int(args[0]) | 3493 issue = int(args[0]) |
3484 except ValueError: | 3494 except ValueError: |
3485 DieWithError('Pass a number to set the issue or none to list it.\n' | 3495 DieWithError('Pass a number to set the issue or none to list it.\n' |
3486 'Maybe you want to run git cl status?') | 3496 'Maybe you want to run git cl status?') |
3487 cl.SetIssue(issue) | 3497 cl.SetIssue(issue) |
3488 print('Issue number: %s (%s)' % (cl.GetIssue(), cl.GetIssueURL())) | 3498 print('Issue number: %s (%s)' % (cl.GetIssue(), cl.GetIssueURL())) |
| 3499 if options.json: |
| 3500 write_json(options.json, { |
| 3501 'issue': cl.GetIssue(), |
| 3502 'issue_url': cl.GetIssueURL(), |
| 3503 }) |
3489 return 0 | 3504 return 0 |
3490 | 3505 |
3491 | 3506 |
3492 def CMDcomments(parser, args): | 3507 def CMDcomments(parser, args): |
3493 """Shows or posts review comments for any changelist.""" | 3508 """Shows or posts review comments for any changelist.""" |
3494 parser.add_option('-a', '--add-comment', dest='comment', | 3509 parser.add_option('-a', '--add-comment', dest='comment', |
3495 help='comment to add to an issue') | 3510 help='comment to add to an issue') |
3496 parser.add_option('-i', dest='issue', | 3511 parser.add_option('-i', dest='issue', |
3497 help="review issue id (defaults to current issue)") | 3512 help="review issue id (defaults to current issue)") |
3498 parser.add_option('-j', '--json-file', | 3513 parser.add_option('-j', '--json-file', |
(...skipping 1688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5187 if __name__ == '__main__': | 5202 if __name__ == '__main__': |
5188 # These affect sys.stdout so do it outside of main() to simplify mocks in | 5203 # These affect sys.stdout so do it outside of main() to simplify mocks in |
5189 # unit testing. | 5204 # unit testing. |
5190 fix_encoding.fix_encoding() | 5205 fix_encoding.fix_encoding() |
5191 setup_color.init() | 5206 setup_color.init() |
5192 try: | 5207 try: |
5193 sys.exit(main(sys.argv[1:])) | 5208 sys.exit(main(sys.argv[1:])) |
5194 except KeyboardInterrupt: | 5209 except KeyboardInterrupt: |
5195 sys.stderr.write('interrupted\n') | 5210 sys.stderr.write('interrupted\n') |
5196 sys.exit(1) | 5211 sys.exit(1) |
OLD | NEW |