Search in sources :

Example 16 with CodeReviewCommit

use of com.google.gerrit.server.git.CodeReviewCommit in project gerrit by GerritCodeReview.

the class SubmitDryRun method run.

public boolean run(@Nullable CurrentUser caller, SubmitType submitType, Repository repo, CodeReviewRevWalk rw, BranchNameKey destBranch, ObjectId tip, ObjectId toMerge, Set<RevCommit> alreadyAccepted) throws NoSuchProjectException, IOException {
    CodeReviewCommit tipCommit = rw.parseCommit(tip);
    CodeReviewCommit toMergeCommit = rw.parseCommit(toMerge);
    RevFlag canMerge = rw.newFlag("CAN_MERGE");
    toMergeCommit.add(canMerge);
    Arguments args = new Arguments(repo, rw, mergeUtilFactory.create(getProject(destBranch)), new MergeSorter(caller, rw, alreadyAccepted, canMerge, queryProvider, ImmutableSet.of(toMergeCommit)));
    switch(submitType) {
        case CHERRY_PICK:
            return CherryPick.dryRun(args, tipCommit, toMergeCommit);
        case FAST_FORWARD_ONLY:
            return FastForwardOnly.dryRun(args, tipCommit, toMergeCommit);
        case MERGE_ALWAYS:
            return MergeAlways.dryRun(args, tipCommit, toMergeCommit);
        case MERGE_IF_NECESSARY:
            return MergeIfNecessary.dryRun(args, tipCommit, toMergeCommit);
        case REBASE_IF_NECESSARY:
            return RebaseIfNecessary.dryRun(args, repo, tipCommit, toMergeCommit);
        case REBASE_ALWAYS:
            return RebaseAlways.dryRun(args, repo, tipCommit, toMergeCommit);
        case INHERIT:
        default:
            String errorMsg = "No submit strategy for: " + submitType;
            logger.atSevere().log("%s", errorMsg);
            throw new StorageException(errorMsg);
    }
}
Also used : RevFlag(org.eclipse.jgit.revwalk.RevFlag) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) StorageException(com.google.gerrit.exceptions.StorageException)

Example 17 with CodeReviewCommit

use of com.google.gerrit.server.git.CodeReviewCommit in project gerrit by GerritCodeReview.

the class BranchTips method getTip.

/**
 * Returns current tip of the branch, taking into account commits created during the submit
 * process or submodule updates.
 *
 * @param branch branch
 * @param repo repository to look for the branch if not cached
 * @return the current tip. Empty if the branch doesn't exist in the repository
 * @throws IOException Cannot access the underlying storage
 */
Optional<CodeReviewCommit> getTip(BranchNameKey branch, OpenRepo repo) throws IOException {
    CodeReviewCommit currentCommit;
    if (branchTips.containsKey(branch)) {
        currentCommit = branchTips.get(branch);
    } else {
        Ref r = repo.repo.exactRef(branch.branch());
        if (r == null) {
            return Optional.empty();
        }
        currentCommit = repo.rw.parseCommit(r.getObjectId());
        branchTips.put(branch, currentCommit);
    }
    return Optional.of(currentCommit);
}
Also used : Ref(org.eclipse.jgit.lib.Ref) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit)

Example 18 with CodeReviewCommit

use of com.google.gerrit.server.git.CodeReviewCommit in project gerrit by GerritCodeReview.

the class SubmoduleCommits method amendGitlinksCommit.

/**
 * Amend an existing commit with gitlink updates
 */
CodeReviewCommit amendGitlinksCommit(BranchNameKey subscriber, CodeReviewCommit currentCommit, Collection<SubmoduleSubscription> subscriptions) throws IOException, SubmoduleConflictException {
    OpenRepo or;
    try {
        or = orm.getRepo(subscriber.project());
    } catch (NoSuchProjectException | IOException e) {
        throw new StorageException("Cannot access superproject", e);
    }
    StringBuilder msgbuf = new StringBuilder();
    DirCache dc = readTree(or.rw, currentCommit);
    DirCacheEditor ed = dc.editor();
    for (SubmoduleSubscription s : sortByPath(subscriptions)) {
        updateSubmodule(dc, ed, msgbuf, s);
    }
    ed.finish();
    ObjectId newTreeId = dc.writeTree(or.ins);
    // Gitlinks are already updated, just return the commit
    if (newTreeId.equals(currentCommit.getTree())) {
        return currentCommit;
    }
    or.rw.parseBody(currentCommit);
    CommitBuilder commit = new CommitBuilder();
    commit.setTreeId(newTreeId);
    commit.setParentIds(currentCommit.getParents());
    if (verboseSuperProject != VerboseSuperprojectUpdate.FALSE) {
        // TODO(czhen): handle cherrypick footer
        commit.setMessage(currentCommit.getFullMessage() + "\n\n* submodules:\n" + msgbuf.toString());
    } else {
        commit.setMessage(currentCommit.getFullMessage());
    }
    commit.setAuthor(currentCommit.getAuthorIdent());
    commit.setCommitter(myIdent);
    ObjectId id = or.ins.insert(commit);
    CodeReviewCommit newCommit = or.getCodeReviewRevWalk().parseCommit(id);
    newCommit.copyFrom(currentCommit);
    return newCommit;
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) ObjectId(org.eclipse.jgit.lib.ObjectId) OpenRepo(com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) SubmoduleSubscription(com.google.gerrit.entities.SubmoduleSubscription) IOException(java.io.IOException) DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor) StorageException(com.google.gerrit.exceptions.StorageException) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit)

