diff --git a/plugins/git4idea/src/git4idea/actions/GitPull.java b/plugins/git4idea/src/git4idea/actions/GitPull.java index a1d071f85a4c..b460f03c24aa 100644 --- a/plugins/git4idea/src/git4idea/actions/GitPull.java +++ b/plugins/git4idea/src/git4idea/actions/GitPull.java @@ -92,9 +92,9 @@ public class GitPull extends GitRepositoryAction { if (!fetchSuccessful) { return; } - handlerReference.set(dialog.pullOrMergeHandler(false)); + handlerReference.set(dialog.pullOrMergeHandler(null)); } else { - handlerReference.set(dialog.pullOrMergeHandler(true)); + handlerReference.set(dialog.pullOrMergeHandler(url)); } diff --git a/plugins/git4idea/src/git4idea/branch/GitBranchOperation.java b/plugins/git4idea/src/git4idea/branch/GitBranchOperation.java index d980c88d2aa7..153d763133d3 100644 --- a/plugins/git4idea/src/git4idea/branch/GitBranchOperation.java +++ b/plugins/git4idea/src/git4idea/branch/GitBranchOperation.java @@ -45,7 +45,7 @@ import static com.intellij.openapi.util.text.StringUtil.pluralize; */ abstract class GitBranchOperation { - private static final Logger LOG = Logger.getInstance(GitBranchOperation.class); + protected static final Logger LOG = Logger.getInstance(GitBranchOperation.class); @NotNull protected final Project myProject; @NotNull protected final GitPlatformFacade myFacade; diff --git a/plugins/git4idea/src/git4idea/branch/GitDeleteRemoteBranchOperation.java b/plugins/git4idea/src/git4idea/branch/GitDeleteRemoteBranchOperation.java index 0fe26df6af57..1f22c6abed37 100644 --- a/plugins/git4idea/src/git4idea/branch/GitDeleteRemoteBranchOperation.java +++ b/plugins/git4idea/src/git4idea/branch/GitDeleteRemoteBranchOperation.java @@ -152,24 +152,31 @@ class GitDeleteRemoteBranchOperation extends GitBranchOperation { private GitCommandResult pushDeletion(@NotNull GitRepository repository, @NotNull String remoteName, @NotNull String branchName) { GitRemote remote = getRemoteByName(repository, remoteName); if (remote == null) { - return pushDeletionNatively(repository, remoteName, branchName); + String error = "Couldn't find remote by name: " + remoteName; + LOG.error(error); + return GitCommandResult.error(error); } String remoteUrl = remote.getFirstUrl(); - if (remoteUrl != null && GitHttpAdapter.shouldUseJGit(remoteUrl)) { + if (remoteUrl == null) { + LOG.warn("No urls are defined for remote: " + remote); + return GitCommandResult.error("There is no urls defined for remote " + remote.getName()); + } + if (GitHttpAdapter.shouldUseJGit(remoteUrl)) { String fullBranchName = branchName.startsWith(GitBranch.REFS_HEADS_PREFIX) ? branchName : GitBranch.REFS_HEADS_PREFIX + branchName; String spec = ":" + fullBranchName; GitSimplePushResult simplePushResult = GitHttpAdapter.push(repository, remote.getName(), remoteUrl, spec); return convertSimplePushResultToCommandResult(simplePushResult); } else { - return pushDeletionNatively(repository, remoteName, branchName); + return pushDeletionNatively(repository, remoteName, remoteUrl, branchName); } } @NotNull - private GitCommandResult pushDeletionNatively(@NotNull GitRepository repository, @NotNull String remoteName, @NotNull String branchName) { - return myGit.push(repository, remoteName, ":" + branchName); + private GitCommandResult pushDeletionNatively(@NotNull GitRepository repository, @NotNull String remoteName, @NotNull String url, + @NotNull String branchName) { + return myGit.push(repository, remoteName, url,":" + branchName); } @NotNull diff --git a/plugins/git4idea/src/git4idea/checkout/GitCloneDialog.java b/plugins/git4idea/src/git4idea/checkout/GitCloneDialog.java index 4837c04bd311..0141d7ebfb19 100644 --- a/plugins/git4idea/src/git4idea/checkout/GitCloneDialog.java +++ b/plugins/git4idea/src/git4idea/checkout/GitCloneDialog.java @@ -164,7 +164,7 @@ public class GitCloneDialog extends DialogWrapper { */ private boolean test(String url) { final GitLineHandlerPasswordRequestAware handler = new GitLineHandlerPasswordRequestAware(myProject, new File("."), GitCommand.LS_REMOTE); - handler.setRemoteProtocol(GitRemoteProtocol.SSH); + handler.setRemoteProtocol(url); handler.addParameters(url, "master"); GitTask task = new GitTask(myProject, handler, GitBundle.message("clone.testing", url)); GitTaskResult result = task.executeModal(); diff --git a/plugins/git4idea/src/git4idea/commands/Git.java b/plugins/git4idea/src/git4idea/commands/Git.java index 480be2106c2a..1b30a1e4518a 100644 --- a/plugins/git4idea/src/git4idea/commands/Git.java +++ b/plugins/git4idea/src/git4idea/commands/Git.java @@ -92,11 +92,11 @@ public interface Git { GitCommandResult tip(@NotNull GitRepository repository, @NotNull String branchName); @NotNull - GitCommandResult push(@NotNull GitRepository repository, @NotNull String remote, @NotNull String spec, + GitCommandResult push(@NotNull GitRepository repository, @NotNull String remote, @NotNull String url, @NotNull String spec, @NotNull GitLineHandlerListener... listeners); @NotNull - GitCommandResult push(@NotNull GitRepository repository, @NotNull GitPushSpec pushSpec, + GitCommandResult push(@NotNull GitRepository repository, @NotNull GitPushSpec spec, @NotNull String url, @NotNull GitLineHandlerListener... listeners); @NotNull diff --git a/plugins/git4idea/src/git4idea/commands/GitCommandResult.java b/plugins/git4idea/src/git4idea/commands/GitCommandResult.java index c0437aa2ad1f..dad9f2d8aaa1 100644 --- a/plugins/git4idea/src/git4idea/commands/GitCommandResult.java +++ b/plugins/git4idea/src/git4idea/commands/GitCommandResult.java @@ -20,6 +20,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; +import java.util.Collections; import java.util.List; /** @@ -80,4 +81,8 @@ public class GitCommandResult { return myException; } + @NotNull + public static GitCommandResult error(@NotNull String error) { + return new GitCommandResult(false, 1, Collections.singletonList(error), Collections.emptyList(), null); + } } diff --git a/plugins/git4idea/src/git4idea/commands/GitHandler.java b/plugins/git4idea/src/git4idea/commands/GitHandler.java index 9832ba19483c..3ebf840158b0 100644 --- a/plugins/git4idea/src/git4idea/commands/GitHandler.java +++ b/plugins/git4idea/src/git4idea/commands/GitHandler.java @@ -217,8 +217,8 @@ public abstract class GitHandler { return file; } - public void setRemoteProtocol(@NotNull GitRemoteProtocol remoteProtocol) { - myRemoteProtocol = remoteProtocol; + public void setRemoteProtocol(@NotNull String url) { + myRemoteProtocol = GitRemoteProtocol.fromUrl(url); } protected boolean isRemote() { diff --git a/plugins/git4idea/src/git4idea/commands/GitImpl.java b/plugins/git4idea/src/git4idea/commands/GitImpl.java index e4ed79da21e5..ec80b83c3b82 100644 --- a/plugins/git4idea/src/git4idea/commands/GitImpl.java +++ b/plugins/git4idea/src/git4idea/commands/GitImpl.java @@ -132,7 +132,7 @@ public class GitImpl implements Git { public GitCommandResult clone(@NotNull Project project, @NotNull File parentDirectory, @NotNull String url, @NotNull String clonedDirectoryName, @NotNull GitLineHandlerListener... listeners) { GitLineHandlerPasswordRequestAware handler = new GitLineHandlerPasswordRequestAware(project, parentDirectory, GitCommand.CLONE); - handler.setRemoteProtocol(GitRemoteProtocol.SSH); + handler.setRemoteProtocol(url); handler.addParameters("--progress"); handler.addParameters(url); handler.addParameters(clonedDirectoryName); @@ -356,11 +356,11 @@ public class GitImpl implements Git { @Override @NotNull - public GitCommandResult push(@NotNull GitRepository repository, @NotNull String remote, @NotNull String spec, - @NotNull GitLineHandlerListener... listeners) { + public GitCommandResult push(@NotNull GitRepository repository, @NotNull String remote, @NotNull String url, @NotNull String spec, + @NotNull GitLineHandlerListener... listeners) { final GitLineHandlerPasswordRequestAware h = new GitLineHandlerPasswordRequestAware(repository.getProject(), repository.getRoot(), GitCommand.PUSH); - h.setRemoteProtocol(GitRemoteProtocol.SSH); + h.setRemoteProtocol(url); h.setSilent(false); addListeners(h, listeners); h.addProgressParameter(); @@ -371,12 +371,12 @@ public class GitImpl implements Git { @Override @NotNull - public GitCommandResult push(@NotNull GitRepository repository, @NotNull GitPushSpec pushSpec, - @NotNull GitLineHandlerListener... listeners) { + public GitCommandResult push(@NotNull GitRepository repository, @NotNull GitPushSpec pushSpec, @NotNull String url, + @NotNull GitLineHandlerListener... listeners) { GitRemote remote = pushSpec.getRemote(); GitBranch remoteBranch = pushSpec.getDest(); String destination = remoteBranch.getName().replaceFirst(remote.getName() + "/", ""); - return push(repository, remote.getName(), pushSpec.getSource().getName() + ":" + destination, listeners); + return push(repository, remote.getName(), url, pushSpec.getSource().getName() + ":" + destination, listeners); } @NotNull diff --git a/plugins/git4idea/src/git4idea/commands/GitRemoteProtocol.java b/plugins/git4idea/src/git4idea/commands/GitRemoteProtocol.java index 9747b06ffd66..c794ff6c6f96 100644 --- a/plugins/git4idea/src/git4idea/commands/GitRemoteProtocol.java +++ b/plugins/git4idea/src/git4idea/commands/GitRemoteProtocol.java @@ -15,11 +15,26 @@ */ package git4idea.commands; +import org.jetbrains.annotations.NotNull; + /** * @author Kirill Likhodedov */ public enum GitRemoteProtocol { GIT, SSH, - HTTP + HTTP; + + @NotNull + public static GitRemoteProtocol fromUrl(@NotNull String url) { + url = url.toLowerCase(); + if (url.startsWith("http")) { + return HTTP; + } + if (url.startsWith("git://")) { // "://" are there not to mix with scp-like syntax used for SSH: git@host.com/path/to.git + return GIT; + } + return SSH; + } + } diff --git a/plugins/git4idea/src/git4idea/merge/GitPullDialog.java b/plugins/git4idea/src/git4idea/merge/GitPullDialog.java index 90680a44df1b..cd1bfa1aded1 100644 --- a/plugins/git4idea/src/git4idea/merge/GitPullDialog.java +++ b/plugins/git4idea/src/git4idea/merge/GitPullDialog.java @@ -27,7 +27,6 @@ import git4idea.GitRemoteBranch; import git4idea.GitUtil; import git4idea.commands.GitCommand; import git4idea.commands.GitLineHandler; -import git4idea.commands.GitRemoteProtocol; import git4idea.i18n.GitBundle; import git4idea.repo.GitBranchTrackInfo; import git4idea.repo.GitRemote; @@ -154,12 +153,13 @@ public class GitPullDialog extends DialogWrapper { /** * @return a pull handler configured according to dialog options */ - public GitLineHandler pullOrMergeHandler(boolean pull) { + public GitLineHandler pullOrMergeHandler(@Nullable String url) { + boolean pull = url != null; GitLineHandler h = new GitLineHandler(myProject, gitRoot(), pull ? GitCommand.PULL : GitCommand.MERGE); // ignore merge failure for the pull h.ignoreErrorCode(1); if (pull) { - h.setRemoteProtocol(GitRemoteProtocol.SSH); + h.setRemoteProtocol(url); h.addProgressParameter(); } h.addParameters("--no-stat"); diff --git a/plugins/git4idea/src/git4idea/push/GitPusher.java b/plugins/git4idea/src/git4idea/push/GitPusher.java index 251b55f50933..75f31576f6fe 100644 --- a/plugins/git4idea/src/git4idea/push/GitPusher.java +++ b/plugins/git4idea/src/git4idea/push/GitPusher.java @@ -318,21 +318,18 @@ public final class GitPusher { } GitRemote remote = pushSpec.getRemote(); - String httpUrl = null; - for (String pushUrl : remote.getPushUrls()) { - if (GitHttpAdapter.shouldUseJGit(pushUrl)) { - httpUrl = pushUrl; - break; // TODO support http and ssh urls in one origin - } + Collection pushUrls = remote.getPushUrls(); + if (pushUrls.isEmpty()) { + LOG.error("No urls or pushUrls are defined for " + remote); + return GitSimplePushResult.error("There are no URLs defined for remote " + remote.getName()); } - + String url = pushUrls.iterator().next(); GitSimplePushResult pushResult; - boolean pushOverHttp = httpUrl != null; - if (pushOverHttp) { - pushResult = GitHttpAdapter.push(repository, remote.getName(), httpUrl, formPushSpec(pushSpec, remote)); + if (GitHttpAdapter.shouldUseJGit(url)) { + pushResult = GitHttpAdapter.push(repository, remote.getName(), url, formPushSpec(pushSpec, remote)); } else { - pushResult = pushNatively(repository, pushSpec); + pushResult = pushNatively(repository, pushSpec, url); } if (pushResult.getType() == GitSimplePushResult.Type.SUCCESS) { @@ -396,10 +393,10 @@ public final class GitPusher { } @NotNull - private GitSimplePushResult pushNatively(GitRepository repository, GitPushSpec pushSpec) { + private GitSimplePushResult pushNatively(GitRepository repository, GitPushSpec pushSpec, @NotNull String url) { GitPushRejectedDetector rejectedDetector = new GitPushRejectedDetector(); GitLineHandlerListener progressListener = GitStandardProgressAnalyzer.createListener(myProgressIndicator); - GitCommandResult res = myGit.push(repository, pushSpec, rejectedDetector, progressListener); + GitCommandResult res = myGit.push(repository, pushSpec, url, rejectedDetector, progressListener); if (rejectedDetector.rejected()) { Collection rejectedBranches = rejectedDetector.getRejectedBranches(); return GitSimplePushResult.reject(rejectedBranches); diff --git a/plugins/git4idea/src/git4idea/update/GitFetcher.java b/plugins/git4idea/src/git4idea/update/GitFetcher.java index bb9f215c63fe..1fe06f7b812f 100644 --- a/plugins/git4idea/src/git4idea/update/GitFetcher.java +++ b/plugins/git4idea/src/git4idea/update/GitFetcher.java @@ -132,7 +132,7 @@ public class GitFetcher { if (GitHttpAdapter.shouldUseJGit(url)) { return GitHttpAdapter.fetch(repository, remote, url, null); } - return fetchNatively(repository.getRoot(), remote, null); + return fetchNatively(repository.getRoot(), remote, url, null); } // leaving this unused method, because the wanted behavior can change again @@ -150,7 +150,7 @@ public class GitFetcher { if (GitHttpAdapter.shouldUseJGit(url)) { return GitHttpAdapter.fetch(repository, remote, url, remoteBranch); } - return fetchNatively(repository.getRoot(), remote, remoteBranch); + return fetchNatively(repository.getRoot(), remote, url, remoteBranch); } @NotNull @@ -198,7 +198,7 @@ public class GitFetcher { } } else { - GitFetchResult res = fetchNatively(repository.getRoot(), remote, null); + GitFetchResult res = fetchNatively(repository.getRoot(), remote, url, null); res.addPruneInfo(fetchResult.getPrunedRefs()); fetchResult = res; if (!fetchResult.isSuccess()) { @@ -209,9 +209,9 @@ public class GitFetcher { return fetchResult; } - private GitFetchResult fetchNatively(@NotNull VirtualFile root, @NotNull GitRemote remote, @Nullable String branch) { + private GitFetchResult fetchNatively(@NotNull VirtualFile root, @NotNull GitRemote remote, @NotNull String url, @Nullable String branch) { final GitLineHandlerPasswordRequestAware h = new GitLineHandlerPasswordRequestAware(myProject, root, GitCommand.FETCH); - h.setRemoteProtocol(GitRemoteProtocol.SSH); + h.setRemoteProtocol(url); h.addProgressParameter(); if (GitVersionSpecialty.SUPPORTS_FETCH_PRUNE.existsIn(myVcs.getVersion())) { h.addParameters("--prune"); diff --git a/plugins/git4idea/testFramework/git4idea/test/GitTestImpl.java b/plugins/git4idea/testFramework/git4idea/test/GitTestImpl.java index f5bf37b79f13..4c04a4779a03 100644 --- a/plugins/git4idea/testFramework/git4idea/test/GitTestImpl.java +++ b/plugins/git4idea/testFramework/git4idea/test/GitTestImpl.java @@ -190,9 +190,7 @@ public class GitTestImpl implements Git { @NotNull @Override - public GitCommandResult push(@NotNull GitRepository repository, - @NotNull String remote, - @NotNull String spec, + public GitCommandResult push(@NotNull GitRepository repository, @NotNull String remote, @NotNull String url, @NotNull String spec, @NotNull GitLineHandlerListener... listeners) { throw new UnsupportedOperationException(); } @@ -200,8 +198,7 @@ public class GitTestImpl implements Git { @NotNull @Override public GitCommandResult push(@NotNull GitRepository repository, - @NotNull GitPushSpec pushSpec, - @NotNull GitLineHandlerListener... listeners) { + @NotNull GitPushSpec spec, @NotNull String url, @NotNull GitLineHandlerListener... listeners) { throw new UnsupportedOperationException(); }