blob: e442be5544e9736c34e609e94a66e0e5894347d5 [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 Borowitz775f2222013-11-05 10:36:12 -080046import org.eclipse.jgit.treewalk.filter.PathFilter;
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;
52import java.util.Collection;
53import java.util.List;
54import java.util.Map;
Dave Borowitz80334b22013-01-11 14:19:11 -080055
56import javax.servlet.http.HttpServletRequest;
57import javax.servlet.http.HttpServletResponse;
Dave Borowitz9de65952012-08-13 16:09:45 -070058
59/** Serves an HTML page with a shortlog for commits and paths. */
60public class LogServlet extends BaseServlet {
Chad Horohoead23f142012-11-12 09:45:39 -080061 private static final long serialVersionUID = 1L;
Dave Borowitz9de65952012-08-13 16:09:45 -070062 private static final Logger log = LoggerFactory.getLogger(LogServlet.class);
63
Dave Borowitzef055812013-11-01 12:05:55 -070064 static final String LIMIT_PARAM = "n";
Dave Borowitzb772cce2012-12-28 13:57:22 -080065 static final String START_PARAM = "s";
Dave Borowitzef055812013-11-01 12:05:55 -070066 private static final int DEFAULT_LIMIT = 100;
67 private static final int MAX_LIMIT = 10000;
Dave Borowitz9de65952012-08-13 16:09:45 -070068
69 private final Linkifier linkifier;
Dave Borowitz9de65952012-08-13 16:09:45 -070070
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070071 public LogServlet(GitilesAccess.Factory accessFactory, Renderer renderer, Linkifier linkifier) {
Dave Borowitz8d6d6872014-03-16 15:18:14 -070072 super(renderer, accessFactory);
Dave Borowitz9de65952012-08-13 16:09:45 -070073 this.linkifier = checkNotNull(linkifier, "linkifier");
Dave Borowitz9de65952012-08-13 16:09:45 -070074 }
75
76 @Override
Dave Borowitz80334b22013-01-11 14:19:11 -080077 protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
Dave Borowitz9de65952012-08-13 16:09:45 -070078 Repository repo = ServletUtils.getRepository(req);
Dave Borowitz80334b22013-01-11 14:19:11 -080079 GitilesView view = getView(req, repo);
Dave Borowitz27fada42013-11-01 11:09:49 -070080 Paginator paginator = newPaginator(repo, view);
81 if (paginator == null) {
Dave Borowitz80334b22013-01-11 14:19:11 -080082 res.setStatus(SC_NOT_FOUND);
83 return;
84 }
85
Dave Borowitz9de65952012-08-13 16:09:45 -070086 try {
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070087 GitilesAccess access = getAccess(req);
88 DateFormatter df = new DateFormatter(access, Format.DEFAULT);
Dave Borowitza03760a2014-01-29 16:17:28 -080089 Map<String, Object> data = new LogSoyData(req, view).toSoyData(paginator, null, df);
Dave Borowitz9de65952012-08-13 16:09:45 -070090
91 if (!view.getRevision().nameIsId()) {
92 List<Map<String, Object>> tags = Lists.newArrayListWithExpectedSize(1);
Dave Borowitz27fada42013-11-01 11:09:49 -070093 for (RevObject o : RevisionServlet.listObjects(paginator.getWalk(), view.getRevision())) {
Dave Borowitz9de65952012-08-13 16:09:45 -070094 if (o instanceof RevTag) {
Dave Borowitza03760a2014-01-29 16:17:28 -080095 tags.add(new TagSoyData(linkifier, req).toSoyData((RevTag) o, df));
Dave Borowitz9de65952012-08-13 16:09:45 -070096 }
97 }
98 if (!tags.isEmpty()) {
99 data.put("tags", tags);
100 }
101 }
102
Dave Borowitz9de65952012-08-13 16:09:45 -0700103 String title = "Log - ";
104 if (view.getOldRevision() != Revision.NULL) {
105 title += view.getRevisionRange();
106 } else {
107 title += view.getRevision().getName();
108 }
109
110 data.put("title", title);
Dave Borowitz2b2f34b2014-04-29 16:47:20 -0700111 GitilesConfig.putVariant(access.getConfig(), "logEntry", "logEntryVariant", data);
Dave Borowitz9de65952012-08-13 16:09:45 -0700112
Dave Borowitzb1c628f2013-01-11 11:28:20 -0800113 renderHtml(req, res, "gitiles.logDetail", data);
Dave Borowitz9de65952012-08-13 16:09:45 -0700114 } catch (RevWalkException e) {
115 log.warn("Error in rev walk", e);
116 res.setStatus(SC_INTERNAL_SERVER_ERROR);
117 return;
118 } finally {
Dave Borowitz27fada42013-11-01 11:09:49 -0700119 paginator.getWalk().release();
Dave Borowitz9de65952012-08-13 16:09:45 -0700120 }
121 }
122
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700123 @Override
124 protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException {
125 Repository repo = ServletUtils.getRepository(req);
126 GitilesView view = getView(req, repo);
127 Paginator paginator = newPaginator(repo, view);
128 if (paginator == null) {
129 res.setStatus(SC_NOT_FOUND);
130 return;
131 }
132
133 try {
Dave Borowitz2b2f34b2014-04-29 16:47:20 -0700134 DateFormatter df = new DateFormatter(getAccess(req), Format.DEFAULT);
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700135 Map<String, Object> result = Maps.newLinkedHashMap();
136 List<CommitJsonData.Commit> entries = Lists.newArrayListWithCapacity(paginator.getLimit());
137 for (RevCommit c : paginator) {
138 paginator.getWalk().parseBody(c);
Dave Borowitzee2d77c2014-03-16 13:49:37 -0700139 entries.add(new CommitJsonData().setRevWalk(paginator.getWalk()).toJsonData(req, c, df));
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700140 }
141 result.put("log", entries);
142 if (paginator.getPreviousStart() != null) {
143 result.put("previous", paginator.getPreviousStart().name());
144 }
145 if (paginator.getNextStart() != null) {
146 result.put("next", paginator.getNextStart().name());
147 }
148 renderJson(req, res, result, new TypeToken<Map<String, Object>>() {}.getType());
149 } finally {
150 paginator.getWalk().release();
151 }
152 }
153
Dave Borowitz80334b22013-01-11 14:19:11 -0800154 private static GitilesView getView(HttpServletRequest req, Repository repo) throws IOException {
155 GitilesView view = ViewFilter.getView(req);
156 if (view.getRevision() != Revision.NULL) {
157 return view;
158 }
159 Ref headRef = repo.getRef(Constants.HEAD);
160 if (headRef == null) {
161 return null;
162 }
163 RevWalk walk = new RevWalk(repo);
164 try {
165 return GitilesView.log()
166 .copyFrom(view)
Dave Borowitz48ca6702013-04-09 11:52:41 -0700167 .setRevision(Revision.peel(Constants.HEAD, walk.parseAny(headRef.getObjectId()), walk))
Dave Borowitz80334b22013-01-11 14:19:11 -0800168 .build();
169 } finally {
170 walk.release();
171 }
172 }
173
Dave Borowitz9de65952012-08-13 16:09:45 -0700174 private static Optional<ObjectId> getStart(ListMultimap<String, String> params,
175 ObjectReader reader) throws IOException {
176 List<String> values = params.get(START_PARAM);
177 switch (values.size()) {
178 case 0:
179 return Optional.absent();
180 case 1:
181 Collection<ObjectId> ids = reader.resolve(AbbreviatedObjectId.fromString(values.get(0)));
182 if (ids.size() != 1) {
183 return null;
184 }
185 return Optional.of(Iterables.getOnlyElement(ids));
186 default:
187 return null;
188 }
189 }
190
191 private static RevWalk newWalk(Repository repo, GitilesView view)
192 throws MissingObjectException, IncorrectObjectTypeException, IOException {
193 RevWalk walk = new RevWalk(repo);
194 walk.markStart(walk.parseCommit(view.getRevision().getId()));
195 if (view.getOldRevision() != Revision.NULL) {
196 walk.markUninteresting(walk.parseCommit(view.getOldRevision().getId()));
197 }
Dave Borowitzdd3c3d92013-03-11 16:38:41 -0700198 if (!Strings.isNullOrEmpty(view.getPathPart())) {
Jonathan Niedercb4d91c2013-12-04 16:59:15 -0800199 walk.setTreeFilter(AndTreeFilter.create(
200 PathFilter.create(view.getPathPart()),
201 TreeFilter.ANY_DIFF));
Dave Borowitz9de65952012-08-13 16:09:45 -0700202 }
203 return walk;
204 }
Dave Borowitz27fada42013-11-01 11:09:49 -0700205
206 private static Paginator newPaginator(Repository repo, GitilesView view) throws IOException {
207 if (view == null) {
208 return null;
209 }
210
211 RevWalk walk = null;
212 try {
213 walk = newWalk(repo, view);
214 } catch (IncorrectObjectTypeException e) {
215 return null;
216 }
217
218 Optional<ObjectId> start;
219 try {
220 start = getStart(view.getParameters(), walk.getObjectReader());
221 } catch (IOException e) {
222 walk.release();
223 throw e;
224 }
225 if (start == null) {
226 return null;
227 }
Dave Borowitzef055812013-11-01 12:05:55 -0700228
229 return new Paginator(walk, getLimit(view), start.orNull());
230 }
231
232 private static int getLimit(GitilesView view) {
233 List<String> values = view.getParameters().get(LIMIT_PARAM);
234 if (values.isEmpty()) {
235 return DEFAULT_LIMIT;
236 }
237 Long limit = Longs.tryParse(values.get(0));
238 if (limit == null) {
239 return DEFAULT_LIMIT;
240 }
241 return (int) Math.min(limit, MAX_LIMIT);
Dave Borowitz27fada42013-11-01 11:09:49 -0700242 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700243}