Fix trivial usages of deprecated RefDatabase.getRefs method

getRefs(String) returns Map<String, Ref>; most callers only call its
values() method and iterate over the list of Ref objects. It's trivial
to replace those calls with getRefsByPrefix which returns a List<Ref>.

Change-Id: Iad142ea8a0dec69b22fcec084bb66eafd79c2a0d
This commit is contained in:
David Pursehouse
2018-06-01 19:55:16 +09:00
parent 895d678b4f
commit f717f06986
26 changed files with 34 additions and 40 deletions

View File

@@ -498,7 +498,7 @@ public class CommentsUtil {
}
private Collection<Ref> getDraftRefs(Repository repo, Change.Id changeId) throws IOException {
return repo.getRefDatabase().getRefs(RefNames.refsDraftCommentsPrefix(changeId)).values();
return repo.getRefDatabase().getRefsByPrefix(RefNames.refsDraftCommentsPrefix(changeId));
}
private static <T extends Comment> List<T> sort(List<T> comments) {

View File

@@ -139,8 +139,7 @@ public class Accounts {
public static Stream<Account.Id> readUserRefs(Repository repo) throws IOException {
return repo.getRefDatabase()
.getRefs(RefNames.REFS_USERS)
.values()
.getRefsByPrefix(RefNames.REFS_USERS)
.stream()
.map(r -> Account.Id.fromRef(r.getName()))
.filter(Objects::nonNull);

View File

@@ -404,7 +404,7 @@ public class ConsistencyChecker {
}
List<PatchSet.Id> thisCommitPsIds = new ArrayList<>();
for (Ref ref : repo.getRefDatabase().getRefs(REFS_CHANGES).values()) {
for (Ref ref : repo.getRefDatabase().getRefsByPrefix(REFS_CHANGES)) {
if (!ref.getObjectId().equals(commit)) {
continue;
}

View File

@@ -85,8 +85,8 @@ public class IncludedInResolver {
private Result resolve() throws IOException {
RefDatabase refDb = repo.getRefDatabase();
Collection<Ref> tags = refDb.getRefs(Constants.R_TAGS).values();
Collection<Ref> branches = refDb.getRefs(Constants.R_HEADS).values();
Collection<Ref> tags = refDb.getRefsByPrefix(Constants.R_TAGS);
Collection<Ref> branches = refDb.getRefsByPrefix(Constants.R_HEADS);
List<Ref> allTagsAndBranches = Lists.newArrayListWithCapacity(tags.size() + branches.size());
allTagsAndBranches.addAll(tags);
allTagsAndBranches.addAll(branches);

View File

@@ -34,7 +34,6 @@ import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectIdOwnerMap;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevSort;
@@ -157,7 +156,7 @@ class TagSet {
try (TagWalk rw = new TagWalk(git)) {
rw.setRetainBody(false);
for (Ref ref : git.getRefDatabase().getRefs(RefDatabase.ALL).values()) {
for (Ref ref : git.getRefDatabase().getRefs()) {
if (skip(ref)) {
continue;

View File

@@ -2468,7 +2468,7 @@ class ReceiveCommits {
return false;
}
for (Ref r : rp.getRepository().getRefDatabase().getRefs("refs/changes").values()) {
for (Ref r : rp.getRepository().getRefDatabase().getRefsByPrefix("refs/changes")) {
if (r.getObjectId().equals(newCommit)) {
reject(inputCommand, "commit already exists (in the project)");
return false;

View File

@@ -136,8 +136,7 @@ public class AllChangesIndexer extends SiteIndexer<Change.Id, ChangeData, Change
// refs may exist for changes whose metadata was never successfully stored. But that's ok, as
// the estimate is just used as a heuristic for sorting projects.
return repo.getRefDatabase()
.getRefs(RefNames.REFS_CHANGES)
.values()
.getRefsByPrefix(RefNames.REFS_CHANGES)
.stream()
.map(r -> Change.Id.fromRef(r.getName()))
.filter(Objects::nonNull)

View File

@@ -247,7 +247,7 @@ public class StalenessChecker {
}
private boolean match(Repository repo, Set<RefState> expected) throws IOException {
for (Ref r : repo.getRefDatabase().getRefs(prefix()).values()) {
for (Ref r : repo.getRefDatabase().getRefsByPrefix(prefix())) {
if (!match(r.getName())) {
continue;
}

View File

@@ -439,7 +439,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
private static ScanResult scanChangeIds(Repository repo) throws IOException {
ImmutableSet.Builder<Change.Id> fromPs = ImmutableSet.builder();
ImmutableSet.Builder<Change.Id> fromMeta = ImmutableSet.builder();
for (Ref r : repo.getRefDatabase().getRefs(RefNames.REFS_CHANGES).values()) {
for (Ref r : repo.getRefDatabase().getRefsByPrefix(RefNames.REFS_CHANGES)) {
Change.Id id = Change.Id.fromRef(r.getName());
if (id != null) {
(r.getName().endsWith(RefNames.META_SUFFIX) ? fromMeta : fromPs).add(id);

View File

@@ -724,7 +724,7 @@ public class NoteDbUpdateManager implements AutoCloseable {
// Just scan repo for ref names, but get "old" values from cmds.
for (Ref r :
allUsersRepo.repo.getRefDatabase().getRefs(RefNames.refsDraftCommentsPrefix(id)).values()) {
allUsersRepo.repo.getRefDatabase().getRefsByPrefix(RefNames.refsDraftCommentsPrefix(id))) {
old = allUsersRepo.cmds.get(r.getName());
if (old.isPresent()) {
allUsersRepo.cmds.add(new ReceiveCommand(old.get(), ObjectId.zeroId(), r.getName()));

View File

@@ -433,7 +433,7 @@ public class PrimaryStorageMigrator {
ImmutableMap.Builder<Account.Id, ObjectId> draftIds = ImmutableMap.builder();
try (Repository repo = repoManager.openRepository(allUsers)) {
for (Ref draftRef :
repo.getRefDatabase().getRefs(RefNames.refsDraftCommentsPrefix(id)).values()) {
repo.getRefDatabase().getRefsByPrefix(RefNames.refsDraftCommentsPrefix(id))) {
Account.Id accountId = Account.Id.fromRef(draftRef.getName());
if (accountId != null) {
draftIds.put(accountId, draftRef.getObjectId().copy());

View File

@@ -620,8 +620,7 @@ public class ChangeRebuilderImpl extends ChangeRebuilder {
allUsersRepo
.repo
.getRefDatabase()
.getRefs(RefNames.refsDraftCommentsPrefix(change.getId()))
.values()) {
.getRefsByPrefix(RefNames.refsDraftCommentsPrefix(change.getId()))) {
allUsersRepo.cmds.add(new ReceiveCommand(r.getObjectId(), ObjectId.zeroId(), r.getName()));
}
}

View File

@@ -70,8 +70,8 @@ public class Reachable {
boolean fromHeadsOrTags(Project.NameKey project, Repository repo, RevCommit commit) {
try {
RefDatabase refdb = repo.getRefDatabase();
Collection<Ref> heads = refdb.getRefs(Constants.R_HEADS).values();
Collection<Ref> tags = refdb.getRefs(Constants.R_TAGS).values();
Collection<Ref> heads = refdb.getRefsByPrefix(Constants.R_HEADS);
Collection<Ref> tags = refdb.getRefsByPrefix(Constants.R_TAGS);
Map<String, Ref> refs = Maps.newHashMapWithExpectedSize(heads.size() + tags.size());
for (Ref r : Iterables.concat(heads, tags)) {
refs.put(r.getName(), r);

View File

@@ -71,7 +71,7 @@ public class RefUtil {
RefDatabase refDb = repo.getRefDatabase();
Iterable<Ref> refs =
Iterables.concat(
refDb.getRefs(Constants.R_HEADS).values(), refDb.getRefs(Constants.R_TAGS).values());
refDb.getRefsByPrefix(Constants.R_HEADS), refDb.getRefsByPrefix(Constants.R_TAGS));
Ref rc = refDb.exactRef(RefNames.REFS_CONFIG);
if (rc != null) {
refs = Iterables.concat(refs, Collections.singleton(rc));

View File

@@ -184,7 +184,7 @@ public class InternalChangeQuery extends InternalQuery<ChangeData> {
throws OrmException, IOException {
Set<Change.Id> changeIds = Sets.newHashSetWithExpectedSize(hashes.size());
String lastPrefix = null;
for (Ref ref : repo.getRefDatabase().getRefs(RefNames.REFS_CHANGES).values()) {
for (Ref ref : repo.getRefDatabase().getRefsByPrefix(RefNames.REFS_CHANGES)) {
String r = ref.getName();
if ((lastPrefix != null && r.startsWith(lastPrefix))
|| !hashes.contains(ref.getObjectId().name())) {

View File

@@ -137,7 +137,7 @@ public class CheckAccess implements RestModifyView<ProjectResource, AccessCheckI
// We say access is okay if there are no refs, but this warrants a warning,
// as access denied looks the same as no branches to the user.
try (Repository repo = gitRepositoryManager.openRepository(rsrc.getNameKey())) {
if (repo.getRefDatabase().getRefs(REFS_HEADS).isEmpty()) {
if (repo.getRefDatabase().getRefsByPrefix(REFS_HEADS).isEmpty()) {
info.message = "access is OK, but repository has no branches under refs/heads/";
}
}

View File

@@ -155,7 +155,7 @@ public class ListBranches implements RestReadView<ProjectResource> {
throws IOException, ResourceNotFoundException, PermissionBackendException {
List<Ref> refs;
try (Repository db = repoManager.openRepository(rsrc.getNameKey())) {
Collection<Ref> heads = db.getRefDatabase().getRefs(Constants.R_HEADS).values();
Collection<Ref> heads = db.getRefDatabase().getRefsByPrefix(Constants.R_HEADS);
refs = new ArrayList<>(heads.size() + 3);
refs.addAll(heads);
refs.addAll(

View File

@@ -103,7 +103,7 @@ public class ListDashboards implements RestReadView<ProjectResource> {
try (Repository git = gitManager.openRepository(state.getNameKey());
RevWalk rw = new RevWalk(git)) {
List<DashboardInfo> all = new ArrayList<>();
for (Ref ref : git.getRefDatabase().getRefs(REFS_DASHBOARDS).values()) {
for (Ref ref : git.getRefDatabase().getRefsByPrefix(REFS_DASHBOARDS)) {
if (perm.ref(ref.getName()).test(RefPermission.READ) && state.statePermitsRead()) {
all.addAll(scanDashboards(state.getProject(), git, rw, ref, project, setDefault));
}

View File

@@ -89,7 +89,7 @@ public class Schema_108 extends SchemaVersion {
rw.sort(RevSort.REVERSE, true);
RefDatabase refdb = repo.getRefDatabase();
for (Ref ref : refdb.getRefs(Constants.R_HEADS).values()) {
for (Ref ref : refdb.getRefsByPrefix(Constants.R_HEADS)) {
RevCommit c = maybeParseCommit(rw, ref.getObjectId(), ui);
if (c != null) {
rw.markUninteresting(c);
@@ -100,7 +100,7 @@ public class Schema_108 extends SchemaVersion {
MultimapBuilder.hashKeys().arrayListValues().build();
ListMultimap<ObjectId, PatchSet.Id> patchSetsBySha =
MultimapBuilder.hashKeys().arrayListValues().build();
for (Ref ref : refdb.getRefs(RefNames.REFS_CHANGES).values()) {
for (Ref ref : refdb.getRefsByPrefix(RefNames.REFS_CHANGES)) {
ObjectId id = ref.getObjectId();
if (ref.getObjectId() == null) {
continue;

View File

@@ -56,8 +56,7 @@ public class Schema_147 extends SchemaVersion {
Set<Account.Id> accountIdsFromReviewDb = scanAccounts(db);
Set<Account.Id> accountIdsFromUserBranches =
repo.getRefDatabase()
.getRefs(RefNames.REFS_USERS)
.values()
.getRefsByPrefix(RefNames.REFS_USERS)
.stream()
.map(r -> Account.Id.fromRef(r.getName()))
.filter(Objects::nonNull)

View File

@@ -60,7 +60,7 @@ public class Schema_161 extends SchemaVersion {
BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
bru.setAllowNonFastForwards(true);
for (Ref ref : git.getRefDatabase().getRefs(RefNames.REFS_STARRED_CHANGES).values()) {
for (Ref ref : git.getRefDatabase().getRefsByPrefix(RefNames.REFS_STARRED_CHANGES)) {
StarRef starRef = StarredChangesUtil.readLabels(git, ref.getName());
Set<Integer> mutedPatchSets =

View File

@@ -693,7 +693,7 @@ public class MergeOp implements AutoCloseable {
}
try {
for (Ref r : or.repo.getRefDatabase().getRefs(Constants.R_HEADS).values()) {
for (Ref r : or.repo.getRefDatabase().getRefsByPrefix(Constants.R_HEADS)) {
try {
CodeReviewCommit aac = or.rw.parseCommit(r.getObjectId());
if (!commitStatus.commits.values().contains(aac)) {

View File

@@ -63,8 +63,8 @@ public class SubmitDryRun {
public static Set<ObjectId> getAlreadyAccepted(Repository repo) throws IOException {
return Streams.concat(
repo.getRefDatabase().getRefs(Constants.R_HEADS).values().stream(),
repo.getRefDatabase().getRefs(Constants.R_TAGS).values().stream())
repo.getRefDatabase().getRefsByPrefix(Constants.R_HEADS).stream(),
repo.getRefDatabase().getRefsByPrefix(Constants.R_TAGS).stream())
.map(Ref::getObjectId)
.filter(Objects::nonNull)
.collect(toSet());

View File

@@ -309,7 +309,7 @@ public class SubmoduleOp {
continue;
}
for (Ref ref : or.repo.getRefDatabase().getRefs(RefNames.REFS_HEADS).values()) {
for (Ref ref : or.repo.getRefDatabase().getRefsByPrefix(RefNames.REFS_HEADS)) {
if (r.getDestination() != null && !r.matchDestination(ref.getName())) {
continue;
}

View File

@@ -18,7 +18,7 @@ import com.google.common.flogger.FluentLogger;
import com.google.gerrit.common.data.Capable;
import com.google.gerrit.reviewdb.client.Project;
import java.io.IOException;
import java.util.Map;
import java.util.List;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
@@ -89,9 +89,9 @@ public final class MagicBranch {
}
private static Capable checkMagicBranchRef(String branchName, Repository repo, Project project) {
Map<String, Ref> blockingFors;
List<Ref> blockingFors;
try {
blockingFors = repo.getRefDatabase().getRefs(branchName);
blockingFors = repo.getRefDatabase().getRefsByPrefix(branchName);
} catch (IOException err) {
String projName = project.getName();
logger.atWarning().withCause(err).log("Cannot scan refs in '%s'", projName);
@@ -101,7 +101,7 @@ public final class MagicBranch {
String projName = project.getName();
logger.atSevere().log(
"Repository '%s' needs the following refs removed to receive changes: %s",
projName, blockingFors.keySet());
projName, blockingFors);
return new Capable("One or more " + branchName + " names blocks change upload");
}

View File

@@ -38,7 +38,6 @@ import java.util.Map;
import org.eclipse.jgit.errors.TooLargeObjectInPackException;
import org.eclipse.jgit.errors.UnpackException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.transport.AdvertiseRefsHook;
import org.eclipse.jgit.transport.ReceivePack;
import org.kohsuke.args4j.Option;
@@ -148,9 +147,9 @@ final class Receive extends AbstractGitCommand {
.append("\n");
}
Map<String, Ref> allRefs = rp.getRepository().getRefDatabase().getRefs(RefDatabase.ALL);
List<Ref> allRefs = rp.getRepository().getRefDatabase().getRefs();
List<Ref> hidden = new ArrayList<>();
for (Ref ref : allRefs.values()) {
for (Ref ref : allRefs) {
if (!adv.containsKey(ref.getName())) {
hidden.add(ref);
}