Example usage for org.eclipse.jgit.api PushCommand setDryRun

List of usage examples for org.eclipse.jgit.api PushCommand setDryRun

Introduction

In this page you can find the example usage for org.eclipse.jgit.api PushCommand setDryRun.

Prototype

public PushCommand setDryRun(boolean dryRun) 

Source Link

Document

Sets whether the push operation should be a dry run

Usage

From source file:com.surevine.gateway.scm.git.jgit.JGitGitFacade.java

License:Open Source License

@Override
public void push(final LocalRepoBean repoBean, final String remoteName) throws GitException {
    try {//  w ww .j a va 2  s  . co m
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        Repository repository = builder.setGitDir(repoBean.getRepoDirectory().toFile()).findGitDir().build();
        Git git = new org.eclipse.jgit.api.Git(repository);

        // dry run the push - if the push cannot be done automatically we have no way to recover so we just log
        // and throw an exception
        PushCommand dryRunPushCommand = git.push();
        dryRunPushCommand.setRemote(remoteName);
        dryRunPushCommand.setDryRun(true);
        Iterable<PushResult> dryRunResult = dryRunPushCommand.call();
        for (PushResult result : dryRunResult) {
            for (RemoteRefUpdate remoteRefUpdate : result.getRemoteUpdates()) {
                switch (remoteRefUpdate.getStatus()) {
                case OK:
                case UP_TO_DATE:
                    continue;
                default:
                    throw new GitException("During a dry run of a push one of the updates would have failed "
                            + "so the push was aborted for repo " + repoBean + " to remote "
                            + dryRunPushCommand.getRemote());
                }
            }
        }

        // if we get to this point the dry run was OK so try the real thing
        PushCommand realPushCommand = git.push();
        realPushCommand.setRemote(remoteName);
        Iterable<PushResult> pushResults = realPushCommand.call();
        for (PushResult result : pushResults) {
            for (RemoteRefUpdate remoteRefUpdate : result.getRemoteUpdates()) {
                switch (remoteRefUpdate.getStatus()) {
                case OK:
                case UP_TO_DATE:
                    continue;
                default:
                    throw new GitException(
                            "Push failed for " + repoBean + " to remote " + dryRunPushCommand.getRemote());
                }
            }
        }
    } catch (GitAPIException | IOException e) {
        LOGGER.error(e);
        throw new GitException(e);
    }
}