| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 'Command "%s" failed.\n%s' % ( | 73 'Command "%s" failed.\n%s' % ( |
| 74 ' '.join(args), error_message or e.stdout or '')) | 74 ' '.join(args), error_message or e.stdout or '')) |
| 75 return e.stdout | 75 return e.stdout |
| 76 | 76 |
| 77 | 77 |
| 78 def RunGit(args, **kwargs): | 78 def RunGit(args, **kwargs): |
| 79 """Returns stdout.""" | 79 """Returns stdout.""" |
| 80 return RunCommand(['git'] + args, **kwargs) | 80 return RunCommand(['git'] + args, **kwargs) |
| 81 | 81 |
| 82 | 82 |
| 83 def RunGitWithCode(args): | 83 def RunGitWithCode(args, suppress_stderr=False): |
| 84 """Returns return code and stdout.""" | 84 """Returns return code and stdout.""" |
| 85 try: | 85 try: |
| 86 env = os.environ.copy() | 86 env = os.environ.copy() |
| 87 # 'cat' is a magical git string that disables pagers on all platforms. | 87 # 'cat' is a magical git string that disables pagers on all platforms. |
| 88 env['GIT_PAGER'] = 'cat' | 88 env['GIT_PAGER'] = 'cat' |
| 89 if suppress_stderr: |
| 90 stderr = subprocess2.VOID |
| 91 else: |
| 92 stderr = sys.stderr |
| 89 out, code = subprocess2.communicate(['git'] + args, | 93 out, code = subprocess2.communicate(['git'] + args, |
| 90 env=env, | 94 env=env, |
| 91 stdout=subprocess2.PIPE) | 95 stdout=subprocess2.PIPE, |
| 96 stderr=stderr) |
| 92 return code, out[0] | 97 return code, out[0] |
| 93 except ValueError: | 98 except ValueError: |
| 94 # When the subprocess fails, it returns None. That triggers a ValueError | 99 # When the subprocess fails, it returns None. That triggers a ValueError |
| 95 # when trying to unpack the return value into (out, code). | 100 # when trying to unpack the return value into (out, code). |
| 96 return 1, '' | 101 return 1, '' |
| 97 | 102 |
| 98 | 103 |
| 99 def IsGitVersionAtLeast(min_version): | 104 def IsGitVersionAtLeast(min_version): |
| 100 prefix = 'git version ' | 105 prefix = 'git version ' |
| 101 version = RunGit(['--version']).strip() | 106 version = RunGit(['--version']).strip() |
| (...skipping 2192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2294 [f.LocalPath() for f in | 2299 [f.LocalPath() for f in |
| 2295 cl.GetChange(base_branch, None).AffectedFiles()], | 2300 cl.GetChange(base_branch, None).AffectedFiles()], |
| 2296 change.RepositoryRoot(), author, | 2301 change.RepositoryRoot(), author, |
| 2297 fopen=file, os_path=os.path, glob=glob.glob, | 2302 fopen=file, os_path=os.path, glob=glob.glob, |
| 2298 disable_color=options.no_color).run() | 2303 disable_color=options.no_color).run() |
| 2299 | 2304 |
| 2300 | 2305 |
| 2301 def CMDformat(parser, args): | 2306 def CMDformat(parser, args): |
| 2302 """Runs clang-format on the diff.""" | 2307 """Runs clang-format on the diff.""" |
| 2303 CLANG_EXTS = ['.cc', '.cpp', '.h'] | 2308 CLANG_EXTS = ['.cc', '.cpp', '.h'] |
| 2304 parser.add_option('--full', action='store_true', default=False) | 2309 parser.add_option('--full', action='store_true', |
| 2310 help='Reformat the full content of all touched files') |
| 2311 parser.add_option('--dry-run', action='store_true', |
| 2312 help='Don\'t modify any file on disk.') |
| 2305 opts, args = parser.parse_args(args) | 2313 opts, args = parser.parse_args(args) |
| 2306 if args: | 2314 if args: |
| 2307 parser.error('Unrecognized args: %s' % ' '.join(args)) | 2315 parser.error('Unrecognized args: %s' % ' '.join(args)) |
| 2308 | 2316 |
| 2309 # git diff generates paths against the root of the repository. Change | 2317 # git diff generates paths against the root of the repository. Change |
| 2310 # to that directory so clang-format can find files even within subdirs. | 2318 # to that directory so clang-format can find files even within subdirs. |
| 2311 rel_base_path = RunGit(['rev-parse', '--show-cdup']).strip() | 2319 rel_base_path = RunGit(['rev-parse', '--show-cdup']).strip() |
| 2312 if rel_base_path: | 2320 if rel_base_path: |
| 2313 os.chdir(rel_base_path) | 2321 os.chdir(rel_base_path) |
| 2314 | 2322 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2351 clang_format_tool = clang_format.FindClangFormatToolInChromiumTree() | 2359 clang_format_tool = clang_format.FindClangFormatToolInChromiumTree() |
| 2352 except clang_format.NotFoundError, e: | 2360 except clang_format.NotFoundError, e: |
| 2353 DieWithError(e) | 2361 DieWithError(e) |
| 2354 | 2362 |
| 2355 if opts.full: | 2363 if opts.full: |
| 2356 # diff_output is a list of files to send to clang-format. | 2364 # diff_output is a list of files to send to clang-format. |
| 2357 files = diff_output.splitlines() | 2365 files = diff_output.splitlines() |
| 2358 if not files: | 2366 if not files: |
| 2359 print "Nothing to format." | 2367 print "Nothing to format." |
| 2360 return 0 | 2368 return 0 |
| 2361 RunCommand([clang_format_tool, '-i'] + files, | 2369 cmd = [clang_format_tool] |
| 2362 cwd=top_dir) | 2370 if not opts.dry_run: |
| 2371 cmd.append('-i') |
| 2372 stdout = RunCommand(cmd + files, cwd=top_dir) |
| 2363 else: | 2373 else: |
| 2364 env = os.environ.copy() | 2374 env = os.environ.copy() |
| 2365 env['PATH'] = os.path.dirname(clang_format_tool) | 2375 env['PATH'] = os.path.dirname(clang_format_tool) |
| 2366 # diff_output is a patch to send to clang-format-diff.py | 2376 # diff_output is a patch to send to clang-format-diff.py |
| 2367 try: | 2377 try: |
| 2368 script = clang_format.FindClangFormatScriptInChromiumTree( | 2378 script = clang_format.FindClangFormatScriptInChromiumTree( |
| 2369 'clang-format-diff.py') | 2379 'clang-format-diff.py') |
| 2370 except clang_format.NotFoundError, e: | 2380 except clang_format.NotFoundError, e: |
| 2371 DieWithError(e) | 2381 DieWithError(e) |
| 2372 | 2382 |
| 2373 cmd = [sys.executable, script, '-p0', '-i'] | 2383 cmd = [sys.executable, script, '-p0'] |
| 2384 if not opts.dry_run: |
| 2385 cmd.append('-i') |
| 2374 | 2386 |
| 2375 RunCommand(cmd, stdin=diff_output, cwd=top_dir, env=env) | 2387 stdout = RunCommand(cmd, stdin=diff_output, cwd=top_dir, env=env) |
| 2388 if opts.dry_run and len(stdout) > 0: |
| 2389 return 2 |
| 2376 | 2390 |
| 2377 return 0 | 2391 return 0 |
| 2378 | 2392 |
| 2379 | 2393 |
| 2380 class OptionParser(optparse.OptionParser): | 2394 class OptionParser(optparse.OptionParser): |
| 2381 """Creates the option parse and add --verbose support.""" | 2395 """Creates the option parse and add --verbose support.""" |
| 2382 def __init__(self, *args, **kwargs): | 2396 def __init__(self, *args, **kwargs): |
| 2383 optparse.OptionParser.__init__( | 2397 optparse.OptionParser.__init__( |
| 2384 self, *args, prog='git cl', version=__version__, **kwargs) | 2398 self, *args, prog='git cl', version=__version__, **kwargs) |
| 2385 self.add_option( | 2399 self.add_option( |
| (...skipping 29 matching lines...) Expand all Loading... |
| 2415 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' | 2429 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' |
| 2416 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e))) | 2430 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e))) |
| 2417 | 2431 |
| 2418 | 2432 |
| 2419 if __name__ == '__main__': | 2433 if __name__ == '__main__': |
| 2420 # These affect sys.stdout so do it outside of main() to simplify mocks in | 2434 # These affect sys.stdout so do it outside of main() to simplify mocks in |
| 2421 # unit testing. | 2435 # unit testing. |
| 2422 fix_encoding.fix_encoding() | 2436 fix_encoding.fix_encoding() |
| 2423 colorama.init() | 2437 colorama.init() |
| 2424 sys.exit(main(sys.argv[1:])) | 2438 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |