From 54fbaffb00d73f1fb5cac8cbf12a40444ec5ae78 Mon Sep 17 00:00:00 2001 From: Constantine Plotnikov Date: Thu, 4 Dec 2008 19:33:31 +0300 Subject: [PATCH] git4idea: IDEADEV-32985, IDEADEV-32986 (make escaping only to be enabled if actions fail) --- .../git4idea/src/git4idea/actions/GitUnstash.java | 28 ++++++-- .../git4idea/src/git4idea/commands/GitHandler.java | 31 ++------- .../src/git4idea/commands/GitSimpleHandler.java | 13 ++++ .../git4idea/src/git4idea/ui/GitUnstashDialog.java | 76 ++++++++++++++++++---- 4 files changed, 105 insertions(+), 43 deletions(-) diff --git a/plugins/git4idea/src/git4idea/actions/GitUnstash.java b/plugins/git4idea/src/git4idea/actions/GitUnstash.java index 633ff9c789..f015df5741 100644 --- a/plugins/git4idea/src/git4idea/actions/GitUnstash.java +++ b/plugins/git4idea/src/git4idea/actions/GitUnstash.java @@ -18,9 +18,9 @@ package git4idea.actions; import com.intellij.openapi.project.Project; import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vfs.VirtualFile; -import git4idea.commands.GitHandlerUtil; -import git4idea.commands.GitLineHandler; +import git4idea.commands.GitSimpleHandler; import git4idea.i18n.GitBundle; +import git4idea.ui.GitUIUtil; import git4idea.ui.GitUnstashDialog; import org.jetbrains.annotations.NotNull; @@ -53,8 +53,28 @@ public class GitUnstash extends GitRepositoryAction { if (!d.isOK()) { return; } - final GitLineHandler h = d.handler(); affectedRoots.add(d.getGitRoot()); - GitHandlerUtil.doSynchronously(h, GitBundle.getString("unstashing.title"), h.printableCommandLine()); + GitSimpleHandler h = d.handler(false); + try { + h.setSilent(true); + h.run(); + h.unsilence(); + } + catch (VcsException ex) { + try { + //noinspection HardCodedStringLiteral + if (ex.getMessage().startsWith("fatal: Needed a single revision")) { + h = d.handler(true); + h.run(); + } + else { + h.unsilence(); + throw ex; + } + } + catch (VcsException ex2) { + GitUIUtil.showOperationError(project, ex, h.printableCommandLine()); + } + } } } diff --git a/plugins/git4idea/src/git4idea/commands/GitHandler.java b/plugins/git4idea/src/git4idea/commands/GitHandler.java index 2f1f977345..92707a6e1d 100644 --- a/plugins/git4idea/src/git4idea/commands/GitHandler.java +++ b/plugins/git4idea/src/git4idea/commands/GitHandler.java @@ -22,7 +22,6 @@ import com.intellij.execution.process.ProcessListener; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Key; -import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.vcs.FilePath; import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vfs.VfsUtil; @@ -348,9 +347,7 @@ public abstract class GitHandler { @SuppressWarnings({"WeakerAccess"}) public void addParameters(@NonNls @NotNull String... parameters) { checkNotStarted(); - for (String s : parameters) { - myCommandLine.addParameter(translateParameter(s)); - } + myCommandLine.addParameters(parameters); } /** @@ -373,7 +370,7 @@ public abstract class GitHandler { public void addRelativePaths(@NotNull final Collection filePaths) { checkNotStarted(); for (FilePath path : filePaths) { - myCommandLine.addParameter(translateParameter(GitUtil.relativePath(myWorkingDirectory, path))); + myCommandLine.addParameter(GitUtil.relativePath(myWorkingDirectory, path)); } } @@ -387,7 +384,7 @@ public abstract class GitHandler { public void addRelativeFiles(@NotNull final Collection files) { checkNotStarted(); for (VirtualFile file : files) { - myCommandLine.addParameter(translateParameter(GitUtil.relativePath(myWorkingDirectory, file))); + myCommandLine.addParameter(GitUtil.relativePath(myWorkingDirectory, file)); } } @@ -509,7 +506,7 @@ public abstract class GitHandler { public String printableCommandLine() { final GeneralCommandLine line = myCommandLine.clone(); line.setExePath("git"); - return restoreParameter(line.getCommandLineString()); + return line.getCommandLineString(); } /** @@ -648,24 +645,4 @@ public abstract class GitHandler { public void setenv(String name, String value) { myEnv.put(name, value); } - - /** - * Translate parameter - * - * @param s a string to translate - * @return a translated string - */ - public static String translateParameter(String s) { - return SystemInfo.isWindows ? s.replaceAll("([\\{}])", "\\\\$1") : s; - } - - /** - * Restore parameter - * - * @param s a string to translate - * @return a translated string - */ - public static String restoreParameter(String s) { - return SystemInfo.isWindows ? s.replaceAll("\\\\([{}])", "$1") : s; - } } diff --git a/plugins/git4idea/src/git4idea/commands/GitSimpleHandler.java b/plugins/git4idea/src/git4idea/commands/GitSimpleHandler.java index 901c5f8161..46dc81a5e2 100644 --- a/plugins/git4idea/src/git4idea/commands/GitSimpleHandler.java +++ b/plugins/git4idea/src/git4idea/commands/GitSimpleHandler.java @@ -95,6 +95,19 @@ public class GitSimpleHandler extends GitHandler { } /** + * For silent handlers, print out everithng + */ + public void unsilence() { + myVcs.showCommandLine(printableCommandLine()); + if (myStderr.length() != 0) { + myVcs.showErrorMessages(myStderr.toString()); + } + if (myStdout.length() != 0) { + myVcs.showMessages(myStdout.toString()); + } + } + + /** * {@inheritDoc} */ protected void onTextAvailable(final String text, final Key outputType) { diff --git a/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java b/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java index 1fcf9f5129..d7b9c61d7c 100644 --- a/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java +++ b/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java @@ -133,27 +133,66 @@ public class GitUnstashDialog extends DialogWrapper { }); myDropButton.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { - GitLineHandler h = new GitLineHandler(myProject, getGitRoot(), GitHandler.STASH); - h.setNoSSH(true); final String stash = getSelectedStash(); - h.addParameters("drop", stash); - GitHandlerUtil.doSynchronously(h, GitBundle.message("unstash.dropping.stash", stash), h.printableCommandLine()); + GitSimpleHandler h = dropHandler(stash); + try { + h.setSilent(true); + h.run(); + h.unsilence(); + } + catch (VcsException ex) { + try { + //noinspection HardCodedStringLiteral + if (ex.getMessage().startsWith("fatal: Needed a single revision")) { + h = dropHandler(translateStash(stash)); + h.run(); + } + else { + h.unsilence(); + throw ex; + } + } + catch (VcsException ex2) { + GitUIUtil.showOperationError(myProject, ex, h.printableCommandLine()); + return; + } + } refreshStashList(); updateDialogState(); } + + private GitSimpleHandler dropHandler(String stash) { + GitSimpleHandler h = new GitSimpleHandler(myProject, getGitRoot(), GitHandler.STASH); + h.setNoSSH(true); + h.addParameters("drop", stash); + return h; + } }); myViewButton.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { final VirtualFile root = getGitRoot(); - String stash; + String resolvedStash; + String selectedStash = getSelectedStash(); try { - stash = GitRevisionNumber.resolve(myProject, root, getSelectedStash()).asString(); + resolvedStash = GitRevisionNumber.resolve(myProject, root, selectedStash).asString(); } catch (VcsException ex) { - GitUIUtil.showOperationError(myProject, ex, "resolving revision"); - return; + try { + //noinspection HardCodedStringLiteral + if (ex.getMessage().startsWith("fatal: bad revision 'stash@")) { + selectedStash = translateStash(selectedStash); + resolvedStash = GitRevisionNumber.resolve(myProject, root, selectedStash).asString(); + } + else { + throw ex; + } + } + catch (VcsException ex2) { + GitUIUtil.showOperationError(myProject, ex, "resolving revision"); + return; + } } - GitShowAllSubmittedFilesAction.showSubmittedFiles(myProject, stash, root); + GitShowAllSubmittedFilesAction.showSubmittedFiles(myProject, resolvedStash, root); } }); init(); @@ -161,6 +200,16 @@ public class GitUnstashDialog extends DialogWrapper { } /** + * Translate stash name so that { } are escaped. + * + * @param selectedStash a selected stash + * @return translated name + */ + private static String translateStash(String selectedStash) { + return selectedStash.replaceAll("([\\{}])", "\\\\$1"); + } + + /** * Update state dialog depending on the current state of the fields */ public void updateDialogState() { @@ -255,10 +304,12 @@ public class GitUnstashDialog extends DialogWrapper { } /** + * @param escaped if true stash name will be escaped * @return unstash handler */ - public GitLineHandler handler() { - GitLineHandler h = new GitLineHandler(myProject, getGitRoot(), GitHandler.STASH); + public GitSimpleHandler handler(boolean escaped) { + GitSimpleHandler h = new GitSimpleHandler(myProject, getGitRoot(), GitHandler.STASH); + h.setNoSSH(true); String branch = myBranchTextField.getText(); if (branch.length() == 0) { h.addParameters(myPopStashCheckBox.isSelected() ? "pop" : "apply"); @@ -269,7 +320,8 @@ public class GitUnstashDialog extends DialogWrapper { else { h.addParameters("branch", branch); } - h.addParameters(getSelectedStash()); + final String selectedStash = getSelectedStash(); + h.addParameters(escaped ? translateStash(selectedStash) : selectedStash); return h; } -- 2.11.4.GIT