blob: fc6db2e0634280f44bb439215e95a8c66fd8e45e [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;
27import com.google.common.primitives.Longs;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070028import com.google.gitiles.DateFormatter.Format;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070029import com.google.gson.reflect.TypeToken;
Dave Borowitz9de65952012-08-13 16:09:45 -070030
31import org.eclipse.jgit.errors.IncorrectObjectTypeException;
32import org.eclipse.jgit.errors.MissingObjectException;
33import org.eclipse.jgit.errors.RevWalkException;
34import org.eclipse.jgit.http.server.ServletUtils;
35import org.eclipse.jgit.lib.AbbreviatedObjectId;
Dave Borowitz80334b22013-01-11 14:19:11 -080036import org.eclipse.jgit.lib.Constants;
Dave Borowitz9de65952012-08-13 16:09:45 -070037import org.eclipse.jgit.lib.ObjectId;
38import org.eclipse.jgit.lib.ObjectReader;
39import org.eclipse.jgit.lib.Ref;
40import org.eclipse.jgit.lib.Repository;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070041import org.eclipse.jgit.revwalk.RevCommit;
Dave Borowitz9de65952012-08-13 16:09:45 -070042import org.eclipse.jgit.revwalk.RevObject;
43import org.eclipse.jgit.revwalk.RevTag;
44import org.eclipse.jgit.revwalk.RevWalk;
Jonathan Niedercb4d91c2013-12-04 16:59:15 -080045import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
Dave Borowitz1b08aea2014-05-05 12:02:21 -070046import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
Jonathan Niedercb4d91c2013-12-04 16:59:15 -080047import org.eclipse.jgit.treewalk.filter.TreeFilter;
Dave Borowitz9de65952012-08-13 16:09:45 -070048import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
Dave Borowitz80334b22013-01-11 14:19:11 -080051import java.io.IOException;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070052import java.io.OutputStream;
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070053import java.io.Writer;
Dave Borowitz80334b22013-01-11 14:19:11 -080054import java.util.Collection;
55import java.util.List;
56import java.util.Map;
Dave Borowitz80334b22013-01-11 14:19:11 -080057
58import javax.servlet.http.HttpServletRequest;
59import javax.servlet.http.HttpServletResponse;
Dave Borowitz9de65952012-08-13 16:09:45 -070060
61/** Serves an HTML page with a shortlog for commits and paths. */
62public class LogServlet extends BaseServlet {
Chad Horohoead23f142012-11-12 09:45:39 -080063 private static final long serialVersionUID = 1L;
Dave Borowitz9de65952012-08-13 16:09:45 -070064 private static final Logger log = LoggerFactory.getLogger(LogServlet.class);
65
Dave Borowitzef055812013-11-01 12:05:55 -070066 static final String LIMIT_PARAM = "n";
Dave Borowitzb772cce2012-12-28 13:57:22 -080067 static final String START_PARAM = "s";
Michael Moss558f8642014-04-15 09:29:21 -070068 private static final String PRETTY_PARAM = "pretty";
Dave Borowitzef055812013-11-01 12:05:55 -070069 private static final int DEFAULT_LIMIT = 100;
70 private static final int MAX_LIMIT = 10000;
Dave Borowitz9de65952012-08-13 16:09:45 -070071
72 private final Linkifier linkifier;
Dave Borowitz9de65952012-08-13 16:09:45 -070073
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070074 public LogServlet(GitilesAccess.Factory accessFactory, Renderer renderer, Linkifier linkifier) {
Dave Borowitz8d6d6872014-03-16 15:18:14 -070075 super(renderer, accessFactory);
Dave Borowitz9de65952012-08-13 16:09:45 -070076 this.linkifier = checkNotNull(linkifier, "linkifier");
Dave Borowitz9de65952012-08-13 16:09:45 -070077 }
78
79 @Override
Dave Borowitz80334b22013-01-11 14:19:11 -080080 protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
Dave Borowitz9de65952012-08-13 16:09:45 -070081 Repository repo = ServletUtils.getRepository(req);
Dave Borowitz80334b22013-01-11 14:19:11 -080082 GitilesView view = getView(req, repo);
Dave Borowitz27fada42013-11-01 11:09:49 -070083 Paginator paginator = newPaginator(repo, view);
84 if (paginator == null) {
Dave Borowitz80334b22013-01-11 14:19:11 -080085 res.setStatus(SC_NOT_FOUND);
86 return;
87 }
88
Dave Borowitz9de65952012-08-13 16:09:45 -070089 try {
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070090 GitilesAccess access = getAccess(req);
91 DateFormatter df = new DateFormatter(access, Format.DEFAULT);
Michael Moss558f8642014-04-15 09:29:21 -070092
93 // Allow the user to select a logView variant with the "pretty" param.
94 String pretty = Iterables.getFirst(view.getParameters().get(PRETTY_PARAM), "default");
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -070095 Map<String, Object> data = Maps.newHashMapWithExpectedSize(2);
Dave Borowitz9de65952012-08-13 16:09:45 -070096
97 if (!view.getRevision().nameIsId()) {
98 List<Map<String, Object>> tags = Lists.newArrayListWithExpectedSize(1);
Dave Borowitz27fada42013-11-01 11:09:49 -070099 for (RevObject o : RevisionServlet.listObjects(paginator.getWalk(), view.getRevision())) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700100 if (o instanceof RevTag) {
Dave Borowitza03760a2014-01-29 16:17:28 -0800101 tags.add(new TagSoyData(linkifier, req).toSoyData((RevTag) o, df));
Dave Borowitz9de65952012-08-13 16:09:45 -0700102 }
103 }
104 if (!tags.isEmpty()) {
105 data.put("tags", tags);
106 }
107 }
108
Dave Borowitz9de65952012-08-13 16:09:45 -0700109 String title = "Log - ";
110 if (view.getOldRevision() != Revision.NULL) {
111 title += view.getRevisionRange();
112 } else {
113 title += view.getRevision().getName();
114 }
115
116 data.put("title", title);
Dave Borowitz9de65952012-08-13 16:09:45 -0700117
Shawn Pearce4c2eb852014-08-26 15:35:33 -0700118 try (OutputStream out = startRenderStreamingHtml(req, res, "gitiles.logDetail", data)) {
119 Writer w = newWriter(out, res);
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700120 new LogSoyData(req, access, pretty)
121 .renderStreaming(paginator, null, renderer, w, df);
Shawn Pearce4c2eb852014-08-26 15:35:33 -0700122 w.flush();
Dave Borowitzf6dcf7a2014-07-30 10:26:58 -0700123 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700124 } catch (RevWalkException e) {
125 log.warn("Error in rev walk", e);
126 res.setStatus(SC_INTERNAL_SERVER_ERROR);
127 return;
128 } finally {
Dave Borowitz27fada42013-11-01 11:09:49 -0700129 paginator.getWalk().release();
Dave Borowitz9de65952012-08-13 16:09:45 -0700130 }
131 }
132
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700133 @Override
134 protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException {
135 Repository repo = ServletUtils.getRepository(req);
136 GitilesView view = getView(req, repo);
137 Paginator paginator = newPaginator(repo, view);
138 if (paginator == null) {
139 res.setStatus(SC_NOT_FOUND);
140 return;
141 }
142
143 try {
Dave Borowitz2b2f34b2014-04-29 16:47:20 -0700144 DateFormatter df = new DateFormatter(getAccess(req), Format.DEFAULT);
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700145 Map<String, Object> result = Maps.newLinkedHashMap();
146 List<CommitJsonData.Commit> entries = Lists.newArrayListWithCapacity(paginator.getLimit());
147 for (RevCommit c : paginator) {
148 paginator.getWalk().parseBody(c);
Dave Borowitzee2d77c2014-03-16 13:49:37 -0700149 entries.add(new CommitJsonData().setRevWalk(paginator.getWalk()).toJsonData(req, c, df));
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700150 }
151 result.put("log", entries);
152 if (paginator.getPreviousStart() != null) {
153 result.put("previous", paginator.getPreviousStart().name());
154 }
155 if (paginator.getNextStart() != null) {
156 result.put("next", paginator.getNextStart().name());
157 }
158 renderJson(req, res, result, new TypeToken<Map<String, Object>>() {}.getType());
159 } finally {
160 paginator.getWalk().release();
161 }
162 }
163
Dave Borowitz80334b22013-01-11 14:19:11 -0800164 private static GitilesView getView(HttpServletRequest req, Repository repo) throws IOException {
165 GitilesView view = ViewFilter.getView(req);
166 if (view.getRevision() != Revision.NULL) {
167 return view;
168 }
169 Ref headRef = repo.getRef(Constants.HEAD);
170 if (headRef == null) {
171 return null;
172 }
173 RevWalk walk = new RevWalk(repo);
174 try {
175 return GitilesView.log()
176 .copyFrom(view)
Dave Borowitz48ca6702013-04-09 11:52:41 -0700177 .setRevision(Revision.peel(Constants.HEAD, walk.parseAny(headRef.getObjectId()), walk))
Dave Borowitz80334b22013-01-11 14:19:11 -0800178 .build();
179 } finally {
180 walk.release();
181 }
182 }
183
Dave Borowitz9de65952012-08-13 16:09:45 -0700184 private static Optional<ObjectId> getStart(ListMultimap<String, String> params,
185 ObjectReader reader) throws IOException {
186 List<String> values = params.get(START_PARAM);
187 switch (values.size()) {
188 case 0:
189 return Optional.absent();
190 case 1:
191 Collection<ObjectId> ids = reader.resolve(AbbreviatedObjectId.fromString(values.get(0)));
192 if (ids.size() != 1) {
193 return null;
194 }
195 return Optional.of(Iterables.getOnlyElement(ids));
196 default:
197 return null;
198 }
199 }
200
201 private static RevWalk newWalk(Repository repo, GitilesView view)
202 throws MissingObjectException, IncorrectObjectTypeException, IOException {
203 RevWalk walk = new RevWalk(repo);
204 walk.markStart(walk.parseCommit(view.getRevision().getId()));
205 if (view.getOldRevision() != Revision.NULL) {
206 walk.markUninteresting(walk.parseCommit(view.getOldRevision().getId()));
207 }
Dave Borowitzdd3c3d92013-03-11 16:38:41 -0700208 if (!Strings.isNullOrEmpty(view.getPathPart())) {
Dave Borowitz44434272014-05-06 11:21:08 -0700209 walk.setRewriteParents(false);
Jonathan Niedercb4d91c2013-12-04 16:59:15 -0800210 walk.setTreeFilter(AndTreeFilter.create(
Dave Borowitz1b08aea2014-05-05 12:02:21 -0700211 PathFilterGroup.createFromStrings(view.getPathPart()),
Jonathan Niedercb4d91c2013-12-04 16:59:15 -0800212 TreeFilter.ANY_DIFF));
Dave Borowitz9de65952012-08-13 16:09:45 -0700213 }
Benjamin Kalmanbcc19fa2014-08-21 17:24:41 -0700214 String author = Iterables.getFirst(view.getParameters().get("author"), null);
215 if (author != null) {
Dave Borowitz8646ff52014-09-05 16:18:56 -0700216 walk.setRevFilter(IdentRevFilter.author(author));
217 }
218 String committer = Iterables.getFirst(view.getParameters().get("committer"), null);
219 if (committer != null) {
220 walk.setRevFilter(IdentRevFilter.committer(committer));
Benjamin Kalmanbcc19fa2014-08-21 17:24:41 -0700221 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700222 return walk;
223 }
Dave Borowitz27fada42013-11-01 11:09:49 -0700224
225 private static Paginator newPaginator(Repository repo, GitilesView view) throws IOException {
226 if (view == null) {
227 return null;
228 }
229
230 RevWalk walk = null;
231 try {
232 walk = newWalk(repo, view);
233 } catch (IncorrectObjectTypeException e) {
234 return null;
235 }
236
237 Optional<ObjectId> start;
238 try {
239 start = getStart(view.getParameters(), walk.getObjectReader());
240 } catch (IOException e) {
241 walk.release();
242 throw e;
243 }
244 if (start == null) {
245 return null;
246 }
Dave Borowitzef055812013-11-01 12:05:55 -0700247
248 return new Paginator(walk, getLimit(view), start.orNull());
249 }
250
251 private static int getLimit(GitilesView view) {
252 List<String> values = view.getParameters().get(LIMIT_PARAM);
253 if (values.isEmpty()) {
254 return DEFAULT_LIMIT;
255 }
256 Long limit = Longs.tryParse(values.get(0));
257 if (limit == null) {
258 return DEFAULT_LIMIT;
259 }
260 return (int) Math.min(limit, MAX_LIMIT);
Dave Borowitz27fada42013-11-01 11:09:49 -0700261 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700262}