Github: remove duplication

This commit is contained in:
Aleksey Pivovarov
2013-09-10 17:06:26 +04:00
parent 1d09e47880
commit 2c384d04e2
4 changed files with 40 additions and 66 deletions
@@ -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;
}
}
@@ -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) {
@@ -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,
@@ -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;
}
}
}