summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.pgm.test/tst
diff options
context:
space:
mode:
authorAndre Bossert <andre.bossert@siemens.com>2020-01-19 20:50:14 +0100
committerAndrey Loskutov <loskutov@gmx.de>2022-05-25 13:23:50 +0200
commitb63c2f39a16f1607cc65e82c0271c8c29a6038f1 (patch)
treebbb9e4155b28c72fb2bb986cfbe4c376c2f5cfce /org.eclipse.jgit.pgm.test/tst
parent0c749d33ba59f864e18400141b2510398a8dd77c (diff)
downloadjgit-b63c2f39a16f1607cc65e82c0271c8c29a6038f1.tar.gz
jgit-b63c2f39a16f1607cc65e82c0271c8c29a6038f1.zip
Add difftool compare feature (execute external tool)
see: http://git-scm.com/docs/git-difftool * add CommandExecutor that handles tool execution with help of "jgit.FS" * it handles tool execution with temporary created "command file" --> for for all "command interpreters" and parameters with spaces etc. * using of external bash.exe at Windows (MinGW) if shell-script is used as difftool command. It can be enabled with parameter "jgit.usemsys2bash=auto" that checks if command contains ".sh" or enabled / disabled with "jgit.usemsys2bash=true|false" * added special handling for empty files (e.g. deleted, added etc.) that are named "/dev/null" * added creation and deletion of temporary files needed for compare * added own Exception class for reporting to pgm / command line / EGit * added prompt option handling before executing difftool * reworked trustExitCode option for specific difftool and override for all difftools from config and command line * tested with command line options "--[no]-trust-exit-code", "--tool=<toolname>", "--[no]-gui", --[no]-prompt * ContentSource * added close() methods to close / cleanup used resources (like ObjectReader TreeWalk etc.) * added isWorkingTreeSource() methods to check if file can be used from working tree instead of copy from "ObjectLoader / ObjectReader" to temporary file (fixes "difftool <commit> <commit>") Bug: 356832 Change-Id: I5462fb6dbe4ecfd9da7c74117fce4070bbfd4d7a Signed-off-by: Andre Bossert <andre.bossert@siemens.com> Signed-off-by: Simeon Andreev <simeon.danailov.andreev@gmail.com>
Diffstat (limited to 'org.eclipse.jgit.pgm.test/tst')
-rw-r--r--org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DiffToolTest.java63
1 files changed, 53 insertions, 10 deletions
diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DiffToolTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DiffToolTest.java
index e7bf48417d..e2ff189276 100644
--- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DiffToolTest.java
+++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/DiffToolTest.java
@@ -9,7 +9,13 @@
*/
package org.eclipse.jgit.pgm;
+import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DIFFTOOL_SECTION;
+import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DIFF_SECTION;
+import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_CMD;
+import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PROMPT;
+import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_TOOL;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Arrays;
@@ -19,6 +25,7 @@ import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.internal.diffmergetool.CommandLineDiffTool;
import org.eclipse.jgit.lib.CLIRepositoryTestCase;
+import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.pgm.opt.CmdLineParser;
import org.eclipse.jgit.pgm.opt.SubcommandHandler;
import org.eclipse.jgit.revwalk.RevCommit;
@@ -60,6 +67,7 @@ public class DiffToolTest extends CLIRepositoryTestCase {
return result.outLines().toArray(new String[0]);
}
+ private static final String TOOL_NAME = "some_tool";
private Git git;
@Override
@@ -68,6 +76,15 @@ public class DiffToolTest extends CLIRepositoryTestCase {
super.setUp();
git = new Git(db);
git.commit().setMessage("initial commit").call();
+ configureEchoTool(TOOL_NAME);
+ }
+
+ @Test(expected = Die.class)
+ public void testNotDefinedTool() throws Exception {
+ createUnstagedChanges();
+
+ runAndCaptureUsingInitRaw("difftool", "--tool", "undefined");
+ fail("Expected exception when trying to run undefined tool");
}
@Test
@@ -85,7 +102,7 @@ public class DiffToolTest extends CLIRepositoryTestCase {
assertArrayOfLinesEquals("Incorrect output for option: " + option,
expectedOutput,
runAndCaptureUsingInitRaw("difftool", option,
- "some_tool"));
+ TOOL_NAME));
}
}
@@ -100,7 +117,7 @@ public class DiffToolTest extends CLIRepositoryTestCase {
for (String option : options) {
assertArrayOfLinesEquals("Incorrect output for option: " + option,
expectedOutput, runAndCaptureUsingInitRaw("difftool",
- "--trust-exit-code", option, "some_tool"));
+ "--trust-exit-code", option, TOOL_NAME));
}
}
@@ -116,7 +133,7 @@ public class DiffToolTest extends CLIRepositoryTestCase {
assertArrayOfLinesEquals("Incorrect output for option: " + option,
expectedOutput, runAndCaptureUsingInitRaw("difftool",
"--no-gui", "--no-prompt", "--no-trust-exit-code",
- option, "some_tool"));
+ option, TOOL_NAME));
}
}
@@ -131,7 +148,7 @@ public class DiffToolTest extends CLIRepositoryTestCase {
for (String option : options) {
assertArrayOfLinesEquals("Incorrect output for option: " + option,
expectedOutput, runAndCaptureUsingInitRaw("difftool",
- option, "--tool", "some_tool"));
+ option, "--tool", TOOL_NAME));
}
}
@@ -144,8 +161,11 @@ public class DiffToolTest extends CLIRepositoryTestCase {
String toolName = defaultTool.name();
expectedOutput.add(toolName);
}
+ String customToolHelpLine = TOOL_NAME + "." + CONFIG_KEY_CMD + " "
+ + getEchoCommand();
+ expectedOutput.add("user-defined:");
+ expectedOutput.add(customToolHelpLine);
String[] userDefinedToolsHelp = {
- "user-defined:",
"The following tools are valid, but not currently available:",
"Some of the tools listed above only work in a windowed",
"environment. If run in a terminal-only session, they will fail.",
@@ -157,6 +177,25 @@ public class DiffToolTest extends CLIRepositoryTestCase {
expectedOutput.toArray(new String[0]), runAndCaptureUsingInitRaw("difftool", option));
}
+ private void configureEchoTool(String toolName) {
+ StoredConfig config = db.getConfig();
+ // the default diff tool is configured without a subsection
+ String subsection = null;
+ config.setString(CONFIG_DIFF_SECTION, subsection, CONFIG_KEY_TOOL,
+ toolName);
+
+ String command = getEchoCommand();
+
+ config.setString(CONFIG_DIFFTOOL_SECTION, toolName, CONFIG_KEY_CMD,
+ command);
+ /*
+ * prevent prompts as we are running in tests and there is no user to
+ * interact with on the command line
+ */
+ config.setString(CONFIG_DIFFTOOL_SECTION, toolName, CONFIG_KEY_PROMPT,
+ String.valueOf(false));
+ }
+
private RevCommit createUnstagedChanges() throws Exception {
writeTrashFile("a", "Hello world a");
writeTrashFile("b", "Hello world b");
@@ -188,11 +227,7 @@ public class DiffToolTest extends CLIRepositoryTestCase {
for (int i = 0; i < changes.size(); ++i) {
DiffEntry change = changes.get(i);
String newPath = change.getNewPath();
- String oldPath = change.getOldPath();
- String newIdName = change.getNewId().name();
- String oldIdName = change.getOldId().name();
- String expectedLine = "M\t" + newPath + " (" + newIdName + ")"
- + "\t" + oldPath + " (" + oldIdName + ")";
+ String expectedLine = newPath;
expectedToolOutput[i] = expectedLine;
}
return expectedToolOutput;
@@ -202,4 +237,12 @@ public class DiffToolTest extends CLIRepositoryTestCase {
String[] expected, String[] actual) {
assertEquals(failMessage, toString(expected), toString(actual));
}
+
+ private static String getEchoCommand() {
+ /*
+ * use 'MERGED' placeholder, as both 'LOCAL' and 'REMOTE' will be
+ * replaced with full paths to a temporary file during some of the tests
+ */
+ return "(echo \"$MERGED\")";
+ }
}