From 44ea78063d8d66a807066d5f5fc24f55999a6c7b Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Tue, 31 Jul 2018 15:19:07 +0300 Subject: [PATCH] git: introduce GitCommandResult.throwOnError method --- .../remoteServer/util/CloudGitDeploymentRuntime.java | 4 ++-- .../git4idea/src/git4idea/branch/GitBranchUtil.java | 2 +- .../src/git4idea/checkin/GitCheckinEnvironment.java | 10 +++++----- .../src/git4idea/commands/GitCommandResult.java | 12 +++++++++++- .../git4idea/src/git4idea/config/GitConfigUtil.java | 2 +- .../git4idea/src/git4idea/history/GitLogUtil.java | 2 +- .../git4idea/src/git4idea/index/GitIndexUtil.java | 6 +++--- plugins/git4idea/src/git4idea/merge/GitMerger.java | 2 +- plugins/git4idea/src/git4idea/rebase/GitRebaser.java | 2 +- .../src/git4idea/repo/GitRepositoryInitializer.java | 2 +- .../git4idea/rollback/GitRollbackEnvironment.java | 2 +- .../src/git4idea/status/GitOldChangesCollector.java | 2 +- .../git4idea/src/git4idea/ui/GitUnstashDialog.java | 7 +++++-- plugins/git4idea/src/git4idea/util/GitFileUtils.java | 4 ++-- .../src/git4idea/util/GitLocalCommitCompareInfo.java | 2 +- .../github/GithubCreatePullRequestWorker.java | 12 +++++++++--- .../jetbrains/plugins/github/GithubRebaseAction.java | 2 +- .../jetbrains/plugins/github/GithubShareAction.kt | 4 ++-- 18 files changed, 49 insertions(+), 30 deletions(-) diff --git a/plugins/git4idea/remote-servers-git/src/com/intellij/remoteServer/util/CloudGitDeploymentRuntime.java b/plugins/git4idea/remote-servers-git/src/com/intellij/remoteServer/util/CloudGitDeploymentRuntime.java index 74205af8a3c8..49baf6ffd84a 100644 --- a/plugins/git4idea/remote-servers-git/src/com/intellij/remoteServer/util/CloudGitDeploymentRuntime.java +++ b/plugins/git4idea/remote-servers-git/src/com/intellij/remoteServer/util/CloudGitDeploymentRuntime.java @@ -343,7 +343,7 @@ public class CloudGitDeploymentRuntime extends CloudDeploymentRuntime { handler.setSilent(false); handler.addParameters(subCommand, remoteName, application.getGitUrl()); GitCommandResult result = myGit.runCommand(handler); - result.getOutputOrThrow(); + result.throwOnError(); getRepository().update(); if (result.getExitCode() != 0) { throw new ServerRuntimeException(failMessage); @@ -421,7 +421,7 @@ public class CloudGitDeploymentRuntime extends CloudDeploymentRuntime { handler.setStdoutSuppressed(false); handler.addParameters("-m", message); handler.endOptions(); - Git.getInstance().runCommand(handler).getOutputOrThrow(); + Git.getInstance().runCommand(handler).throwOnError(); } } catch (VcsException e) { diff --git a/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java b/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java index 45d6eda02596..4ab512d58b4e 100644 --- a/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java +++ b/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java @@ -150,7 +150,7 @@ public class GitBranchUtil { }); GitCommandResult result = Git.getInstance().runCommandWithoutCollectingOutput(h); - result.getOutputOrThrow(); + result.throwOnError(); return tags; } diff --git a/plugins/git4idea/src/git4idea/checkin/GitCheckinEnvironment.java b/plugins/git4idea/src/git4idea/checkin/GitCheckinEnvironment.java index ab82e742b90f..6e94b3a96c77 100644 --- a/plugins/git4idea/src/git4idea/checkin/GitCheckinEnvironment.java +++ b/plugins/git4idea/src/git4idea/checkin/GitCheckinEnvironment.java @@ -83,8 +83,8 @@ import java.awt.event.*; import java.io.*; import java.nio.charset.Charset; import java.text.SimpleDateFormat; -import java.util.*; import java.util.List; +import java.util.*; import java.util.concurrent.ExecutionException; import static com.intellij.dvcs.DvcsUtil.getShortRepositoryName; @@ -674,7 +674,7 @@ public class GitCheckinEnvironment implements CheckinEnvironment { GitLineHandler handler = new GitLineHandler(project, root, GitCommand.RESET); handler.endOptions(); handler.addParameters(paths); - Git.getInstance().runCommand(handler).getOutputOrThrow(); + Git.getInstance().runCommand(handler).throwOnError(); } } @@ -707,7 +707,7 @@ public class GitCheckinEnvironment implements CheckinEnvironment { LOG.debug(String.format("Restoring staged case-only rename after commit: %s", change)); GitLineHandler h = new GitLineHandler(project, root, GitCommand.MV); h.addParameters("-f", beforePath.getPath(), afterPath.getPath()); - Git.getInstance().runCommandWithoutCollectingOutput(h).getOutputOrThrow(); + Git.getInstance().runCommandWithoutCollectingOutput(h).throwOnError(); return true; } catch (VcsException e) { @@ -850,7 +850,7 @@ public class GitCheckinEnvironment implements CheckinEnvironment { handler.addParameters("--no-verify"); } handler.endOptions(); - Git.getInstance().runCommand(handler).getOutputOrThrow(); + Git.getInstance().runCommand(handler).throwOnError(); } /** @@ -1003,7 +1003,7 @@ public class GitCheckinEnvironment implements CheckinEnvironment { } handler.endOptions(); handler.addParameters(paths); - Git.getInstance().runCommand(handler).getOutputOrThrow(); + Git.getInstance().runCommand(handler).throwOnError(); } } diff --git a/plugins/git4idea/src/git4idea/commands/GitCommandResult.java b/plugins/git4idea/src/git4idea/commands/GitCommandResult.java index c516abcb0354..ea70d7f14847 100644 --- a/plugins/git4idea/src/git4idea/commands/GitCommandResult.java +++ b/plugins/git4idea/src/git4idea/commands/GitCommandResult.java @@ -141,10 +141,20 @@ public class GitCommandResult { */ @NotNull public String getOutputOrThrow(int... ignoredErrorCodes) throws VcsException { - if (!success(ignoredErrorCodes)) throw new VcsException(getErrorOutputAsJoinedString()); + throwOnError(ignoredErrorCodes); return getOutputAsJoinedString(); } + /** + * Check if execution was successful and do nothing or throw exception + * + * @param ignoredErrorCodes list of non-zero exit codes the are considered success exit codes + * @throws VcsException with message from {@link #getErrorOutputAsJoinedString()} + */ + public void throwOnError(int... ignoredErrorCodes) throws VcsException { + if (!success(ignoredErrorCodes)) throw new VcsException(getErrorOutputAsJoinedString()); + } + /** * @return null * @deprecated use {@link #getErrorOutput()} diff --git a/plugins/git4idea/src/git4idea/config/GitConfigUtil.java b/plugins/git4idea/src/git4idea/config/GitConfigUtil.java index 0b8a89017a89..f83e15c594d4 100644 --- a/plugins/git4idea/src/git4idea/config/GitConfigUtil.java +++ b/plugins/git4idea/src/git4idea/config/GitConfigUtil.java @@ -180,6 +180,6 @@ public class GitConfigUtil { h.setSilent(true); h.addParameters(additionalParameters); h.addParameters(key, value); - Git.getInstance().runCommand(h).getOutputOrThrow(1); + Git.getInstance().runCommand(h).throwOnError(1); } } diff --git a/plugins/git4idea/src/git4idea/history/GitLogUtil.java b/plugins/git4idea/src/git4idea/history/GitLogUtil.java index d34cfd714cdb..8c60658db7e4 100644 --- a/plugins/git4idea/src/git4idea/history/GitLogUtil.java +++ b/plugins/git4idea/src/git4idea/history/GitLogUtil.java @@ -291,7 +291,7 @@ public class GitLogUtil { StopWatch sw = StopWatch.start("loading details in [" + root.getName() + "]"); GitLogOutputSplitter handlerListener = new GitLogOutputSplitter(handler, parser, converter); - Git.getInstance().runCommandWithoutCollectingOutput(handler).getOutputOrThrow(); + Git.getInstance().runCommandWithoutCollectingOutput(handler).throwOnError(); handlerListener.reportErrors(); sw.report(); diff --git a/plugins/git4idea/src/git4idea/index/GitIndexUtil.java b/plugins/git4idea/src/git4idea/index/GitIndexUtil.java index d400f626053b..1eaaa364a1b5 100644 --- a/plugins/git4idea/src/git4idea/index/GitIndexUtil.java +++ b/plugins/git4idea/src/git4idea/index/GitIndexUtil.java @@ -75,7 +75,7 @@ public class GitIndexUtil { ContainerUtil.addIfNotNull(result, parseListFilesStagedRecord(root, line)); } }); - Git.getInstance().runCommandWithoutCollectingOutput(h).getOutputOrThrow(); + Git.getInstance().runCommandWithoutCollectingOutput(h).throwOnError(); return result; } @@ -108,7 +108,7 @@ public class GitIndexUtil { ContainerUtil.addIfNotNull(result, parseListTreeRecord(root, line)); } }); - Git.getInstance().runCommandWithoutCollectingOutput(h).getOutputOrThrow(); + Git.getInstance().runCommandWithoutCollectingOutput(h).throwOnError(); return result; } @@ -205,7 +205,7 @@ public class GitIndexUtil { h.addParameters("--cacheinfo", mode, blobHash.asString(), path); } h.endOptions(); - Git.getInstance().runCommandWithoutCollectingOutput(h).getOutputOrThrow(); + Git.getInstance().runCommandWithoutCollectingOutput(h).throwOnError(); } @NotNull diff --git a/plugins/git4idea/src/git4idea/merge/GitMerger.java b/plugins/git4idea/src/git4idea/merge/GitMerger.java index 11acc1bfccbf..1a8e2d757795 100644 --- a/plugins/git4idea/src/git4idea/merge/GitMerger.java +++ b/plugins/git4idea/src/git4idea/merge/GitMerger.java @@ -70,7 +70,7 @@ public class GitMerger { handler.addParameters("-F", messageFile.getAbsolutePath()); } handler.endOptions(); - Git.getInstance().runCommand(handler).getOutputOrThrow(); + Git.getInstance().runCommand(handler).throwOnError(); } } diff --git a/plugins/git4idea/src/git4idea/rebase/GitRebaser.java b/plugins/git4idea/src/git4idea/rebase/GitRebaser.java index 280a97521567..fa5dc3ab5dad 100644 --- a/plugins/git4idea/src/git4idea/rebase/GitRebaser.java +++ b/plugins/git4idea/src/git4idea/rebase/GitRebaser.java @@ -275,7 +275,7 @@ public class GitRebaser { GitLineHandler handler = new GitLineHandler(myProject, root, GitCommand.ADD); handler.setSilent(false); handler.addParameters("--update"); - myGit.runCommand(handler).getOutputOrThrow(); + myGit.runCommand(handler).throwOnError(); } private GitConflictResolver.Params makeParamsForRebaseConflict() { diff --git a/plugins/git4idea/src/git4idea/repo/GitRepositoryInitializer.java b/plugins/git4idea/src/git4idea/repo/GitRepositoryInitializer.java index f3f96d4622ec..94e2c732faff 100644 --- a/plugins/git4idea/src/git4idea/repo/GitRepositoryInitializer.java +++ b/plugins/git4idea/src/git4idea/repo/GitRepositoryInitializer.java @@ -19,7 +19,7 @@ public class GitRepositoryInitializer implements VcsRepositoryInitializer { public void initRepository(@NotNull File rootDir) throws VcsException { // TODO remove the fake project instance when GitHandler knows how to run without project GitLineHandler handler = new GitLineHandler(ProjectManager.getInstance().getDefaultProject(), rootDir, GitCommand.INIT); - Git.getInstance().runCommand(handler).getOutputOrThrow(); + Git.getInstance().runCommand(handler).throwOnError(); } @NotNull diff --git a/plugins/git4idea/src/git4idea/rollback/GitRollbackEnvironment.java b/plugins/git4idea/src/git4idea/rollback/GitRollbackEnvironment.java index 7f32fa35059f..58152f3b6c1e 100644 --- a/plugins/git4idea/src/git4idea/rollback/GitRollbackEnvironment.java +++ b/plugins/git4idea/src/git4idea/rollback/GitRollbackEnvironment.java @@ -185,7 +185,7 @@ public class GitRollbackEnvironment implements RollbackEnvironment { handler.addParameters("HEAD"); handler.endOptions(); handler.addParameters(paths); - Git.getInstance().runCommand(handler).getOutputOrThrow(); + Git.getInstance().runCommand(handler).throwOnError(); } } diff --git a/plugins/git4idea/src/git4idea/status/GitOldChangesCollector.java b/plugins/git4idea/src/git4idea/status/GitOldChangesCollector.java index 7ad891c88ee2..4a5b8768b21d 100644 --- a/plugins/git4idea/src/git4idea/status/GitOldChangesCollector.java +++ b/plugins/git4idea/src/git4idea/status/GitOldChangesCollector.java @@ -136,7 +136,7 @@ class GitOldChangesCollector extends GitChangesCollector { handler.addParameters("--refresh", "--ignore-missing"); handler.setSilent(true); handler.setStdoutSuppressed(true); - Git.getInstance().runCommand(handler).getOutputOrThrow(1); + Git.getInstance().runCommand(handler).throwOnError(1); } /** diff --git a/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java b/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java index b374c77bd0ec..b25820c47c42 100644 --- a/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java +++ b/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java @@ -22,7 +22,10 @@ import com.intellij.ui.DocumentAdapter; import git4idea.GitRevisionNumber; import git4idea.GitUtil; import git4idea.branch.GitBranchUtil; -import git4idea.commands.*; +import git4idea.commands.Git; +import git4idea.commands.GitCommand; +import git4idea.commands.GitCommandResult; +import git4idea.commands.GitLineHandler; import git4idea.i18n.GitBundle; import git4idea.merge.GitConflictResolver; import git4idea.repo.GitRepository; @@ -134,7 +137,7 @@ public class GitUnstashDialog extends DialogWrapper { public void run(@NotNull ProgressIndicator indicator) { final GitLineHandler h = dropHandler(stash.getStash()); try { - Git.getInstance().runCommand(h).getOutputOrThrow(); + Git.getInstance().runCommand(h).throwOnError(); } catch (final VcsException ex) { ApplicationManager.getApplication().invokeLater(() -> GitUIUtil.showOperationError(myProject, ex, h.printableCommandLine()), current); diff --git a/plugins/git4idea/src/git4idea/util/GitFileUtils.java b/plugins/git4idea/src/git4idea/util/GitFileUtils.java index 850f44b8d49b..0089055f21e4 100644 --- a/plugins/git4idea/src/git4idea/util/GitFileUtils.java +++ b/plugins/git4idea/src/git4idea/util/GitFileUtils.java @@ -67,7 +67,7 @@ public class GitFileUtils { handler.addParameters(additionalOptions); handler.endOptions(); handler.addParameters(paths); - Git.getInstance().runCommand(handler).getOutputOrThrow(); + Git.getInstance().runCommand(handler).throwOnError(); } public static void deleteFilesFromCache(@NotNull Project project, @NotNull VirtualFile root, @NotNull Collection files) @@ -136,7 +136,7 @@ public class GitFileUtils { handler.addParameters("--ignore-errors", "-A"); handler.endOptions(); handler.addParameters(paths); - Git.getInstance().runCommand(handler).getOutputOrThrow(); + Git.getInstance().runCommand(handler).throwOnError(); } } diff --git a/plugins/git4idea/src/git4idea/util/GitLocalCommitCompareInfo.java b/plugins/git4idea/src/git4idea/util/GitLocalCommitCompareInfo.java index d74705ae96f3..2ad9b4f79cd5 100644 --- a/plugins/git4idea/src/git4idea/util/GitLocalCommitCompareInfo.java +++ b/plugins/git4idea/src/git4idea/util/GitLocalCommitCompareInfo.java @@ -97,7 +97,7 @@ public class GitLocalCommitCompareInfo extends LocalCommitCompareInfo { handler.endOptions(); handler.addParameters(paths); GitCommandResult result = Git.getInstance().runCommand(handler); - result.getOutputOrThrow(); + result.throwOnError(); } GitFileUtils.addPaths(myProject, root, rootPaths); diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubCreatePullRequestWorker.java b/plugins/github/src/org/jetbrains/plugins/github/GithubCreatePullRequestWorker.java index b677081c11ac..85052d33be86 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubCreatePullRequestWorker.java +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubCreatePullRequestWorker.java @@ -45,8 +45,14 @@ import git4idea.update.GitFetchResult; import git4idea.update.GitFetcher; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.plugins.github.api.*; -import org.jetbrains.plugins.github.api.data.*; +import org.jetbrains.plugins.github.api.GithubApiRequestExecutor; +import org.jetbrains.plugins.github.api.GithubApiRequests; +import org.jetbrains.plugins.github.api.GithubFullPath; +import org.jetbrains.plugins.github.api.GithubServerPath; +import org.jetbrains.plugins.github.api.data.GithubBranch; +import org.jetbrains.plugins.github.api.data.GithubPullRequest; +import org.jetbrains.plugins.github.api.data.GithubRepo; +import org.jetbrains.plugins.github.api.data.GithubRepoDetailed; import org.jetbrains.plugins.github.api.util.GithubApiPagesLoader; import org.jetbrains.plugins.github.exceptions.GithubConfusingException; import org.jetbrains.plugins.github.exceptions.GithubOperationCanceledException; @@ -384,7 +390,7 @@ public class GithubCreatePullRequestWorker { String url = myGitHelper.getRemoteUrl(myServer, path); try { - myGit.addRemote(myGitRepository, path.getUser(), url).getOutputOrThrow(); + myGit.addRemote(myGitRepository, path.getUser(), url).throwOnError(); myGitRepository.update(); fork.setRemoteName(path.getUser()); } diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubRebaseAction.java b/plugins/github/src/org/jetbrains/plugins/github/GithubRebaseAction.java index bdafb5001718..248be94f4556 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubRebaseAction.java +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubRebaseAction.java @@ -178,7 +178,7 @@ public class GithubRebaseAction extends LegacySingleAccountActionGroup { LOG.info("Adding GitHub parent as a remote host"); indicator.setText("Adding GitHub parent as a remote host..."); try { - myGit.addRemote(myRepository, "upstream", parentRepoUrl).getOutputOrThrow(); + myGit.addRemote(myRepository, "upstream", parentRepoUrl).throwOnError(); } catch (VcsException e) { GithubNotifications diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubShareAction.kt b/plugins/github/src/org/jetbrains/plugins/github/GithubShareAction.kt index 4efc298a432d..1e51fc03b61e 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubShareAction.kt +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubShareAction.kt @@ -208,7 +208,7 @@ class GithubShareAction : DumbAwareAction("Share Project on GitHub", "Easily sha //git remote add origin git@github.com:login/name.git LOG.info("Adding GitHub as a remote host") indicator.text = "Adding GitHub as a remote host..." - git.addRemote(repository, remoteName, remoteUrl).getOutputOrThrow() + git.addRemote(repository, remoteName, remoteUrl).throwOnError() repository.update() // create sample commit for binding project @@ -293,7 +293,7 @@ class GithubShareAction : DumbAwareAction("Share Project on GitHub", "Easily sha handler.setStdoutSuppressed(false) handler.addParameters("-m", dialog.commitMessage) handler.endOptions() - Git.getInstance().runCommand(handler).getOutputOrThrow() + Git.getInstance().runCommand(handler).throwOnError() VcsFileUtil.markFilesDirty(project, modified) }