Search in sources :

Example 96 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class ReviewCommand method addPatchSetId.

@Argument(index = 0, required = true, multiValued = true, metaVar = "{COMMIT | CHANGE,PATCHSET}", usage = "list of commits or patch sets to review")
void addPatchSetId(final String token) {
    try {
        PatchSet ps = psParser.parsePatchSet(token, projectControl, branch);
        patchSets.add(ps);
    } catch (UnloggedFailure e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    } catch (OrmException e) {
        throw new IllegalArgumentException("database error", e);
    }
}
Also used : OrmException(com.google.gwtorm.server.OrmException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Argument(org.kohsuke.args4j.Argument)

Example 97 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class PatchSetParser method parsePatchSet.

public PatchSet parsePatchSet(String token, ProjectControl projectControl, String branch) throws UnloggedFailure, OrmException {
    //
    if (token.matches("^([0-9a-fA-F]{4," + RevId.LEN + "})$")) {
        InternalChangeQuery query = queryProvider.get();
        List<ChangeData> cds;
        if (projectControl != null) {
            Project.NameKey p = projectControl.getProject().getNameKey();
            if (branch != null) {
                cds = query.byBranchCommit(p.get(), branch, token);
            } else {
                cds = query.byProjectCommit(p, token);
            }
        } else {
            cds = query.byCommit(token);
        }
        List<PatchSet> matches = new ArrayList<>(cds.size());
        for (ChangeData cd : cds) {
            Change c = cd.change();
            if (!(inProject(c, projectControl) && inBranch(c, branch))) {
                continue;
            }
            for (PatchSet ps : cd.patchSets()) {
                if (ps.getRevision().matches(token)) {
                    matches.add(ps);
                }
            }
        }
        switch(matches.size()) {
            case 1:
                return matches.iterator().next();
            case 0:
                throw error("\"" + token + "\" no such patch set");
            default:
                throw error("\"" + token + "\" matches multiple patch sets");
        }
    }
    //
    if (token.matches("^[1-9][0-9]*,[1-9][0-9]*$")) {
        PatchSet.Id patchSetId;
        try {
            patchSetId = PatchSet.Id.parse(token);
        } catch (IllegalArgumentException e) {
            throw error("\"" + token + "\" is not a valid patch set");
        }
        ChangeNotes notes = getNotes(projectControl, patchSetId.getParentKey());
        PatchSet patchSet = psUtil.get(db.get(), notes, patchSetId);
        if (patchSet == null) {
            throw error("\"" + token + "\" no such patch set");
        }
        if (projectControl != null || branch != null) {
            Change change = notes.getChange();
            if (!inProject(change, projectControl)) {
                throw error("change " + change.getId() + " not in project " + projectControl.getProject().getName());
            }
            if (!inBranch(change, branch)) {
                throw error("change " + change.getId() + " not in branch " + branch);
            }
        }
        return patchSet;
    }
    throw error("\"" + token + "\" is not a valid patch set");
}
Also used : InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) Project(com.google.gerrit.reviewdb.client.Project) ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 98 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class RelatedChangesSorter method sort.

public List<PatchSetData> sort(List<ChangeData> in, PatchSet startPs) throws OrmException, IOException {
    checkArgument(!in.isEmpty(), "Input may not be empty");
    // Map of all patch sets, keyed by commit SHA-1.
    Map<String, PatchSetData> byId = collectById(in);
    PatchSetData start = byId.get(startPs.getRevision().get());
    checkArgument(start != null, "%s not found in %s", startPs, in);
    ProjectControl ctl = start.data().changeControl().getProjectControl();
    // Map of patch set -> immediate parent.
    ListMultimap<PatchSetData, PatchSetData> parents = MultimapBuilder.hashKeys(in.size()).arrayListValues(3).build();
    // Map of patch set -> immediate children.
    ListMultimap<PatchSetData, PatchSetData> children = MultimapBuilder.hashKeys(in.size()).arrayListValues(3).build();
    // All other patch sets of the same change as startPs.
    List<PatchSetData> otherPatchSetsOfStart = new ArrayList<>();
    for (ChangeData cd : in) {
        for (PatchSet ps : cd.patchSets()) {
            PatchSetData thisPsd = checkNotNull(byId.get(ps.getRevision().get()));
            if (cd.getId().equals(start.id()) && !ps.getId().equals(start.psId())) {
                otherPatchSetsOfStart.add(thisPsd);
            }
            for (RevCommit p : thisPsd.commit().getParents()) {
                PatchSetData parentPsd = byId.get(p.name());
                if (parentPsd != null) {
                    parents.put(thisPsd, parentPsd);
                    children.put(parentPsd, thisPsd);
                }
            }
        }
    }
    Collection<PatchSetData> ancestors = walkAncestors(ctl, parents, start);
    List<PatchSetData> descendants = walkDescendants(ctl, children, start, otherPatchSetsOfStart, ancestors);
    List<PatchSetData> result = new ArrayList<>(ancestors.size() + descendants.size() - 1);
    result.addAll(Lists.reverse(descendants));
    result.addAll(ancestors);
    return result;
}
Also used : ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ProjectControl(com.google.gerrit.server.project.ProjectControl) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 99 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class RebaseUtil method parseBase.

