blob: ff1fb7b3d8d8b301005079fcd9f9a96fb8046c0d [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
Michael Moss558f8642014-04-15 09:29:21 -070021import com.google.common.base.Objects;
Dave Borowitz80334b22013-01-11 14:19:11 -080022import com.google.common.base.Optional;
23import com.google.common.base.Strings;
24import com.google.common.collect.Iterables;
25import com.google.common.collect.ListMultimap;
26import com.google.common.collect.Lists;
Dave Borowitzef055812013-11-01 12:05:55 -070027import com.google.common.collect.Maps;
28import com.google.common.primitives.Longs;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070029import com.google.gitiles.DateFormatter.Format;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070030import com.google.gson.reflect.TypeToken;
Dave Borowitz9de65952012-08-13 16:09:45 -070031
32import org.eclipse.jgit.errors.IncorrectObjectTypeException;
33import org.eclipse.jgit.errors.MissingObjectException;
34import org.eclipse.jgit.errors.RevWalkException;
35import org.eclipse.jgit.http.server.ServletUtils;
36import org.eclipse.jgit.lib.AbbreviatedObjectId;
Michael Moss558f8642014-04-15 09:29:21 -070037import org.eclipse.jgit.lib.Config;
Dave Borowitz80334b22013-01-11 14:19:11 -080038import org.eclipse.jgit.lib.Constants;
Dave Borowitz9de65952012-08-13 16:09:45 -070039import org.eclipse.jgit.lib.ObjectId;
40import org.eclipse.jgit.lib.ObjectReader;
41import org.eclipse.jgit.lib.Ref;
42import org.eclipse.jgit.lib.Repository;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070043import org.eclipse.jgit.revwalk.RevCommit;
Dave Borowitz9de65952012-08-13 16:09:45 -070044import org.eclipse.jgit.revwalk.RevObject;
45import org.eclipse.jgit.revwalk.RevTag;
46import org.eclipse.jgit.revwalk.RevWalk;
Jonathan Niedercb4d91c2013-12-04 16:59:15 -080047import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
Dave Borowitz1b08aea2014-05-05 12:02:21 -070048import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
Jonathan Niedercb4d91c2013-12-04 16:59:15 -080049import org.eclipse.jgit.treewalk.filter.TreeFilter;
Dave Borowitz9de65952012-08-13 16:09:45 -070050import org.slf4j.Logger;
51import org.slf4j.LoggerFactory;
52
Dave Borowitz80334b22013-01-11 14:19:11 -080053import java.io.IOException;
54import 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);
Michael Moss558f8642014-04-15 09:29:21 -070091 Config config = access.getConfig();
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070092 DateFormatter df = new DateFormatter(access, Format.DEFAULT);
Michael Moss558f8642014-04-15 09:29:21 -070093
94 // Allow the user to select a logView variant with the "pretty" param.
95 String pretty = Iterables.getFirst(view.getParameters().get(PRETTY_PARAM), "default");
96 Map<String, Object> data = new LogSoyData(req, view,
97 config.getBoolean("logFormat", pretty, "verbose", false)).toSoyData(paginator, null, df);
98 String variant = config.getString("logFormat", pretty, "variant");
99 data.put("logEntryPretty", pretty);
100 data.put("logEntryVariant", Objects.firstNonNull(variant, pretty));
Dave Borowitz9de65952012-08-13 16:09:45 -0700101
102 if (!view.getRevision().nameIsId()) {
103 List<Map<String, Object>> tags = Lists.newArrayListWithExpectedSize(1);
Dave Borowitz27fada42013-11-01 11:09:49 -0700104 for (RevObject o : RevisionServlet.listObjects(paginator.getWalk(), view.getRevision())) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700105 if (o instanceof RevTag) {
Dave Borowitza03760a2014-01-29 16:17:28 -0800106 tags.add(new TagSoyData(linkifier, req).toSoyData((RevTag) o, df));
Dave Borowitz9de65952012-08-13 16:09:45 -0700107 }
108 }
109 if (!tags.isEmpty()) {
110 data.put("tags", tags);
111 }
112 }
113
Dave Borowitz9de65952012-08-13 16:09:45 -0700114 String title = "Log - ";
115 if (view.getOldRevision() != Revision.NULL) {
116 title += view.getRevisionRange();
117 } else {
118 title += view.getRevision().getName();
119 }
120
121 data.put("title", title);
Dave Borowitz9de65952012-08-13 16:09:45 -0700122
Dave Borowitzb1c628f2013-01-11 11:28:20 -0800123 renderHtml(req, res, "gitiles.logDetail", data);
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 }
214 return walk;
215 }
Dave Borowitz27fada42013-11-01 11:09:49 -0700216
217 private static Paginator newPaginator(Repository repo, GitilesView view) throws IOException {
218 if (view == null) {
219 return null;
220 }
221
222 RevWalk walk = null;
223 try {
224 walk = newWalk(repo, view);
225 } catch (IncorrectObjectTypeException e) {
226 return null;
227 }
228
229 Optional<ObjectId> start;
230 try {
231 start = getStart(view.getParameters(), walk.getObjectReader());
232 } catch (IOException e) {
233 walk.release();
234 throw e;
235 }
236 if (start == null) {
237 return null;
238 }
Dave Borowitzef055812013-11-01 12:05:55 -0700239
240 return new Paginator(walk, getLimit(view), start.orNull());
241 }
242
243 private static int getLimit(GitilesView view) {
244 List<String> values = view.getParameters().get(LIMIT_PARAM);
245 if (values.isEmpty()) {
246 return DEFAULT_LIMIT;
247 }
248 Long limit = Longs.tryParse(values.get(0));
249 if (limit == null) {
250 return DEFAULT_LIMIT;
251 }
252 return (int) Math.min(limit, MAX_LIMIT);
Dave Borowitz27fada42013-11-01 11:09:49 -0700253 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700254}