| 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.""" | 8 """A git-command for integrating reviews on Rietveld.""" |
| 9 | 9 |
| 10 from distutils.version import LooseVersion | 10 from distutils.version import LooseVersion |
| (...skipping 3516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3527 return diff_cmd | 3527 return diff_cmd |
| 3528 | 3528 |
| 3529 def MatchingFileType(file_name, extensions): | 3529 def MatchingFileType(file_name, extensions): |
| 3530 """Returns true if the file name ends with one of the given extensions.""" | 3530 """Returns true if the file name ends with one of the given extensions.""" |
| 3531 return bool([ext for ext in extensions if file_name.lower().endswith(ext)]) | 3531 return bool([ext for ext in extensions if file_name.lower().endswith(ext)]) |
| 3532 | 3532 |
| 3533 @subcommand.usage('[files or directories to diff]') | 3533 @subcommand.usage('[files or directories to diff]') |
| 3534 def CMDformat(parser, args): | 3534 def CMDformat(parser, args): |
| 3535 """Runs auto-formatting tools (clang-format etc.) on the diff.""" | 3535 """Runs auto-formatting tools (clang-format etc.) on the diff.""" |
| 3536 CLANG_EXTS = ['.cc', '.cpp', '.h', '.mm', '.proto', '.java'] | 3536 CLANG_EXTS = ['.cc', '.cpp', '.h', '.mm', '.proto', '.java'] |
| 3537 GN_EXTS = ['.gn', '.gni'] |
| 3537 parser.add_option('--full', action='store_true', | 3538 parser.add_option('--full', action='store_true', |
| 3538 help='Reformat the full content of all touched files') | 3539 help='Reformat the full content of all touched files') |
| 3539 parser.add_option('--dry-run', action='store_true', | 3540 parser.add_option('--dry-run', action='store_true', |
| 3540 help='Don\'t modify any file on disk.') | 3541 help='Don\'t modify any file on disk.') |
| 3541 parser.add_option('--python', action='store_true', | 3542 parser.add_option('--python', action='store_true', |
| 3542 help='Format python code with yapf (experimental).') | 3543 help='Format python code with yapf (experimental).') |
| 3543 parser.add_option('--diff', action='store_true', | 3544 parser.add_option('--diff', action='store_true', |
| 3544 help='Print diff to stdout rather than modifying files.') | 3545 help='Print diff to stdout rather than modifying files.') |
| 3545 opts, args = parser.parse_args(args) | 3546 opts, args = parser.parse_args(args) |
| 3546 | 3547 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 3567 | 3568 |
| 3568 changed_files_cmd = BuildGitDiffCmd('--name-only', upstream_commit, args) | 3569 changed_files_cmd = BuildGitDiffCmd('--name-only', upstream_commit, args) |
| 3569 diff_output = RunGit(changed_files_cmd) | 3570 diff_output = RunGit(changed_files_cmd) |
| 3570 diff_files = diff_output.splitlines() | 3571 diff_files = diff_output.splitlines() |
| 3571 # Filter out files deleted by this CL | 3572 # Filter out files deleted by this CL |
| 3572 diff_files = [x for x in diff_files if os.path.isfile(x)] | 3573 diff_files = [x for x in diff_files if os.path.isfile(x)] |
| 3573 | 3574 |
| 3574 clang_diff_files = [x for x in diff_files if MatchingFileType(x, CLANG_EXTS)] | 3575 clang_diff_files = [x for x in diff_files if MatchingFileType(x, CLANG_EXTS)] |
| 3575 python_diff_files = [x for x in diff_files if MatchingFileType(x, ['.py'])] | 3576 python_diff_files = [x for x in diff_files if MatchingFileType(x, ['.py'])] |
| 3576 dart_diff_files = [x for x in diff_files if MatchingFileType(x, ['.dart'])] | 3577 dart_diff_files = [x for x in diff_files if MatchingFileType(x, ['.dart'])] |
| 3578 gn_diff_files = [x for x in diff_files if MatchingFileType(x, GN_EXTS)] |
| 3577 | 3579 |
| 3578 top_dir = os.path.normpath( | 3580 top_dir = os.path.normpath( |
| 3579 RunGit(["rev-parse", "--show-toplevel"]).rstrip('\n')) | 3581 RunGit(["rev-parse", "--show-toplevel"]).rstrip('\n')) |
| 3580 | 3582 |
| 3581 # Locate the clang-format binary in the checkout | 3583 # Locate the clang-format binary in the checkout |
| 3582 try: | 3584 try: |
| 3583 clang_format_tool = clang_format.FindClangFormatToolInChromiumTree() | 3585 clang_format_tool = clang_format.FindClangFormatToolInChromiumTree() |
| 3584 except clang_format.NotFoundError, e: | 3586 except clang_format.NotFoundError, e: |
| 3585 DieWithError(e) | 3587 DieWithError(e) |
| 3586 | 3588 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3648 command.extend(dart_diff_files) | 3650 command.extend(dart_diff_files) |
| 3649 | 3651 |
| 3650 stdout = RunCommand(command, cwd=top_dir, env=env) | 3652 stdout = RunCommand(command, cwd=top_dir, env=env) |
| 3651 if opts.dry_run and stdout: | 3653 if opts.dry_run and stdout: |
| 3652 return_value = 2 | 3654 return_value = 2 |
| 3653 except dart_format.NotFoundError as e: | 3655 except dart_format.NotFoundError as e: |
| 3654 print ('Warning: Unable to check Dart code formatting. Dart SDK not ' + | 3656 print ('Warning: Unable to check Dart code formatting. Dart SDK not ' + |
| 3655 'found in this checkout. Files in other languages are still ' + | 3657 'found in this checkout. Files in other languages are still ' + |
| 3656 'formatted.') | 3658 'formatted.') |
| 3657 | 3659 |
| 3660 # Format GN build files. Always run on full build files for canonical form. |
| 3661 if gn_diff_files: |
| 3662 cmd = ['gn', 'format'] |
| 3663 if not opts.dry_run and not opts.diff: |
| 3664 cmd.append('--in-place') |
| 3665 for gn_diff_file in gn_diff_files: |
| 3666 stdout = RunCommand(cmd + [gn_diff_file], cwd=top_dir) |
| 3667 if opts.diff: |
| 3668 sys.stdout.write(stdout) |
| 3669 |
| 3658 return return_value | 3670 return return_value |
| 3659 | 3671 |
| 3660 | 3672 |
| 3661 @subcommand.usage('<codereview url or issue id>') | 3673 @subcommand.usage('<codereview url or issue id>') |
| 3662 def CMDcheckout(parser, args): | 3674 def CMDcheckout(parser, args): |
| 3663 """Checks out a branch associated with a given Rietveld issue.""" | 3675 """Checks out a branch associated with a given Rietveld issue.""" |
| 3664 _, args = parser.parse_args(args) | 3676 _, args = parser.parse_args(args) |
| 3665 | 3677 |
| 3666 if len(args) != 1: | 3678 if len(args) != 1: |
| 3667 parser.print_help() | 3679 parser.print_help() |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3754 if __name__ == '__main__': | 3766 if __name__ == '__main__': |
| 3755 # These affect sys.stdout so do it outside of main() to simplify mocks in | 3767 # These affect sys.stdout so do it outside of main() to simplify mocks in |
| 3756 # unit testing. | 3768 # unit testing. |
| 3757 fix_encoding.fix_encoding() | 3769 fix_encoding.fix_encoding() |
| 3758 colorama.init() | 3770 colorama.init() |
| 3759 try: | 3771 try: |
| 3760 sys.exit(main(sys.argv[1:])) | 3772 sys.exit(main(sys.argv[1:])) |
| 3761 except KeyboardInterrupt: | 3773 except KeyboardInterrupt: |
| 3762 sys.stderr.write('interrupted\n') | 3774 sys.stderr.write('interrupted\n') |
| 3763 sys.exit(1) | 3775 sys.exit(1) |
| OLD | NEW |