Chromium Code Reviews| 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 import json | 10 import json | 
| (...skipping 1934 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1945 | 1945 | 
| 1946 | 1946 | 
| 1947 def CMDformat(parser, args): | 1947 def CMDformat(parser, args): | 
| 1948 """run clang-format on the diff""" | 1948 """run clang-format on the diff""" | 
| 1949 CLANG_EXTS = ['.cc', '.cpp', '.h'] | 1949 CLANG_EXTS = ['.cc', '.cpp', '.h'] | 
| 1950 parser.add_option('--full', action='store_true', default=False) | 1950 parser.add_option('--full', action='store_true', default=False) | 
| 1951 opts, args = parser.parse_args(args) | 1951 opts, args = parser.parse_args(args) | 
| 1952 if args: | 1952 if args: | 
| 1953 parser.error('Unrecognized args: %s' % ' '.join(args)) | 1953 parser.error('Unrecognized args: %s' % ' '.join(args)) | 
| 1954 | 1954 | 
| 1955 # Generate diff for the current branch's changes. | |
| 1956 diff_cmd = ['diff', '--no-ext-diff'] | |
| 1955 if opts.full: | 1957 if opts.full: | 
| 1956 cmd = ['diff', '--name-only', '--'] + ['.*' + ext for ext in CLANG_EXTS] | 1958 # Only list the names of modified files. | 
| 1957 files = RunGit(cmd).split() | 1959 diff_cmd += ['--name-only'] | 
| 
 
M-A Ruel
2013/05/17 12:41:48
I highly prefer append() even if it is clunky. It'
 
 | |
| 1960 else: | |
| 1961 # Only generate context-less patches. | |
| 1962 diff_cmd += ['-U0'] | |
| 1963 | |
| 1964 # Handle branch and source file filtering. | |
| 1965 diff_cmd += ['@{u}', '--'] + ['*' + ext for ext in CLANG_EXTS] | |
| 
 
M-A Ruel
2013/05/17 12:41:48
Note that @{u} may not be exactly what you want. I
 
 | |
| 1966 diff_output = RunGit(diff_cmd) | |
| 1967 | |
| 1968 if opts.full: | |
| 1969 # diff_output is a list of files to send to clang-format. | |
| 1970 files = diff_output.split() | |
| 1958 if not files: | 1971 if not files: | 
| 1959 print "Nothing to format." | 1972 print "Nothing to format." | 
| 1960 return 0 | 1973 return 0 | 
| 1961 RunCommand(['clang-format', '-i'] + files) | 1974 RunCommand(['clang-format', '-i', '-style', 'Chromium'] + files) | 
| 1962 else: | 1975 else: | 
| 1976 # diff_output is a patch to send to clang-format-diff.py | |
| 1963 cfd_path = os.path.join('/usr', 'lib', 'clang-format', | 1977 cfd_path = os.path.join('/usr', 'lib', 'clang-format', | 
| 1964 'clang-format-diff.py') | 1978 'clang-format-diff.py') | 
| 1965 if not os.path.exists(cfd_path): | 1979 if not os.path.exists(cfd_path): | 
| 1966 print >> sys.stderr, 'Could not find clang-format-diff at %s.' % cfd_path | 1980 print >> sys.stderr, 'Could not find clang-format-diff at %s.' % cfd_path | 
| 1967 return 2 | 1981 return 2 | 
| 1968 cmd = ['diff', '-U0', '@{u}', '--'] + ['.*' + ext for ext in CLANG_EXTS] | 1982 cmd = [sys.executable, cfd_path, '-style', 'Chromium'] | 
| 1969 diff = RunGit(cmd) | 1983 RunCommand(cmd, stdin=diff_output) | 
| 1970 cmd = [sys.executable, '/usr/lib/clang-format/clang-format-diff.py', | |
| 1971 '-style', 'Chromium'] | |
| 1972 RunCommand(cmd, stdin=diff) | |
| 1973 | 1984 | 
| 1974 return 0 | 1985 return 0 | 
| 1975 | 1986 | 
| 1976 | 1987 | 
| 1977 def Command(name): | 1988 def Command(name): | 
| 1978 return getattr(sys.modules[__name__], 'CMD' + name, None) | 1989 return getattr(sys.modules[__name__], 'CMD' + name, None) | 
| 1979 | 1990 | 
| 1980 | 1991 | 
| 1981 def CMDhelp(parser, args): | 1992 def CMDhelp(parser, args): | 
| 1982 """print list of commands or help for a specific command""" | 1993 """print list of commands or help for a specific command""" | 
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2052 GenUsage(parser, 'help') | 2063 GenUsage(parser, 'help') | 
| 2053 return CMDhelp(parser, argv) | 2064 return CMDhelp(parser, argv) | 
| 2054 | 2065 | 
| 2055 | 2066 | 
| 2056 if __name__ == '__main__': | 2067 if __name__ == '__main__': | 
| 2057 # These affect sys.stdout so do it outside of main() to simplify mocks in | 2068 # These affect sys.stdout so do it outside of main() to simplify mocks in | 
| 2058 # unit testing. | 2069 # unit testing. | 
| 2059 fix_encoding.fix_encoding() | 2070 fix_encoding.fix_encoding() | 
| 2060 colorama.init() | 2071 colorama.init() | 
| 2061 sys.exit(main(sys.argv[1:])) | 2072 sys.exit(main(sys.argv[1:])) | 
| OLD | NEW |