Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Unified Diff: git_cl.py

Issue 933383002: Run dartfmt when invoking git cl format. (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Don't error on dart sdk not being installed. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « gclient_utils.py ('k') | presubmit_canned_checks.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: git_cl.py
diff --git a/git_cl.py b/git_cl.py
index 1b375e856ca190fa4c7217cdfb30316f5c37a088..08491de97dfe9f3ceb0f31c9ae0fc9eae0fb18de 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -36,6 +36,7 @@ from third_party import colorama
from third_party import upload
import breakpad # pylint: disable=W0611
import clang_format
+import dart_format
import fix_encoding
import gclient_utils
import git_common
@@ -2893,6 +2894,24 @@ def CMDowners(parser, args):
fopen=file, os_path=os.path, glob=glob.glob,
disable_color=options.no_color).run()
M-A Ruel 2015/02/23 18:47:26 2 empty lines between file level symbols.
+def BuildGitDiffCmd(diff_type, upstream_commit, args, extensions):
+ """Generates a diff command."""
+ # Generate diff for the current branch's changes.
+ diff_cmd = ['diff', '--no-ext-diff', '--no-prefix', diff_type,
+ upstream_commit, '--' ]
+
+ if args:
+ for arg in args:
+ if os.path.isdir(arg):
+ diff_cmd += [os.path.join(arg, '*' + ext) for ext in extensions]
M-A Ruel 2015/02/23 18:47:26 diff_cmd.extend(os.path.join(arg, '*' + ext) for e
+ elif os.path.isfile(arg):
+ diff_cmd.append(arg)
+ else:
+ DieWithError('Argument "%s" is not a file or a directory' % arg)
+ else:
+ diff_cmd += ['*' + ext for ext in extensions]
M-A Ruel 2015/02/23 18:47:25 diff_cmd.extend('*' + ext for ext in extensions)
+
+ return diff_cmd
M-A Ruel 2015/02/23 18:47:25 2 empty lines
@subcommand.usage('[files or directories to diff]')
def CMDformat(parser, args):
@@ -2912,15 +2931,6 @@ def CMDformat(parser, args):
if rel_base_path:
os.chdir(rel_base_path)
- # Generate diff for the current branch's changes.
- diff_cmd = ['diff', '--no-ext-diff', '--no-prefix']
- if opts.full:
- # Only list the names of modified files.
- diff_cmd.append('--name-only')
- else:
- # Only generate context-less patches.
- diff_cmd.append('-U0')
-
# Grab the merge-base commit, i.e. the upstream commit of the current
# branch when it was created or the last time it was rebased. This is
# to cover the case where the user may have called "git fetch origin",
@@ -2936,20 +2946,14 @@ def CMDformat(parser, args):
DieWithError('Could not find base commit for this branch. '
'Are you in detached state?')
- diff_cmd.append(upstream_commit)
-
- # Handle source file filtering.
- diff_cmd.append('--')
- if args:
- for arg in args:
- if os.path.isdir(arg):
- diff_cmd += [os.path.join(arg, '*' + ext) for ext in CLANG_EXTS]
- elif os.path.isfile(arg):
- diff_cmd.append(arg)
- else:
- DieWithError('Argument "%s" is not a file or a directory' % arg)
+ if opts.full:
+ # Only list the names of modified files.
+ clang_diff_type = '--name-only'
else:
- diff_cmd += ['*' + ext for ext in CLANG_EXTS]
+ # Only generate context-less patches.
+ clang_diff_type = '-U0'
+
+ diff_cmd = BuildGitDiffCmd(clang_diff_type, upstream_commit, args, CLANG_EXTS)
diff_output = RunGit(diff_cmd)
top_dir = os.path.normpath(
@@ -2961,18 +2965,20 @@ def CMDformat(parser, args):
except clang_format.NotFoundError, e:
DieWithError(e)
+ # Set to 2 to signal to CheckPatchFormatted() that this patch isn't
+ # formatted. This is used to block during the presubmit.
+ return_value = 0
+
if opts.full:
# diff_output is a list of files to send to clang-format.
files = diff_output.splitlines()
- if not files:
- print "Nothing to format."
- return 0
- cmd = [clang_format_tool]
- if not opts.dry_run and not opts.diff:
- cmd.append('-i')
- stdout = RunCommand(cmd + files, cwd=top_dir)
- if opts.diff:
- sys.stdout.write(stdout)
+ if files:
+ cmd = [clang_format_tool]
+ if not opts.dry_run and not opts.diff:
+ cmd.append('-i')
+ stdout = RunCommand(cmd + files, cwd=top_dir)
+ if opts.diff:
+ sys.stdout.write(stdout)
else:
env = os.environ.copy()
env['PATH'] = str(os.path.dirname(clang_format_tool))
@@ -2991,9 +2997,32 @@ def CMDformat(parser, args):
if opts.diff:
sys.stdout.write(stdout)
if opts.dry_run and len(stdout) > 0:
- return 2
-
- return 0
+ return_value = 2
+
+ # Build a diff command that only operates on dart files. dart's formatter
+ # does not have the nice property of only operating on modified chunks, so
+ # hard code full.
+ dart_diff_cmd = BuildGitDiffCmd('--name-only', upstream_commit,
+ args, ['.dart'])
+ dart_diff_output = RunGit(dart_diff_cmd)
+ if dart_diff_output:
+ try:
+ dartfmt = dart_format.FindDartFmtToolInChromiumTree()
+
+ command = [dartfmt]
M-A Ruel 2015/02/23 18:47:25 command = [dart_format.FindDartFmtToolInChromiumTr
+ if not opts.dry_run and not opts.diff:
+ command.append('-w')
+ files = dart_diff_output.splitlines()
+ command.extend(files)
M-A Ruel 2015/02/23 18:47:26 command.extend(dart_diff_output.splitlines())
+
+ stdout = RunCommand(command, cwd=top_dir, env=env)
+ if opts.dry_run and len(stdout) > 0:
M-A Ruel 2015/02/23 18:47:26 if opts.dry_run and stdout:
+ return_value = 2
+ except dart_format.NotFoundError, e:
M-A Ruel 2015/02/23 18:47:26 except dart_format.NotFoundError as e:
+ print ("Unable to check dart code formatting. Dart SDK is not in " +
M-A Ruel 2015/02/23 18:47:26 single quotes
+ "this checkout.")
+
+ return return_value
def CMDlol(parser, args):
« no previous file with comments | « gclient_utils.py ('k') | presubmit_canned_checks.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698