blob: ffad8d8223a905b22a3a08172f9bacf90ec980f8 [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 Borowitzd6ebc2d2013-11-01 11:44:06 -070028import com.google.gson.reflect.TypeToken;
Dave Borowitz9de65952012-08-13 16:09:45 -070029
30import org.eclipse.jgit.errors.IncorrectObjectTypeException;
31import org.eclipse.jgit.errors.MissingObjectException;
32import org.eclipse.jgit.errors.RevWalkException;
33import org.eclipse.jgit.http.server.ServletUtils;
34import org.eclipse.jgit.lib.AbbreviatedObjectId;
Dave Borowitz54271462013-11-11 11:43:11 -080035import org.eclipse.jgit.lib.Config;
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 Borowitz54271462013-11-11 11:43:11 -080071 public LogServlet(Config cfg, Renderer renderer, Linkifier linkifier) {
72 super(cfg, renderer);
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 Borowitz27fada42013-11-01 11:09:49 -070087 Map<String, Object> data = new LogSoyData(req, view).toSoyData(paginator, null);
Dave Borowitz9de65952012-08-13 16:09:45 -070088
89 if (!view.getRevision().nameIsId()) {
90 List<Map<String, Object>> tags = Lists.newArrayListWithExpectedSize(1);
Dave Borowitz27fada42013-11-01 11:09:49 -070091 for (RevObject o : RevisionServlet.listObjects(paginator.getWalk(), view.getRevision())) {
Dave Borowitz9de65952012-08-13 16:09:45 -070092 if (o instanceof RevTag) {
93 tags.add(new TagSoyData(linkifier, req).toSoyData((RevTag) o));
94 }
95 }
96 if (!tags.isEmpty()) {
97 data.put("tags", tags);
98 }
99 }
100
Dave Borowitz9de65952012-08-13 16:09:45 -0700101 String title = "Log - ";
102 if (view.getOldRevision() != Revision.NULL) {
103 title += view.getRevisionRange();
104 } else {
105 title += view.getRevision().getName();
106 }
107
108 data.put("title", title);
Dave Borowitz9de65952012-08-13 16:09:45 -0700109
Dave Borowitzb1c628f2013-01-11 11:28:20 -0800110 renderHtml(req, res, "gitiles.logDetail", data);
Dave Borowitz9de65952012-08-13 16:09:45 -0700111 } catch (RevWalkException e) {
112 log.warn("Error in rev walk", e);
113 res.setStatus(SC_INTERNAL_SERVER_ERROR);
114 return;
115 } finally {
Dave Borowitz27fada42013-11-01 11:09:49 -0700116 paginator.getWalk().release();
Dave Borowitz9de65952012-08-13 16:09:45 -0700117 }
118 }
119
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700120 @Override
121 protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException {
122 Repository repo = ServletUtils.getRepository(req);
123 GitilesView view = getView(req, repo);
124 Paginator paginator = newPaginator(repo, view);
125 if (paginator == null) {
126 res.setStatus(SC_NOT_FOUND);
127 return;
128 }
129
130 try {
131 Map<String, Object> result = Maps.newLinkedHashMap();
132 List<CommitJsonData.Commit> entries = Lists.newArrayListWithCapacity(paginator.getLimit());
133 for (RevCommit c : paginator) {
134 paginator.getWalk().parseBody(c);
135 entries.add(CommitJsonData.toJsonData(c));
136 }
137 result.put("log", entries);
138 if (paginator.getPreviousStart() != null) {
139 result.put("previous", paginator.getPreviousStart().name());
140 }
141 if (paginator.getNextStart() != null) {
142 result.put("next", paginator.getNextStart().name());
143 }
144 renderJson(req, res, result, new TypeToken<Map<String, Object>>() {}.getType());
145 } finally {
146 paginator.getWalk().release();
147 }
148 }
149
Dave Borowitz80334b22013-01-11 14:19:11 -0800150 private static GitilesView getView(HttpServletRequest req, Repository repo) throws IOException {
151 GitilesView view = ViewFilter.getView(req);
152 if (view.getRevision() != Revision.NULL) {
153 return view;
154 }
155 Ref headRef = repo.getRef(Constants.HEAD);
156 if (headRef == null) {
157 return null;
158 }
159 RevWalk walk = new RevWalk(repo);
160 try {
161 return GitilesView.log()
162 .copyFrom(view)
Dave Borowitz48ca6702013-04-09 11:52:41 -0700163 .setRevision(Revision.peel(Constants.HEAD, walk.parseAny(headRef.getObjectId()), walk))
Dave Borowitz80334b22013-01-11 14:19:11 -0800164 .build();
165 } finally {
166 walk.release();
167 }
168 }
169
Dave Borowitz9de65952012-08-13 16:09:45 -0700170 private static Optional<ObjectId> getStart(ListMultimap<String, String> params,
171 ObjectReader reader) throws IOException {
172 List<String> values = params.get(START_PARAM);
173 switch (values.size()) {
174 case 0:
175 return Optional.absent();
176 case 1:
177 Collection<ObjectId> ids = reader.resolve(AbbreviatedObjectId.fromString(values.get(0)));
178 if (ids.size() != 1) {
179 return null;
180 }
181 return Optional.of(Iterables.getOnlyElement(ids));
182 default:
183 return null;
184 }
185 }
186
187 private static RevWalk newWalk(Repository repo, GitilesView view)
188 throws MissingObjectException, IncorrectObjectTypeException, IOException {
189 RevWalk walk = new RevWalk(repo);
190 walk.markStart(walk.parseCommit(view.getRevision().getId()));
191 if (view.getOldRevision() != Revision.NULL) {
192 walk.markUninteresting(walk.parseCommit(view.getOldRevision().getId()));
193 }
Dave Borowitzdd3c3d92013-03-11 16:38:41 -0700194 if (!Strings.isNullOrEmpty(view.getPathPart())) {
Jonathan Niedercb4d91c2013-12-04 16:59:15 -0800195 walk.setTreeFilter(AndTreeFilter.create(
196 PathFilter.create(view.getPathPart()),
197 TreeFilter.ANY_DIFF));
Dave Borowitz9de65952012-08-13 16:09:45 -0700198 }
199 return walk;
200 }
Dave Borowitz27fada42013-11-01 11:09:49 -0700201
202 private static Paginator newPaginator(Repository repo, GitilesView view) throws IOException {
203 if (view == null) {
204 return null;
205 }
206
207 RevWalk walk = null;
208 try {
209 walk = newWalk(repo, view);
210 } catch (IncorrectObjectTypeException e) {
211 return null;
212 }
213
214 Optional<ObjectId> start;
215 try {
216 start = getStart(view.getParameters(), walk.getObjectReader());
217 } catch (IOException e) {
218 walk.release();
219 throw e;
220 }
221 if (start == null) {
222 return null;
223 }
Dave Borowitzef055812013-11-01 12:05:55 -0700224
225 return new Paginator(walk, getLimit(view), start.orNull());
226 }
227
228 private static int getLimit(GitilesView view) {
229 List<String> values = view.getParameters().get(LIMIT_PARAM);
230 if (values.isEmpty()) {
231 return DEFAULT_LIMIT;
232 }
233 Long limit = Longs.tryParse(values.get(0));
234 if (limit == null) {
235 return DEFAULT_LIMIT;
236 }
237 return (int) Math.min(limit, MAX_LIMIT);
Dave Borowitz27fada42013-11-01 11:09:49 -0700238 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700239}