Skip to content

Commit

Permalink
[feat] Add Git compatibility mode, fix #84
Browse files Browse the repository at this point in the history
This consists of hard linking .merge_file_xxx files to .merge_file_xxx.java, and then removing those hard links when the JVM exits.
  • Loading branch information
slarse committed Mar 26, 2020
1 parent 0a8828c commit f76420c
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/main/java/se/kth/spork/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,31 @@ private static class MergeCommand implements Callable<Integer> {
description = "Path to the output file. Existing files are overwritten")
File out;

@CommandLine.Option(
names = {"--git-mode"},
description = "Enable Git compatibility mode. Required to use Spork as a Git merge driver."
)
boolean gitMode;

@Override
public Integer call() throws IOException {
long start = System.nanoTime();

String pretty = merge(base.toPath(), left.toPath(), right.toPath());
Path basePath = base.toPath();
Path leftPath = left.toPath();
Path rightPath = right.toPath();

if (gitMode) {
basePath = gitCompatHardLink(basePath);
leftPath = gitCompatHardLink(leftPath);
rightPath = gitCompatHardLink(rightPath);

basePath.toFile().deleteOnExit();
leftPath.toFile().deleteOnExit();
rightPath.toFile().deleteOnExit();
}

String pretty = merge(basePath, leftPath, rightPath);

if (out != null) {
LOGGER.info("Writing merge to " + out);
Expand Down Expand Up @@ -193,6 +213,29 @@ public static String merge(Path base, Path left, Path right) {
}
}

/**
* Create a hard link from a temporary git .merge_xxx file, with the name .merge_xxx.java. This is necessary for
* Spork to be compatible with Git, as Spoon will only parse Java files if they actually have the .java file
* extension.
*
* @param path Path to the temporary merge file.
* @return A path to a new hard link to the file, but with a .java file extension.
*/
private static Path gitCompatHardLink(Path path) throws IOException {
if (!path.getFileName().toString().startsWith(".merge_file")) {
throw new IllegalArgumentException(path + " not a Git merge file");
}

Path compatLink = path.resolveSibling(path.getFileName().toString() + ".java");
try {
Files.createLink(compatLink, path);
} catch (UnsupportedOperationException x) {
throw new IllegalStateException("Creating Git compatibility hard link not supported by file system");
}

return compatLink;
}

private static boolean containsTypes(CtElement elem) {
List<CtType<?>> types = elem.getElements(e -> true);
return types.size() > 0;
Expand Down

0 comments on commit f76420c

Please sign in to comment.