bazelbuild/bazel

native patch fails to apply multi-file patches without file headers

Open

#10,267 opened on Nov 18, 2019

View on GitHub
 (4 comments) (1 reaction) (1 assignee)Java (25,384 stars) (4,465 forks)batch import
P2help wantedteam-ExternalDepstype: feature request

Description

patch(1) can apply the following patch, but the repository rule ctx's patch method can't:

--- a/newfile	1969-12-31 16:00:00.000000000 -0800
+++ b/newfile	2019-11-18 09:41:29.206408378 -0800
@@ -0,0 +1,2 @@
+I'm a new file
+hello, world
--- a/newfile2	1969-12-31 16:00:00.000000000 -0800
+++ b/newfile2	2019-11-18 09:46:48.146505886 -0800
@@ -0,0 +1,2 @@
+foo
+bar

Here's a test for PatchUtil:

diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/PatchUtilTest.java b/src/test/java/com/google/devtools/build/lib/bazel/repository/PatchUtilTest.java
index 5fbcace554..e888c947bb 100644
--- a/src/test/java/com/google/devtools/build/lib/bazel/repository/PatchUtilTest.java
+++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/PatchUtilTest.java
@@ -65,6 +65,29 @@ public class PatchUtilTest {
     assertThat(PatchUtil.readFile(newFile)).containsExactlyElementsIn(newFileContent);
   }
 
+  @Test
+  public void addTwoFiles() throws Exception {
+    Path patchFile =
+        scratch.file(
+            "/root/patchfile",
+            "--- a/newfile     1969-12-31 16:00:00.000000000 -0800",
+            "+++ b/newfile     2019-11-18 09:41:29.206408378 -0800",
+            "@@ -0,0 +1,2 @@",
+            "+I'm a new file",
+            "+hello, world",
+            "--- a/newfile2    1969-12-31 16:00:00.000000000 -0800",
+            "+++ b/newfile2    2019-11-18 09:46:48.146505886 -0800",
+            "@@ -0,0 +1,2 @@",
+            "+foo",
+            "+bar");
+
+    PatchUtil.apply(patchFile, 1, root);
+    assertThat(PatchUtil.readFile(root.getRelative("newfile")))
+        .containsExactlyElementsIn(ImmutableList.of("I'm a new file", "hello, world"));
+    assertThat(PatchUtil.readFile(root.getRelative("newfile2")))
+        .containsExactlyElementsIn(ImmutableList.of("foo", "bar"));
+  }
+
   @Test
   public void testAddOneLineFile() throws IOException, PatchFailedException {
     Path patchFile =

Contributor guide