git: introduce GitCommandResult.throwOnError method

This commit is contained in:
Aleksey Pivovarov
2018-07-31 15:24:46 +03:00
parent 5bb6b660aa
commit 44ea78063d
18 changed files with 49 additions and 30 deletions
@@ -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) {
@@ -150,7 +150,7 @@ public class GitBranchUtil {
});
GitCommandResult result = Git.getInstance().runCommandWithoutCollectingOutput(h);
result.getOutputOrThrow();
result.throwOnError();
return tags;
}
@@ -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();
}
}
@@ -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()}
@@ -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);
}
}
@@ -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();
@@ -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
@@ -70,7 +70,7 @@ public class GitMerger {
handler.addParameters("-F", messageFile.getAbsolutePath());
}
handler.endOptions();
Git.getInstance().runCommand(handler).getOutputOrThrow();
Git.getInstance().runCommand(handler).throwOnError();
}
}
@@ -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() {
@@ -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
@@ -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();
}
}
@@ -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);
}
/**
@@ -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);
@@ -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<VirtualFile> 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();
}
}
@@ -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);
@@ -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());
}
@@ -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
@@ -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)
}