Search in sources :

Example 1 with ThreeWayMerger

use of org.eclipse.jgit.merge.ThreeWayMerger in project gerrit by GerritCodeReview.

the class MergeUtil method createCherryPickFromCommit.

public CodeReviewCommit createCherryPickFromCommit(ObjectInserter inserter, Config repoConfig, RevCommit mergeTip, RevCommit originalCommit, PersonIdent cherryPickCommitterIdent, String commitMsg, CodeReviewRevWalk rw, int parentIndex, boolean ignoreIdenticalTree) throws MissingObjectException, IncorrectObjectTypeException, IOException, MergeIdenticalTreeException, MergeConflictException {
    final ThreeWayMerger m = newThreeWayMerger(inserter, repoConfig);
    m.setBase(originalCommit.getParent(parentIndex));
    if (m.merge(mergeTip, originalCommit)) {
        ObjectId tree = m.getResultTreeId();
        if (tree.equals(mergeTip.getTree()) && !ignoreIdenticalTree) {
            throw new MergeIdenticalTreeException("identical tree");
        }
        CommitBuilder mergeCommit = new CommitBuilder();
        mergeCommit.setTreeId(tree);
        mergeCommit.setParentId(mergeTip);
        mergeCommit.setAuthor(originalCommit.getAuthorIdent());
        mergeCommit.setCommitter(cherryPickCommitterIdent);
        mergeCommit.setMessage(commitMsg);
        return rw.parseCommit(inserter.insert(mergeCommit));
    }
    throw new MergeConflictException("merge conflict");
}
Also used : MergeConflictException(com.google.gerrit.extensions.restapi.MergeConflictException) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) ThreeWayMerger(org.eclipse.jgit.merge.ThreeWayMerger)

Example 2 with ThreeWayMerger

use of org.eclipse.jgit.merge.ThreeWayMerger in project gerrit by GerritCodeReview.

the class MergeUtil method canCherryPick.

