Skip to content

Commit

Permalink
[fix] Fix false positive conflict in line-based merge on pure addition (
Browse files Browse the repository at this point in the history
  • Loading branch information
slarse authored Jul 4, 2020
1 parent 72e73a8 commit 111eb31
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/se/kth/spork/util/LineBasedMerge.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public class LineBasedMerge {
* @return A pair containing the merge and the amount of conflicts.
*/
public static Pair<String, Integer> merge(String base, String left, String right) {
if (base.isEmpty() && (left.isEmpty() || right.isEmpty())) {
// For some reason, this merge implementation reports a conflict on pure additions.
// This is an easy fix for that. See #144 for details.
return Pair.of(left.isEmpty() ? right : left, 0);
}

RawText baseRaw = new RawText(base.getBytes());
RawText leftRaw = new RawText(left.getBytes());
RawText rightRaw = new RawText(right.getBytes());
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/se/kth/spork/cli/CliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ private static void runTestMerge(Util.TestSources sources, Path tempDir) throws

Object expectedImports = mergeTree.getMetadata(Parser.IMPORT_STATEMENTS);
assert expectedImports != null;
Object expectedCuComment = mergeTree.getMetadata(Parser.COMPILATION_UNIT_COMMENT);
assert expectedCuComment != null;

String expectedPrettyPrint = Cli.prettyPrint(mergeTree);

Expand All @@ -140,12 +142,14 @@ private static void runTestMerge(Util.TestSources sources, Path tempDir) throws

CtModule reParsedMerge = Parser.parse(outFile);
Object reParsedImports = reParsedMerge.getMetadata(Parser.IMPORT_STATEMENTS);
Object reparsedCuComment = reParsedMerge.getMetadata(Parser.COMPILATION_UNIT_COMMENT);

System.out.println(Cli.prettyPrint(mergeTree));
assertEquals(Cli.prettyPrint(mergeTree), expectedPrettyPrint);

assertEquals(mergeTree, reParsedMerge);

assertEquals(reParsedImports, expectedImports);
assertEquals(reparsedCuComment, expectedCuComment);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package se.kth.spork;

public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* MIT License
*
* Copyright (c) 2020, Simon Larsén
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package se.kth.spork;

/**
* This is the main class :)
*/
public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
10 changes: 10 additions & 0 deletions src/test/resources/clean/both_modified/add_file_header/Left.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package se.kth.spork;

/**
* This is the main class :)
*/
public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
30 changes: 30 additions & 0 deletions src/test/resources/clean/both_modified/add_file_header/Right.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* MIT License
*
* Copyright (c) 2020, Simon Larsén
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package se.kth.spork;

public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}

0 comments on commit 111eb31

Please sign in to comment.