blob: 1e89986945dda129b3169911455629c82a5fda81 [file] [log] [blame]
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.sshd.commands;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.RevId;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.sshd.BaseCommand.UnloggedFailure;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.ResultSet;
import java.util.HashSet;
import java.util.Set;
public class CommandUtils {
public static PatchSet parsePatchSet(final String patchIdentity, ReviewDb db,
ProjectControl projectControl, String branch)
throws UnloggedFailure, OrmException {
// By commit?
//
if (patchIdentity.matches("^([0-9a-fA-F]{4," + RevId.LEN + "})$")) {
final RevId id = new RevId(patchIdentity);
final ResultSet<PatchSet> patches;
if (id.isComplete()) {
patches = db.patchSets().byRevision(id);
} else {
patches = db.patchSets().byRevisionRange(id, id.max());
}
final Set<PatchSet> matches = new HashSet<>();
for (final PatchSet ps : patches) {
final Change change = db.changes().get(ps.getId().getParentKey());
if (inProject(change, projectControl) && inBranch(change, branch)) {
matches.add(ps);
}
}
switch (matches.size()) {
case 1:
return matches.iterator().next();
case 0:
throw error("\"" + patchIdentity + "\" no such patch set");
default:
throw error("\"" + patchIdentity + "\" matches multiple patch sets");
}
}
// By older style change,patchset?
//
if (patchIdentity.matches("^[1-9][0-9]*,[1-9][0-9]*$")) {
final PatchSet.Id patchSetId;
try {
patchSetId = PatchSet.Id.parse(patchIdentity);
} catch (IllegalArgumentException e) {
throw error("\"" + patchIdentity + "\" is not a valid patch set");
}
final PatchSet patchSet = db.patchSets().get(patchSetId);
if (patchSet == null) {
throw error("\"" + patchIdentity + "\" no such patch set");
}
if (projectControl != null || branch != null) {
final Change change = db.changes().get(patchSetId.getParentKey());
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 "
+ change.getDest().get());
}
}
return patchSet;
}
throw error("\"" + patchIdentity + "\" is not a valid patch set");
}
private static boolean inProject(final Change change,
ProjectControl projectControl) {
if (projectControl == null) {
// No --project option, so they want every project.
return true;
}
return projectControl.getProject().getNameKey().equals(change.getProject());
}
private static boolean inBranch(final Change change, String branch) {
if (branch == null) {
// No --branch option, so they want every branch.
return true;
}
return change.getDest().get().equals(branch);
}
public static UnloggedFailure error(final String msg) {
return new UnloggedFailure(1, msg);
}
}