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.append('--name-only') |
| 1960 else: | |
| 1961 # Only generate context-less patches. | |
| 1962 diff_cmd.append('-U0') | |
| 1963 | |
| 1964 # Grab the merge-base commit, i.e. the upstream commit of the current | |
| 1965 # branch when it was created or the last time it was rebased. This is | |
| 1966 # to cover the case where the user may have called "git fetch origin", | |
| 1967 # moving the origin branch to a newer commit, but hasn't rebased yet. | |
| 1968 cl = Changelist() | |
| 1969 upstream_commit = RunGit(['merge-base', 'HEAD', cl.GetUpstreamBranch()]) | |
|
M-A Ruel
2013/05/17 14:13:59
Note that cl.GetUpstreamBranch() could return not
digit1
2013/05/17 15:00:03
I see, I've updated the code to do that. Thanks, t
| |
| 1970 diff_cmd.append(upstream_commit.strip()) | |
| 1971 | |
| 1972 # Handle source file filtering. | |
| 1973 diff_cmd.append('--') | |
| 1974 diff_cmd += ['*' + ext for ext in CLANG_EXTS] | |
| 1975 diff_output = RunGit(diff_cmd) | |
| 1976 | |
| 1977 if opts.full: | |
| 1978 # diff_output is a list of files to send to clang-format. | |
| 1979 files = diff_output.split() | |
|
M-A Ruel
2013/05/17 14:13:59
I'd prefer splitlines() so you don't break files w
digit1
2013/05/17 15:00:03
Done.
| |
| 1958 if not files: | 1980 if not files: |
| 1959 print "Nothing to format." | 1981 print "Nothing to format." |
| 1960 return 0 | 1982 return 0 |
| 1961 RunCommand(['clang-format', '-i'] + files) | 1983 RunCommand(['clang-format', '-i', '-style', 'Chromium'] + files) |
| 1962 else: | 1984 else: |
| 1985 # diff_output is a patch to send to clang-format-diff.py | |
| 1963 cfd_path = os.path.join('/usr', 'lib', 'clang-format', | 1986 cfd_path = os.path.join('/usr', 'lib', 'clang-format', |
| 1964 'clang-format-diff.py') | 1987 'clang-format-diff.py') |
| 1965 if not os.path.exists(cfd_path): | 1988 if not os.path.exists(cfd_path): |
| 1966 print >> sys.stderr, 'Could not find clang-format-diff at %s.' % cfd_path | 1989 print >> sys.stderr, 'Could not find clang-format-diff at %s.' % cfd_path |
| 1967 return 2 | 1990 return 2 |
| 1968 cmd = ['diff', '-U0', '@{u}', '--'] + ['.*' + ext for ext in CLANG_EXTS] | 1991 cmd = [sys.executable, cfd_path, '-style', 'Chromium'] |
| 1969 diff = RunGit(cmd) | 1992 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 | 1993 |
| 1974 return 0 | 1994 return 0 |
| 1975 | 1995 |
| 1976 | 1996 |
| 1977 def Command(name): | 1997 def Command(name): |
| 1978 return getattr(sys.modules[__name__], 'CMD' + name, None) | 1998 return getattr(sys.modules[__name__], 'CMD' + name, None) |
| 1979 | 1999 |
| 1980 | 2000 |
| 1981 def CMDhelp(parser, args): | 2001 def CMDhelp(parser, args): |
| 1982 """print list of commands or help for a specific command""" | 2002 """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') | 2072 GenUsage(parser, 'help') |
| 2053 return CMDhelp(parser, argv) | 2073 return CMDhelp(parser, argv) |
| 2054 | 2074 |
| 2055 | 2075 |
| 2056 if __name__ == '__main__': | 2076 if __name__ == '__main__': |
| 2057 # These affect sys.stdout so do it outside of main() to simplify mocks in | 2077 # These affect sys.stdout so do it outside of main() to simplify mocks in |
| 2058 # unit testing. | 2078 # unit testing. |
| 2059 fix_encoding.fix_encoding() | 2079 fix_encoding.fix_encoding() |
| 2060 colorama.init() | 2080 colorama.init() |
| 2061 sys.exit(main(sys.argv[1:])) | 2081 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |