Search in sources :

Example 6 with OpenRepo

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

the class MergeOp method getSubmitStrategies.

private List<SubmitStrategy> getSubmitStrategies(Map<Branch.NameKey, BranchBatch> toSubmit, SubmoduleOp submoduleOp, boolean dryrun) throws IntegrationException, NoSuchProjectException, IOException {
    List<SubmitStrategy> strategies = new ArrayList<>();
    Set<Branch.NameKey> allBranches = submoduleOp.getBranchesInOrder();
    Set<CodeReviewCommit> allCommits = toSubmit.values().stream().map(BranchBatch::commits).flatMap(Set::stream).collect(toSet());
    for (Branch.NameKey branch : allBranches) {
        OpenRepo or = orm.getRepo(branch.getParentKey());
        if (toSubmit.containsKey(branch)) {
            BranchBatch submitting = toSubmit.get(branch);
            OpenBranch ob = or.getBranch(branch);
            checkNotNull(submitting.submitType(), "null submit type for %s; expected to previously fail fast", submitting);
            Set<CodeReviewCommit> commitsToSubmit = submitting.commits();
            ob.mergeTip = new MergeTip(ob.oldTip, commitsToSubmit);
            SubmitStrategy strategy = submitStrategyFactory.create(submitting.submitType(), db, or.rw, or.canMergeFlag, getAlreadyAccepted(or, ob.oldTip), allCommits, branch, caller, ob.mergeTip, commitStatus, submissionId, submitInput.notify, accountsToNotify, submoduleOp, dryrun);
            strategies.add(strategy);
            strategy.addOps(or.getUpdate(batchUpdateFactory), commitsToSubmit);
            if (submitting.submitType().equals(SubmitType.FAST_FORWARD_ONLY) && submoduleOp.hasSubscription(branch)) {
                submoduleOp.addOp(or.getUpdate(batchUpdateFactory), branch);
            }
        } else {
            // no open change for this branch
            // add submodule triggered op into BatchUpdate
            submoduleOp.addOp(or.getUpdate(batchUpdateFactory), branch);
        }
    }
    return strategies;
}
Also used : ArrayList(java.util.ArrayList) Branch(com.google.gerrit.reviewdb.client.Branch) OpenBranch(com.google.gerrit.server.git.MergeOpRepoManager.OpenBranch) OpenRepo(com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo) SubmitStrategy(com.google.gerrit.server.git.strategy.SubmitStrategy) OpenBranch(com.google.gerrit.server.git.MergeOpRepoManager.OpenBranch)

Example 7 with OpenRepo

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

the class MergeSuperSet method getRepo.

private OpenRepo getRepo(Project.NameKey project) throws IOException {
    if (orm == null) {
        orm = repoManagerProvider.get();
        closeOrm = true;
    }
    try {
        OpenRepo or = orm.getRepo(project);
        checkState(or.rw.hasRevSort(RevSort.TOPO));
        return or;
    } catch (NoSuchProjectException e) {
        throw new IOException(e);
    }
}
Also used : NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) OpenRepo(com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo) IOException(java.io.IOException)

Example 8 with OpenRepo

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

the class MergeSuperSet method completeChangeSetWithoutTopic.

