3-way merge - context
Trying to generate three-way merge using diff and applyTo.
Is there a functionality to require a context between adjacent modifications?
This would be reported as a conflict by git diff, as there is not enough context between the changes.
BASE Imagine there's no heaven It's easy if you try LEFT Imagine there's no HEAVEN It's easy if you try RIGHT Imagine there's no heaven It's easy if you TRY
but you would merge to
Imagine there's no HEAVEN It's easy if you TRY
which is correct but kinda dangerous.
Git would say it is a conflict.
<<<<<<< test.txt
Imagine there's no heaven
It's easy if you TRY
=======
Imagine there's no HEAVEN
It's easy if you try
>>>>>>> test.txt
Could you give me your sourcecode?
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Assert;
import org.junit.Test;
import com.github.difflib.patch.Patch;
import com.github.difflib.patch.PatchFailedException;
public class ContextlessMergeTest {
@Test
public void test() throws PatchFailedException {
String base = "Imagine there's no heaven\nIt's easy if you try";
String left = "Imagine there's no HEAVEN\nIt's easy if you try";
String right = "Imagine there's no heaven\nIt's easy if you TRY";
Patch<String> rightPatch = com.github.difflib.DiffUtils.diff(base, right, null);
List<String> applied = rightPatch.applyTo(Arrays.asList(left.split("\n")));
String merged = applied.stream().collect(Collectors.joining("\n"));
// the texts are merged but I would expect to be rejected
String mergedExpected="Imagine there's no HEAVEN\nIt's easy if you TRY";
Assert.assertEquals(mergedExpected, merged);
}
}
To provide some background, I am looking for robust algorithm than can reliably do three-way merges of text files.
Maybe what I am looking for is patience diff algorithm - which looks to be much less prone to mismerges. Any chance to implement it? Looks like its not v ery difficult.
After second thought - this is independent problem.
I need something that is not vulerable to mismerges (like pure LCS algorithms are) and something that requires security contexts.
Stale issue message