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.
This commit is contained in:
Nadya.Zabrodina
2012-12-27 18:19:05 +04:00
parent 00a348c3a9
commit bdc8341575
10 changed files with 44 additions and 15 deletions
@@ -31,7 +31,7 @@ public class HgCloneCommand {
final List<String> arguments = new ArrayList<String>(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);
}
@@ -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),
@@ -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<String> arguments = new LinkedList<String>();
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);
}
@@ -25,7 +25,7 @@ public class HgInitCommand {
public void execute(@NotNull VirtualFile repositoryRoot,final HgCommandResultHandler resultHandler) {
final List<String> args = new ArrayList<String>(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
@@ -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)) {
@@ -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
@@ -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);
@@ -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<String> 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<String> 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;
@@ -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();
}
}
});
}
@@ -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;
}
}