diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/change/Revisions.java b/gerrit-server/src/main/java/com/google/gerrit/server/change/Revisions.java index ac44a9d314..dc5e445b13 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/change/Revisions.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/change/Revisions.java @@ -33,6 +33,7 @@ import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.server.edit.ChangeEdit; import com.google.gerrit.server.edit.ChangeEditUtil; import com.google.gwtorm.server.OrmException; +import com.google.gwtorm.server.ResultSet; import com.google.inject.Inject; import com.google.inject.Provider; import com.google.inject.Singleton; @@ -77,11 +78,10 @@ public class Revisions implements ChildCollection match = Lists.newArrayListWithExpectedSize(2); for (RevisionResource rsrc : find(change, id.get())) { - Change.Id changeId = rsrc.getChange().getId(); - if (changeId.equals(change.getChange().getId()) - && visible(change, rsrc.getPatchSet())) { + if (visible(change, rsrc.getPatchSet())) { match.add(rsrc); } } @@ -104,19 +104,11 @@ public class Revisions implements ChildCollection find(ChangeResource change, String id) throws OrmException { - ReviewDb db = dbProvider.get(); - if (id.equals("0")) { return loadEdit(change, null); } else if (id.length() < 6 && id.matches("^[1-9][0-9]{0,4}$")) { // Legacy patch set number syntax. - PatchSet ps = dbProvider.get().patchSets().get(new PatchSet.Id( - change.getChange().getId(), - Integer.parseInt(id))); - if (ps != null) { - return toResources(change, ps); - } - return Collections.emptyList(); + return byLegacyPatchSetId(change, id); } else if (id.length() < 4 || id.length() > RevId.LEN) { // Require a minimum of 4 digits. // Impossibly long identifier will never match. @@ -128,20 +120,15 @@ public class Revisions implements ChildCollection list = - toResources(change, db.patchSets().byRevision(revid)); - if (list.isEmpty()) { - return loadEdit(change, revid); - } - return list; - } else { - return toResources( - change, db.patchSets().byRevisionRange(revid, revid.max())); + List m = toResources(change, findExactMatch(revid)); + return m.isEmpty() ? loadEdit(change, revid) : m; } + return toResources(change, findByPrefix(revid)); } else { // Chance of collision rises; look at all patch sets on the change. List out = Lists.newArrayList(); - for (PatchSet ps : db.patchSets().byChange(change.getChange().getId())) { + for (PatchSet ps : dbProvider.get().patchSets() + .byChange(change.getChange().getId())) { if (ps.getRevision() != null && ps.getRevision().get().startsWith(id)) { out.add(new RevisionResource(change, ps)); } @@ -150,6 +137,25 @@ public class Revisions implements ChildCollection byLegacyPatchSetId(ChangeResource change, + String id) throws OrmException { + PatchSet ps = dbProvider.get().patchSets().get(new PatchSet.Id( + change.getChange().getId(), + Integer.parseInt(id))); + if (ps != null) { + return Collections.singletonList(new RevisionResource(change, ps)); + } + return Collections.emptyList(); + } + + private ResultSet findExactMatch(RevId revid) throws OrmException { + return dbProvider.get().patchSets().byRevision(revid); + } + + private ResultSet findByPrefix(RevId revid) throws OrmException { + return dbProvider.get().patchSets().byRevisionRange(revid, revid.max()); + } + private List loadEdit(ChangeResource change, RevId revid) throws OrmException { try { @@ -163,10 +169,10 @@ public class Revisions implements ChildCollection toResources(final ChangeResource change, @@ -185,9 +191,4 @@ public class Revisions implements ChildCollection toResources(ChangeResource change, - PatchSet ps) { - return Collections.singletonList(new RevisionResource(change, ps)); - } }