Example 19 with CodeReviewCommit

use of com.google.gerrit.server.git.CodeReviewCommit in project gerrit by GerritCodeReview.

the class SubmoduleOp method forceRefTips.

private void forceRefTips(Map<BranchNameKey, ReceiveCommand> updatedBranches, SubmoduleCommits submoduleCommits) {
    // This is dryrun, all commands succeeded (no need to filter success).
    for (Map.Entry<BranchNameKey, ReceiveCommand> updateBranch : updatedBranches.entrySet()) {
        try {
            ReceiveCommand command = updateBranch.getValue();
            if (command.getType() == ReceiveCommand.Type.DELETE) {
                continue;
            }
            BranchNameKey branchNameKey = updateBranch.getKey();
            OpenRepo openRepo = orm.getRepo(branchNameKey.project());
            CodeReviewCommit fakeTip = openRepo.rw.parseCommit(command.getNewId());
            submoduleCommits.addBranchTip(branchNameKey, fakeTip);
        } catch (NoSuchProjectException | IOException e) {
            throw new StorageException("Cannot find branch tip target in dryrun", e);
        }
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) BranchNameKey(com.google.gerrit.entities.BranchNameKey) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) OpenRepo(com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo) IOException(java.io.IOException) Map(java.util.Map) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) StorageException(com.google.gerrit.exceptions.StorageException)

Example 20 with CodeReviewCommit

use of com.google.gerrit.server.git.CodeReviewCommit in project gerrit by GerritCodeReview.

the class FastForwardOnly method buildOps.

@Override
public ImmutableList<SubmitStrategyOp> buildOps(Collection<CodeReviewCommit> toMerge) {
    List<CodeReviewCommit> sorted = args.mergeUtil.reduceToMinimalMerge(args.mergeSorter, toMerge);
    Map<BranchNameKey, CodeReviewCommit> branchToCommit = new HashMap<>();
    for (CodeReviewCommit codeReviewCommit : sorted) {
        BranchNameKey branchNameKey = codeReviewCommit.change().getDest();
        CodeReviewCommit otherCommitInBranch = branchToCommit.get(branchNameKey);
        if (otherCommitInBranch == null) {
            branchToCommit.put(branchNameKey, codeReviewCommit);
        } else {
            // we found another change with the same destination branch.
            codeReviewCommit.setStatusCode(CommitMergeStatus.FAST_FORWARD_INDEPENDENT_CHANGES);
            otherCommitInBranch.setStatusCode(CommitMergeStatus.FAST_FORWARD_INDEPENDENT_CHANGES);
            return ImmutableList.of();
        }
    }
    ImmutableList.Builder<SubmitStrategyOp> ops = ImmutableList.builderWithExpectedSize(sorted.size());
    CodeReviewCommit newTipCommit = args.mergeUtil.getFirstFastForward(args.mergeTip.getInitialTip(), args.rw, sorted);
    if (!newTipCommit.equals(args.mergeTip.getInitialTip())) {
        ops.add(new FastForwardOp(args, newTipCommit));
    } else {
        for (CodeReviewCommit c : toMerge) {
            ops.add(new NotFastForwardOp(c));
        }
    }
    return ops.build();
}
Also used : BranchNameKey(com.google.gerrit.entities.BranchNameKey) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit)

Aggregations

CodeReviewCommit (com.google.gerrit.server.git.CodeReviewCommit)45 ArrayList (java.util.ArrayList)13 ObjectId (org.eclipse.jgit.lib.ObjectId)11 RevCommit (org.eclipse.jgit.revwalk.RevCommit)11 IOException (java.io.IOException)10 StorageException (com.google.gerrit.exceptions.StorageException)9 PersonIdent (org.eclipse.jgit.lib.PersonIdent)9 BranchNameKey (com.google.gerrit.entities.BranchNameKey)7 Change (com.google.gerrit.entities.Change)7 PatchSet (com.google.gerrit.entities.PatchSet)7 CodeReviewRevWalk (com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk)7 ImmutableList (com.google.common.collect.ImmutableList)6 Repository (org.eclipse.jgit.lib.Repository)6 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)5 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)5 ProjectState (com.google.gerrit.server.project.ProjectState)5 OpenRepo (com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo)5 SubmoduleSubscription (com.google.gerrit.entities.SubmoduleSubscription)4 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)4 MergeConflictException (com.google.gerrit.extensions.restapi.MergeConflictException)4