Add unidiff-zero to git apply

The presubmit format validation hook and git cl format is given or
generate a patch such that they check/format the modified lines only.

The context lines matter because the diff parsers extract the line
numbers and length from the diffs, which include the context lines,
if included. For instance, if a patch is generated with -U3,
the presubmit hook actually verifies the format of not only the changed
lines but also the context lines. Therefore, it's important to make sure
that all the diff(s) commands use the same -U to ensure that they check
and format the same lines.

crrev.com/c/6952890 was submitted to match the context line numbers.
However, it was missing one case. The Chromium Cider extension also
generates the diff but with -U3. Therefore, cl/834917755 was made
to have it generating patches with -U0.

Another issue is that "git apply" fails by default if a given patch
was generated with -U0. It expects context lines in case a user tries
to apply a patch into a different commit so that it could adjust
the patches and applies even if the location of the corresponding,
original block has changed in the file to apply the patch.

However, the scenario of smartly modifying the patch to make it
applicable to as many commits as possible is not applicable to
depot_tools' case. The patch is not made to be shared with others.
It's generated instantly for a specific, base commit and applies it
to the current commit.

"--unidiff-zero" is the option to allow patches even if it was
generated with -U0. It's fine to use the option because the smart
patch adjustment is not applicable to our use cases.

This CL adds the option to _ProvidedDiffCache() so that it is
happy with the patch generated with -U0. The reason why the other
functionalities have been working, even though crrev.com/c/6952890
changed the context option to -U0 is that other logic doesn't use
`git apply`. They just parse the hunks and use the numbers.

Bug: 458442277
Change-Id: I8af98e9a8bcbd8d3175a162fced127ff89ce526c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/7211480
Reviewed-by: Yiwei Zhang <yiwzhang@google.com>
Commit-Queue: Scott Lee <ddoman@chromium.org>
This commit is contained in:
Scott Lee
2025-12-01 11:16:31 -08:00
committed by LUCI CQ
parent c81b50d234
commit 2e88a3f08b

View File

@@ -1107,7 +1107,9 @@ class _ProvidedDiffCache(_DiffCache):
with gclient_utils.temporary_file() as diff_file:
gclient_utils.FileWrite(diff_file, diff)
try:
scm.GIT.Capture(['apply', '--reverse', '--check', diff_file],
scm.GIT.Capture([
'apply', '--reverse', '--unidiff-zero', '--check', diff_file
],
cwd=local_root)
except subprocess.CalledProcessError:
raise RuntimeError('Provided diff does not apply cleanly.')
@@ -1119,8 +1121,13 @@ class _ProvidedDiffCache(_DiffCache):
if is_file:
shutil.copyfile(full_path, copy_dst)
scm.GIT.Capture([
'apply', '--reverse', '--directory', tmp_dir,
'--unsafe-paths', diff_file
'apply',
'--reverse',
'--directory',
tmp_dir,
'--unidiff-zero',
'--unsafe-paths',
diff_file,
],
cwd=tmp_dir)
# Applying the patch can create a new file if the file at