Search in sources :

Example 1 with NoMergeBaseException

use of org.eclipse.jgit.errors.NoMergeBaseException in project gerrit by GerritCodeReview.

the class CheckMergeability method apply.

@Override
public Response<MergeableInfo> apply(BranchResource resource) throws IOException, BadRequestException, ResourceNotFoundException, ResourceConflictException {
    if (!(submitType.equals(SubmitType.MERGE_ALWAYS) || submitType.equals(SubmitType.MERGE_IF_NECESSARY))) {
        throw new BadRequestException("Submit type: " + submitType + " is not supported");
    }
    MergeableInfo result = new MergeableInfo();
    result.submitType = submitType;
    result.strategy = strategy;
    try (Repository git = gitManager.openRepository(resource.getNameKey());
        RevWalk rw = new RevWalk(git);
        ObjectInserter inserter = new InMemoryInserter(git)) {
        Merger m = MergeUtil.newMerger(inserter, git.getConfig(), strategy);
        Ref destRef = git.getRefDatabase().exactRef(resource.getRef());
        if (destRef == null) {
            throw new ResourceNotFoundException(resource.getRef());
        }
        RevCommit targetCommit = rw.parseCommit(destRef.getObjectId());
        RevCommit sourceCommit = null;
        try {
            sourceCommit = MergeUtil.resolveCommit(git, rw, source);
            if (!commits.canRead(resource.getProjectState(), git, sourceCommit)) {
                throw new BadRequestException("do not have read permission for: " + source);
            }
        } catch (BadRequestException e) {
            // Throw a unified exception for permission denied and unresolvable commits.
            throw new BadRequestException("Error resolving: '" + source + "'. Do not have read permission, or failed to resolve to a commit.", e);
        }
        if (rw.isMergedInto(sourceCommit, targetCommit)) {
            result.mergeable = true;
            result.commitMerged = true;
            result.contentMerged = true;
            return Response.ok(result);
        }
        if (m.merge(false, targetCommit, sourceCommit)) {
            result.mergeable = true;
            result.commitMerged = false;
            result.contentMerged = m.getResultTreeId().equals(targetCommit.getTree());
        } else {
            result.mergeable = false;
            if (m instanceof ResolveMerger) {
                result.conflicts = ((ResolveMerger) m).getUnmergedPaths();
            }
        }
    } catch (InvalidMergeStrategyException e) {
        throw new BadRequestException(e.getMessage());
    } catch (NoMergeBaseException e) {
        // adapted to show the message to the user.
        throw new ResourceConflictException(String.format("Change cannot be merged: %s", e.getMessage()), e);
    }
    return Response.ok(result);
}
Also used : NoMergeBaseException(org.eclipse.jgit.errors.NoMergeBaseException) InMemoryInserter(com.google.gerrit.server.git.InMemoryInserter) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ResolveMerger(org.eclipse.jgit.merge.ResolveMerger) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Merger(org.eclipse.jgit.merge.Merger) ResolveMerger(org.eclipse.jgit.merge.ResolveMerger) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) MergeableInfo(com.google.gerrit.extensions.common.MergeableInfo) InvalidMergeStrategyException(com.google.gerrit.exceptions.InvalidMergeStrategyException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with NoMergeBaseException

use of org.eclipse.jgit.errors.NoMergeBaseException in project gerrit by GerritCodeReview.

the class CreateChange method newMergeCommit.

private CodeReviewCommit newMergeCommit(Repository repo, ObjectInserter oi, CodeReviewRevWalk rw, ProjectState projectState, RevCommit mergeTip, MergeInput merge, PersonIdent authorIdent, PersonIdent committerIdent, String commitMessage) throws RestApiException, IOException {
    logger.atFine().log("Creating merge commit: source = %s, strategy = %s, allowConflicts = %s", merge.source, merge.strategy, merge.allowConflicts);
    if (Strings.isNullOrEmpty(merge.source)) {
        throw new BadRequestException("merge.source must be non-empty");
    }
    RevCommit sourceCommit = MergeUtil.resolveCommit(repo, rw, merge.source);
    if (merge.sourceBranch != null) {
        Ref ref = repo.findRef(merge.sourceBranch);
        logger.atFine().log("checking visibility for branch %s", merge.sourceBranch);
        if (ref == null || !commits.canRead(projectState, repo, sourceCommit, ref)) {
            throw new BadRequestException("do not have read permission for: " + merge.source);
        }
    } else if (!commits.canRead(projectState, repo, sourceCommit)) {
        throw new BadRequestException("do not have read permission for: " + merge.source);
    }
    MergeUtil mergeUtil = mergeUtilFactory.create(projectState);
    // default merge strategy from project settings
    String mergeStrategy = firstNonNull(Strings.emptyToNull(merge.strategy), mergeUtil.mergeStrategyName());
    logger.atFine().log("merge strategy = %s", mergeStrategy);
    try {
        return MergeUtil.createMergeCommit(oi, repo.getConfig(), mergeTip, sourceCommit, mergeStrategy, merge.allowConflicts, authorIdent, committerIdent, commitMessage, rw);
    } catch (NoMergeBaseException e) {
        throw new ResourceConflictException(String.format("Cannot create merge commit: %s", e.getMessage()), e);
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) NoMergeBaseException(org.eclipse.jgit.errors.NoMergeBaseException) MergeUtil(com.google.gerrit.server.git.MergeUtil) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 NoMergeBaseException (org.eclipse.jgit.errors.NoMergeBaseException)2 Ref (org.eclipse.jgit.lib.Ref)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 InvalidMergeStrategyException (com.google.gerrit.exceptions.InvalidMergeStrategyException)1 MergeableInfo (com.google.gerrit.extensions.common.MergeableInfo)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 InMemoryInserter (com.google.gerrit.server.git.InMemoryInserter)1 MergeUtil (com.google.gerrit.server.git.MergeUtil)1 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)1 Repository (org.eclipse.jgit.lib.Repository)1 Merger (org.eclipse.jgit.merge.Merger)1 ResolveMerger (org.eclipse.jgit.merge.ResolveMerger)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1