Search in sources :

Example 6 with ThreeWayMerger

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

the class RebaseChangeOp method rebaseCommit.

/**
   * Rebase a commit.
   *
   * @param ctx repo context.
   * @param original the commit to rebase.
   * @param base base to rebase against.
   * @return the rebased commit.
   * @throws MergeConflictException the rebase failed due to a merge conflict.
   * @throws IOException the merge failed for another reason.
   */
private RevCommit rebaseCommit(RepoContext ctx, RevCommit original, ObjectId base, String commitMessage) throws ResourceConflictException, IOException {
    RevCommit parentCommit = original.getParent(0);
    if (base.equals(parentCommit)) {
        throw new ResourceConflictException("Change is already up to date.");
    }
    ThreeWayMerger merger = newMergeUtil().newThreeWayMerger(ctx.getInserter(), ctx.getRepoView().getConfig());
    merger.setBase(parentCommit);
    merger.merge(original, base);
    if (merger.getResultTreeId() == null) {
        throw new MergeConflictException("The change could not be rebased due to a conflict during merge.");
    }
    CommitBuilder cb = new CommitBuilder();
    cb.setTreeId(merger.getResultTreeId());
    cb.setParentId(base);
    cb.setAuthor(original.getAuthorIdent());
    cb.setMessage(commitMessage);
    if (committerIdent != null) {
        cb.setCommitter(committerIdent);
    } else {
        cb.setCommitter(ctx.getIdentifiedUser().newCommitterIdent(ctx.getWhen(), ctx.getTimeZone()));
    }
    ObjectId objectId = ctx.getInserter().insert(cb);
    ctx.getInserter().flush();
    return ctx.getRevWalk().parseCommit(objectId);
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) MergeConflictException(com.google.gerrit.extensions.restapi.MergeConflictException) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) ThreeWayMerger(org.eclipse.jgit.merge.ThreeWayMerger) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

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