Skip to content

Commit 29e4727

Browse files
Fix 'git cl format'
This patch fixes the implementation as follows: 1/ Fix '--full' option to check for all changes in the current branch relative to upstream. The old code only considered currently modified files, which made little sense. 2/ Fix '--full' option to apply Chromium style too. 3/ Use a proper source file filter. The original code used ".*.cc .*.cpp .*.h" as the filter, which didn't catch anything !? 4/ Add --no-ext-diff to make it work with custom "git diff" handlers. [email protected], [email protected], [email protected] BUG=NONE Review URL: https://chromiumcodereview.appspot.com/14942012 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@200832 0039d316-1c4b-4281-b951-d872f2087c98
1 parent 1561be9 commit 29e4727

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

git_cl.py

+38-10
Original file line numberDiff line numberDiff line change
@@ -1952,24 +1952,52 @@ def CMDformat(parser, args):
19521952
if args:
19531953
parser.error('Unrecognized args: %s' % ' '.join(args))
19541954

1955+
# Generate diff for the current branch's changes.
1956+
diff_cmd = ['diff', '--no-ext-diff']
19551957
if opts.full:
1956-
cmd = ['diff', '--name-only', '--'] + ['.*' + ext for ext in CLANG_EXTS]
1957-
files = RunGit(cmd).split()
1958+
# Only list the names of modified files.
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()
19581989
if not files:
19591990
print "Nothing to format."
19601991
return 0
1961-
RunCommand(['clang-format', '-i'] + files)
1992+
RunCommand(['clang-format', '-i', '-style', 'Chromium'] + files)
19621993
else:
1994+
# diff_output is a patch to send to clang-format-diff.py
19631995
cfd_path = os.path.join('/usr', 'lib', 'clang-format',
19641996
'clang-format-diff.py')
19651997
if not os.path.exists(cfd_path):
1966-
print >> sys.stderr, 'Could not find clang-format-diff at %s.' % cfd_path
1967-
return 2
1968-
cmd = ['diff', '-U0', '@{u}', '--'] + ['.*' + ext for ext in CLANG_EXTS]
1969-
diff = RunGit(cmd)
1970-
cmd = [sys.executable, '/usr/lib/clang-format/clang-format-diff.py',
1971-
'-style', 'Chromium']
1972-
RunCommand(cmd, stdin=diff)
1998+
DieWithError('Could not find clang-format-diff at %s.' % cfd_path)
1999+
cmd = [sys.executable, cfd_path, '-style', 'Chromium']
2000+
RunCommand(cmd, stdin=diff_output)
19732001

19742002
return 0
19752003

0 commit comments

Comments
 (0)