Search in sources :

Example 16 with CommitCommand

use of org.eclipse.jgit.api.CommitCommand in project fabric8 by fabric8io.

the class GitUtils method doCommitAndPush.

public static RevCommit doCommitAndPush(Git git, String message, UserDetails userDetails, PersonIdent author, String branch, String origin, boolean pushOnCommit, int pushRetries) throws GitAPIException {
    CommitCommand commit = git.commit().setAll(true).setMessage(message);
    if (author != null) {
        commit = commit.setAuthor(author);
    }
    RevCommit answer = commit.call();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Committed " + answer.getId() + " " + answer.getFullMessage());
    }
    if (pushOnCommit) {
        GitAPIException exception = null;
        for (int i = 1; i <= pushRetries; i++) {
            try {
                if (i > 1) {
                    try {
                        Thread.sleep(700);
                    } catch (InterruptedException e) {
                    // 
                    }
                    LOG.info("Retrying git push attempt " + i);
                }
                PushCommand push = git.push();
                configureCommand(push, userDetails);
                Iterable<PushResult> results = push.setRemote(origin).call();
                for (PushResult result : results) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Pushed " + result.getMessages() + " " + result.getURI() + " branch: " + branch + " updates: " + toString(result.getRemoteUpdates()));
                    }
                }
                return answer;
            } catch (GitAPIException e) {
                if (exception == null) {
                    exception = e;
                }
                LOG.error("Failed to git push attempt " + i + " with " + e, e);
            }
        }
        if (exception != null) {
            throw exception;
        }
    }
    return answer;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) CommitCommand(org.eclipse.jgit.api.CommitCommand) PushResult(org.eclipse.jgit.transport.PushResult) PushCommand(org.eclipse.jgit.api.PushCommand) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

CommitCommand (org.eclipse.jgit.api.CommitCommand)16 Git (org.eclipse.jgit.api.Git)10 AddCommand (org.eclipse.jgit.api.AddCommand)7 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)7 File (java.io.File)4 PushCommand (org.eclipse.jgit.api.PushCommand)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 PushResult (org.eclipse.jgit.transport.PushResult)3 IOException (java.io.IOException)2 JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)2 PersonIdent (org.eclipse.jgit.lib.PersonIdent)2 Repository (org.eclipse.jgit.lib.Repository)2 RefSpec (org.eclipse.jgit.transport.RefSpec)2 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)2 Context (android.content.Context)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Files (com.google.common.io.Files)1 JSch (com.jcraft.jsch.JSch)1