Chromium Code Reviews

Side by Side Diff: git_cl.py

Issue 14942012: Fix 'git cl format' (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Use splitlines() Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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...)
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 if cl:
M-A Ruel 2013/05/17 15:13:03 It's an object so it'll always be not false.
digit1 2013/05/17 15:21:08 Doh :)
1971 upstream_commit = RunGit(['merge-base', 'HEAD', cl.GetUpstreamBranch()])
1972 upstream_commit = upstream_commit.splitlines()
M-A Ruel 2013/05/17 15:13:03 strip(), not splitlines
digit1 2013/05/17 15:21:08 Done.
1973 if len(upstream_commit) != 1:
M-A Ruel 2013/05/17 15:13:03 if not upstream_commit: DieWithError
digit1 2013/05/17 15:21:08 Done.
1974 upstream_commit = None
1975 else:
1976 upstream_commit = upstream_commit[0].strip()
1977
1978 if not upstream_commit:
1979 print >> sys.stderr, \
1980 'Could not find base commit for this branch. Are you in detached state?'
1981 return 2
1982
1983 diff_cmd.append(upstream_commit)
1984
1985 # Handle source file filtering.
1986 diff_cmd.append('--')
1987 diff_cmd += ['*' + ext for ext in CLANG_EXTS]
1988 diff_output = RunGit(diff_cmd)
1989
1990 if opts.full:
1991 # diff_output is a list of files to send to clang-format.
1992 files = diff_output.splitlines()
1958 if not files: 1993 if not files:
1959 print "Nothing to format." 1994 print "Nothing to format."
1960 return 0 1995 return 0
1961 RunCommand(['clang-format', '-i'] + files) 1996 RunCommand(['clang-format', '-i', '-style', 'Chromium'] + files)
1962 else: 1997 else:
1998 # diff_output is a patch to send to clang-format-diff.py
1963 cfd_path = os.path.join('/usr', 'lib', 'clang-format', 1999 cfd_path = os.path.join('/usr', 'lib', 'clang-format',
1964 'clang-format-diff.py') 2000 'clang-format-diff.py')
1965 if not os.path.exists(cfd_path): 2001 if not os.path.exists(cfd_path):
1966 print >> sys.stderr, 'Could not find clang-format-diff at %s.' % cfd_path 2002 print >> sys.stderr, 'Could not find clang-format-diff at %s.' % cfd_path
1967 return 2 2003 return 2
1968 cmd = ['diff', '-U0', '@{u}', '--'] + ['.*' + ext for ext in CLANG_EXTS] 2004 cmd = [sys.executable, cfd_path, '-style', 'Chromium']
1969 diff = RunGit(cmd) 2005 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 2006
1974 return 0 2007 return 0
1975 2008
1976 2009
1977 def Command(name): 2010 def Command(name):
1978 return getattr(sys.modules[__name__], 'CMD' + name, None) 2011 return getattr(sys.modules[__name__], 'CMD' + name, None)
1979 2012
1980 2013
1981 def CMDhelp(parser, args): 2014 def CMDhelp(parser, args):
1982 """print list of commands or help for a specific command""" 2015 """print list of commands or help for a specific command"""
(...skipping 69 matching lines...)
2052 GenUsage(parser, 'help') 2085 GenUsage(parser, 'help')
2053 return CMDhelp(parser, argv) 2086 return CMDhelp(parser, argv)
2054 2087
2055 2088
2056 if __name__ == '__main__': 2089 if __name__ == '__main__':
2057 # These affect sys.stdout so do it outside of main() to simplify mocks in 2090 # These affect sys.stdout so do it outside of main() to simplify mocks in
2058 # unit testing. 2091 # unit testing.
2059 fix_encoding.fix_encoding() 2092 fix_encoding.fix_encoding()
2060 colorama.init() 2093 colorama.init()
2061 sys.exit(main(sys.argv[1:])) 2094 sys.exit(main(sys.argv[1:]))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine