ChangeUtil: Avoid RefDatabase#getRefs
Change-Id: I9c2ce8e233fc4e74f5ff738e0185c27fe79a4cc1
This commit is contained in:
@@ -15,8 +15,8 @@
|
||||
package com.google.gerrit.server;
|
||||
|
||||
import static java.util.Comparator.comparingInt;
|
||||
import static java.util.stream.Collectors.toSet;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Ordering;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
@@ -24,9 +24,11 @@ import com.google.gerrit.reviewdb.client.PatchSet;
|
||||
import com.google.inject.Singleton;
|
||||
import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
|
||||
@@ -66,18 +68,25 @@ public class ChangeUtil {
|
||||
/**
|
||||
* Get the next patch set ID from a previously-read map of refs below the change prefix.
|
||||
*
|
||||
* @param changeRefs map of ref suffix to SHA-1, where the keys are ref names with the {@code
|
||||
* refs/changes/CD/ABCD/} prefix stripped. All refs should be under {@code id}'s change ref
|
||||
* prefix.
|
||||
* @param changeRefNames existing full change ref names with the same change ID as {@code id}.
|
||||
* @param id previous patch set ID.
|
||||
* @return next unused patch set ID for the same change, skipping any IDs whose corresponding ref
|
||||
* names appear in the {@code changeRefs} map.
|
||||
*/
|
||||
public static PatchSet.Id nextPatchSetIdFromChangeRefsMap(
|
||||
Map<String, ObjectId> changeRefs, PatchSet.Id id) {
|
||||
int prefixLen = id.getParentKey().toRefPrefix().length();
|
||||
public static PatchSet.Id nextPatchSetIdFromChangeRefs(
|
||||
Collection<String> changeRefNames, PatchSet.Id id) {
|
||||
return nextPatchSetIdFromChangeRefs(changeRefNames.stream(), id);
|
||||
}
|
||||
|
||||
private static PatchSet.Id nextPatchSetIdFromChangeRefs(
|
||||
Stream<String> changeRefNames, PatchSet.Id id) {
|
||||
Set<PatchSet.Id> existing =
|
||||
changeRefNames
|
||||
.map(PatchSet.Id::fromRef)
|
||||
.filter(psId -> psId != null && psId.getParentKey().equals(id.getParentKey()))
|
||||
.collect(toSet());
|
||||
PatchSet.Id next = nextPatchSetId(id);
|
||||
while (changeRefs.containsKey(next.toRefName().substring(prefixLen))) {
|
||||
while (existing.contains(next)) {
|
||||
next = nextPatchSetId(next);
|
||||
}
|
||||
return next;
|
||||
@@ -88,7 +97,7 @@ public class ChangeUtil {
|
||||
*
|
||||
* <p>This patch set ID may or may not be available in the database; callers that want a
|
||||
* previously-unused ID should use {@link #nextPatchSetIdFromAllRefsMap} or {@link
|
||||
* #nextPatchSetIdFromChangeRefsMap}.
|
||||
* #nextPatchSetIdFromChangeRefs}.
|
||||
*
|
||||
* @param id previous patch set ID.
|
||||
* @return next patch set ID for the same change, incrementing by 1.
|
||||
@@ -106,9 +115,11 @@ public class ChangeUtil {
|
||||
* names appear in the repository.
|
||||
*/
|
||||
public static PatchSet.Id nextPatchSetId(Repository git, PatchSet.Id id) throws IOException {
|
||||
return nextPatchSetIdFromChangeRefsMap(
|
||||
Maps.transformValues(
|
||||
git.getRefDatabase().getRefs(id.getParentKey().toRefPrefix()), Ref::getObjectId),
|
||||
return nextPatchSetIdFromChangeRefs(
|
||||
git.getRefDatabase()
|
||||
.getRefsByPrefix(id.getParentKey().toRefPrefix())
|
||||
.stream()
|
||||
.map(Ref::getName),
|
||||
id);
|
||||
}
|
||||
|
||||
|
||||
@@ -174,8 +174,10 @@ public class RebaseChangeOp implements BatchUpdateOp {
|
||||
baseCommitId.name());
|
||||
|
||||
rebasedPatchSetId =
|
||||
ChangeUtil.nextPatchSetIdFromChangeRefsMap(
|
||||
ctx.getRepoView().getRefs(originalPatchSet.getId().getParentKey().toRefPrefix()),
|
||||
ChangeUtil.nextPatchSetIdFromChangeRefs(
|
||||
ctx.getRepoView()
|
||||
.getRefs(originalPatchSet.getId().getParentKey().toRefPrefix())
|
||||
.keySet(),
|
||||
notes.getChange().currentPatchSetId());
|
||||
patchSetInserter =
|
||||
patchSetInserterFactory
|
||||
|
||||
@@ -97,8 +97,8 @@ public class CherryPick extends SubmitStrategy {
|
||||
// tip.
|
||||
args.rw.parseBody(toMerge);
|
||||
psId =
|
||||
ChangeUtil.nextPatchSetIdFromChangeRefsMap(
|
||||
ctx.getRepoView().getRefs(getId().toRefPrefix()),
|
||||
ChangeUtil.nextPatchSetIdFromChangeRefs(
|
||||
ctx.getRepoView().getRefs(getId().toRefPrefix()).keySet(),
|
||||
toMerge.change().currentPatchSetId());
|
||||
RevCommit mergeTip = args.mergeTip.getCurrentTip();
|
||||
args.rw.parseBody(mergeTip);
|
||||
|
||||
@@ -137,8 +137,8 @@ public class RebaseSubmitStrategy extends SubmitStrategy {
|
||||
// RebaseAlways means we modify commit message.
|
||||
args.rw.parseBody(toMerge);
|
||||
newPatchSetId =
|
||||
ChangeUtil.nextPatchSetIdFromChangeRefsMap(
|
||||
ctx.getRepoView().getRefs(getId().toRefPrefix()),
|
||||
ChangeUtil.nextPatchSetIdFromChangeRefs(
|
||||
ctx.getRepoView().getRefs(getId().toRefPrefix()).keySet(),
|
||||
toMerge.change().currentPatchSetId());
|
||||
RevCommit mergeTip = args.mergeTip.getCurrentTip();
|
||||
args.rw.parseBody(mergeTip);
|
||||
|
||||
Reference in New Issue
Block a user