Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ private static void applyInternal(
throw new PatchFailedException("Cannot find patch file: " + patchFile.getPathString());
}

String singleFileStr =
singleFile != null ? singleFile.relativeTo(outputDirectory).getPathString() : null;
boolean isGitDiff = false;
boolean hasRenameFrom = false;
boolean hasRenameTo = false;
Expand Down Expand Up @@ -594,6 +596,16 @@ private static void applyInternal(
oldFile, newFile, oldFileStr, newFileStr, patchStartLocation);
}
}
if (singleFileStr != null
&& strip == 0
&& ("a/" + singleFileStr).equals(oldFileStr)
&& ("b/" + singleFileStr).equals(newFileStr)) {
throw new PatchFailedException(
String.format(
"error at line %d: the patch file contains a/b prefixes, did you forget to"
+ " set patch_strip = 1?",
patchStartLocation));
}

if (singleFile == null || (singleFile.equals(newFile) && singleFile.equals(oldFile))) {
Patch<String> patch = UnifiedDiffUtils.parseUnifiedDiff(patchContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,47 @@ public void testSingleVersionOverridePatches() throws Exception {
"ccc", InterimModule.DepSpec.fromModuleKey(new ModuleKey("ccc", Version.parse("2.0"))));
}

@Test
public void testSingleVersionOverridePatches_defaultPatchStripForGitPatch() throws Exception {
FakeRegistry registry = registryFactory.newFakeRegistry("/foo");
ModuleFileFunction.REGISTRIES.set(differencer, ImmutableSet.of(registry.getUrl()));
ModuleKey bbb = createModuleKey("bbb", "1.0");
registry.addModule(bbb, "module(name='bbb',version='1.0')");

scratch.file("BUILD");
scratch.file(
"patch.diff",
"""
diff --git a/MODULE.bazel b/MODULE.bazel
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -1,1 +1,1 @@
-module(name='bbb',version='1.0')
+module(name='bbb',version='1.0',bazel_compatibility=[">=7.0.0"])
""");
scratch.overwriteFile(
rootDirectory.getRelative("MODULE.bazel").getPathString(),
"""
single_version_override(
module_name="bbb",
patches = [
"//:patch.diff",
],
)
""");

var moduleFileKey = ModuleFileValue.key(bbb);
EvaluationResult<ModuleFileValue> result =
evaluator.evaluate(ImmutableList.of(moduleFileKey), evaluationContext);
assertThat(result.hasError()).isTrue();
assertThat(result.getError().getException())
.hasMessageThat()
.isEqualTo(
"error applying single_version_override patch /workspace/patch.diff to module file:"
+ " error at line 2: the patch file contains a/b prefixes, did you forget to set"
+ " patch_strip = 1?");
}

@Test
public void testSingleVersionOverridePatches_failsOnRename() throws Exception {
FakeRegistry registry = registryFactory.newFakeRegistry("/foo");
Expand Down