PatchUtil.java: don't sliently ignore patch chunk without prelude lines

Previously, patch content without ---/+++ prelude lines was silently ignored, which may cause patch file to be incorrectly applied. For example, https://github.com/bazelbuild/bazel/pull/16233

This PR makes sure PatchUtil correctly reports the error to users.

Closes #16239.

PiperOrigin-RevId: 472961143
Change-Id: I38d1931f7f2b8603a58de3904c3f525ab6fb3c85
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/PatchUtil.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/PatchUtil.java
index 83f1131..10eb28f 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/repository/PatchUtil.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/PatchUtil.java
@@ -395,6 +395,13 @@
       throws PatchFailedException {
     // If the patchContent is not empty, it should have correct format.
     if (!patchContent.isEmpty()) {
+      if (patchContent.size() < 2
+          || !patchContent.get(0).startsWith("---")
+          || !patchContent.get(1).startsWith("+++")) {
+        throw new PatchFailedException(
+            String.format(
+                "The patch content must start with ---/+++ prelude lines at line %d.", loc));
+      }
       if (header == null) {
         throw new PatchFailedException(
             String.format(
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 79e7b1e..763fe55 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
@@ -405,26 +405,6 @@
   }
 
   @Test
-  public void testMissingBothOldAndNewFile() throws IOException {
-    Path patchFile =
-        scratch.file(
-            "/root/patchfile",
-            "diff --git a/ b/",
-            "index f3008f9..ec4aaa0 100644",
-            "@@ -2,4 +2,5 @@",
-            " ",
-            " void main(){",
-            "   printf(\"Hello foo\");",
-            "+  printf(\"Hello from patch\");",
-            " }");
-    PatchFailedException expected =
-        assertThrows(PatchFailedException.class, () -> PatchUtil.apply(patchFile, 1, root));
-    assertThat(expected)
-        .hasMessageThat()
-        .contains("Wrong patch format near line 3, neither new file or old file are specified.");
-  }
-
-  @Test
   public void testCannotFindFileToPatch() throws IOException {
     Path patchFile =
         scratch.file(
@@ -550,7 +530,7 @@
   }
 
   @Test
-  public void testWrongChunkFormat1() throws IOException {
+  public void testUnexpectedContextLine() throws IOException {
     scratch.file(
         "/root/foo.cc", "#include <stdio.h>", "", "void main(){", "  printf(\"Hello foo\");", "}");
     Path patchFile =
@@ -576,7 +556,7 @@
   }
 
   @Test
-  public void testWrongChunkFormat2() throws IOException {
+  public void testMissingContextLine() throws IOException {
     scratch.file(
         "/root/foo.cc", "#include <stdio.h>", "", "void main(){", "  printf(\"Hello foo\");", "}");
     Path patchFile =
@@ -597,7 +577,7 @@
   }
 
   @Test
-  public void testWrongChunkFormat3() throws IOException {
+  public void testMissingChunkHeader() throws IOException {
     scratch.file(
         "/root/foo.cc", "#include <stdio.h>", "", "void main(){", "  printf(\"Hello foo\");", "}");
     Path patchFile =
@@ -619,4 +599,26 @@
         .hasMessageThat()
         .contains("Looks like a unified diff at line 3, but no patch chunk was found.");
   }
+
+  @Test
+  public void testMissingPreludeLines() throws IOException {
+    Path patchFile =
+        scratch.file(
+            "/root/patchfile",
+            "diff --git a/foo.cc b/foo.cc",
+            "index f3008f9..ec4aaa0 100644",
+            // Missing "--- a/foo.cc",
+            // Missing "+++ b/foo.cc",
+            "@@ -2,4 +2,5 @@",
+            " ",
+            " void main(){",
+            "   printf(\"Hello foo\");",
+            "+  printf(\"Hello from patch\");",
+            " }");
+    PatchFailedException expected =
+        assertThrows(PatchFailedException.class, () -> PatchUtil.apply(patchFile, 1, root));
+    assertThat(expected)
+        .hasMessageThat()
+        .contains("The patch content must start with ---/+++ prelude lines at line 3");
+  }
 }