From bdc834157556c5e29b4afc1110f331c866e809ad Mon Sep 17 00:00:00 2001 From: "Nadya.Zabrodina" Date: Thu, 27 Dec 2012 16:42:25 +0400 Subject: [PATCH] IDEA-69177 Mercurial: if invalid username/password is provided in .hg/hgrc file, give user possibility to login from IDEA *Add notification error if repository url contains invalid login and password (in hgrc file). *Before: if hgrc paths.default contains url with login and password then mercurial doesn't require password,so HgCommandAuthorization class can't control auhtorization process. --- .../org/zmlx/hg4idea/command/HgCloneCommand.java | 2 +- .../org/zmlx/hg4idea/command/HgCopyCommand.java | 2 +- .../zmlx/hg4idea/command/HgIdentifyCommand.java | 4 ++-- .../org/zmlx/hg4idea/command/HgInitCommand.java | 2 +- .../org/zmlx/hg4idea/command/HgPullCommand.java | 2 +- .../org/zmlx/hg4idea/command/HgPushCommand.java | 2 +- .../command/HgRemoteChangesetsCommand.java | 2 +- .../hg4idea/execution/HgCommandExecutor.java | 13 +++++++++++++ .../src/org/zmlx/hg4idea/ui/HgPullDialog.java | 16 +++++++++------- .../src/org/zmlx/hg4idea/util/HgErrorUtil.java | 14 ++++++++++++++ 10 files changed, 44 insertions(+), 15 deletions(-) diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCloneCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCloneCommand.java index b3bfe7e5b8c7..353c20fb54dd 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCloneCommand.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCloneCommand.java @@ -31,7 +31,7 @@ public class HgCloneCommand { final List arguments = new ArrayList(2); arguments.add(repositoryURL); arguments.add(directory); - final HgCommandExecutor executor = new HgCommandExecutor(project); + final HgCommandExecutor executor = new HgCommandExecutor(project, repositoryURL); executor.setShowOutput(true); return executor.executeInCurrentThread(null, "clone", arguments); } diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCopyCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCopyCommand.java index 232b600ef1da..8841179d171a 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCopyCommand.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCopyCommand.java @@ -30,9 +30,9 @@ public class HgCopyCommand { } public void execute(VirtualFile source, VirtualFile target) { - HgCommandExecutor executor = new HgCommandExecutor(myProject); VirtualFile sourceRepo = VcsUtil.getVcsRootFor(myProject, source); VirtualFile targetRepo = VcsUtil.getVcsRootFor(myProject, target); + HgCommandExecutor executor = new HgCommandExecutor(myProject, VcsFileUtil.relativeOrFullPath(sourceRepo, source)); if (sourceRepo != null && targetRepo != null && sourceRepo.equals(targetRepo)) { executor.execute(sourceRepo, "copy", Arrays.asList("--after", VcsFileUtil.relativeOrFullPath(sourceRepo, source), diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgIdentifyCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgIdentifyCommand.java index fe65c06bd869..098f74ed0f86 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgIdentifyCommand.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgIdentifyCommand.java @@ -2,8 +2,8 @@ package org.zmlx.hg4idea.command; import com.intellij.openapi.project.Project; import org.jetbrains.annotations.Nullable; -import org.zmlx.hg4idea.execution.HgCommandResult; import org.zmlx.hg4idea.execution.HgCommandExecutor; +import org.zmlx.hg4idea.execution.HgCommandResult; import java.util.LinkedList; import java.util.List; @@ -29,7 +29,7 @@ public class HgIdentifyCommand { public HgCommandResult execute() { final List arguments = new LinkedList(); arguments.add(source); - final HgCommandExecutor executor = new HgCommandExecutor(project); + final HgCommandExecutor executor = new HgCommandExecutor(project, source); executor.setSilent(true); return executor.executeInCurrentThread(null, "identify", arguments); } diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgInitCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgInitCommand.java index 4c78dba985fc..0dd7604ad81e 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgInitCommand.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgInitCommand.java @@ -25,7 +25,7 @@ public class HgInitCommand { public void execute(@NotNull VirtualFile repositoryRoot,final HgCommandResultHandler resultHandler) { final List args = new ArrayList(1); args.add(repositoryRoot.getPath()); - final HgCommandExecutor executor = new HgCommandExecutor(myProject); + final HgCommandExecutor executor = new HgCommandExecutor(myProject, repositoryRoot.getPath()); executor.setShowOutput(true); executor.execute(null, "init", args, new HgCommandResultHandler() { @Override diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgPullCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgPullCommand.java index 2acb55db2ed4..3be5904b379e 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgPullCommand.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgPullCommand.java @@ -73,7 +73,7 @@ public class HgPullCommand { arguments.add(source); - final HgCommandExecutor executor = new HgCommandExecutor(project); + final HgCommandExecutor executor = new HgCommandExecutor(project, source); executor.setShowOutput(true); HgCommandResult result = executor.executeInCurrentThread(repo, "pull", arguments); if (HgErrorUtil.isAuthorizationError(result)) { diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgPushCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgPushCommand.java index 2ebc13f46523..37f37917f6b3 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgPushCommand.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgPushCommand.java @@ -68,7 +68,7 @@ public class HgPushCommand { } arguments.add(myDestination); - final HgCommandExecutor executor = new HgCommandExecutor(myProject); + final HgCommandExecutor executor = new HgCommandExecutor(myProject, myDestination); executor.setShowOutput(true); executor.execute(myRepo, "push", arguments, new HgCommandResultHandler() { @Override diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgRemoteChangesetsCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgRemoteChangesetsCommand.java index 12650c7c3fa4..118d23a63855 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgRemoteChangesetsCommand.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgRemoteChangesetsCommand.java @@ -65,7 +65,7 @@ public abstract class HgRemoteChangesetsCommand extends HgChangesetsCommand { LOG.info("executeCommand no default path configured"); return null; } - HgCommandExecutor executor = new HgCommandExecutor(project); + HgCommandExecutor executor = new HgCommandExecutor(project, repositoryURL); HgCommandResult result = executor.executeInCurrentThread(repo, command, args); if (result == HgCommandResult.CANCELLED || HgErrorUtil.isAuthorizationError(result)) { final HgVcs vcs = HgVcs.getInstance(project); diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandExecutor.java b/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandExecutor.java index 832fc1f971d7..c67a6bf332a7 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandExecutor.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandExecutor.java @@ -24,6 +24,7 @@ import org.jetbrains.annotations.Nullable; import org.zmlx.hg4idea.HgGlobalSettings; import org.zmlx.hg4idea.HgVcs; import org.zmlx.hg4idea.HgVcsMessages; +import org.zmlx.hg4idea.action.HgCommandResultNotifier; import org.zmlx.hg4idea.util.HgEncodingUtil; import org.zmlx.hg4idea.util.HgErrorUtil; import org.zmlx.hg4idea.util.HgUtil; @@ -57,6 +58,7 @@ public final class HgCommandExecutor { private final Project myProject; private final HgVcs myVcs; + private final String myDestination; private Charset myCharset = HgEncodingUtil.getDefaultCharset(); private boolean myIsSilent = false; @@ -64,8 +66,13 @@ public final class HgCommandExecutor { private List myOptions = DEFAULT_OPTIONS; public HgCommandExecutor(Project project) { + this(project, null); + } + + public HgCommandExecutor(Project project, @Nullable String destination) { myProject = project; myVcs = HgVcs.getInstance(myProject); + myDestination = destination; } public void setCharset(Charset charset) { @@ -110,6 +117,12 @@ public final class HgCommandExecutor { @Nullable final List arguments, @Nullable HgPromptHandler handler) { HgCommandResult result = executeInCurrentThread(repo, operation, arguments, handler, false); if (HgErrorUtil.isAuthorizationError(result)) { + if (HgErrorUtil.hasAuthorizationInDestinationPath(myDestination)) { + new HgCommandResultNotifier(myProject) + .notifyError(result, "Authorization failed", "Your hgrc file settings have wrong username or password in [paths].\n" + + "Please, update your .hg/hgrc file."); + return null; + } result = executeInCurrentThread(repo, operation, arguments, handler, true); } return result; diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/ui/HgPullDialog.java b/plugins/hg4idea/src/org/zmlx/hg4idea/ui/HgPullDialog.java index 93e4911b24b0..12601aaf1f3f 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/ui/HgPullDialog.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/ui/HgPullDialog.java @@ -99,14 +99,16 @@ public class HgPullDialog extends DialogWrapper { VirtualFile repo = hgRepositorySelector.getRepository(); HgShowConfigCommand configCommand = new HgShowConfigCommand(project); final String defaultPath = configCommand.getDefaultPath(repo); - UIUtil.invokeAndWaitIfNeeded(new Runnable() { - @Override - public void run() { - myRepositoryURL.setText(defaultPath); - } - }); + if (!StringUtil.isEmptyOrSpaces(defaultPath)) { + UIUtil.invokeAndWaitIfNeeded(new Runnable() { + @Override + public void run() { + myRepositoryURL.setText(defaultPath); + } + }); - onChangePullSource(); + onChangePullSource(); + } } }); } diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgErrorUtil.java b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgErrorUtil.java index b272862af45e..0533e1b60a78 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgErrorUtil.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgErrorUtil.java @@ -17,6 +17,8 @@ import org.jetbrains.annotations.Nullable; import org.zmlx.hg4idea.execution.HgCommandResult; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public final class HgErrorUtil { @@ -56,4 +58,16 @@ public final class HgErrorUtil { public static boolean hasErrorsInCommandExecution(HgCommandResult result) { return isAbort(result) || result.getExitValue() != 0; } + + public static boolean hasAuthorizationInDestinationPath(String destinationPath) { + if (StringUtil.isEmptyOrSpaces(destinationPath)) { + return false; + } + Pattern pattern = Pattern.compile("(.+):(.+)@(.+)"); + Matcher matcher = pattern.matcher(destinationPath); + if (matcher.matches()) { + return true; + } + return false; + } }