-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathLogServlet.java
More file actions
349 lines (313 loc) · 11.9 KB
/
LogServlet.java
File metadata and controls
349 lines (313 loc) · 11.9 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// 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.checkNotNull;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.primitives.Longs;
import com.google.gitiles.CommitData.Field;
import com.google.gitiles.DateFormatter.Format;
import com.google.gitiles.GitilesRequestFailureException.FailureReason;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jgit.diff.DiffConfig;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.http.server.ServletUtils;
import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.FollowFilter;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevSort;
import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.revwalk.filter.AndRevFilter;
import org.eclipse.jgit.revwalk.filter.RevFilter;
import org.eclipse.jgit.treewalk.filter.ChangedPathTreeFilter;
import org.eclipse.jgit.util.StringUtils;
/** Serves an HTML page with a shortlog for commits and paths. */
public class LogServlet extends BaseServlet {
private static final long serialVersionUID = 1L;
static final String LIMIT_PARAM = "n";
static final String START_PARAM = "s";
private static final String FOLLOW_PARAM = "follow";
private static final String NAME_STATUS_PARAM = "name-status";
private static final String PRETTY_PARAM = "pretty";
private static final String TOPO_ORDER_PARAM = "topo-order";
private static final String REVERSE_PARAM = "reverse";
private static final String FIRST_PARENT_PARAM = "first-parent";
private static final int DEFAULT_LIMIT = 100;
private static final int MAX_LIMIT = 10000;
private final Linkifier linkifier;
public LogServlet(GitilesAccess.Factory accessFactory, Renderer renderer, Linkifier linkifier) {
super(renderer, accessFactory);
this.linkifier = checkNotNull(linkifier, "linkifier");
}
@Override
protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
Repository repo = ServletUtils.getRepository(req);
GitilesView view = getView(req, repo);
Paginator paginator = null;
try {
GitilesAccess access = getAccess(req);
paginator = newPaginator(repo, view, access);
if (paginator == null) {
throw new GitilesRequestFailureException(FailureReason.OBJECT_NOT_FOUND);
}
DateFormatter df = new DateFormatter(access, Format.DEFAULT);
// Allow the user to select a logView variant with the "pretty" param.
String pretty = Iterables.getFirst(view.getParameters().get(PRETTY_PARAM), "default");
Map<String, Object> data = Maps.newHashMapWithExpectedSize(2);
if (!view.getRevision().nameIsId()) {
List<Map<String, Object>> tags = Lists.newArrayListWithExpectedSize(1);
for (RevObject o : RevisionServlet.listObjects(paginator.getWalk(), view.getRevision())) {
if (o instanceof RevTag) {
tags.add(new TagSoyData(linkifier, req).toSoyData(paginator.getWalk(), (RevTag) o, df));
}
}
if (!tags.isEmpty()) {
data.put("tags", tags);
}
}
String title = "Log - ";
if (!Revision.isNull(view.getOldRevision())) {
title += view.getRevisionRange();
} else {
title += view.getRevision().getName();
}
data.put("title", title);
try (OutputStream out =
startRenderStreamingHtml(
req, res, "com.google.gitiles.templates.LogDetail.logDetail", data)) {
Writer w = newWriter(out, res);
new LogSoyData(req, access, pretty)
.renderStreaming(paginator, null, renderer, w, df, LogSoyData.FooterBehavior.NEXT);
w.flush();
}
} finally {
if (paginator != null) {
paginator.getWalk().close();
}
}
}
@Override
protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException {
Repository repo = ServletUtils.getRepository(req);
GitilesView view = getView(req, repo);
Set<Field> fs = Sets.newEnumSet(CommitJsonData.DEFAULT_FIELDS, Field.class);
String nameStatus = Iterables.getFirst(view.getParameters().get(NAME_STATUS_PARAM), null);
if ("1".equals(nameStatus) || "".equals(nameStatus)) {
fs.add(Field.DIFF_TREE);
}
Paginator paginator = null;
try {
GitilesAccess access = getAccess(req);
paginator = newPaginator(repo, view, access);
if (paginator == null) {
throw new GitilesRequestFailureException(FailureReason.OBJECT_NOT_FOUND);
}
DateFormatter df = new DateFormatter(access, Format.DEFAULT);
CommitJsonData.Log result = new CommitJsonData.Log();
List<CommitJsonData.Commit> entries = Lists.newArrayListWithCapacity(paginator.getLimit());
for (RevCommit c : paginator) {
RevWalk walk = paginator.getWalk();
if (!walk.isRetainBody()) {
walk.parseBody(c);
}
entries.add(new CommitJsonData().toJsonData(req, paginator.getWalk(), c, fs, df));
}
result.log = entries;
if (paginator.getPreviousStart() != null) {
result.previous = paginator.getPreviousStart().name();
}
if (paginator.getNextStart() != null) {
result.next = paginator.getNextStart().name();
}
renderJson(req, res, result, new TypeToken<CommitJsonData.Log>() {}.getType());
} finally {
if (paginator != null) {
paginator.getWalk().close();
}
}
}
private static @Nullable GitilesView getView(HttpServletRequest req, Repository repo)
throws IOException {
GitilesView view = ViewFilter.getView(req);
if (!Revision.isNull(view.getRevision())) {
return view;
}
Ref headRef = repo.exactRef(Constants.HEAD);
if (headRef == null) {
return null;
}
ObjectId id = headRef.getObjectId();
if (id == null) {
return null;
}
try (RevWalk walk = new RevWalk(repo)) {
return GitilesView.log()
.copyFrom(view)
.setRevision(Revision.peel(Constants.HEAD, walk.parseAny(id), walk))
.build();
}
}
private static class InvalidStartValueException extends IllegalArgumentException {
private static final long serialVersionUID = 1L;
InvalidStartValueException() {
super();
}
}
private static Optional<ObjectId> getStart(
ListMultimap<String, String> params, ObjectReader reader)
throws IOException, InvalidStartValueException {
List<String> values = params.get(START_PARAM);
switch (values.size()) {
case 0:
return Optional.empty();
case 1:
String id = values.get(0);
if (!AbbreviatedObjectId.isId(id)) {
throw new InvalidStartValueException();
}
Collection<ObjectId> ids = reader.resolve(AbbreviatedObjectId.fromString(id));
if (ids.size() != 1) {
throw new InvalidStartValueException();
}
return Optional.of(Iterables.getOnlyElement(ids));
default:
throw new InvalidStartValueException();
}
}
private static @Nullable RevWalk newWalk(Repository repo, GitilesView view, GitilesAccess access)
throws MissingObjectException, IOException {
RevWalk walk = new RevWalk(repo);
if (isTrue(view, FIRST_PARENT_PARAM)) {
walk.setFirstParent(true);
}
if (isTrue(view, TOPO_ORDER_PARAM)) {
walk.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER, true);
}
if (isTrue(view, REVERSE_PARAM)) {
walk.sort(RevSort.REVERSE, true);
}
try {
walk.markStart(walk.parseCommit(view.getRevision().getId()));
if (!Revision.isNull(view.getOldRevision())) {
walk.markUninteresting(walk.parseCommit(view.getOldRevision().getId()));
}
} catch (IncorrectObjectTypeException iote) {
return null;
}
setTreeFilter(walk, view, access);
setRevFilter(walk, view);
walk.setRetainBody(false);
return walk;
}
private static void setRevFilter(RevWalk walk, GitilesView view) {
List<RevFilter> filters = new ArrayList<>(3);
if (isTrue(view, "no-merges")) {
filters.add(RevFilter.NO_MERGES);
}
String author = Iterables.getFirst(view.getParameters().get("author"), null);
if (author != null) {
filters.add(IdentRevFilter.author(author));
}
String committer = Iterables.getFirst(view.getParameters().get("committer"), null);
if (committer != null) {
filters.add(IdentRevFilter.committer(committer));
}
if (filters.size() > 1) {
walk.setRevFilter(AndRevFilter.create(filters));
} else if (filters.size() == 1) {
walk.setRevFilter(filters.get(0));
}
}
private static void setTreeFilter(RevWalk walk, GitilesView view, GitilesAccess access)
throws IOException {
if (Strings.isNullOrEmpty(view.getPathPart())) {
return;
}
walk.setRewriteParents(false);
String path = view.getPathPart();
List<String> followParams = view.getParameters().get(FOLLOW_PARAM);
boolean follow =
!followParams.isEmpty()
? isTrue(followParams.get(0))
: access.getConfig().getBoolean("log", null, "follow", true);
if (follow) {
walk.setTreeFilter(FollowFilter.create(path, access.getConfig().get(DiffConfig.KEY)));
} else {
walk.setTreeFilter(ChangedPathTreeFilter.create(path));
}
}
private static boolean isTrue(GitilesView view, String param) {
return isTrue(Iterables.getFirst(view.getParameters().get(param), null));
}
private static boolean isTrue(String v) {
if (v == null) {
return false;
} else if (v.isEmpty()) {
return true;
}
return Boolean.TRUE.equals(StringUtils.toBooleanOrNull(v));
}
private static @Nullable Paginator newPaginator(
Repository repo, GitilesView view, GitilesAccess access) throws IOException {
if (view == null) {
return null;
}
try (RevWalk walk = newWalk(repo, view, access)) {
if (walk == null) {
return null;
}
try {
Optional<ObjectId> start = getStart(view.getParameters(), walk.getObjectReader());
return new Paginator(walk, getLimit(view), start.orElse(null));
} catch (InvalidStartValueException e) {
return null;
}
}
}
private static int getLimit(GitilesView view) {
List<String> values = view.getParameters().get(LIMIT_PARAM);
if (values.isEmpty()) {
return DEFAULT_LIMIT;
}
Long limit = Longs.tryParse(values.get(0));
if (limit == null) {
return DEFAULT_LIMIT;
}
return (int) Math.min(limit, MAX_LIMIT);
}
}