blob: a126c7e41223940a830f5ba8aae1669fabbf6e7b [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
17import static com.google.common.base.Preconditions.checkNotNull;
18import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
19import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
20import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
21import static org.eclipse.jgit.lib.Constants.OBJ_TAG;
22import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
23
24import com.google.common.collect.ImmutableMap;
Dave Borowitz01551272014-03-16 13:43:16 -070025import com.google.common.collect.ImmutableSet;
Dave Borowitz9de65952012-08-13 16:09:45 -070026import com.google.common.collect.Lists;
Robert Iannucci5d93b852014-09-27 19:48:44 -070027import com.google.common.io.BaseEncoding;
Dave Borowitz01551272014-03-16 13:43:16 -070028import com.google.gitiles.CommitData.Field;
Dave Borowitz90d1db92014-03-16 14:16:15 -070029import com.google.gitiles.CommitJsonData.Commit;
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070030import com.google.gitiles.DateFormatter.Format;
Dave Borowitz3b744b12016-08-19 16:11:10 -040031import java.io.IOException;
32import java.io.OutputStream;
33import java.io.Writer;
34import java.util.List;
35import java.util.Map;
36import javax.servlet.http.HttpServletRequest;
37import javax.servlet.http.HttpServletResponse;
Dave Borowitz9de65952012-08-13 16:09:45 -070038import org.eclipse.jgit.errors.IncorrectObjectTypeException;
39import org.eclipse.jgit.errors.MissingObjectException;
40import org.eclipse.jgit.http.server.ServletUtils;
Shawn Pearce73e34532015-02-12 16:27:54 -080041import org.eclipse.jgit.lib.Config;
Dave Borowitz9de65952012-08-13 16:09:45 -070042import org.eclipse.jgit.lib.Constants;
43import org.eclipse.jgit.lib.ObjectId;
Robert Iannucci5d93b852014-09-27 19:48:44 -070044import org.eclipse.jgit.lib.ObjectLoader;
Dave Borowitzfe8fdab2014-11-04 16:19:33 -080045import org.eclipse.jgit.lib.ObjectReader;
Dave Borowitz9de65952012-08-13 16:09:45 -070046import org.eclipse.jgit.lib.Repository;
47import org.eclipse.jgit.revwalk.RevCommit;
48import org.eclipse.jgit.revwalk.RevObject;
49import org.eclipse.jgit.revwalk.RevTag;
Shawn Pearce73e34532015-02-12 16:27:54 -080050import org.eclipse.jgit.revwalk.RevTree;
Dave Borowitz9de65952012-08-13 16:09:45 -070051import org.eclipse.jgit.revwalk.RevWalk;
52import org.slf4j.Logger;
53import org.slf4j.LoggerFactory;
54
Dave Borowitz9de65952012-08-13 16:09:45 -070055/** Serves an HTML page with detailed information about a ref. */
56public class RevisionServlet extends BaseServlet {
Dave Borowitz90d1db92014-03-16 14:16:15 -070057 private static final ImmutableSet<Field> COMMIT_SOY_FIELDS =
58 Field.setOf(CommitSoyData.DEFAULT_FIELDS, Field.DIFF_TREE);
59 private static final ImmutableSet<Field> COMMIT_JSON_FIELDS =
60 Field.setOf(CommitJsonData.DEFAULT_FIELDS, Field.DIFF_TREE);
Dave Borowitz01551272014-03-16 13:43:16 -070061
Chad Horohoead23f142012-11-12 09:45:39 -080062 private static final long serialVersionUID = 1L;
Dave Borowitz9de65952012-08-13 16:09:45 -070063 private static final Logger log = LoggerFactory.getLogger(RevisionServlet.class);
64
65 private final Linkifier linkifier;
66
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020067 public RevisionServlet(
68 GitilesAccess.Factory accessFactory, Renderer renderer, Linkifier linkifier) {
Dave Borowitz8d6d6872014-03-16 15:18:14 -070069 super(renderer, accessFactory);
Dave Borowitz9de65952012-08-13 16:09:45 -070070 this.linkifier = checkNotNull(linkifier, "linkifier");
71 }
72
73 @Override
Dave Borowitz90d1db92014-03-16 14:16:15 -070074 protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException {
Dave Borowitz9de65952012-08-13 16:09:45 -070075 GitilesView view = ViewFilter.getView(req);
76 Repository repo = ServletUtils.getRepository(req);
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070077 GitilesAccess access = getAccess(req);
Shawn Pearce73e34532015-02-12 16:27:54 -080078 Config cfg = getAccess(req).getConfig();
Dave Borowitz9de65952012-08-13 16:09:45 -070079
Shawn Pearceb5ad0a02015-05-24 20:33:17 -070080 try (RevWalk walk = new RevWalk(repo)) {
Dave Borowitz2b2f34b2014-04-29 16:47:20 -070081 DateFormatter df = new DateFormatter(access, Format.DEFAULT);
Dave Borowitz558005d2012-12-20 15:48:08 -080082 List<RevObject> objects = listObjects(walk, view.getRevision());
Dave Borowitz9de65952012-08-13 16:09:45 -070083 List<Map<String, ?>> soyObjects = Lists.newArrayListWithCapacity(objects.size());
84 boolean hasBlob = false;
Shawn Pearce73e34532015-02-12 16:27:54 -080085 boolean hasReadme = false;
Dave Borowitz9de65952012-08-13 16:09:45 -070086
87 // TODO(sop): Allow caching commits by SHA-1 when no S cookie is sent.
88 for (RevObject obj : objects) {
89 try {
90 switch (obj.getType()) {
91 case OBJ_COMMIT:
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020092 soyObjects.add(
93 ImmutableMap.of(
94 "type",
95 Constants.TYPE_COMMIT,
96 "data",
97 new CommitSoyData()
98 .setLinkifier(linkifier)
99 .setRevWalk(walk)
100 .setArchiveFormat(getArchiveFormat(access))
101 .toSoyData(req, (RevCommit) obj, COMMIT_SOY_FIELDS, df)));
Dave Borowitz9de65952012-08-13 16:09:45 -0700102 break;
103 case OBJ_TREE:
Shawn Pearce73e34532015-02-12 16:27:54 -0800104 Map<String, Object> tree =
Shawn Pearcec68ad0b2016-05-28 16:52:47 -0700105 new TreeSoyData(
106 walk.getObjectReader(), view, cfg, (RevTree) obj, req.getRequestURI())
107 .toSoyData(obj);
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200108 soyObjects.add(ImmutableMap.of("type", Constants.TYPE_TREE, "data", tree));
Shawn Pearce73e34532015-02-12 16:27:54 -0800109 hasReadme = tree.containsKey("readmeHtml");
Dave Borowitz9de65952012-08-13 16:09:45 -0700110 break;
111 case OBJ_BLOB:
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200112 soyObjects.add(
113 ImmutableMap.of(
114 "type",
115 Constants.TYPE_BLOB,
116 "data",
117 new BlobSoyData(walk.getObjectReader(), view).toSoyData(obj)));
Dave Borowitz9de65952012-08-13 16:09:45 -0700118 hasBlob = true;
119 break;
120 case OBJ_TAG:
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200121 soyObjects.add(
122 ImmutableMap.of(
123 "type",
124 Constants.TYPE_TAG,
125 "data",
126 new TagSoyData(linkifier, req).toSoyData((RevTag) obj, df)));
Dave Borowitz9de65952012-08-13 16:09:45 -0700127 break;
128 default:
Dave Borowitzfd25c3a2013-01-11 14:37:11 -0800129 log.warn("Bad object type for {}: {}", ObjectId.toString(obj.getId()), obj.getType());
Dave Borowitz9de65952012-08-13 16:09:45 -0700130 res.setStatus(SC_NOT_FOUND);
131 return;
132 }
133 } catch (MissingObjectException e) {
134 log.warn("Missing object " + ObjectId.toString(obj.getId()), e);
135 res.setStatus(SC_NOT_FOUND);
136 return;
137 } catch (IncorrectObjectTypeException e) {
138 log.warn("Incorrect object type for " + ObjectId.toString(obj.getId()), e);
139 res.setStatus(SC_NOT_FOUND);
140 return;
141 }
142 }
143
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200144 renderHtml(
145 req,
146 res,
147 "gitiles.revisionDetail",
148 ImmutableMap.of(
149 "title", view.getRevision().getName(),
150 "objects", soyObjects,
151 "hasBlob", hasBlob,
152 "hasReadme", hasReadme));
Dave Borowitz9de65952012-08-13 16:09:45 -0700153 }
154 }
155
Dave Borowitz90d1db92014-03-16 14:16:15 -0700156 @Override
Robert Iannucci5d93b852014-09-27 19:48:44 -0700157 protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
158 GitilesView view = ViewFilter.getView(req);
159 Repository repo = ServletUtils.getRepository(req);
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700160 try (ObjectReader reader = repo.newObjectReader()) {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200161 ObjectLoader loader = reader.open(view.getRevision().getId());
162 if (loader.getType() != OBJ_COMMIT) {
163 res.setStatus(SC_NOT_FOUND);
164 } else {
165 PathServlet.setTypeHeader(res, loader.getType());
166 try (Writer writer = startRenderText(req, res);
167 OutputStream out = BaseEncoding.base64().encodingStream(writer)) {
168 loader.copyTo(out);
Robert Iannucci5d93b852014-09-27 19:48:44 -0700169 }
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200170 }
Robert Iannucci5d93b852014-09-27 19:48:44 -0700171 }
172 }
173
174 @Override
Dave Borowitz90d1db92014-03-16 14:16:15 -0700175 protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException {
176 GitilesView view = ViewFilter.getView(req);
177 Repository repo = ServletUtils.getRepository(req);
178
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700179 try (RevWalk walk = new RevWalk(repo)) {
Dave Borowitz2b2f34b2014-04-29 16:47:20 -0700180 DateFormatter df = new DateFormatter(getAccess(req), Format.DEFAULT);
Dave Borowitz90d1db92014-03-16 14:16:15 -0700181 RevObject obj = walk.parseAny(view.getRevision().getId());
182 switch (obj.getType()) {
183 case OBJ_COMMIT:
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +0200184 renderJson(
185 req,
186 res,
187 new CommitJsonData()
188 .setRevWalk(walk)
189 .toJsonData(req, (RevCommit) obj, COMMIT_JSON_FIELDS, df),
Dave Borowitz90d1db92014-03-16 14:16:15 -0700190 Commit.class);
191 break;
192 default:
193 // TODO(dborowitz): Support showing other types.
194 res.setStatus(SC_NOT_FOUND);
195 break;
196 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700197 }
198 }
199
Dave Borowitz9de65952012-08-13 16:09:45 -0700200 // TODO(dborowitz): Extract this.
Dave Borowitz558005d2012-12-20 15:48:08 -0800201 static List<RevObject> listObjects(RevWalk walk, Revision rev)
Dave Borowitz9de65952012-08-13 16:09:45 -0700202 throws MissingObjectException, IOException {
203 List<RevObject> objects = Lists.newArrayListWithExpectedSize(1);
Dave Borowitz558005d2012-12-20 15:48:08 -0800204 ObjectId id = rev.getId();
205 RevObject cur;
Dave Borowitz9de65952012-08-13 16:09:45 -0700206 while (true) {
Dave Borowitz558005d2012-12-20 15:48:08 -0800207 cur = walk.parseAny(id);
Dave Borowitz9de65952012-08-13 16:09:45 -0700208 objects.add(cur);
Dave Borowitz558005d2012-12-20 15:48:08 -0800209 if (cur.getType() != Constants.OBJ_TAG) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700210 break;
211 }
Dave Borowitz558005d2012-12-20 15:48:08 -0800212 id = ((RevTag) cur).getObject();
213 }
214 if (cur.getType() == Constants.OBJ_COMMIT) {
215 objects.add(walk.parseTree(((RevCommit) cur).getTree()));
Dave Borowitz9de65952012-08-13 16:09:45 -0700216 }
217 return objects;
218 }
219}