-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRefServlet.java
More file actions
254 lines (225 loc) · 8.39 KB
/
RefServlet.java
File metadata and controls
254 lines (225 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gitiles;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import com.google.common.primitives.Ints;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.gitiles.GitilesRequestFailureException.FailureReason;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jgit.http.server.ServletUtils;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefComparator;
import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.RefAdvertiser;
/** Serves an HTML page with all the refs in a repository. */
public class RefServlet extends BaseServlet {
private static final long serialVersionUID = 1L;
private final TimeCache timeCache;
protected RefServlet(
GitilesAccess.Factory accessFactory, Renderer renderer, TimeCache timeCache) {
super(renderer, accessFactory);
this.timeCache = checkNotNull(timeCache, "timeCache");
}
@Override
protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
if (!ViewFilter.getView(req).getPathPart().isEmpty()) {
throw new GitilesRequestFailureException(FailureReason.INCORECT_PARAMETER);
}
List<Map<String, Object>> tags;
try (RevWalk walk = new RevWalk(ServletUtils.getRepository(req))) {
tags = getTagsSoyData(req, timeCache, walk, 0);
}
renderHtml(
req,
res,
"com.google.gitiles.templates.RefList.refsDetail",
ImmutableMap.of("branches", getBranchesSoyData(req, 0), "tags", tags));
}
@Override
protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
GitilesView view = ViewFilter.getView(req);
RefsResult refs = getRefs(ServletUtils.getRepository(req).getRefDatabase(), view.getPathPart());
TextRefAdvertiser adv = new TextRefAdvertiser(startRenderText(req, res));
adv.setDerefTags(true);
adv.send(refs.refs);
adv.end();
}
@Override
protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException {
GitilesView view = ViewFilter.getView(req);
RefsResult refs = getRefs(ServletUtils.getRepository(req).getRefDatabase(), view.getPathPart());
Map<String, RefJsonData> jsonRefs = new LinkedHashMap<>();
int prefixLen = refs.prefix.length();
for (Ref ref : refs.refs) {
jsonRefs.put(ref.getName().substring(prefixLen), new RefJsonData(ref));
}
renderJson(req, res, jsonRefs, new TypeToken<Map<String, RefJsonData>>() {}.getType());
}
static List<Map<String, Object>> getBranchesSoyData(HttpServletRequest req, int limit)
throws IOException {
RefDatabase refdb = ServletUtils.getRepository(req).getRefDatabase();
Ref head = refdb.exactRef(Constants.HEAD);
Ref headLeaf = head != null && head.isSymbolic() ? head.getLeaf() : null;
return getRefsSoyData(
refdb,
ViewFilter.getView(req),
Constants.R_HEADS,
branchComparator(headLeaf),
headLeaf,
limit);
}
private static Ordering<Ref> branchComparator(Ref headLeaf) {
if (headLeaf == null) {
return Ordering.from(RefComparator.INSTANCE);
}
final String headLeafName = headLeaf.getName();
return new Ordering<Ref>() {
@Override
public int compare(@Nullable Ref left, @Nullable Ref right) {
int l = isHead(left) ? 1 : 0;
int r = isHead(right) ? 1 : 0;
return r - l;
}
private boolean isHead(Ref ref) {
return ref != null && ref.getName().equals(headLeafName);
}
}.compound(RefComparator.INSTANCE);
}
static List<Map<String, Object>> getTagsSoyData(
HttpServletRequest req, TimeCache timeCache, RevWalk walk, int limit) throws IOException {
return getRefsSoyData(
ServletUtils.getRepository(req).getRefDatabase(),
ViewFilter.getView(req),
Constants.R_TAGS,
tagComparator(timeCache, walk),
null,
limit);
}
private static Long getTime(RevWalk walk, TimeCache timeCache, Ref ref) {
try {
return timeCache.getTime(walk, ref.getObjectId());
} catch (IOException e) {
throw new UncheckedExecutionException(e);
}
}
private static Ordering<Ref> tagComparator(TimeCache timeCache, RevWalk walk) {
return Ordering.natural()
.onResultOf((Ref r) -> getTime(walk, timeCache, r))
.reverse()
.compound(RefComparator.INSTANCE);
}
private static List<Map<String, Object>> getRefsSoyData(
RefDatabase refdb,
GitilesView view,
String prefix,
Ordering<Ref> ordering,
@Nullable Ref headLeaf,
int limit)
throws IOException {
checkArgument(prefix.endsWith("/"), "ref hierarchy prefix should end with /: %s", prefix);
Collection<Ref> refs = refdb.getRefsByPrefix(prefix);
refs = ordering.leastOf(refs, limit > 0 ? Ints.saturatedCast(limit + 1L) : refs.size());
List<Map<String, Object>> result = Lists.newArrayListWithCapacity(refs.size());
for (Ref ref : refs) {
String name = ref.getName().substring(prefix.length());
Map<String, Object> value = Maps.newHashMapWithExpectedSize(3);
value.put(
"url",
GitilesView.revision()
.copyFrom(view)
.setRevision(Revision.unpeeled(ref.getName(), ref.getObjectId()))
.toUrl());
value.put("name", name);
if (headLeaf != null) {
value.put("isHead", headLeaf.equals(ref));
}
result.add(value);
}
return result;
}
static String sanitizeRefForText(String refName) {
return refName.replace("&", "&").replace("<", "<").replace(">", ">");
}
private static class RefsResult {
String prefix;
List<Ref> refs;
RefsResult(String prefix, List<Ref> refs) {
this.prefix = prefix;
this.refs = refs;
}
}
private static RefsResult getRefs(RefDatabase refdb, String path) throws IOException {
path = GitilesView.maybeTrimLeadingAndTrailingSlash(path);
if (path.isEmpty()) {
return new RefsResult(path, refdb.getRefs());
}
path = Constants.R_REFS + path;
Ref singleRef = refdb.exactRef(path);
if (singleRef != null) {
return new RefsResult("", ImmutableList.of(singleRef));
}
path = path + '/';
return new RefsResult(path, refdb.getRefsByPrefix(path));
}
private static class TextRefAdvertiser extends RefAdvertiser {
private final Writer writer;
private TextRefAdvertiser(Writer writer) {
this.writer = writer;
}
@Override
public void advertiseId(AnyObjectId id, String refName) throws IOException {
super.advertiseId(id, sanitizeRefForText(refName));
}
@Override
protected void writeOne(CharSequence line) throws IOException {
writer.append(line);
}
@Override
public void end() throws IOException {
writer.close();
}
}
static class RefJsonData {
RefJsonData(Ref ref) {
value = ref.getObjectId().getName();
if (ref.getPeeledObjectId() != null) {
peeled = ref.getPeeledObjectId().getName();
}
if (ref.isSymbolic()) {
target = ref.getTarget().getName();
}
}
String value;
String peeled;
String target;
}
}