diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubCreatePullRequestAction.java b/plugins/github/src/org/jetbrains/plugins/github/GithubCreatePullRequestAction.java index 4147d9cc8beb..485fc2de572d 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubCreatePullRequestAction.java +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubCreatePullRequestAction.java @@ -494,22 +494,10 @@ public class GithubCreatePullRequestAction extends DumbAwareAction { @NotNull GithubFullPath forkPath) { String url = GithubUrlUtil.getCloneUrl(forkPath); - final GitSimpleHandler handler = new GitSimpleHandler(project, gitRepository.getRoot(), GitCommand.REMOTE); - handler.setSilent(true); - - try { - handler.addParameters("add", user, url); - handler.run(); - if (handler.getExitCode() != 0) { - GithubNotifications.showError(project, "Can't add remote", "Failed to add GitHub remote: '" + url + "'. " + handler.getStderr()); - return null; - } - // catch newly added remote - gitRepository.update(); + if (GithubUtil.addGithubRemote(project, gitRepository, user, url)) { return new TargetBranchInfo(user, branch); } - catch (VcsException e) { - GithubNotifications.showError(project, "Can't add remote", e); + else { return null; } } diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubRebaseAction.java b/plugins/github/src/org/jetbrains/plugins/github/GithubRebaseAction.java index 2bdd8c2cbb60..01f717fb8561 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubRebaseAction.java +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubRebaseAction.java @@ -175,7 +175,12 @@ public class GithubRebaseAction extends DumbAwareAction { LOG.info("Adding GitHub parent as a remote host"); indicator.setText("Adding GitHub parent as a remote host..."); - return addParentAsUpstreamRemote(project, root, parentRepoUrl, gitRepository); + + if (GithubUtil.addGithubRemote(project, gitRepository, "upstream", parentRepoUrl)) { + return parentRepoUrl; + } else { + return null; + } } @Nullable @@ -211,33 +216,6 @@ public class GithubRebaseAction extends DumbAwareAction { } } - @Nullable - private static String addParentAsUpstreamRemote(@NotNull Project project, - @NotNull VirtualFile root, - @NotNull String parentRepoUrl, - @NotNull GitRepository gitRepository) { - final GitSimpleHandler handler = new GitSimpleHandler(project, root, GitCommand.REMOTE); - handler.setSilent(true); - - try { - handler.addParameters("add", "upstream", parentRepoUrl); - handler.run(); - if (handler.getExitCode() != 0) { - GithubNotifications - .showError(project, CANNOT_PERFORM_GITHUB_REBASE, "Failed to add GitHub remote: '" + parentRepoUrl + "'. " + handler.getStderr()); - return null; - } - // catch newly added remote - gitRepository.update(); - - return parentRepoUrl; - } - catch (VcsException e) { - GithubNotifications.showError(project, CANNOT_PERFORM_GITHUB_REBASE, e); - return null; - } - } - private static boolean fetchParent(@NotNull final Project project, @NotNull final GitRepository repository, @NotNull final ProgressIndicator indicator) { diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubShareAction.java b/plugins/github/src/org/jetbrains/plugins/github/GithubShareAction.java index 5aa1934dc209..7dc69533d846 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubShareAction.java +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubShareAction.java @@ -167,6 +167,10 @@ public class GithubShareAction extends DumbAwareAction { GitRepositoryManager repositoryManager = GitUtil.getRepositoryManager(project); final GitRepository repository = repositoryManager.getRepositoryForRoot(root); LOG.assertTrue(repository != null, "GitRepository is null for root " + root); + if (repository == null) { + GithubNotifications.showError(project, "Failed to create GitHub Repository", "Can't find Git repository"); + return; + } final String remoteUrl = GithubUrlUtil.getGitHost() + "/" + githubInfo.getUser().getLogin() + "/" + name + ".git"; final String remoteName = finalExternalRemoteDetected ? "github" : "origin"; @@ -174,7 +178,7 @@ public class GithubShareAction extends DumbAwareAction { //git remote add origin git@github.com:login/name.git LOG.info("Adding GitHub as a remote host"); indicator.setText("Adding GitHub as a remote host..."); - if (!addGithubRemote(project, root, remoteName, remoteUrl, repository)) { + if (!GithubUtil.addGithubRemote(project, repository, remoteName, remoteUrl)) { return; } @@ -262,29 +266,6 @@ public class GithubShareAction extends DumbAwareAction { return true; } - private static boolean addGithubRemote(@NotNull Project project, - @NotNull VirtualFile root, - @NotNull String remoteName, - @NotNull String remoteUrl, - @NotNull GitRepository repository) { - final GitSimpleHandler addRemoteHandler = new GitSimpleHandler(project, root, GitCommand.REMOTE); - addRemoteHandler.setSilent(true); - addRemoteHandler.addParameters("add", remoteName, remoteUrl); - try { - addRemoteHandler.run(); - repository.update(); - if (addRemoteHandler.getExitCode() != 0) { - GithubNotifications.showError(project, "Failed to add GitHub repository as remote", "Failed to add GitHub repository as remote"); - return false; - } - } - catch (VcsException e) { - GithubNotifications.showError(project, "Failed to add GitHub repository as remote", e); - return false; - } - return true; - } - private static boolean performFirstCommitIfRequired(@NotNull final Project project, @NotNull VirtualFile root, @NotNull GitRepository repository, diff --git a/plugins/github/src/org/jetbrains/plugins/github/util/GithubUtil.java b/plugins/github/src/org/jetbrains/plugins/github/util/GithubUtil.java index c6f13675e562..cd7bcb5e6f7d 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/util/GithubUtil.java +++ b/plugins/github/src/org/jetbrains/plugins/github/util/GithubUtil.java @@ -25,12 +25,15 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.ThrowableConsumer; import com.intellij.util.ThrowableConvertor; import com.intellij.util.ThrowableRunnable; import com.intellij.util.containers.Convertor; import git4idea.GitUtil; +import git4idea.commands.GitCommand; +import git4idea.commands.GitSimpleHandler; import git4idea.config.GitVcsApplicationSettings; import git4idea.config.GitVersion; import git4idea.i18n.GitBundle; @@ -379,4 +382,28 @@ public class GithubUtil { } return manager.getRepositoryForFile(project.getBaseDir()); } + + public static boolean addGithubRemote(@NotNull Project project, + @NotNull GitRepository repository, + @NotNull String remote, + @NotNull String url) { + final GitSimpleHandler handler = new GitSimpleHandler(project, repository.getRoot(), GitCommand.REMOTE); + handler.setSilent(true); + + try { + handler.addParameters("add", remote, url); + handler.run(); + if (handler.getExitCode() != 0) { + GithubNotifications.showError(project, "Can't add remote", "Failed to add GitHub remote: '" + url + "'. " + handler.getStderr()); + return false; + } + // catch newly added remote + repository.update(); + return true; + } + catch (VcsException e) { + GithubNotifications.showError(project, "Can't add remote", e); + return false; + } + } }