From 927f7dad38bfc746ae35d6c32ff0667fa60e2e08 Mon Sep 17 00:00:00 2001 From: Scott Lee Date: Tue, 16 Sep 2025 09:50:19 -0700 Subject: [PATCH] presubmit: use git diff -U0 for CheckPatchFormatted() git cl format uses `git diff -U0` when it has to find the files and ranges to format. Similarly, CiderG generates diffs with -U0 and run presubmits for the checks. However, the scm.py:GenerateDiff(), which CheckPatchFormatted() uses, doesn't specify -U and it defaults to -U3. This CL updates CheckPatchFormatted() to pass -U0 to scm.py:GenerateDiff() so that Presubmit and git cl format are always given diffs generated with -U0. Bug: 386840832 Change-Id: I19479ab139b7dba8ba4e5e5fed89eca4d2e66412 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6952890 Auto-Submit: Scott Lee Commit-Queue: Brian Ryner Commit-Queue: Scott Lee Reviewed-by: Brian Ryner --- presubmit_support.py | 3 ++- scm.py | 4 +++- tests/presubmit_unittest.py | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/presubmit_support.py b/presubmit_support.py index 3e549a281a..65e6710baf 100755 --- a/presubmit_support.py +++ b/presubmit_support.py @@ -1060,7 +1060,8 @@ class _GitDiffCache(_DiffCache): full_move=True, branch=self._upstream, branch_head=self._end_commit, - allow_prefix=True) + allow_prefix=True, + context=0) # Compute a single diff for all files and parse the output; with git # this is much faster than computing one diff for each file. self._diffs_by_file = _parse_unified_diff(unified_diff) diff --git a/scm.py b/scm.py index 1df082af97..65be1b65ba 100644 --- a/scm.py +++ b/scm.py @@ -1021,7 +1021,8 @@ class GIT(object): branch_head: str = 'HEAD', full_move: bool = False, files: Optional[Iterable[str]] = None, - allow_prefix: bool = False) -> str: + allow_prefix: bool = False, + context: int = 3) -> str: """Diffs against the upstream branch or optionally another branch. full_move means that move or copy operations should completely recreate the @@ -1036,6 +1037,7 @@ class GIT(object): '-p', '--no-color', '--no-ext-diff', + f'-U{context}', ] if allow_prefix: diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py index c863b9edd4..3ac93bc8ad 100755 --- a/tests/presubmit_unittest.py +++ b/tests/presubmit_unittest.py @@ -420,7 +420,8 @@ class PresubmitUnittest(PresubmitTestsBase): full_move=True, branch='upstream', branch_head='end_commit', - allow_prefix=True) + allow_prefix=True, + context=0) f_blat = os.path.normpath('boo/blat.cc')