Example usage for org.eclipse.jgit.api CherryPickResult CherryPickResult

List of usage examples for org.eclipse.jgit.api CherryPickResult CherryPickResult

Introduction

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

Prototype

public CherryPickResult(RevCommit newHead, List<Ref> cherryPickedRefs) 

Source Link

Document

Constructor for CherryPickResult

Usage

From source file:org.craftercms.studio.impl.v1.util.git.CherryPickCommandEx.java

License:Eclipse Distribution License

/**
 * Executes the {@code Cherry-Pick} command with all the options and
 * parameters collected by the setter methods (e.g. {@link #include(Ref)} of
 * this class. Each instance of this class should only be used for one
 * invocation of the command. Don't call this method twice on an instance.
 *
 * @return the result of the cherry-pick
 * @throws GitAPIException//from  w  ww  .  j av  a  2  s  . com
 * @throws WrongRepositoryStateException
 * @throws ConcurrentRefUpdateException
 * @throws UnmergedPathsException
 * @throws NoMessageException
 * @throws NoHeadException
 */
@Override
public CherryPickResult call() throws GitAPIException {
    RevCommit newHead = null;
    List<Ref> cherryPickedRefs = new LinkedList<>();
    checkCallable();

    try (RevWalk revWalk = new RevWalk(repo)) {

        // get the head commit
        Ref headRef = repo.exactRef(Constants.HEAD);
        if (headRef == null)
            throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);

        newHead = revWalk.parseCommit(headRef.getObjectId());

        // loop through all refs to be cherry-picked
        for (Ref src : commits) {
            // get the commit to be cherry-picked
            // handle annotated tags
            ObjectId srcObjectId = src.getPeeledObjectId();
            if (srcObjectId == null)
                srcObjectId = src.getObjectId();
            RevCommit srcCommit = revWalk.parseCommit(srcObjectId);

            Merger merger = strategy.newMerger(repo);
            if (merger.merge(newHead, srcCommit)) {
                if (AnyObjectId.equals(newHead.getTree().getId(), merger.getResultTreeId()))
                    continue;
                DirCacheCheckout dco = new DirCacheCheckout(repo, newHead.getTree(), repo.lockDirCache(),
                        merger.getResultTreeId());
                dco.setFailOnConflict(true);
                dco.checkout();
                if (!noCommit)
                    newHead = new Git(getRepository()).commit().setMessage(srcCommit.getFullMessage())
                            .setReflogComment(reflogPrefix + " " //$NON-NLS-1$
                                    + srcCommit.getShortMessage())
                            .setAuthor(srcCommit.getAuthorIdent()).setNoVerify(true).call();
                cherryPickedRefs.add(src);
            } else {
                return CherryPickResult.CONFLICT;
            }
        }
    } catch (IOException e) {
        throw new JGitInternalException(
                MessageFormat.format(JGitText.get().exceptionCaughtDuringExecutionOfCherryPickCommand, e), e);
    }
    return new CherryPickResult(newHead, cherryPickedRefs);
}