Browse Source

Handle diff without chunk header

_process_diff assumes "@@" will always be present in a diff, but those
aren't present in a git diff where only the file mode changed.

Bug: b/323243527
Change-Id: Id129bc6c14994affc43a77feab553fa119612b7b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5384645
Reviewed-by: Joanna Wang <jojwang@chromium.org>
Commit-Queue: Gavin Mak <gavinmak@google.com>
Gavin Mak 1 year ago
parent
commit
b83a3038f1
2 changed files with 29 additions and 2 deletions
  1. 10 2
      presubmit_diff.py
  2. 19 0
      tests/presubmit_diff_test.py

+ 10 - 2
presubmit_diff.py

@@ -71,7 +71,13 @@ def _process_diff(diff: str, src_root: str, dst_root: str) -> str:
     if not diff:
         return ""
 
-    header, body = diff.split(HEADER_DELIMITER, maxsplit=1)
+    has_chunk_header = HEADER_DELIMITER in diff
+    if has_chunk_header:
+        header, body = diff.split(HEADER_DELIMITER, maxsplit=1)
+    else:
+        # Only the file mode changed.
+        header = diff
+
     norm_src = src_root.rstrip(os.sep)
     norm_dst = dst_root.rstrip(os.sep)
 
@@ -88,7 +94,9 @@ def _process_diff(diff: str, src_root: str, dst_root: str) -> str:
         header = header.replace(norm_src, "")
         header = header.replace(norm_dst, "")
 
-    return header + HEADER_DELIMITER + body
+    if has_chunk_header:
+        return header + HEADER_DELIMITER + body
+    return header
 
 
 def _create_diff(host: str, repo: str, ref: str, root: str, file: str) -> str:

+ 19 - 0
tests/presubmit_diff_test.py

@@ -218,6 +218,25 @@ index ce013625..dd7e1c6f 100644
                                          "C:\\path\\to\\dst\\"),
         )
 
+    @mock.patch("platform.system", return_value="Linux")
+    def test_process_diff_without_chunk_header(self, sys_mock):
+        diff = """diff --git a/path/to/src/file.txt b/path/to/dst/file.txt
+old mode 100644
+new mode 100755
+"""
+        expected = """diff --git a/file.txt b/file.txt
+old mode 100644
+new mode 100755
+"""
+        self.assertEqual(
+            presubmit_diff._process_diff(
+                diff,
+                "/path/to/src",
+                "/path/to/dst",
+            ),
+            expected,
+        )
+
 
 if __name__ == "__main__":
     unittest.main()