Base parseBase(RevisionResource rsrc, String base) throws OrmException {
    ReviewDb db = dbProvider.get();
    // Try parsing the base as a ref string.
    PatchSet.Id basePatchSetId = PatchSet.Id.fromRef(base);
    if (basePatchSetId != null) {
        Change.Id baseChangeId = basePatchSetId.getParentKey();
        ChangeControl baseCtl = controlFor(rsrc, baseChangeId);
        if (baseCtl != null) {
            return Base.create(controlFor(rsrc, basePatchSetId.getParentKey()), psUtil.get(db, baseCtl.getNotes(), basePatchSetId));
        }
    }
    // Try parsing base as a change number (assume current patch set).
    Integer baseChangeId = Ints.tryParse(base);
    if (baseChangeId != null) {
        ChangeControl baseCtl = controlFor(rsrc, new Change.Id(baseChangeId));
        if (baseCtl != null) {
            return Base.create(baseCtl, psUtil.current(db, baseCtl.getNotes()));
        }
    }
    // Try parsing as SHA-1.
    Base ret = null;
    for (ChangeData cd : queryProvider.get().byProjectCommit(rsrc.getProject(), base)) {
        for (PatchSet ps : cd.patchSets()) {
            if (!ps.getRevision().matches(base)) {
                continue;
            }
            if (ret == null || ret.patchSet().getId().get() < ps.getId().get()) {
                ret = Base.create(rsrc.getControl().getProjectControl().controlFor(cd.notes()), ps);
            }
        }
    }
    return ret;
}
Also used : ChangeControl(com.google.gerrit.server.project.ChangeControl) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 100 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class Revisions method loadEdit.

private List<RevisionResource> loadEdit(ChangeResource change, RevId revid) throws AuthException, IOException, OrmException {
    Optional<ChangeEdit> edit = editUtil.byChange(change.getChange());
    if (edit.isPresent()) {
        PatchSet ps = new PatchSet(new PatchSet.Id(change.getId(), 0));
        RevId editRevId = new RevId(ObjectId.toString(edit.get().getEditCommit()));
        ps.setRevision(editRevId);
        if (revid == null || editRevId.equals(revid)) {
            return Collections.singletonList(new RevisionResource(change, ps, edit));
        }
    }
    return Collections.emptyList();
}
Also used : ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) RevId(com.google.gerrit.reviewdb.client.RevId)

Aggregations

PatchSet (com.google.gerrit.reviewdb.client.PatchSet)124 Change (com.google.gerrit.reviewdb.client.Change)51 Test (org.junit.Test)44 ObjectId (org.eclipse.jgit.lib.ObjectId)35 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)27 RevCommit (org.eclipse.jgit.revwalk.RevCommit)26 Repository (org.eclipse.jgit.lib.Repository)21 RevId (com.google.gerrit.reviewdb.client.RevId)20 ChangeControl (com.google.gerrit.server.project.ChangeControl)20 ChangeData (com.google.gerrit.server.query.change.ChangeData)19 OrmException (com.google.gwtorm.server.OrmException)19 Timestamp (java.sql.Timestamp)18 RevWalk (org.eclipse.jgit.revwalk.RevWalk)15 Project (com.google.gerrit.reviewdb.client.Project)14 PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)11 TestChanges.newPatchSet (com.google.gerrit.testutil.TestChanges.newPatchSet)11 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)10 Account (com.google.gerrit.reviewdb.client.Account)10