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 upstream_commit = None |
| 1969 cl = Changelist() |
| 1970 upstream_branch = cl.GetUpstreamBranch() |
| 1971 if upstream_branch: |
| 1972 upstream_commit = RunGit(['merge-base', 'HEAD', upstream_branch]) |
| 1973 upstream_commit = upstream_commit.strip() |
| 1974 |
| 1975 if not upstream_commit: |
| 1976 DieWithError('Could not find base commit for this branch. ' |
| 1977 'Are you in detached state?') |
| 1978 |
| 1979 diff_cmd.append(upstream_commit) |
| 1980 |
| 1981 # Handle source file filtering. |
| 1982 diff_cmd.append('--') |
| 1983 diff_cmd += ['*' + ext for ext in CLANG_EXTS] |
| 1984 diff_output = RunGit(diff_cmd) |
| 1985 |
| 1986 if opts.full: |
| 1987 # diff_output is a list of files to send to clang-format. |
| 1988 files = diff_output.splitlines() |
1958 if not files: | 1989 if not files: |
1959 print "Nothing to format." | 1990 print "Nothing to format." |
1960 return 0 | 1991 return 0 |
1961 RunCommand(['clang-format', '-i'] + files) | 1992 RunCommand(['clang-format', '-i', '-style', 'Chromium'] + files) |
1962 else: | 1993 else: |
| 1994 # diff_output is a patch to send to clang-format-diff.py |
1963 cfd_path = os.path.join('/usr', 'lib', 'clang-format', | 1995 cfd_path = os.path.join('/usr', 'lib', 'clang-format', |
1964 'clang-format-diff.py') | 1996 'clang-format-diff.py') |
1965 if not os.path.exists(cfd_path): | 1997 if not os.path.exists(cfd_path): |
1966 print >> sys.stderr, 'Could not find clang-format-diff at %s.' % cfd_path | 1998 DieWithError('Could not find clang-format-diff at %s.' % cfd_path) |
1967 return 2 | 1999 cmd = [sys.executable, cfd_path, '-style', 'Chromium'] |
1968 cmd = ['diff', '-U0', '@{u}', '--'] + ['.*' + ext for ext in CLANG_EXTS] | 2000 RunCommand(cmd, stdin=diff_output) |
1969 diff = RunGit(cmd) | |
1970 cmd = [sys.executable, '/usr/lib/clang-format/clang-format-diff.py', | |
1971 '-style', 'Chromium'] | |
1972 RunCommand(cmd, stdin=diff) | |
1973 | 2001 |
1974 return 0 | 2002 return 0 |
1975 | 2003 |
1976 | 2004 |
1977 def Command(name): | 2005 def Command(name): |
1978 return getattr(sys.modules[__name__], 'CMD' + name, None) | 2006 return getattr(sys.modules[__name__], 'CMD' + name, None) |
1979 | 2007 |
1980 | 2008 |
1981 def CMDhelp(parser, args): | 2009 def CMDhelp(parser, args): |
1982 """print list of commands or help for a specific command""" | 2010 """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') | 2080 GenUsage(parser, 'help') |
2053 return CMDhelp(parser, argv) | 2081 return CMDhelp(parser, argv) |
2054 | 2082 |
2055 | 2083 |
2056 if __name__ == '__main__': | 2084 if __name__ == '__main__': |
2057 # These affect sys.stdout so do it outside of main() to simplify mocks in | 2085 # These affect sys.stdout so do it outside of main() to simplify mocks in |
2058 # unit testing. | 2086 # unit testing. |
2059 fix_encoding.fix_encoding() | 2087 fix_encoding.fix_encoding() |
2060 colorama.init() | 2088 colorama.init() |
2061 sys.exit(main(sys.argv[1:])) | 2089 sys.exit(main(sys.argv[1:])) |
OLD | NEW |