|
14 | 14 |
|
15 | 15 | package com.google.gitiles;
|
16 | 16 |
|
| 17 | +import static com.google.common.base.Preconditions.checkArgument; |
17 | 18 | import static com.google.common.base.Preconditions.checkNotNull;
|
18 | 19 | import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
|
19 | 20 |
|
| 21 | +import com.google.common.collect.ImmutableList; |
20 | 22 | import com.google.common.collect.ImmutableMap;
|
21 | 23 | import com.google.common.collect.Lists;
|
22 | 24 | import com.google.common.collect.Maps;
|
@@ -74,22 +76,21 @@ protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws
|
74 | 76 | @Override
|
75 | 77 | protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
|
76 | 78 | 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()); |
79 | 80 | TextRefAdvertiser adv = new TextRefAdvertiser(startRenderText(req, res));
|
80 | 81 | adv.setDerefTags(true);
|
81 |
| - adv.send(refs); |
| 82 | + adv.send(refs.refs); |
82 | 83 | adv.end();
|
83 | 84 | }
|
84 | 85 |
|
85 | 86 | @Override
|
86 | 87 | protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException {
|
87 | 88 | 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()); |
90 | 90 | 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)); |
93 | 94 | }
|
94 | 95 | renderJson(req, res, jsonRefs, new TypeToken<Map<String, RefJsonData>>() {}.getType());
|
95 | 96 | }
|
@@ -161,7 +162,8 @@ private static List<Map<String, Object>> getRefsSoyData(
|
161 | 162 | @Nullable Ref headLeaf,
|
162 | 163 | int limit)
|
163 | 164 | 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); |
165 | 167 | refs = ordering.leastOf(refs, limit > 0 ? Ints.saturatedCast(limit + 1L) : refs.size());
|
166 | 168 | List<Map<String, Object>> result = Lists.newArrayListWithCapacity(refs.size());
|
167 | 169 |
|
@@ -192,17 +194,28 @@ static String sanitizeRefForText(String refName) {
|
192 | 194 | return refName.replace("&", "&").replace("<", "<").replace(">", ">");
|
193 | 195 | }
|
194 | 196 |
|
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 { |
196 | 208 | path = GitilesView.maybeTrimLeadingAndTrailingSlash(path);
|
197 | 209 | if (path.isEmpty()) {
|
198 |
| - return refdb.getRefs(RefDatabase.ALL); |
| 210 | + return new RefsResult(path, refdb.getRefs()); |
199 | 211 | }
|
200 | 212 | path = Constants.R_REFS + path;
|
201 | 213 | Ref singleRef = refdb.exactRef(path);
|
202 | 214 | if (singleRef != null) {
|
203 |
| - return ImmutableMap.of(singleRef.getName(), singleRef); |
| 215 | + return new RefsResult(path, ImmutableList.of(singleRef)); |
204 | 216 | }
|
205 |
| - return refdb.getRefs(path + '/'); |
| 217 | + path = path + '/'; |
| 218 | + return new RefsResult(path, refdb.getRefsByPrefix(path)); |
206 | 219 | }
|
207 | 220 |
|
208 | 221 | private static class TextRefAdvertiser extends RefAdvertiser {
|
|
0 commit comments