Browse Source

Merge "Fix Severe Bug in Merge Algorithm"

stable-0.10
Shawn Pearce 14 years ago committed by Code Review
parent
commit
7f939ba86e
  1. 11
      org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergeAlgorithmTest.java
  2. 27
      org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeAlgorithm.java

11
org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergeAlgorithmTest.java

@ -98,6 +98,7 @@ public class MergeAlgorithmTest extends TestCase {
String replace_BCDEGI_by_ZZZZZZ=A+Z+Z+Z+Z+F+Z+H+Z+J; String replace_BCDEGI_by_ZZZZZZ=A+Z+Z+Z+Z+F+Z+H+Z+J;
String replace_CEFGHJ_by_YYYYYY=A+B+Y+D+Y+Y+Y+Y+I+Y; String replace_CEFGHJ_by_YYYYYY=A+B+Y+D+Y+Y+Y+Y+I+Y;
String replace_BDE_by_ZZY=A+Z+C+Z+Y+F+G+H+I+J; String replace_BDE_by_ZZY=A+Z+C+Z+Y+F+G+H+I+J;
String delete_C=A+B+D+E+F+G+H+I+J;
/** /**
* Check for a conflict where the second text was changed similar to the * Check for a conflict where the second text was changed similar to the
@ -190,6 +191,16 @@ public class MergeAlgorithmTest extends TestCase {
merge(base, replace_C_by_Z, replace_C_by_Z)); merge(base, replace_C_by_Z, replace_C_by_Z));
} }
/**
* Check that a deleted vs. a modified line shows up as conflict (see Bug
* 328551)
*
* @throws IOException
*/
public void testDeleteVsModify() throws IOException {
assertEquals(A+B+XXX_0+XXX_1+Z+XXX_2+D+E+F+G+H+I+J, merge(base, delete_C, replace_C_by_Z));
}
private String merge(String commonBase, String ours, String theirs) throws IOException { private String merge(String commonBase, String ours, String theirs) throws IOException {
MergeResult r=MergeAlgorithm.merge(RawTextComparator.DEFAULT, new RawText(Constants.encode(commonBase)), new RawText(Constants.encode(ours)), new RawText(Constants.encode(theirs))); MergeResult r=MergeAlgorithm.merge(RawTextComparator.DEFAULT, new RawText(Constants.encode(commonBase)), new RawText(Constants.encode(ours)), new RawText(Constants.encode(theirs)));
ByteArrayOutputStream bo=new ByteArrayOutputStream(50); ByteArrayOutputStream bo=new ByteArrayOutputStream(50);

27
org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeAlgorithm.java

@ -201,28 +201,39 @@ public final class MergeAlgorithm {
// A conflicting region is found. Strip off common lines in // A conflicting region is found. Strip off common lines in
// in the beginning and the end of the conflicting region // in the beginning and the end of the conflicting region
int conflictLen = Math.min(oursEndB - oursBeginB, theirsEndB
- theirsBeginB); // Determine the minimum length of the conflicting areas in OURS
// and THEIRS. Also determine how much bigger the conflicting
// area in THEIRS is compared to OURS. All that is needed to
// limit the search for common areas at the beginning or end
// (the common areas cannot be bigger then the smaller
// conflicting area. The delta is needed to know whether the
// complete conflicting area is common in OURS and THEIRS.
int minBSize = oursEndB - oursBeginB;
int BSizeDelta = minBSize - (theirsEndB - theirsBeginB);
if (BSizeDelta > 0)
minBSize -= BSizeDelta;
int commonPrefix = 0; int commonPrefix = 0;
while (commonPrefix < conflictLen while (commonPrefix < minBSize
&& cmp.equals(ours, oursBeginB + commonPrefix, theirs, && cmp.equals(ours, oursBeginB + commonPrefix, theirs,
theirsBeginB + commonPrefix)) theirsBeginB + commonPrefix))
commonPrefix++; commonPrefix++;
conflictLen -= commonPrefix; minBSize -= commonPrefix;
int commonSuffix = 0; int commonSuffix = 0;
while (commonSuffix < conflictLen while (commonSuffix < minBSize
&& cmp.equals(ours, oursEndB - commonSuffix - 1, theirs, && cmp.equals(ours, oursEndB - commonSuffix - 1, theirs,
theirsEndB - commonSuffix - 1)) theirsEndB - commonSuffix - 1))
commonSuffix++; commonSuffix++;
conflictLen -= commonSuffix; minBSize -= commonSuffix;
// Add the common lines at start of conflict // Add the common lines at start of conflict
if (commonPrefix > 0) if (commonPrefix > 0)
result.add(1, oursBeginB, oursBeginB + commonPrefix, result.add(1, oursBeginB, oursBeginB + commonPrefix,
ConflictState.NO_CONFLICT); ConflictState.NO_CONFLICT);
// Add the conflict // Add the conflict (Only if there is a conflict left to report)
if (conflictLen > 0) { if (minBSize > 0 || BSizeDelta != 0) {
result.add(1, oursBeginB + commonPrefix, oursEndB result.add(1, oursBeginB + commonPrefix, oursEndB
- commonSuffix, - commonSuffix,
ConflictState.FIRST_CONFLICTING_RANGE); ConflictState.FIRST_CONFLICTING_RANGE);

Loading…
Cancel
Save