Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3966413

Browse filesBrowse files
committed
Fix UnifiedDiffReader to use correct DeltaType
finalizeChunk() previously always created ChangeDelta regardless of whether the hunk contained only insertions, only deletions, or only context lines. This made it impossible for consumers to determine the actual type of change from the delta alone. Now uses InsertDelta when a hunk contains only additions (no context), DeleteDelta when only deletions (no context), EqualDelta when only context, and ChangeDelta otherwise. Hunks with context lines still use ChangeDelta to preserve backward-compatible applyTo behavior. Fixes #201
1 parent b35ebf4 commit 3966413
Copy full SHA for 3966413

2 files changed

+40-18Lines changed: 40 additions & 18 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: java-diff-utils/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java
+39-17Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
*/
1616
package com.github.difflib.unifieddiff;
1717

18+
import com.github.difflib.patch.AbstractDelta;
1819
import com.github.difflib.patch.ChangeDelta;
1920
import com.github.difflib.patch.Chunk;
21+
import com.github.difflib.patch.DeleteDelta;
22+
import com.github.difflib.patch.EqualDelta;
23+
import com.github.difflib.patch.InsertDelta;
2024
import java.io.BufferedReader;
2125
import java.io.IOException;
2226
import java.io.InputStream;
@@ -298,23 +302,41 @@ private void processSimilarityIndex(MatchResult match, String line) {
298302
private int delLineIdx = 0;
299303
private int addLineIdx = 0;
300304

301-
private void finalizeChunk() {
302-
if (!originalTxt.isEmpty() || !revisedTxt.isEmpty()) {
303-
actualFile
304-
.getPatch()
305-
.addDelta(new ChangeDelta<>(
306-
new Chunk<>(old_ln - 1, originalTxt, delLineIdxList),
307-
new Chunk<>(new_ln - 1, revisedTxt, addLineIdxList)));
308-
old_ln = 0;
309-
new_ln = 0;
310-
originalTxt.clear();
311-
revisedTxt.clear();
312-
addLineIdxList.clear();
313-
delLineIdxList.clear();
314-
delLineIdx = 0;
315-
addLineIdx = 0;
316-
}
317-
}
305+
private void finalizeChunk() {
306+
if (!originalTxt.isEmpty() || !revisedTxt.isEmpty()) {
307+
boolean hasDeletes = !delLineIdxList.isEmpty();
308+
boolean hasInserts = !addLineIdxList.isEmpty();
309+
boolean hasContext = originalTxt.size() != delLineIdxList.size()
310+
|| revisedTxt.size() != addLineIdxList.size();
311+
AbstractDelta<String> delta;
312+
if (hasContext || (hasDeletes && hasInserts)) {
313+
delta = new ChangeDelta<>(
314+
new Chunk<>(old_ln - 1, originalTxt, delLineIdxList),
315+
new Chunk<>(new_ln - 1, revisedTxt, addLineIdxList));
316+
} else if (hasDeletes) {
317+
delta = new DeleteDelta<>(
318+
new Chunk<>(old_ln - 1, originalTxt, delLineIdxList),
319+
new Chunk<>(new_ln - 1, revisedTxt, addLineIdxList));
320+
} else if (hasInserts) {
321+
delta = new InsertDelta<>(
322+
new Chunk<>(old_ln - 1, originalTxt, delLineIdxList),
323+
new Chunk<>(new_ln - 1, revisedTxt, addLineIdxList));
324+
} else {
325+
delta = new EqualDelta<>(
326+
new Chunk<>(old_ln - 1, originalTxt, delLineIdxList),
327+
new Chunk<>(new_ln - 1, revisedTxt, addLineIdxList));
328+
}
329+
actualFile.getPatch().addDelta(delta);
330+
old_ln = 0;
331+
new_ln = 0;
332+
originalTxt.clear();
333+
revisedTxt.clear();
334+
addLineIdxList.clear();
335+
delLineIdxList.clear();
336+
delLineIdx = 0;
337+
addLineIdx = 0;
338+
}
339+
}
318340

319341
private void processNormalLine(MatchResult match, String line) {
320342
String cline = line.substring(1);
Collapse file

‎java-diff-utils/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java‎

Copy file name to clipboardExpand all lines: java-diff-utils/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public void testParseIssue104() throws IOException {
275275
assertThat(file.getToFile()).isEqualTo("doc/samba_data_tool_path.xml.in");
276276

277277
assertThat(file.getPatch().toString())
278-
.isEqualTo("Patch{deltas=[[ChangeDelta, position: 0, lines: [] to [@SAMBA_DATA_TOOL@]]]}");
278+
.isEqualTo("Patch{deltas=[[InsertDelta, position: 0, lines: [@SAMBA_DATA_TOOL@]]]}");
279279

280280
assertThat(diff.getTail()).isEqualTo("2.14.4");
281281
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.