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

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

Introduction

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

Prototype

CherryPickResult CONFLICT

To view the source code for org.eclipse.jgit.api CherryPickResult CONFLICT.

Click Source Link

Document

A CherryPickResult with status CherryPickStatus#CONFLICTING

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/*ww  w .j a va  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);
}

From source file:org.jboss.forge.addon.git.GitUtilsImpl.java

License:Open Source License

@Override
public CherryPickResult cherryPickNoMerge(final Git git, Ref src)
        throws GitAPIException, CantMergeCommitException {
    // Does the same as the original git-cherryPick
    // except commiting after running merger
    Repository repo = git.getRepository();

    RevCommit newHead = null;/*from   w  w w  .  j a  va  2s. c  om*/
    List<Ref> cherryPickedRefs = new LinkedList<Ref>();

    try (RevWalk revWalk = new RevWalk(repo)) {
        // get the head commit
        Ref headRef = repo.findRef(Constants.HEAD);
        if (headRef == null)
            throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
        RevCommit headCommit = revWalk.parseCommit(headRef.getObjectId());

        newHead = headCommit;

        // 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);

        // get the parent of the commit to cherry-pick
        if (srcCommit.getParentCount() == 0)
            throw new CantMergeCommitException("Commit with zero parents cannot be merged");

        if (srcCommit.getParentCount() > 1)
            throw new MultipleParentsNotAllowedException(
                    MessageFormat.format(JGitText.get().canOnlyCherryPickCommitsWithOneParent, srcCommit.name(),
                            Integer.valueOf(srcCommit.getParentCount())));

        RevCommit srcParent = srcCommit.getParent(0);
        revWalk.parseHeaders(srcParent);

        ResolveMerger merger = (ResolveMerger) MergeStrategy.RESOLVE.newMerger(repo);
        merger.setWorkingTreeIterator(new FileTreeIterator(repo));
        merger.setBase(srcParent.getTree());
        if (merger.merge(headCommit, srcCommit)) {
            DirCacheCheckout dco = new DirCacheCheckout(repo, headCommit.getTree(), repo.lockDirCache(),
                    merger.getResultTreeId());
            dco.setFailOnConflict(true);
            dco.checkout();

            cherryPickedRefs.add(src);
        } else {
            if (merger.failed())
                return new CherryPickResult(merger.getFailingPaths());

            // there are merge conflicts
            String message = new MergeMessageFormatter().formatWithConflicts(srcCommit.getFullMessage(),
                    merger.getUnmergedPaths());

            repo.writeCherryPickHead(srcCommit.getId());
            repo.writeMergeCommitMsg(message);

            return CherryPickResult.CONFLICT;
        }
    } catch (IOException e) {
        throw new JGitInternalException(
                MessageFormat.format(JGitText.get().exceptionCaughtDuringExecutionOfCherryPickCommand, e), e);
    }
    return new CherryPickResult(newHead, cherryPickedRefs);
}

From source file:org.jboss.forge.git.GitUtils.java

License:Open Source License

public static CherryPickResult cherryPickNoMerge(final Git git, Ref src)
        throws GitAPIException, CantMergeCommitWithZeroParentsException {
    // Does the same as the original git-cherryPick
    // except commiting after running merger
    Repository repo = git.getRepository();

    RevCommit newHead = null;/*from   www  .ja v a 2  s .  co  m*/
    List<Ref> cherryPickedRefs = new LinkedList<Ref>();

    RevWalk revWalk = new RevWalk(repo);
    try {
        // get the head commit
        Ref headRef = repo.getRef(Constants.HEAD);
        if (headRef == null)
            throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
        RevCommit headCommit = revWalk.parseCommit(headRef.getObjectId());

        newHead = headCommit;

        // 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);

        // get the parent of the commit to cherry-pick
        if (srcCommit.getParentCount() == 0)
            throw new CantMergeCommitWithZeroParentsException("Commit with zero parents cannot be merged");

        if (srcCommit.getParentCount() > 1)
            throw new MultipleParentsNotAllowedException(
                    MessageFormat.format(JGitText.get().canOnlyCherryPickCommitsWithOneParent, srcCommit.name(),
                            Integer.valueOf(srcCommit.getParentCount())));

        RevCommit srcParent = srcCommit.getParent(0);
        revWalk.parseHeaders(srcParent);

        ResolveMerger merger = (ResolveMerger) MergeStrategy.RESOLVE.newMerger(repo);
        merger.setWorkingTreeIterator(new FileTreeIterator(repo));
        merger.setBase(srcParent.getTree());
        if (merger.merge(headCommit, srcCommit)) {
            DirCacheCheckout dco = new DirCacheCheckout(repo, headCommit.getTree(), repo.lockDirCache(),
                    merger.getResultTreeId());
            dco.setFailOnConflict(true);
            dco.checkout();

            cherryPickedRefs.add(src);
        } else {
            if (merger.failed())
                return new CherryPickResult(merger.getFailingPaths());

            // there are merge conflicts
            String message = new MergeMessageFormatter().formatWithConflicts(srcCommit.getFullMessage(),
                    merger.getUnmergedPaths());

            repo.writeCherryPickHead(srcCommit.getId());
            repo.writeMergeCommitMsg(message);

            return CherryPickResult.CONFLICT;
        }
    } catch (IOException e) {
        throw new JGitInternalException(
                MessageFormat.format(JGitText.get().exceptionCaughtDuringExecutionOfCherryPickCommand, e), e);
    } finally {
        revWalk.release();
    }

    return new CherryPickResult(newHead, cherryPickedRefs);
}