Skip to content

Commit 55a49f4

Browse files
committed
RefServlet: Fix usage of deprecated RefDatabase#getRefs(String)
Change-Id: I088ef4480f431b0d971669177eefdbc9a41c5553
1 parent a750e87 commit 55a49f4

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

java/com/google/gitiles/RefServlet.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
package com.google.gitiles;
1616

17+
import static com.google.common.base.Preconditions.checkArgument;
1718
import static com.google.common.base.Preconditions.checkNotNull;
1819
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
1920

21+
import com.google.common.collect.ImmutableList;
2022
import com.google.common.collect.ImmutableMap;
2123
import com.google.common.collect.Lists;
2224
import com.google.common.collect.Maps;
@@ -74,22 +76,21 @@ protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws
7476
@Override
7577
protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
7678
GitilesView view = ViewFilter.getView(req);
77-
Map<String, Ref> refs =
78-
getRefs(ServletUtils.getRepository(req).getRefDatabase(), view.getPathPart());
79+
RefsResult refs = getRefs(ServletUtils.getRepository(req).getRefDatabase(), view.getPathPart());
7980
TextRefAdvertiser adv = new TextRefAdvertiser(startRenderText(req, res));
8081
adv.setDerefTags(true);
81-
adv.send(refs);
82+
adv.send(refs.refs);
8283
adv.end();
8384
}
8485

8586
@Override
8687
protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException {
8788
GitilesView view = ViewFilter.getView(req);
88-
Map<String, Ref> refs =
89-
getRefs(ServletUtils.getRepository(req).getRefDatabase(), view.getPathPart());
89+
RefsResult refs = getRefs(ServletUtils.getRepository(req).getRefDatabase(), view.getPathPart());
9090
Map<String, RefJsonData> jsonRefs = new LinkedHashMap<>();
91-
for (Map.Entry<String, Ref> ref : refs.entrySet()) {
92-
jsonRefs.put(ref.getKey(), new RefJsonData(ref.getValue()));
91+
int prefixLen = refs.prefix.length();
92+
for (Ref ref : refs.refs) {
93+
jsonRefs.put(ref.getName().substring(prefixLen), new RefJsonData(ref));
9394
}
9495
renderJson(req, res, jsonRefs, new TypeToken<Map<String, RefJsonData>>() {}.getType());
9596
}
@@ -161,7 +162,8 @@ private static List<Map<String, Object>> getRefsSoyData(
161162
@Nullable Ref headLeaf,
162163
int limit)
163164
throws IOException {
164-
Collection<Ref> refs = refdb.getRefs(prefix).values();
165+
checkArgument(prefix.endsWith("/"), "ref hierarchy prefix should end with /: %s", prefix);
166+
Collection<Ref> refs = refdb.getRefsByPrefix(prefix);
165167
refs = ordering.leastOf(refs, limit > 0 ? Ints.saturatedCast(limit + 1L) : refs.size());
166168
List<Map<String, Object>> result = Lists.newArrayListWithCapacity(refs.size());
167169

@@ -192,17 +194,28 @@ static String sanitizeRefForText(String refName) {
192194
return refName.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
193195
}
194196

195-
private static Map<String, Ref> getRefs(RefDatabase refdb, String path) throws IOException {
197+
private static class RefsResult {
198+
String prefix;
199+
List<Ref> refs;
200+
201+
RefsResult(String prefix, List<Ref> refs) {
202+
this.prefix = prefix;
203+
this.refs = refs;
204+
}
205+
}
206+
207+
private static RefsResult getRefs(RefDatabase refdb, String path) throws IOException {
196208
path = GitilesView.maybeTrimLeadingAndTrailingSlash(path);
197209
if (path.isEmpty()) {
198-
return refdb.getRefs(RefDatabase.ALL);
210+
return new RefsResult(path, refdb.getRefs());
199211
}
200212
path = Constants.R_REFS + path;
201213
Ref singleRef = refdb.exactRef(path);
202214
if (singleRef != null) {
203-
return ImmutableMap.of(singleRef.getName(), singleRef);
215+
return new RefsResult(path, ImmutableList.of(singleRef));
204216
}
205-
return refdb.getRefs(path + '/');
217+
path = path + '/';
218+
return new RefsResult(path, refdb.getRefsByPrefix(path));
206219
}
207220

208221
private static class TextRefAdvertiser extends RefAdvertiser {

0 commit comments

Comments
 (0)