private ChangeSet completeChangeSetWithoutTopic(ReviewDb db, ChangeSet changes, CurrentUser user) throws IOException, OrmException {
    Collection<ChangeData> visibleChanges = new ArrayList<>();
    Collection<ChangeData> nonVisibleChanges = new ArrayList<>();
    // For each target branch we run a separate rev walk to find open changes
    // reachable from changes already in the merge super set.
    ImmutableListMultimap<Branch.NameKey, ChangeData> bc = byBranch(Iterables.concat(changes.changes(), changes.nonVisibleChanges()));
    for (Branch.NameKey b : bc.keySet()) {
        OpenRepo or = getRepo(b.getParentKey());
        List<RevCommit> visibleCommits = new ArrayList<>();
        List<RevCommit> nonVisibleCommits = new ArrayList<>();
        for (ChangeData cd : bc.get(b)) {
            checkState(cd.hasChangeControl(), "completeChangeSet forgot to set changeControl for current user" + " at ChangeData creation time");
            boolean visible = changes.ids().contains(cd.getId());
            if (visible && !cd.changeControl().isVisible(db, cd)) {
                // We thought the change was visible, but it isn't.
                // This can happen if the ACL changes during the
                // completeChangeSet computation, for example.
                visible = false;
            }
            Collection<RevCommit> toWalk = visible ? visibleCommits : nonVisibleCommits;
            // Pick a revision to use for traversal.  If any of the patch sets
            // is visible, we use the most recent one.  Otherwise, use the current
            // patch set.
            PatchSet ps = cd.currentPatchSet();
            boolean visiblePatchSet = visible;
            if (!cd.changeControl().isPatchVisible(ps, cd)) {
                Iterable<PatchSet> visiblePatchSets = cd.visiblePatchSets();
                if (Iterables.isEmpty(visiblePatchSets)) {
                    visiblePatchSet = false;
                } else {
                    ps = Iterables.getLast(visiblePatchSets);
                }
            }
            if (submitType(cd, ps, visiblePatchSet) == SubmitType.CHERRY_PICK) {
                if (visible) {
                    visibleChanges.add(cd);
                } else {
                    nonVisibleChanges.add(cd);
                }
                continue;
            }
            // Get the underlying git commit object
            String objIdStr = ps.getRevision().get();
            RevCommit commit = or.rw.parseCommit(ObjectId.fromString(objIdStr));
            // Always include the input, even if merged. This allows
            // SubmitStrategyOp to correct the situation later, assuming it gets
            // returned by byCommitsOnBranchNotMerged below.
            toWalk.add(commit);
        }
        Set<String> emptySet = Collections.emptySet();
        Set<String> visibleHashes = walkChangesByHashes(visibleCommits, emptySet, or, b);
        List<ChangeData> cds = byCommitsOnBranchNotMerged(or, db, user, b, visibleHashes);
        for (ChangeData chd : cds) {
            chd.changeControl(user);
            visibleChanges.add(chd);
        }
        Set<String> nonVisibleHashes = walkChangesByHashes(nonVisibleCommits, visibleHashes, or, b);
        Iterables.addAll(nonVisibleChanges, byCommitsOnBranchNotMerged(or, db, user, b, nonVisibleHashes));
    }
    return new ChangeSet(visibleChanges, nonVisibleChanges);
}
Also used : ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ChangeData(com.google.gerrit.server.query.change.ChangeData) Branch(com.google.gerrit.reviewdb.client.Branch) OpenRepo(com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 9 with OpenRepo

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

the class SubmoduleOp method composeGitlinksCommit.

/** Amend an existing commit with gitlink updates */
public CodeReviewCommit composeGitlinksCommit(final Branch.NameKey subscriber, CodeReviewCommit currentCommit) throws IOException, SubmoduleException {
    OpenRepo or;
    try {
        or = orm.getRepo(subscriber.getParentKey());
    } catch (NoSuchProjectException | IOException e) {
        throw new SubmoduleException("Cannot access superproject", e);
    }
    StringBuilder msgbuf = new StringBuilder("");
    DirCache dc = readTree(or.rw, currentCommit);
    DirCacheEditor ed = dc.editor();
    for (SubmoduleSubscription s : targets.get(subscriber)) {
        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.rw.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.git.MergeOpRepoManager.OpenRepo) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) SubmoduleSubscription(com.google.gerrit.reviewdb.client.SubmoduleSubscription) IOException(java.io.IOException) DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor)

Example 10 with OpenRepo

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

the class MergeOp method integrateIntoHistory.