public boolean canCherryPick(MergeSorter mergeSorter, Repository repo, CodeReviewCommit mergeTip, CodeReviewRevWalk rw, CodeReviewCommit toMerge) throws IntegrationException {
    if (mergeTip == null) {
        //
        return true;
    }
    if (toMerge.getParentCount() == 0) {
        //
        return false;
    }
    if (toMerge.getParentCount() == 1) {
        //
        try (ObjectInserter ins = new InMemoryInserter(repo)) {
            ThreeWayMerger m = newThreeWayMerger(ins, repo.getConfig());
            m.setBase(toMerge.getParent(0));
            return m.merge(mergeTip, toMerge);
        } catch (IOException e) {
            throw new IntegrationException(String.format("Cannot merge commit %s with mergetip %s", toMerge.name(), mergeTip.name()), e);
        }
    }
    //
    return canFastForward(mergeSorter, mergeTip, rw, toMerge) || canMerge(mergeSorter, repo, mergeTip, toMerge);
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ThreeWayMerger(org.eclipse.jgit.merge.ThreeWayMerger) IOException(java.io.IOException)

Example 3 with ThreeWayMerger

use of org.eclipse.jgit.merge.ThreeWayMerger in project gerrit by GerritCodeReview.

the class ChangeEditModifier method merge.

private static ObjectId merge(Repository repository, ChangeEdit changeEdit, ObjectId newTreeId) throws IOException, MergeConflictException {
    PatchSet basePatchSet = changeEdit.getBasePatchSet();
    ObjectId basePatchSetCommitId = getPatchSetCommitId(basePatchSet);
    ObjectId editCommitId = changeEdit.getEditCommit();
    ThreeWayMerger threeWayMerger = MergeStrategy.RESOLVE.newMerger(repository, true);
    threeWayMerger.setBase(basePatchSetCommitId);
    boolean successful = threeWayMerger.merge(newTreeId, editCommitId);
    if (!successful) {
        throw new MergeConflictException("The existing change edit could not be merged with another tree.");
    }
    return threeWayMerger.getResultTreeId();
}
Also used : MergeConflictException(com.google.gerrit.extensions.restapi.MergeConflictException) ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ThreeWayMerger(org.eclipse.jgit.merge.ThreeWayMerger)

Example 4 with ThreeWayMerger

use of org.eclipse.jgit.merge.ThreeWayMerger in project gitblit by gitblit.

the class JGitUtils method commitIndex.

public static boolean commitIndex(Repository db, String branch, DirCache index, ObjectId parentId, boolean forceCommit, String author, String authorEmail, String message) throws IOException, ConcurrentRefUpdateException {
    boolean success = false;
    ObjectId headId = db.resolve(branch + "^{commit}");
    ObjectId baseId = parentId;
    if (baseId == null || headId == null) {
        return false;
    }
    ObjectInserter odi = db.newObjectInserter();
    try {
        // Create the in-memory index of the new/updated ticket
        ObjectId indexTreeId = index.writeTree(odi);
        // Create a commit object
        PersonIdent ident = new PersonIdent(author, authorEmail);
        if (forceCommit == false) {
            ThreeWayMerger merger = MergeStrategy.RECURSIVE.newMerger(db, true);
            merger.setObjectInserter(odi);
            merger.setBase(baseId);
            boolean mergeSuccess = merger.merge(indexTreeId, headId);
            if (mergeSuccess) {
                indexTreeId = merger.getResultTreeId();
            } else {
                //Manual merge required
                return false;
            }
        }
        CommitBuilder commit = new CommitBuilder();
        commit.setAuthor(ident);
        commit.setCommitter(ident);
        commit.setEncoding(com.gitblit.Constants.ENCODING);
        commit.setMessage(message);
        commit.setParentId(headId);
        commit.setTreeId(indexTreeId);
        // Insert the commit into the repository
        ObjectId commitId = odi.insert(commit);
        odi.flush();
        RevWalk revWalk = new RevWalk(db);
        try {
            RevCommit revCommit = revWalk.parseCommit(commitId);
            RefUpdate ru = db.updateRef(branch);
            ru.setForceUpdate(forceCommit);
            ru.setNewObjectId(commitId);
            ru.setExpectedOldObjectId(headId);
            ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
            Result rc = ru.update();
            switch(rc) {
                case NEW:
                case FORCED:
                case FAST_FORWARD:
                    success = true;
                    break;
                case REJECTED:
                case LOCK_FAILURE:
                    throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
                default:
                    throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed, branch, commitId.toString(), rc));
            }
        } finally {
            revWalk.close();
        }
    } finally {
        odi.close();
    }
    return success;
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) PersonIdent(org.eclipse.jgit.lib.PersonIdent) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) ThreeWayMerger(org.eclipse.jgit.merge.ThreeWayMerger) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate) FetchResult(org.eclipse.jgit.transport.FetchResult) Result(org.eclipse.jgit.lib.RefUpdate.Result)

Example 5 with ThreeWayMerger

use of org.eclipse.jgit.merge.ThreeWayMerger in project gerrit by GerritCodeReview.

the class MergeUtil method newThreeWayMerger.

public static ThreeWayMerger newThreeWayMerger(ObjectInserter inserter, Config repoConfig, String strategyName) {
    Merger m = newMerger(inserter, repoConfig, strategyName);
    checkArgument(m instanceof ThreeWayMerger, "merge strategy %s does not support three-way merging", strategyName);
    return (ThreeWayMerger) m;
}
Also used : Merger(org.eclipse.jgit.merge.Merger) ResolveMerger(org.eclipse.jgit.merge.ResolveMerger) ThreeWayMerger(org.eclipse.jgit.merge.ThreeWayMerger) ThreeWayMerger(org.eclipse.jgit.merge.ThreeWayMerger)

Aggregations

ThreeWayMerger (org.eclipse.jgit.merge.ThreeWayMerger)6 ObjectId (org.eclipse.jgit.lib.ObjectId)4 MergeConflictException (com.google.gerrit.extensions.restapi.MergeConflictException)3 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)3 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)2 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)1 IOException (java.io.IOException)1 ConcurrentRefUpdateException (org.eclipse.jgit.api.errors.ConcurrentRefUpdateException)1 JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)1 PersonIdent (org.eclipse.jgit.lib.PersonIdent)1 RefUpdate (org.eclipse.jgit.lib.RefUpdate)1 Result (org.eclipse.jgit.lib.RefUpdate.Result)1 Merger (org.eclipse.jgit.merge.Merger)1 ResolveMerger (org.eclipse.jgit.merge.ResolveMerger)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1 FetchResult (org.eclipse.jgit.transport.FetchResult)1