| 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 2317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2328 | 2328 |
| 2329 | 2329 |
| 2330 @subcommand.usage('[files or directories to diff]') | 2330 @subcommand.usage('[files or directories to diff]') |
| 2331 def CMDformat(parser, args): | 2331 def CMDformat(parser, args): |
| 2332 """Runs clang-format on the diff.""" | 2332 """Runs clang-format on the diff.""" |
| 2333 CLANG_EXTS = ['.cc', '.cpp', '.h', '.mm', '.proto'] | 2333 CLANG_EXTS = ['.cc', '.cpp', '.h', '.mm', '.proto'] |
| 2334 parser.add_option('--full', action='store_true', | 2334 parser.add_option('--full', action='store_true', |
| 2335 help='Reformat the full content of all touched files') | 2335 help='Reformat the full content of all touched files') |
| 2336 parser.add_option('--dry-run', action='store_true', | 2336 parser.add_option('--dry-run', action='store_true', |
| 2337 help='Don\'t modify any file on disk.') | 2337 help='Don\'t modify any file on disk.') |
| 2338 parser.add_option('--diff', action='store_true', |
| 2339 help='Print diff to stdout rather than modifying files.') |
| 2338 opts, args = parser.parse_args(args) | 2340 opts, args = parser.parse_args(args) |
| 2339 | 2341 |
| 2340 # git diff generates paths against the root of the repository. Change | 2342 # git diff generates paths against the root of the repository. Change |
| 2341 # to that directory so clang-format can find files even within subdirs. | 2343 # to that directory so clang-format can find files even within subdirs. |
| 2342 rel_base_path = settings.GetRelativeRoot() | 2344 rel_base_path = settings.GetRelativeRoot() |
| 2343 if rel_base_path: | 2345 if rel_base_path: |
| 2344 os.chdir(rel_base_path) | 2346 os.chdir(rel_base_path) |
| 2345 | 2347 |
| 2346 # Generate diff for the current branch's changes. | 2348 # Generate diff for the current branch's changes. |
| 2347 diff_cmd = ['diff', '--no-ext-diff', '--no-prefix'] | 2349 diff_cmd = ['diff', '--no-ext-diff', '--no-prefix'] |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2392 except clang_format.NotFoundError, e: | 2394 except clang_format.NotFoundError, e: |
| 2393 DieWithError(e) | 2395 DieWithError(e) |
| 2394 | 2396 |
| 2395 if opts.full: | 2397 if opts.full: |
| 2396 # diff_output is a list of files to send to clang-format. | 2398 # diff_output is a list of files to send to clang-format. |
| 2397 files = diff_output.splitlines() | 2399 files = diff_output.splitlines() |
| 2398 if not files: | 2400 if not files: |
| 2399 print "Nothing to format." | 2401 print "Nothing to format." |
| 2400 return 0 | 2402 return 0 |
| 2401 cmd = [clang_format_tool] | 2403 cmd = [clang_format_tool] |
| 2402 if not opts.dry_run: | 2404 if not opts.dry_run and not opts.diff: |
| 2403 cmd.append('-i') | 2405 cmd.append('-i') |
| 2404 stdout = RunCommand(cmd + files, cwd=top_dir) | 2406 stdout = RunCommand(cmd + files, cwd=top_dir) |
| 2407 if opts.diff: |
| 2408 sys.stdout.write(stdout) |
| 2405 else: | 2409 else: |
| 2406 env = os.environ.copy() | 2410 env = os.environ.copy() |
| 2407 env['PATH'] = os.path.dirname(clang_format_tool) | 2411 env['PATH'] = os.path.dirname(clang_format_tool) |
| 2408 # diff_output is a patch to send to clang-format-diff.py | 2412 # diff_output is a patch to send to clang-format-diff.py |
| 2409 try: | 2413 try: |
| 2410 script = clang_format.FindClangFormatScriptInChromiumTree( | 2414 script = clang_format.FindClangFormatScriptInChromiumTree( |
| 2411 'clang-format-diff.py') | 2415 'clang-format-diff.py') |
| 2412 except clang_format.NotFoundError, e: | 2416 except clang_format.NotFoundError, e: |
| 2413 DieWithError(e) | 2417 DieWithError(e) |
| 2414 | 2418 |
| 2415 cmd = [sys.executable, script, '-p0'] | 2419 cmd = [sys.executable, script, '-p0'] |
| 2416 if not opts.dry_run: | 2420 if not opts.dry_run and not opts.diff: |
| 2417 cmd.append('-i') | 2421 cmd.append('-i') |
| 2418 | 2422 |
| 2419 stdout = RunCommand(cmd, stdin=diff_output, cwd=top_dir, env=env) | 2423 stdout = RunCommand(cmd, stdin=diff_output, cwd=top_dir, env=env) |
| 2424 if opts.diff: |
| 2425 sys.stdout.write(stdout) |
| 2420 if opts.dry_run and len(stdout) > 0: | 2426 if opts.dry_run and len(stdout) > 0: |
| 2421 return 2 | 2427 return 2 |
| 2422 | 2428 |
| 2423 return 0 | 2429 return 0 |
| 2424 | 2430 |
| 2425 | 2431 |
| 2426 class OptionParser(optparse.OptionParser): | 2432 class OptionParser(optparse.OptionParser): |
| 2427 """Creates the option parse and add --verbose support.""" | 2433 """Creates the option parse and add --verbose support.""" |
| 2428 def __init__(self, *args, **kwargs): | 2434 def __init__(self, *args, **kwargs): |
| 2429 optparse.OptionParser.__init__( | 2435 optparse.OptionParser.__init__( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2461 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' | 2467 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' |
| 2462 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e))) | 2468 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e))) |
| 2463 | 2469 |
| 2464 | 2470 |
| 2465 if __name__ == '__main__': | 2471 if __name__ == '__main__': |
| 2466 # These affect sys.stdout so do it outside of main() to simplify mocks in | 2472 # These affect sys.stdout so do it outside of main() to simplify mocks in |
| 2467 # unit testing. | 2473 # unit testing. |
| 2468 fix_encoding.fix_encoding() | 2474 fix_encoding.fix_encoding() |
| 2469 colorama.init() | 2475 colorama.init() |
| 2470 sys.exit(main(sys.argv[1:])) | 2476 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |