From 111eb31bd058c3b34071c71dd4f61e5e3f659e5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Lars=C3=A9n?= Date: Sat, 4 Jul 2020 19:54:55 +0200 Subject: [PATCH] [fix] Fix false positive conflict in line-based merge on pure addition (#145) --- .../se/kth/spork/util/LineBasedMerge.java | 6 ++++ src/test/java/se/kth/spork/cli/CliTest.java | 4 +++ .../both_modified/add_file_header/Base.java | 7 ++++ .../add_file_header/Expected.java | 33 +++++++++++++++++++ .../both_modified/add_file_header/Left.java | 10 ++++++ .../both_modified/add_file_header/Right.java | 30 +++++++++++++++++ 6 files changed, 90 insertions(+) create mode 100644 src/test/resources/clean/both_modified/add_file_header/Base.java create mode 100644 src/test/resources/clean/both_modified/add_file_header/Expected.java create mode 100644 src/test/resources/clean/both_modified/add_file_header/Left.java create mode 100644 src/test/resources/clean/both_modified/add_file_header/Right.java diff --git a/src/main/java/se/kth/spork/util/LineBasedMerge.java b/src/main/java/se/kth/spork/util/LineBasedMerge.java index 28accaf5..d0847c94 100644 --- a/src/main/java/se/kth/spork/util/LineBasedMerge.java +++ b/src/main/java/se/kth/spork/util/LineBasedMerge.java @@ -31,6 +31,12 @@ public class LineBasedMerge { * @return A pair containing the merge and the amount of conflicts. */ public static Pair 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()); diff --git a/src/test/java/se/kth/spork/cli/CliTest.java b/src/test/java/se/kth/spork/cli/CliTest.java index 4d672079..1ec5e513 100644 --- a/src/test/java/se/kth/spork/cli/CliTest.java +++ b/src/test/java/se/kth/spork/cli/CliTest.java @@ -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); @@ -140,6 +142,7 @@ 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); @@ -147,5 +150,6 @@ private static void runTestMerge(Util.TestSources sources, Path tempDir) throws assertEquals(mergeTree, reParsedMerge); assertEquals(reParsedImports, expectedImports); + assertEquals(reparsedCuComment, expectedCuComment); } } \ No newline at end of file diff --git a/src/test/resources/clean/both_modified/add_file_header/Base.java b/src/test/resources/clean/both_modified/add_file_header/Base.java new file mode 100644 index 00000000..71aa5a71 --- /dev/null +++ b/src/test/resources/clean/both_modified/add_file_header/Base.java @@ -0,0 +1,7 @@ +package se.kth.spork; + +public class Main { + public static void main(String[] args) { + System.out.println("Hello, world!"); + } +} \ No newline at end of file diff --git a/src/test/resources/clean/both_modified/add_file_header/Expected.java b/src/test/resources/clean/both_modified/add_file_header/Expected.java new file mode 100644 index 00000000..b721dfd4 --- /dev/null +++ b/src/test/resources/clean/both_modified/add_file_header/Expected.java @@ -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!"); + } +} diff --git a/src/test/resources/clean/both_modified/add_file_header/Left.java b/src/test/resources/clean/both_modified/add_file_header/Left.java new file mode 100644 index 00000000..db413003 --- /dev/null +++ b/src/test/resources/clean/both_modified/add_file_header/Left.java @@ -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!"); + } +} diff --git a/src/test/resources/clean/both_modified/add_file_header/Right.java b/src/test/resources/clean/both_modified/add_file_header/Right.java new file mode 100644 index 00000000..b908680d --- /dev/null +++ b/src/test/resources/clean/both_modified/add_file_header/Right.java @@ -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!"); + } +}