Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8880ec5

Browse files
committed
introduced conflict processing
1 parent ce05cbf commit 8880ec5

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

java-diff-utils/src/main/java/com/github/difflib/patch/ConflictOutput.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
*/
2020
package com.github.difflib.patch;
2121

22+
import java.io.Serializable;
2223
import java.util.List;
2324

2425
/**
2526
*
2627
* @author tw
2728
*/
2829
@FunctionalInterface
29-
public interface ConflictOutput<T> {
30+
public interface ConflictOutput<T> extends Serializable {
3031

3132
public void processConflict(VerifyChunk verifyChunk, AbstractDelta<T> delta, List<T> result) throws PatchFailedException;
3233
}

java-diff-utils/src/main/java/com/github/difflib/patch/Patch.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,24 @@ public List<T> applyTo(List<T> target) throws PatchFailedException {
6060
AbstractDelta<T> delta = it.previous();
6161
VerifyChunk valid = delta.verifyAntApplyTo(result);
6262
if (valid != VerifyChunk.OK) {
63-
63+
conflictOutput.processConflict(valid, delta, result);
6464
}
6565
}
6666
return result;
6767
}
68-
69-
private ConflictOutput<T> conflictOutput = (VerifyChunk verifyChunk, AbstractDelta<T> delta, List<T> result) -> {
68+
69+
/**
70+
* Standard Patch behaviour to throw an exception for pathching conflicts.
71+
*/
72+
public final ConflictOutput<T> CONFLICT_PRODUCES_EXCEPTION = (VerifyChunk verifyChunk, AbstractDelta<T> delta, List<T> result) -> {
7073
throw new PatchFailedException("could not apply patch due to " + verifyChunk.toString());
7174
};
72-
75+
76+
private ConflictOutput<T> conflictOutput = CONFLICT_PRODUCES_EXCEPTION;
77+
7378
/**
74-
* Alter normal conflict output behaviour to e.g. inclide some conflict statements in the result, like git does it.
79+
* Alter normal conflict output behaviour to e.g. inclide some conflict
80+
* statements in the result, like git does it.
7581
*/
7682
public Patch withConflictOutput(ConflictOutput<T> conflictOutput) {
7783
this.conflictOutput = conflictOutput;

java-diff-utils/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.List;
1414
import static java.util.stream.Collectors.joining;
1515
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertThrows;
1617
import static org.junit.jupiter.api.Assertions.assertTrue;
1718
import static org.junit.jupiter.api.Assertions.fail;
1819
import org.junit.jupiter.api.Test;
@@ -133,7 +134,7 @@ public void testNewFileCreation() {
133134
* Issue 89
134135
*/
135136
@Test
136-
public void testChagngePosition() throws IOException {
137+
public void testChangePosition() throws IOException {
137138
final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue89_patch.txt");
138139
final Patch<String> patch = UnifiedDiffUtils.parseUnifiedDiff(patchLines);
139140
List<Integer> realRemoveListOne = Collections.singletonList(3);
@@ -191,4 +192,20 @@ private void verify(List<String> origLines, List<String> revLines,
191192
fail(e.getMessage());
192193
}
193194
}
195+
196+
197+
@Test
198+
public void testFailingPatchByException() throws IOException {
199+
final List<String> baseLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_base.txt");
200+
final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_patch.txt");
201+
final Patch<String> p = UnifiedDiffUtils.parseUnifiedDiff(patchLines);
202+
203+
//make original not fitting
204+
baseLines.set(40, baseLines.get(40) + " corrupted ");
205+
206+
assertThrows(PatchFailedException.class, () -> DiffUtils.patch(baseLines, p));
207+
208+
209+
210+
}
194211
}

0 commit comments

Comments
 (0)