blob: 748c5c99451b8488803951b397651ed4fc594f60 [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;
Dave Borowitzb772cce2012-12-28 13:57:22 -080018
Dave Borowitz9de65952012-08-13 16:09:45 -070019import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
20import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
21
Dave Borowitzb772cce2012-12-28 13:57:22 -080022import java.io.IOException;
23import java.util.Collection;
24import java.util.List;
25import java.util.Map;
26import java.util.Set;
27
28import javax.servlet.http.HttpServletRequest;
29import javax.servlet.http.HttpServletResponse;
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;
36import org.eclipse.jgit.lib.AnyObjectId;
37import org.eclipse.jgit.lib.ObjectId;
38import org.eclipse.jgit.lib.ObjectReader;
39import org.eclipse.jgit.lib.Ref;
40import org.eclipse.jgit.lib.Repository;
41import org.eclipse.jgit.revwalk.FollowFilter;
42import org.eclipse.jgit.revwalk.RevCommit;
43import org.eclipse.jgit.revwalk.RevObject;
44import org.eclipse.jgit.revwalk.RevTag;
45import org.eclipse.jgit.revwalk.RevWalk;
46import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
Dave Borowitzb772cce2012-12-28 13:57:22 -080049import com.google.common.base.Optional;
50import com.google.common.base.Strings;
51import com.google.common.collect.Iterables;
52import com.google.common.collect.ListMultimap;
53import com.google.common.collect.Lists;
54import com.google.gitiles.CommitSoyData.KeySet;
Dave Borowitz9de65952012-08-13 16:09:45 -070055
56/** Serves an HTML page with a shortlog for commits and paths. */
57public class LogServlet extends BaseServlet {
Chad Horohoead23f142012-11-12 09:45:39 -080058 private static final long serialVersionUID = 1L;
Dave Borowitz9de65952012-08-13 16:09:45 -070059 private static final Logger log = LoggerFactory.getLogger(LogServlet.class);
60
Dave Borowitzb772cce2012-12-28 13:57:22 -080061 static final String START_PARAM = "s";
62 private static final int LIMIT = 100;
Dave Borowitz9de65952012-08-13 16:09:45 -070063
64 private final Linkifier linkifier;
Dave Borowitz9de65952012-08-13 16:09:45 -070065
66 public LogServlet(Renderer renderer, Linkifier linkifier) {
Dave Borowitz9de65952012-08-13 16:09:45 -070067 super(renderer);
68 this.linkifier = checkNotNull(linkifier, "linkifier");
Dave Borowitz9de65952012-08-13 16:09:45 -070069 }
70
71 @Override
72 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
73 GitilesView view = ViewFilter.getView(req);
74 Repository repo = ServletUtils.getRepository(req);
75 RevWalk walk = null;
76 try {
77 try {
78 walk = newWalk(repo, view);
79 } catch (IncorrectObjectTypeException e) {
80 res.setStatus(SC_NOT_FOUND);
81 return;
82 }
83
84 Optional<ObjectId> start = getStart(view.getParameters(), walk.getObjectReader());
85 if (start == null) {
86 res.setStatus(SC_NOT_FOUND);
87 return;
88 }
89
Dave Borowitzb772cce2012-12-28 13:57:22 -080090 Map<String, Object> data = new LogSoyData(req, repo, view)
91 .toSoyData(walk, LIMIT, null, start.orNull());
Dave Borowitz9de65952012-08-13 16:09:45 -070092
93 if (!view.getRevision().nameIsId()) {
94 List<Map<String, Object>> tags = Lists.newArrayListWithExpectedSize(1);
Dave Borowitz558005d2012-12-20 15:48:08 -080095 for (RevObject o : RevisionServlet.listObjects(walk, view.getRevision())) {
Dave Borowitz9de65952012-08-13 16:09:45 -070096 if (o instanceof RevTag) {
97 tags.add(new TagSoyData(linkifier, req).toSoyData((RevTag) o));
98 }
99 }
100 if (!tags.isEmpty()) {
101 data.put("tags", tags);
102 }
103 }
104
Dave Borowitzb772cce2012-12-28 13:57:22 -0800105 Paginator paginator = new Paginator(walk, LIMIT, start.orNull());
Dave Borowitz9de65952012-08-13 16:09:45 -0700106 Map<AnyObjectId, Set<Ref>> refsById = repo.getAllRefsByPeeledObjectId();
Dave Borowitzb772cce2012-12-28 13:57:22 -0800107 List<Map<String, Object>> entries = Lists.newArrayListWithCapacity(LIMIT);
Dave Borowitz9de65952012-08-13 16:09:45 -0700108 for (RevCommit c : paginator) {
109 entries.add(new CommitSoyData(null, req, repo, walk, view, refsById)
110 .toSoyData(c, KeySet.SHORTLOG));
111 }
112
113 String title = "Log - ";
114 if (view.getOldRevision() != Revision.NULL) {
115 title += view.getRevisionRange();
116 } else {
117 title += view.getRevision().getName();
118 }
119
120 data.put("title", title);
Dave Borowitz9de65952012-08-13 16:09:45 -0700121
122 render(req, res, "gitiles.logDetail", data);
123 } catch (RevWalkException e) {
124 log.warn("Error in rev walk", e);
125 res.setStatus(SC_INTERNAL_SERVER_ERROR);
126 return;
127 } finally {
128 if (walk != null) {
129 walk.release();
130 }
131 }
132 }
133
Dave Borowitz9de65952012-08-13 16:09:45 -0700134 private static Optional<ObjectId> getStart(ListMultimap<String, String> params,
135 ObjectReader reader) throws IOException {
136 List<String> values = params.get(START_PARAM);
137 switch (values.size()) {
138 case 0:
139 return Optional.absent();
140 case 1:
141 Collection<ObjectId> ids = reader.resolve(AbbreviatedObjectId.fromString(values.get(0)));
142 if (ids.size() != 1) {
143 return null;
144 }
145 return Optional.of(Iterables.getOnlyElement(ids));
146 default:
147 return null;
148 }
149 }
150
151 private static RevWalk newWalk(Repository repo, GitilesView view)
152 throws MissingObjectException, IncorrectObjectTypeException, IOException {
153 RevWalk walk = new RevWalk(repo);
154 walk.markStart(walk.parseCommit(view.getRevision().getId()));
155 if (view.getOldRevision() != Revision.NULL) {
156 walk.markUninteresting(walk.parseCommit(view.getOldRevision().getId()));
157 }
158 if (!Strings.isNullOrEmpty(view.getTreePath())) {
159 walk.setTreeFilter(FollowFilter.create(view.getTreePath()));
160 }
161 return walk;
162 }
163}