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);
}
}
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");
}
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;
}
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;
}
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();
}
Aggregations