blob: 5f170816800b52365deec6f538cf2f40aa110126 [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gitiles;
16
Dave Borowitz9de65952012-08-13 16:09:45 -070017import static com.google.common.base.Preconditions.checkNotNull;
18import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
19import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
20
Dave Borowitz80334b22013-01-11 14:19:11 -080021import com.google.common.base.Optional;
22import com.google.common.base.Strings;
23import com.google.common.collect.Iterables;
24import com.google.common.collect.ListMultimap;
25import com.google.common.collect.Lists;
Dave Borowitzef055812013-11-01 12:05:55 -070026import com.google.common.collect.Maps;
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020027import com.google.common.collect.Sets;
Dave Borowitzef055812013-11-01 12:05:55 -070028import com.google.common.primitives.Longs;
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020029import com.google.gitiles.CommitData.Field;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070030import com.google.gitiles.DateFormatter.Format;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070031import com.google.gson.reflect.TypeToken;
Dave Borowitz3b744b12016-08-19 16:11:10 -040032import java.io.IOException;
33import java.io.OutputStream;
34import java.io.Writer;
35import java.util.ArrayList;
36import java.util.Collection;
37import java.util.List;
38import java.util.Map;
39import java.util.Set;
40import javax.servlet.http.HttpServletRequest;
41import javax.servlet.http.HttpServletResponse;
Dave Borowitz344c4dd2015-10-26 11:02:13 -040042import org.eclipse.jgit.diff.DiffConfig;
Dave Borowitz9de65952012-08-13 16:09:45 -070043import org.eclipse.jgit.errors.IncorrectObjectTypeException;
44import org.eclipse.jgit.errors.MissingObjectException;
45import org.eclipse.jgit.errors.RevWalkException;
46import org.eclipse.jgit.http.server.ServletUtils;
47import org.eclipse.jgit.lib.AbbreviatedObjectId;
Dave Borowitz80334b22013-01-11 14:19:11 -080048import org.eclipse.jgit.lib.Constants;
Dave Borowitz9de65952012-08-13 16:09:45 -070049import org.eclipse.jgit.lib.ObjectId;
50import org.eclipse.jgit.lib.ObjectReader;
51import org.eclipse.jgit.lib.Ref;
52import org.eclipse.jgit.lib.Repository;
Dave Borowitz344c4dd2015-10-26 11:02:13 -040053import org.eclipse.jgit.revwalk.FollowFilter;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070054import org.eclipse.jgit.revwalk.RevCommit;
Dave Borowitz9de65952012-08-13 16:09:45 -070055import org.eclipse.jgit.revwalk.RevObject;
Shawn Pearce31037672016-08-18 21:44:43 -070056import org.eclipse.jgit.revwalk.RevSort;
Dave Borowitz9de65952012-08-13 16:09:45 -070057import org.eclipse.jgit.revwalk.RevTag;
58import org.eclipse.jgit.revwalk.RevWalk;
Shawn Pearce20b42292015-03-17 21:53:55 -070059import org.eclipse.jgit.revwalk.filter.AndRevFilter;
60import org.eclipse.jgit.revwalk.filter.RevFilter;
Jonathan Niedercb4d91c2013-12-04 16:59:15 -080061import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
Dave Borowitz1b08aea2014-05-05 12:02:21 -070062import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
Jonathan Niedercb4d91c2013-12-04 16:59:15 -080063import org.eclipse.jgit.treewalk.filter.TreeFilter;
Shawn Pearce20b42292015-03-17 21:53:55 -070064import org.eclipse.jgit.util.StringUtils;
Dave Borowitz9de65952012-08-13 16:09:45 -070065import org.slf4j.Logger;
66import org.slf4j.LoggerFactory;
67
Dave Borowitz9de65952012-08-13 16:09:45 -070068/** Serves an HTML page with a shortlog for commits and paths. */
69public class LogServlet extends BaseServlet {
Chad Horohoead23f142012-11-12 09:45:39 -080070 private static final long serialVersionUID = 1L;
Dave Borowitz9de65952012-08-13 16:09:45 -070071 private static final Logger log = LoggerFactory.getLogger(LogServlet.class);
72
Dave Borowitzef055812013-11-01 12:05:55 -070073 static final String LIMIT_PARAM = "n";
Dave Borowitzb772cce2012-12-28 13:57:22 -080074 static final String START_PARAM = "s";
Dave Borowitz344c4dd2015-10-26 11:02:13 -040075
76 private static final String FOLLOW_PARAM = "follow";
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020077 private static final String NAME_STATUS_PARAM = "name-status";
Dave Borowitz344c4dd2015-10-26 11:02:13 -040078 private static final String PRETTY_PARAM = "pretty";
79
Dave Borowitzef055812013-11-01 12:05:55 -070080 private static final int DEFAULT_LIMIT = 100;
81 private static final int MAX_LIMIT = 10000;
Dave Borowitz9de65952012-08-13 16:09:45 -070082
83 private final Linkifier linkifier;
Dave Borowitz9de65952012-08-13 16:09:45 -070084
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070085 public LogServlet(GitilesAccess.Factory accessFactory, Renderer renderer, Linkifier linkifier) {
Dave Borowitz8d6d6872014-03-16 15:18:14 -070086 super(renderer, accessFactory);
Dave Borowitz9de65952012-08-13 16:09:45 -070087 this.linkifier = checkNotNull(linkifier, "linkifier");
Dave Borowitz9de65952012-08-13 16:09:45 -070088 }
89
90 @Override
Dave Borowitz80334b22013-01-11 14:19:11 -080091 protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
Dave Borowitz9de65952012-08-13 16:09:45 -070092 Repository repo = ServletUtils.getRepository(req);
Dave Borowitz80334b22013-01-11 14:19:11 -080093 GitilesView view = getView(req, repo);
Dave Borowitz80334b22013-01-11 14:19:11 -080094
Dave Borowitz344c4dd2015-10-26 11:02:13 -040095 Paginator paginator = null;
Dave Borowitz9de65952012-08-13 16:09:45 -070096 try {
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070097 GitilesAccess access = getAccess(req);
Shawn Pearce31037672016-08-18 21:44:43 -070098 paginator = newPaginator(repo, view, access);
Dave Borowitz344c4dd2015-10-26 11:02:13 -040099 if (paginator == null) {
100 res.setStatus(SC_NOT_FOUND);
101 return;
102 }
Dave Borowitz2b2f34b2014-04-29 16:47:20 -0700103 DateFormatter df = new DateFormatter(access, Format.DEFAULT);
Michael Moss558f8642014-04-15 09:29:21 -0700104
105 // Allow the user to select a logView variant with the "pretty" param.
106 String pretty = Iterables.getFirst(view.getParameters().get(PRETTY_PARAM), "default");
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700107 Map<String, Object> data = Maps.newHashMapWithExpectedSize(2);
Dave Borowitz9de65952012-08-13 16:09:45 -0700108
109 if (!view.getRevision().nameIsId()) {
110 List<Map<String, Object>> tags = Lists.newArrayListWithExpectedSize(1);
Dave Borowitz27fada42013-11-01 11:09:49 -0700111 for (RevObject o : RevisionServlet.listObjects(paginator.getWalk(), view.getRevision())) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700112 if (o instanceof RevTag) {
Dave Borowitza03760a2014-01-29 16:17:28 -0800113 tags.add(new TagSoyData(linkifier, req).toSoyData((RevTag) o, df));
Dave Borowitz9de65952012-08-13 16:09:45 -0700114 }
115 }
116 if (!tags.isEmpty()) {
117 data.put("tags", tags);
118 }
119 }
120
Dave Borowitz9de65952012-08-13 16:09:45 -0700121 String title = "Log - ";
122 if (view.getOldRevision() != Revision.NULL) {
123 title += view.getRevisionRange();
124 } else {
125 title += view.getRevision().getName();
126 }
127
128 data.put("title", title);
Dave Borowitz9de65952012-08-13 16:09:45 -0700129
Shawn Pearce4c2eb852014-08-26 15:35:33 -0700130 try (OutputStream out = startRenderStreamingHtml(req, res, "gitiles.logDetail", data)) {
131 Writer w = newWriter(out, res);
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200132 new LogSoyData(req, access, pretty).renderStreaming(paginator, null, renderer, w, df);
Shawn Pearce4c2eb852014-08-26 15:35:33 -0700133 w.flush();
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700134 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700135 } catch (RevWalkException e) {
136 log.warn("Error in rev walk", e);
137 res.setStatus(SC_INTERNAL_SERVER_ERROR);
138 return;
139 } finally {
Dave Borowitz344c4dd2015-10-26 11:02:13 -0400140 if (paginator != null) {
141 paginator.getWalk().close();
142 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700143 }
144 }
145
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700146 @Override
147 protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException {
148 Repository repo = ServletUtils.getRepository(req);
149 GitilesView view = getView(req, repo);
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700150
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +0200151 Set<Field> fs = Sets.newEnumSet(CommitJsonData.DEFAULT_FIELDS, Field.class);
152 String nameStatus = Iterables.getFirst(view.getParameters().get(NAME_STATUS_PARAM), null);
153 if ("1".equals(nameStatus) || "".equals(nameStatus)) {
154 fs.add(Field.DIFF_TREE);
155 }
156
Dave Borowitz344c4dd2015-10-26 11:02:13 -0400157 Paginator paginator = null;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700158 try {
Shawn Pearce31037672016-08-18 21:44:43 -0700159 GitilesAccess access = getAccess(req);
160 paginator = newPaginator(repo, view, access);
Dave Borowitz344c4dd2015-10-26 11:02:13 -0400161 if (paginator == null) {
162 res.setStatus(SC_NOT_FOUND);
163 return;
164 }
Shawn Pearce31037672016-08-18 21:44:43 -0700165 DateFormatter df = new DateFormatter(access, Format.DEFAULT);
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400166 CommitJsonData.Log result = new CommitJsonData.Log();
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700167 List<CommitJsonData.Commit> entries = Lists.newArrayListWithCapacity(paginator.getLimit());
168 for (RevCommit c : paginator) {
169 paginator.getWalk().parseBody(c);
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200170 entries.add(
171 new CommitJsonData().setRevWalk(paginator.getWalk()).toJsonData(req, c, fs, df));
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700172 }
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400173 result.log = entries;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700174 if (paginator.getPreviousStart() != null) {
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400175 result.previous = paginator.getPreviousStart().name();
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700176 }
177 if (paginator.getNextStart() != null) {
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400178 result.next = paginator.getNextStart().name();
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700179 }
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400180 renderJson(req, res, result, new TypeToken<CommitJsonData.Log>() {}.getType());
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700181 } finally {
Dave Borowitz344c4dd2015-10-26 11:02:13 -0400182 if (paginator != null) {
183 paginator.getWalk().close();
184 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700185 }
186 }
187
Dave Borowitz80334b22013-01-11 14:19:11 -0800188 private static GitilesView getView(HttpServletRequest req, Repository repo) throws IOException {
189 GitilesView view = ViewFilter.getView(req);
190 if (view.getRevision() != Revision.NULL) {
191 return view;
192 }
Dave Borowitz14cad732016-05-26 17:34:19 -0400193 Ref headRef = repo.exactRef(Constants.HEAD);
Dave Borowitz80334b22013-01-11 14:19:11 -0800194 if (headRef == null) {
195 return null;
196 }
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700197 try (RevWalk walk = new RevWalk(repo)) {
Dave Borowitz80334b22013-01-11 14:19:11 -0800198 return GitilesView.log()
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200199 .copyFrom(view)
200 .setRevision(Revision.peel(Constants.HEAD, walk.parseAny(headRef.getObjectId()), walk))
201 .build();
Dave Borowitz80334b22013-01-11 14:19:11 -0800202 }
203 }
204
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200205 private static Optional<ObjectId> getStart(
206 ListMultimap<String, String> params, ObjectReader reader) throws IOException {
Dave Borowitz9de65952012-08-13 16:09:45 -0700207 List<String> values = params.get(START_PARAM);
208 switch (values.size()) {
209 case 0:
210 return Optional.absent();
211 case 1:
Dave Borowitz95ef58a2016-05-26 17:30:10 -0400212 String id = values.get(0);
213 if (!AbbreviatedObjectId.isId(id)) {
214 return null;
215 }
216 Collection<ObjectId> ids = reader.resolve(AbbreviatedObjectId.fromString(id));
Dave Borowitz9de65952012-08-13 16:09:45 -0700217 if (ids.size() != 1) {
218 return null;
219 }
220 return Optional.of(Iterables.getOnlyElement(ids));
221 default:
222 return null;
223 }
224 }
225
Dave Borowitz344c4dd2015-10-26 11:02:13 -0400226 private static RevWalk newWalk(Repository repo, GitilesView view, GitilesAccess access)
David Pursehousec3e772a2016-06-15 21:49:35 +0900227 throws MissingObjectException, IOException {
Dave Borowitz9de65952012-08-13 16:09:45 -0700228 RevWalk walk = new RevWalk(repo);
David Pursehousec3e772a2016-06-15 21:49:35 +0900229 try {
230 walk.markStart(walk.parseCommit(view.getRevision().getId()));
231 if (view.getOldRevision() != Revision.NULL) {
232 walk.markUninteresting(walk.parseCommit(view.getOldRevision().getId()));
233 }
234 } catch (IncorrectObjectTypeException iote) {
235 return null;
Dave Borowitz9de65952012-08-13 16:09:45 -0700236 }
Dave Borowitz344c4dd2015-10-26 11:02:13 -0400237 setTreeFilter(walk, view, access);
Shawn Pearce31037672016-08-18 21:44:43 -0700238 setRevFilter(walk, view);
239 if (isTrue(view, "topo-order")) {
240 walk.sort(RevSort.TOPO, true);
241 }
242 if (isTrue(view, "reverse")) {
243 walk.sort(RevSort.REVERSE, true);
244 }
245 return walk;
246 }
247
248 private static void setRevFilter(RevWalk walk, GitilesView view) {
Shawn Pearce20b42292015-03-17 21:53:55 -0700249 List<RevFilter> filters = new ArrayList<>(3);
Shawn Pearce31037672016-08-18 21:44:43 -0700250 if (isTrue(view, "no-merges")) {
Shawn Pearce20b42292015-03-17 21:53:55 -0700251 filters.add(RevFilter.NO_MERGES);
252 }
Shawn Pearce31037672016-08-18 21:44:43 -0700253
Benjamin Kalmanbcc19fa2014-08-21 17:24:41 -0700254 String author = Iterables.getFirst(view.getParameters().get("author"), null);
255 if (author != null) {
Shawn Pearce20b42292015-03-17 21:53:55 -0700256 filters.add(IdentRevFilter.author(author));
Dave Borowitz8646ff52014-09-05 16:18:56 -0700257 }
Shawn Pearce31037672016-08-18 21:44:43 -0700258
Dave Borowitz8646ff52014-09-05 16:18:56 -0700259 String committer = Iterables.getFirst(view.getParameters().get("committer"), null);
260 if (committer != null) {
Shawn Pearce20b42292015-03-17 21:53:55 -0700261 filters.add(IdentRevFilter.committer(committer));
262 }
Shawn Pearce31037672016-08-18 21:44:43 -0700263
Shawn Pearce20b42292015-03-17 21:53:55 -0700264 if (filters.size() > 1) {
265 walk.setRevFilter(AndRevFilter.create(filters));
266 } else if (filters.size() == 1) {
267 walk.setRevFilter(filters.get(0));
Benjamin Kalmanbcc19fa2014-08-21 17:24:41 -0700268 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700269 }
Dave Borowitz27fada42013-11-01 11:09:49 -0700270
Dave Borowitz344c4dd2015-10-26 11:02:13 -0400271 private static void setTreeFilter(RevWalk walk, GitilesView view, GitilesAccess access)
272 throws IOException {
273 if (Strings.isNullOrEmpty(view.getPathPart())) {
274 return;
275 }
276 walk.setRewriteParents(false);
277 String path = view.getPathPart();
Dave Borowitz45dde172016-03-22 10:10:58 -0400278
279 List<String> followParams = view.getParameters().get(FOLLOW_PARAM);
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200280 boolean follow =
281 !followParams.isEmpty()
282 ? isTrue(followParams.get(0))
283 : access.getConfig().getBoolean("log", null, "follow", true);
Dave Borowitz45dde172016-03-22 10:10:58 -0400284 if (follow) {
Dave Borowitz344c4dd2015-10-26 11:02:13 -0400285 walk.setTreeFilter(FollowFilter.create(path, access.getConfig().get(DiffConfig.KEY)));
286 } else {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200287 walk.setTreeFilter(
288 AndTreeFilter.create(PathFilterGroup.createFromStrings(path), TreeFilter.ANY_DIFF));
Dave Borowitz344c4dd2015-10-26 11:02:13 -0400289 }
290 }
291
Shawn Pearce31037672016-08-18 21:44:43 -0700292 private static boolean isTrue(GitilesView view, String param) {
293 return isTrue(Iterables.getFirst(view.getParameters().get(param), null));
294 }
295
Shawn Pearce20b42292015-03-17 21:53:55 -0700296 private static boolean isTrue(String v) {
297 if (v == null) {
298 return false;
299 } else if (v.isEmpty()) {
300 return true;
301 }
302 return Boolean.TRUE.equals(StringUtils.toBooleanOrNull(v));
303 }
304
Dave Borowitz344c4dd2015-10-26 11:02:13 -0400305 private static Paginator newPaginator(Repository repo, GitilesView view, GitilesAccess access)
306 throws IOException {
Dave Borowitz27fada42013-11-01 11:09:49 -0700307 if (view == null) {
308 return null;
309 }
310
David Pursehousec3e772a2016-06-15 21:49:35 +0900311 try (RevWalk walk = newWalk(repo, view, access)) {
312 if (walk == null) {
313 return null;
314 }
Dave Borowitz27fada42013-11-01 11:09:49 -0700315
David Pursehousec3e772a2016-06-15 21:49:35 +0900316 Optional<ObjectId> start = getStart(view.getParameters(), walk.getObjectReader());
317 if (start == null) {
318 return null;
319 }
Dave Borowitzef055812013-11-01 12:05:55 -0700320
David Pursehousec3e772a2016-06-15 21:49:35 +0900321 return new Paginator(walk, getLimit(view), start.orNull());
322 }
Dave Borowitzef055812013-11-01 12:05:55 -0700323 }
324
325 private static int getLimit(GitilesView view) {
326 List<String> values = view.getParameters().get(LIMIT_PARAM);
327 if (values.isEmpty()) {
328 return DEFAULT_LIMIT;
329 }
330 Long limit = Longs.tryParse(values.get(0));
331 if (limit == null) {
332 return DEFAULT_LIMIT;
333 }
334 return (int) Math.min(limit, MAX_LIMIT);
Dave Borowitz27fada42013-11-01 11:09:49 -0700335 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700336}