diff options
author | Andre Bossert <andre.bossert@siemens.com> | 2020-01-19 20:56:28 +0100 |
---|---|---|
committer | Andrey Loskutov <loskutov@gmx.de> | 2022-06-01 14:23:48 +0200 |
commit | 973e955ead1a9bf41efb3168baf5b68527e78023 (patch) | |
tree | 70e6b916308e20da790ac41759e1b9bd3c28b324 /org.eclipse.jgit.pgm/src/org | |
parent | bb30be6b3335c67ee59d3705d5e04e2dbb688128 (diff) | |
download | jgit-973e955ead1a9bf41efb3168baf5b68527e78023.tar.gz jgit-973e955ead1a9bf41efb3168baf5b68527e78023.zip |
Add availability check of pre-defined tools
see: https://git-scm.com/docs/git-difftool
see: https://git-scm.com/docs/git-mergetool
* now all available tools are printed with "--tool-help"
* if no diff.tool or merge.tool is defined the first available
pre-defined tool is used
TODO:
- add mergetools to difftools --> extra change or merge to this
- return the exit-code of the tool to jgit / java runtime
Bug: 356832
Change-Id: I20fb04e71ced981f5625020f461bbac24e6cec70
Signed-off-by: Andre Bossert <andre.bossert@siemens.com>
Diffstat (limited to 'org.eclipse.jgit.pgm/src/org')
3 files changed, 90 insertions, 37 deletions
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/DiffTool.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/DiffTool.java index 74d91cd3d7..e5a3c53e3f 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/DiffTool.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/DiffTool.java @@ -23,29 +23,30 @@ import java.text.MessageFormat; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; + import org.eclipse.jgit.diff.ContentSource; import org.eclipse.jgit.diff.ContentSource.Pair; import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.DiffEntry.Side; -import org.eclipse.jgit.internal.diffmergetool.ToolException; -import org.eclipse.jgit.internal.diffmergetool.DiffTools; -import org.eclipse.jgit.internal.diffmergetool.FileElement; -import org.eclipse.jgit.internal.diffmergetool.ExternalDiffTool; import org.eclipse.jgit.diff.DiffFormatter; import org.eclipse.jgit.dircache.DirCacheCheckout; -import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.dircache.DirCacheCheckout.CheckoutMetadata; +import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.errors.AmbiguousObjectException; import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.NoWorkTreeException; import org.eclipse.jgit.errors.RevisionSyntaxException; +import org.eclipse.jgit.internal.diffmergetool.DiffTools; +import org.eclipse.jgit.internal.diffmergetool.ExternalDiffTool; +import org.eclipse.jgit.internal.diffmergetool.FileElement; +import org.eclipse.jgit.internal.diffmergetool.ToolException; import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.CoreConfig.EolStreamType; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectReader; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.TextProgressMonitor; -import org.eclipse.jgit.lib.CoreConfig.EolStreamType; import org.eclipse.jgit.lib.internal.BooleanTriState; import org.eclipse.jgit.pgm.internal.CLIText; import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler; @@ -58,8 +59,8 @@ import org.eclipse.jgit.treewalk.WorkingTreeIterator; import org.eclipse.jgit.treewalk.WorkingTreeOptions; import org.eclipse.jgit.treewalk.filter.PathFilterGroup; import org.eclipse.jgit.treewalk.filter.TreeFilter; -import org.eclipse.jgit.util.StringUtils; import org.eclipse.jgit.util.FS.ExecutionResult; +import org.eclipse.jgit.util.StringUtils; import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.Option; @@ -144,19 +145,14 @@ class DiffTool extends TextBuiltin { if (prompt != BooleanTriState.UNSET) { showPrompt = prompt == BooleanTriState.TRUE; } - String toolNamePrompt = toolName; - if (showPrompt) { - if (StringUtils.isEmptyOrNull(toolNamePrompt)) { - toolNamePrompt = diffTools.getDefaultToolName(gui); - } - } + // get passed or default tool name + String toolNameToUse = promptToolName(); // get the changed files List<DiffEntry> files = getFiles(); if (files.size() > 0) { - compare(files, showPrompt, toolNamePrompt); + compare(files, showPrompt, toolNameToUse); } } - outw.flush(); } catch (RevisionSyntaxException | IOException e) { throw die(e.getMessage(), e); } finally { @@ -164,8 +160,32 @@ class DiffTool extends TextBuiltin { } } + private String promptToolName() throws IOException { + String toolNameToUse = toolName; + if (StringUtils.isEmptyOrNull(toolNameToUse)) { + toolNameToUse = diffTools.getDefaultToolName(gui); + } + if (StringUtils.isEmptyOrNull(toolNameToUse)) { + Map<String, ExternalDiffTool> predefTools = diffTools + .getPredefinedTools(false); + StringBuilder toolNames = new StringBuilder(); + for (String name : predefTools.keySet()) { + toolNames.append(name + " "); //$NON-NLS-1$ + } + outw.println(MessageFormat.format( + CLIText.get().diffToolPromptToolName, toolNames)); + outw.flush(); + toolNameToUse = diffTools.getFirstAvailableTool(); + } + if (StringUtils.isEmptyOrNull(toolNameToUse)) { + throw new IOException(MessageFormat + .format(CLIText.get().diffToolUnknownToolName, toolName)); + } + return toolNameToUse; + } + private void compare(List<DiffEntry> files, boolean showPrompt, - String toolNamePrompt) throws IOException { + String toolNameToUse) throws IOException { ContentSource.Pair sourcePair = new ContentSource.Pair(source(oldTree), source(newTree)); try { @@ -179,7 +199,7 @@ class DiffTool extends TextBuiltin { boolean launchCompare = true; if (showPrompt) { launchCompare = isLaunchCompare(fileIndex + 1, files.size(), - mergedFilePath, toolNamePrompt); + mergedFilePath, toolNameToUse); } if (launchCompare) { try { @@ -195,17 +215,21 @@ class DiffTool extends TextBuiltin { // to jgit / java runtime ? // int rc =... ExecutionResult result = diffTools.compare(local, - remote, merged, toolName, prompt, gui, + remote, merged, toolNameToUse, prompt, gui, trustExitCode); outw.println(new String(result.getStdout().toByteArray())); + outw.flush(); errw.println( new String(result.getStderr().toByteArray())); + errw.flush(); } catch (ToolException e) { outw.println(e.getResultStdout()); outw.flush(); errw.println(e.getMessage()); + errw.flush(); throw die(MessageFormat.format( - CLIText.get().diffToolDied, mergedFilePath), e); + CLIText.get().diffToolDied, mergedFilePath, e), + e); } } else { break; @@ -232,16 +256,17 @@ class DiffTool extends TextBuiltin { } return launchCompare; } - private void showToolHelp() throws IOException { + Map<String, ExternalDiffTool> predefTools = diffTools + .getPredefinedTools(true); StringBuilder availableToolNames = new StringBuilder(); - for (String name : diffTools.getAvailableTools().keySet()) { - availableToolNames.append(MessageFormat.format("\t\t{0}\n", name)); //$NON-NLS-1$ - } StringBuilder notAvailableToolNames = new StringBuilder(); - for (String name : diffTools.getNotAvailableTools().keySet()) { - notAvailableToolNames - .append(MessageFormat.format("\t\t{0}\n", name)); //$NON-NLS-1$ + for (String name : predefTools.keySet()) { + if (predefTools.get(name).isAvailable()) { + availableToolNames.append(MessageFormat.format("\t\t{0}\n", name)); //$NON-NLS-1$ + } else { + notAvailableToolNames.append(MessageFormat.format("\t\t{0}\n", name)); //$NON-NLS-1$ + } } StringBuilder userToolNames = new StringBuilder(); Map<String, ExternalDiffTool> userTools = diffTools diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/MergeTool.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/MergeTool.java index 9712770758..f5884c44da 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/MergeTool.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/MergeTool.java @@ -48,6 +48,7 @@ import org.eclipse.jgit.treewalk.WorkingTreeOptions; import org.eclipse.jgit.treewalk.filter.PathFilterGroup; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.util.StringUtils; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.internal.BooleanTriState; import org.eclipse.jgit.lib.CoreConfig.EolStreamType; @@ -121,14 +122,11 @@ class MergeTool extends TextBuiltin { showPrompt = prompt == BooleanTriState.TRUE; } // get passed or default tool name - String toolNameSelected = toolName; - if ((toolNameSelected == null) || toolNameSelected.isEmpty()) { - toolNameSelected = mergeTools.getDefaultToolName(gui); - } + String toolNameToUse = promptToolName(); // get the changed files Map<String, StageState> files = getFiles(); if (files.size() > 0) { - merge(files, showPrompt, toolNameSelected); + merge(files, showPrompt, toolNameToUse); } else { outw.println(CLIText.get().mergeToolNoFiles); } @@ -139,6 +137,30 @@ class MergeTool extends TextBuiltin { } } + private String promptToolName() throws IOException { + String toolNameToUse = toolName; + if (StringUtils.isEmptyOrNull(toolNameToUse)) { + toolNameToUse = mergeTools.getDefaultToolName(gui); + } + if (StringUtils.isEmptyOrNull(toolNameToUse)) { + Map<String, ExternalMergeTool> predefTools = mergeTools + .getPredefinedTools(false); + StringBuilder toolNames = new StringBuilder(); + for (String name : predefTools.keySet()) { + toolNames.append(name + " "); //$NON-NLS-1$ + } + outw.println(MessageFormat + .format(CLIText.get().mergeToolPromptToolName, toolNames)); + outw.flush(); + toolNameToUse = mergeTools.getFirstAvailableTool(); + } + if (StringUtils.isEmptyOrNull(toolNameToUse)) { + throw new IOException(MessageFormat + .format(CLIText.get().mergeToolUnknownToolName, toolName)); + } + return toolNameToUse; + } + private void merge(Map<String, StageState> files, boolean showPrompt, String toolNamePrompt) throws Exception { // sort file names @@ -420,14 +442,16 @@ class MergeTool extends TextBuiltin { } private void showToolHelp() throws IOException { + Map<String, ExternalMergeTool> predefTools = mergeTools + .getPredefinedTools(true); StringBuilder availableToolNames = new StringBuilder(); - for (String name : mergeTools.getAvailableTools().keySet()) { - availableToolNames.append(MessageFormat.format("\t\t{0}\n", name)); //$NON-NLS-1$ - } StringBuilder notAvailableToolNames = new StringBuilder(); - for (String name : mergeTools.getNotAvailableTools().keySet()) { - notAvailableToolNames - .append(MessageFormat.format("\t\t{0}\n", name)); //$NON-NLS-1$ + for (String name : predefTools.keySet()) { + if (predefTools.get(name).isAvailable()) { + availableToolNames.append(MessageFormat.format("\t\t{0}\n", name)); //$NON-NLS-1$ + } else { + notAvailableToolNames.append(MessageFormat.format("\t\t{0}\n", name)); //$NON-NLS-1$ + } } StringBuilder userToolNames = new StringBuilder(); Map<String, ExternalMergeTool> userTools = mergeTools diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java index 989e649b72..e06f150e51 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/internal/CLIText.java @@ -139,6 +139,8 @@ public class CLIText extends TranslationBundle { /***/ public String diffToolHelpSetToFollowing; /***/ public String diffToolLaunch; /***/ public String diffToolDied; + /***/ public String diffToolPromptToolName; + /***/ public String diffToolUnknownToolName; /***/ public String doesNotExist; /***/ public String dontOverwriteLocalChanges; /***/ public String everythingUpToDate; @@ -185,6 +187,8 @@ public class CLIText extends TranslationBundle { /***/ public String mergeToolContinueUnresolvedPaths; /***/ public String mergeToolWasMergeSuccessfull; /***/ public String mergeToolDeletedMergeDecision; + /***/ public String mergeToolPromptToolName; + /***/ public String mergeToolUnknownToolName; /***/ public String mergeFailed; /***/ public String mergeCheckoutFailed; /***/ public String mergeMadeBy; |