Skip to content

Commit 9a76f4d

Browse files
authored
Merge pull request #240 from Xujian66/fix/header-only-file-boundaries
Preserve header-only Git file boundaries in UnifiedDiffReader
2 parents 9a6f529 + 83d419d commit 9a76f4d

3 files changed

Lines changed: 141 additions & 1 deletion

File tree

‎java-diff-utils/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ private void initFileIfNecessary() {
279279
}
280280

281281
private void processDiff(MatchResult match, String line) {
282-
// initFileIfNecessary();
282+
if (actualFile.getDiffCommand() != null) {
283+
initFileIfNecessary();
284+
}
283285
LOG.log(Level.FINE, "start {0}", line);
284286
String[] fromTo = parseFileNames(READER.lastLine());
285287
actualFile.setFromFile(fromTo[0]);
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2026 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.difflib.unifieddiff;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import com.github.difflib.patch.PatchFailedException;
21+
import java.io.ByteArrayInputStream;
22+
import java.io.IOException;
23+
import java.nio.charset.StandardCharsets;
24+
import java.util.Collections;
25+
import org.junit.jupiter.api.Test;
26+
27+
public class UnifiedDiffReaderFileBoundaryTest {
28+
29+
@Test
30+
public void testParseHeaderOnlyFilesBeforeTextChange() throws IOException, PatchFailedException {
31+
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(getClass().getResourceAsStream("header_only_files.diff"));
32+
33+
assertThat(diff.getFiles()).hasSize(5);
34+
UnifiedDiffFile binary = diff.getFiles().get(0);
35+
assertThat(binary.getFromFile()).isEqualTo("a-image.bin");
36+
assertThat(binary.getToFile()).isEqualTo("a-image.bin");
37+
assertThat(binary.getBinaryEdited()).isEqualTo("a-image.bin");
38+
assertThat(binary.getPatch().getDeltas()).isEmpty();
39+
40+
UnifiedDiffFile empty = diff.getFiles().get(1);
41+
assertThat(empty.getToFile()).isEqualTo("b-empty.txt");
42+
assertThat(empty.getNewFileMode()).isEqualTo("100644");
43+
assertThat(empty.getBinaryEdited()).isNull();
44+
assertThat(empty.getPatch().getDeltas()).isEmpty();
45+
46+
UnifiedDiffFile mode = diff.getFiles().get(2);
47+
assertThat(mode.getFromFile()).isEqualTo("c-script.sh");
48+
assertThat(mode.getOldMode()).isEqualTo("100644");
49+
assertThat(mode.getNewMode()).isEqualTo("100755");
50+
assertThat(mode.getNewFileMode()).isNull();
51+
assertThat(mode.getPatch().getDeltas()).isEmpty();
52+
53+
UnifiedDiffFile rename = diff.getFiles().get(3);
54+
assertThat(rename.getFromFile()).isEqualTo("d-old.txt");
55+
assertThat(rename.getToFile()).isEqualTo("e-renamed.txt");
56+
assertThat(rename.getSimilarityIndex()).isEqualTo(100);
57+
assertThat(rename.getRenameFrom()).isEqualTo("d-old.txt");
58+
assertThat(rename.getRenameTo()).isEqualTo("e-renamed.txt");
59+
assertThat(rename.getOldMode()).isNull();
60+
assertThat(rename.getPatch().getDeltas()).isEmpty();
61+
62+
UnifiedDiffFile text = diff.getFiles().get(4);
63+
assertThat(text.getFromFile()).isEqualTo("z-text.txt");
64+
assertThat(text.getToFile()).isEqualTo("z-text.txt");
65+
assertThat(text.getBinaryEdited()).isNull();
66+
assertThat(text.getNewFileMode()).isNull();
67+
assertThat(text.getNewMode()).isNull();
68+
assertThat(text.getRenameFrom()).isNull();
69+
assertThat(text.getPatch().applyTo(Collections.singletonList("old"))).containsExactly("new");
70+
}
71+
72+
@Test
73+
public void testParseConsecutiveHeaderOnlyFilesAtEndOfDiff() throws IOException {
74+
String text = "diff --git a/first.sh b/first.sh\n"
75+
+ "old mode 100644\nnew mode 100755\n"
76+
+ "diff --git a/second.sh b/second.sh\n"
77+
+ "old mode 100755\nnew mode 100644\n";
78+
UnifiedDiff diff =
79+
UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)));
80+
81+
assertThat(diff.getFiles()).hasSize(2);
82+
assertThat(diff.getFiles().get(0).getFromFile()).isEqualTo("first.sh");
83+
assertThat(diff.getFiles().get(0).getNewMode()).isEqualTo("100755");
84+
assertThat(diff.getFiles().get(1).getFromFile()).isEqualTo("second.sh");
85+
assertThat(diff.getFiles().get(1).getNewMode()).isEqualTo("100644");
86+
assertThat(diff.getFiles())
87+
.allSatisfy(file -> assertThat(file.getPatch().getDeltas()).isEmpty());
88+
}
89+
90+
@Test
91+
public void testParseTextChangeBeforeHeaderOnlyAndBinaryFiles() throws IOException, PatchFailedException {
92+
String text = "diff --git a/text.txt b/text.txt\n"
93+
+ "--- a/text.txt\n+++ b/text.txt\n@@ -1 +1 @@\n-old\n+new\n"
94+
+ "diff --git a/script.sh b/script.sh\n"
95+
+ "old mode 100644\nnew mode 100755\n"
96+
+ "diff --git a/image.bin b/image.bin\n"
97+
+ "Binary files a/image.bin and b/image.bin differ\n";
98+
UnifiedDiff diff =
99+
UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)));
100+
101+
assertThat(diff.getFiles()).hasSize(3);
102+
UnifiedDiffFile changed = diff.getFiles().get(0);
103+
assertThat(changed.getFromFile()).isEqualTo("text.txt");
104+
assertThat(changed.getNewMode()).isNull();
105+
assertThat(changed.getBinaryEdited()).isNull();
106+
assertThat(changed.getPatch().applyTo(Collections.singletonList("old"))).containsExactly("new");
107+
UnifiedDiffFile mode = diff.getFiles().get(1);
108+
assertThat(mode.getFromFile()).isEqualTo("script.sh");
109+
assertThat(mode.getNewMode()).isEqualTo("100755");
110+
assertThat(mode.getBinaryEdited()).isNull();
111+
assertThat(mode.getPatch().getDeltas()).isEmpty();
112+
UnifiedDiffFile binary = diff.getFiles().get(2);
113+
assertThat(binary.getFromFile()).isEqualTo("image.bin");
114+
assertThat(binary.getBinaryEdited()).isEqualTo("image.bin");
115+
assertThat(binary.getNewMode()).isNull();
116+
assertThat(binary.getPatch().getDeltas()).isEmpty();
117+
}
118+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
diff --git a/a-image.bin b/a-image.bin
2+
index eaf36c1..f75c32a 100644
3+
Binary files a/a-image.bin and b/a-image.bin differ
4+
diff --git a/b-empty.txt b/b-empty.txt
5+
new file mode 100644
6+
index 0000000..e69de29
7+
diff --git a/c-script.sh b/c-script.sh
8+
old mode 100644
9+
new mode 100755
10+
diff --git a/d-old.txt b/e-renamed.txt
11+
similarity index 100%
12+
rename from d-old.txt
13+
rename to e-renamed.txt
14+
diff --git a/z-text.txt b/z-text.txt
15+
index 3367afd..3e75765 100644
16+
--- a/z-text.txt
17+
+++ b/z-text.txt
18+
@@ -1 +1 @@
19+
-old
20+
+new

0 commit comments

Comments
 (0)