git-cl: Add diff lines of context setting.

Clients using some Clang-Format options can run into bugs when using
0 lines of context. AlignConsecutiveAssignments in particular is picky
and behaves differently with 0 lines of context.

This behaviour lead to 'git cl format' behaving differently from
'git cl format --full'. That could also break presubmit format checks.
Using >0 lines of context for 'git diff' fixes the inconsistency.

Add a setting 'DIFF_LINES_OF_CONTEXT' that can be controlled via
codereview.settings. Defaults to zero to preserve the old behaviour.
The setting allows the client to control the number of lines of context
to use when running 'git cl format'.

Bug: angleproject:4003
Change-Id: Ied2ebf23df4c41ba19bfbd5b8ddf526b56a20b31
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1864309
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
changes/09/1864309/6
Jamie Madill 6 years ago committed by Commit Bot
parent a056817880
commit 812ac228e3

@ -694,7 +694,8 @@ def _ComputeDiffLineRanges(files, upstream_commit):
return {}
# Take the git diff and find the line ranges where there are changes.
diff_cmd = BuildGitDiffCmd('-U0', upstream_commit, files, allow_prefix=True)
lines = '-U%s' % settings.GetDiffLinesOfContext()
diff_cmd = BuildGitDiffCmd(lines, upstream_commit, files, allow_prefix=True)
diff_output = RunGit(diff_cmd)
pattern = r'(?:^diff --git a/(?:.*) b/(.*))|(?:^@@.*\+(.*) @@)'
@ -943,6 +944,10 @@ class Settings(object):
return (self._GetConfig('rietveld.cpplint-ignore-regex', error_ok=True) or
DEFAULT_LINT_IGNORE_REGEX)
def GetDiffLinesOfContext(self):
return (self._GetConfig('rietveld.diff-lines-of-context', error_ok=True) or
"0")
def _GetConfig(self, param, **kwargs):
self.LazyUpdateIfNeeded()
return RunGit(['config', param], **kwargs).strip()
@ -3154,6 +3159,8 @@ def LoadCodereviewSettingsFromFile(fileobj):
SetProperty('cpplint-ignore-regex', 'LINT_IGNORE_REGEX', unset_error_ok=True)
SetProperty('run-post-upload-hook', 'RUN_POST_UPLOAD_HOOK',
unset_error_ok=True)
SetProperty(
'diff-lines-of-context', 'DIFF_LINES_OF_CONTEXT', unset_error_ok=True)
if 'GERRIT_HOST' in keyvals:
RunGit(['config', 'gerrit.host', keyvals['GERRIT_HOST']])
@ -5195,7 +5202,8 @@ def CMDformat(parser, args):
if not opts.dry_run and not opts.diff:
cmd.append('-i')
diff_cmd = BuildGitDiffCmd('-U0', upstream_commit, clang_diff_files)
lines = '-U%s' % settings.GetDiffLinesOfContext()
diff_cmd = BuildGitDiffCmd(lines, upstream_commit, clang_diff_files)
diff_output = RunGit(diff_cmd)
stdout = RunCommand(cmd, stdin=diff_output, cwd=top_dir, env=env)

@ -742,6 +742,8 @@ class TestGitCl(TestCase):
CERR1),
((['git', 'config', '--unset-all', 'rietveld.run-post-upload-hook'],),
CERR1),
((['git', 'config', '--unset-all', 'rietveld.diff-lines-of-context'],),
CERR1),
((['git', 'config', 'gerrit.host', 'true'],), ''),
]
self.assertIsNone(git_cl.LoadCodereviewSettingsFromFile(codereview_file))

Loading…
Cancel
Save