Search in sources :

Example 1 with BranchOrderSection

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

the class Mergeable method apply.

@Override
public MergeableInfo apply(RevisionResource resource) throws AuthException, ResourceConflictException, BadRequestException, OrmException, IOException {
    Change change = resource.getChange();
    PatchSet ps = resource.getPatchSet();
    MergeableInfo result = new MergeableInfo();
    if (!change.getStatus().isOpen()) {
        throw new ResourceConflictException("change is " + ChangeUtil.status(change));
    } else if (!ps.getId().equals(change.currentPatchSetId())) {
        // Only the current revision is mergeable. Others always fail.
        return result;
    }
    ChangeData cd = changeDataFactory.create(db.get(), resource.getControl());
    result.submitType = getSubmitType(cd, ps);
    try (Repository git = gitManager.openRepository(change.getProject())) {
        ObjectId commit = toId(ps);
        Ref ref = git.getRefDatabase().exactRef(change.getDest().get());
        ProjectState projectState = projectCache.get(change.getProject());
        String strategy = mergeUtilFactory.create(projectState).mergeStrategyName();
        result.strategy = strategy;
        result.mergeable = isMergable(git, change, commit, ref, result.submitType, strategy);
        if (otherBranches) {
            result.mergeableInto = new ArrayList<>();
            BranchOrderSection branchOrder = projectState.getBranchOrderSection();
            if (branchOrder != null) {
                int prefixLen = Constants.R_HEADS.length();
                String[] names = branchOrder.getMoreStable(ref.getName());
                Map<String, Ref> refs = git.getRefDatabase().exactRef(names);
                for (String n : names) {
                    Ref other = refs.get(n);
                    if (other == null) {
                        continue;
                    }
                    if (cache.get(commit, other, SubmitType.CHERRY_PICK, strategy, change.getDest(), git)) {
                        result.mergeableInto.add(other.getName().substring(prefixLen));
                    }
                }
            }
        }
    }
    return result;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) BranchOrderSection(com.google.gerrit.server.git.BranchOrderSection) ChangeData(com.google.gerrit.server.query.change.ChangeData) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) MergeableInfo(com.google.gerrit.extensions.common.MergeableInfo) ProjectState(com.google.gerrit.server.project.ProjectState)

Aggregations

MergeableInfo (com.google.gerrit.extensions.common.MergeableInfo)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 Change (com.google.gerrit.reviewdb.client.Change)1 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)1 BranchOrderSection (com.google.gerrit.server.git.BranchOrderSection)1 ProjectState (com.google.gerrit.server.project.ProjectState)1 ChangeData (com.google.gerrit.server.query.change.ChangeData)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 Ref (org.eclipse.jgit.lib.Ref)1 Repository (org.eclipse.jgit.lib.Repository)1