private void integrateIntoHistory(ChangeSet cs) throws IntegrationException, RestApiException {
    checkArgument(!cs.furtherHiddenChanges(), "cannot integrate hidden changes into history");
    logDebug("Beginning merge attempt on {}", cs);
    Map<Branch.NameKey, BranchBatch> toSubmit = new HashMap<>();
    ListMultimap<Branch.NameKey, ChangeData> cbb;
    try {
        cbb = cs.changesByBranch();
    } catch (OrmException e) {
        throw new IntegrationException("Error reading changes to submit", e);
    }
    Set<Branch.NameKey> branches = cbb.keySet();
    for (Branch.NameKey branch : branches) {
        OpenRepo or = openRepo(branch.getParentKey());
        if (or != null) {
            toSubmit.put(branch, validateChangeList(or, cbb.get(branch)));
        }
    }
    // Done checks that don't involve running submit strategies.
    commitStatus.maybeFailVerbose();
    try {
        SubmoduleOp submoduleOp = subOpFactory.create(branches, orm);
        List<SubmitStrategy> strategies = getSubmitStrategies(toSubmit, submoduleOp, dryrun);
        this.allProjects = submoduleOp.getProjectsInOrder();
        batchUpdateFactory.execute(orm.batchUpdates(batchUpdateFactory, allProjects), new SubmitStrategyListener(submitInput, strategies, commitStatus), submissionId, dryrun);
    } catch (NoSuchProjectException e) {
        throw new ResourceNotFoundException(e.getMessage());
    } catch (IOException | SubmoduleException e) {
        throw new IntegrationException(e);
    } catch (UpdateException e) {
        // BatchUpdate may have inadvertently wrapped an IntegrationException
        // thrown by some legacy SubmitStrategyOp code that intended the error
        // message to be user-visible. Copy the message from the wrapped
        // exception.
        //
        // If you happen across one of these, the correct fix is to convert the
        // inner IntegrationException to a ResourceConflictException.
        String msg;
        if (e.getCause() instanceof IntegrationException) {
            msg = e.getCause().getMessage();
        } else {
            msg = "Error submitting change" + (cs.size() != 1 ? "s" : "");
        }
        throw new IntegrationException(msg, e);
    }
}
Also used : HashMap(java.util.HashMap) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) IOException(java.io.IOException) ChangeData(com.google.gerrit.server.query.change.ChangeData) OrmException(com.google.gwtorm.server.OrmException) Branch(com.google.gerrit.reviewdb.client.Branch) OpenBranch(com.google.gerrit.server.git.MergeOpRepoManager.OpenBranch) SubmitStrategyListener(com.google.gerrit.server.git.strategy.SubmitStrategyListener) OpenRepo(com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo) UpdateException(com.google.gerrit.server.update.UpdateException) SubmitStrategy(com.google.gerrit.server.git.strategy.SubmitStrategy) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Aggregations

OpenRepo (com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo)10 NoSuchProjectException (com.google.gerrit.server.project.NoSuchProjectException)8 Branch (com.google.gerrit.reviewdb.client.Branch)6 IOException (java.io.IOException)6 SubmoduleSubscription (com.google.gerrit.reviewdb.client.SubmoduleSubscription)3 ArrayList (java.util.ArrayList)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 Ref (org.eclipse.jgit.lib.Ref)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 Project (com.google.gerrit.reviewdb.client.Project)2 OpenBranch (com.google.gerrit.server.git.MergeOpRepoManager.OpenBranch)2 SubmitStrategy (com.google.gerrit.server.git.strategy.SubmitStrategy)2 ChangeData (com.google.gerrit.server.query.change.ChangeData)2 UpdateException (com.google.gerrit.server.update.UpdateException)2 LinkedHashSet (java.util.LinkedHashSet)2 DirCache (org.eclipse.jgit.dircache.DirCache)2 DirCacheEditor (org.eclipse.jgit.dircache.DirCacheEditor)2 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)2 SubscribeSection (com.google.gerrit.common.data.SubscribeSection)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1