java-diff-utils icon indicating copy to clipboard operation
java-diff-utils copied to clipboard

3-way merge - context

Open msevcenko opened this issue 4 years ago • 4 comments

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

msevcenko avatar Dec 10 '21 17:12 msevcenko

Could you give me your sourcecode?

wumpz avatar Dec 12 '21 22:12 wumpz

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);
	}
	
}

msevcenko avatar Dec 13 '21 06:12 msevcenko

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.

msevcenko avatar Dec 13 '21 07:12 msevcenko

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.

msevcenko avatar Dec 13 '21 07:12 msevcenko

Stale issue message

github-actions[bot] avatar Jan 05 '23 02:01 github-actions[bot]