Replace non-trivial uses of RefDatabase.getRefs with getRefsByPrefix

getRefs returns Map<String, Ref> where the key is the ref name with the
given prefix stripped off. However, getRefsByPrefix returns a list of
Refs with the full refname.

Therefore, when converting to getRefsByPrefix, we have to explicitly
remove the ref prefix from each ref name before using it.

Change-Id: I9a3ecf942ea47325fffa27596265fde217c2af0c
This commit is contained in:
David Pursehouse
2018-06-02 19:08:28 +09:00
parent f717f06986
commit 8d36f77af5
3 changed files with 16 additions and 12 deletions

View File

@@ -296,7 +296,11 @@ public class StarredChangesUtil {
private static Set<String> getRefNames(Repository repo, String prefix) throws IOException { private static Set<String> getRefNames(Repository repo, String prefix) throws IOException {
RefDatabase refDb = repo.getRefDatabase(); RefDatabase refDb = repo.getRefDatabase();
return refDb.getRefs(prefix).keySet(); return refDb
.getRefsByPrefix(prefix)
.stream()
.map(r -> r.getName().substring(prefix.length()))
.collect(toSet());
} }
public ObjectId getObjectId(Account.Id accountId, Change.Id changeId) { public ObjectId getObjectId(Account.Id accountId, Change.Id changeId) {

View File

@@ -1042,12 +1042,12 @@ public class ChangeData {
editsByUser = new HashMap<>(); editsByUser = new HashMap<>();
Change.Id id = checkNotNull(change.getId()); Change.Id id = checkNotNull(change.getId());
try (Repository repo = repoManager.openRepository(project())) { try (Repository repo = repoManager.openRepository(project())) {
for (Map.Entry<String, Ref> e : for (Ref ref : repo.getRefDatabase().getRefsByPrefix(RefNames.REFS_USERS)) {
repo.getRefDatabase().getRefs(RefNames.REFS_USERS).entrySet()) { String name = ref.getName().substring(RefNames.REFS_USERS.length());
if (id.equals(Change.Id.fromEditRefPart(e.getKey()))) { if (id.equals(Change.Id.fromEditRefPart(name))) {
Account.Id accountId = Account.Id.fromRefPart(e.getKey()); Account.Id accountId = Account.Id.fromRefPart(name);
if (accountId != null) { if (accountId != null) {
editsByUser.put(accountId, e.getValue()); editsByUser.put(accountId, ref);
} }
} }
} }

View File

@@ -16,12 +16,11 @@ package com.google.gerrit.server.update;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.stream.Collectors.toMap;
import com.google.common.collect.Maps;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.git.GitRepositoryManager;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Config;
@@ -133,14 +132,15 @@ public class RepoView {
* *
* @param prefix ref prefix; must end in '/' or else be empty. * @param prefix ref prefix; must end in '/' or else be empty.
* @return a map of ref suffixes to SHA-1s. The refs are all under {@code prefix} and have the * @return a map of ref suffixes to SHA-1s. The refs are all under {@code prefix} and have the
* prefix stripped; this matches the behavior of {@link * prefix stripped.
* org.eclipse.jgit.lib.RefDatabase#getRefs(String)}.
* @throws IOException if an error occurred. * @throws IOException if an error occurred.
*/ */
public Map<String, ObjectId> getRefs(String prefix) throws IOException { public Map<String, ObjectId> getRefs(String prefix) throws IOException {
Map<String, ObjectId> result = Map<String, ObjectId> result =
new HashMap<>( repo.getRefDatabase()
Maps.transformValues(repo.getRefDatabase().getRefs(prefix), Ref::getObjectId)); .getRefsByPrefix(prefix)
.stream()
.collect(toMap(r -> r.getName().substring(prefix.length()), Ref::getObjectId));
// First, overwrite any cached reads from the underlying RepoRefCache. If any of these differ, // First, overwrite any cached reads from the underlying RepoRefCache. If any of these differ,
// it's because a ref was updated after the RepoRefCache read it. It feels a little odd to // it's because a ref was updated after the RepoRefCache read it. It feels a little odd to