| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| 17 | import static com.google.common.base.Preconditions.checkNotNull; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 18 | import static org.eclipse.jgit.lib.Constants.OBJ_BLOB; |
| 19 | import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT; |
| 20 | import static org.eclipse.jgit.lib.Constants.OBJ_TAG; |
| 21 | import static org.eclipse.jgit.lib.Constants.OBJ_TREE; |
| 22 | |
| 23 | import com.google.common.collect.ImmutableMap; |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableSet; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Lists; |
| Robert Iannucci | 5d93b85 | 2014-09-27 19:48:44 -0700 | [diff] [blame] | 26 | import com.google.common.io.BaseEncoding; |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 27 | import com.google.gitiles.CommitData.Field; |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 28 | import com.google.gitiles.CommitJsonData.Commit; |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 29 | import com.google.gitiles.DateFormatter.Format; |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame^] | 30 | import com.google.gitiles.GitilesRequestFailureException.FailureReason; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 31 | import java.io.IOException; |
| 32 | import java.io.OutputStream; |
| 33 | import java.io.Writer; |
| 34 | import java.util.List; |
| 35 | import java.util.Map; |
| 36 | import javax.servlet.http.HttpServletRequest; |
| 37 | import javax.servlet.http.HttpServletResponse; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 38 | import org.eclipse.jgit.errors.IncorrectObjectTypeException; |
| 39 | import org.eclipse.jgit.errors.MissingObjectException; |
| 40 | import org.eclipse.jgit.http.server.ServletUtils; |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame] | 41 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 42 | import org.eclipse.jgit.lib.Constants; |
| 43 | import org.eclipse.jgit.lib.ObjectId; |
| Robert Iannucci | 5d93b85 | 2014-09-27 19:48:44 -0700 | [diff] [blame] | 44 | import org.eclipse.jgit.lib.ObjectLoader; |
| Dave Borowitz | fe8fdab | 2014-11-04 16:19:33 -0800 | [diff] [blame] | 45 | import org.eclipse.jgit.lib.ObjectReader; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 46 | import org.eclipse.jgit.lib.Repository; |
| 47 | import org.eclipse.jgit.revwalk.RevCommit; |
| 48 | import org.eclipse.jgit.revwalk.RevObject; |
| 49 | import org.eclipse.jgit.revwalk.RevTag; |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame] | 50 | import org.eclipse.jgit.revwalk.RevTree; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 51 | import org.eclipse.jgit.revwalk.RevWalk; |
| 52 | import org.slf4j.Logger; |
| 53 | import org.slf4j.LoggerFactory; |
| 54 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 55 | /** Serves an HTML page with detailed information about a ref. */ |
| 56 | public class RevisionServlet extends BaseServlet { |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 57 | 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 Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 61 | |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 62 | private static final long serialVersionUID = 1L; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 63 | private static final Logger log = LoggerFactory.getLogger(RevisionServlet.class); |
| 64 | |
| 65 | private final Linkifier linkifier; |
| 66 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 67 | public RevisionServlet( |
| 68 | GitilesAccess.Factory accessFactory, Renderer renderer, Linkifier linkifier) { |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 69 | super(renderer, accessFactory); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 70 | this.linkifier = checkNotNull(linkifier, "linkifier"); |
| 71 | } |
| 72 | |
| 73 | @Override |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 74 | protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 75 | GitilesView view = ViewFilter.getView(req); |
| 76 | Repository repo = ServletUtils.getRepository(req); |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 77 | GitilesAccess access = getAccess(req); |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame] | 78 | Config cfg = getAccess(req).getConfig(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 79 | |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 80 | try (RevWalk walk = new RevWalk(repo)) { |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 81 | DateFormatter df = new DateFormatter(access, Format.DEFAULT); |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 82 | List<RevObject> objects = listObjects(walk, view.getRevision()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 83 | List<Map<String, ?>> soyObjects = Lists.newArrayListWithCapacity(objects.size()); |
| 84 | boolean hasBlob = false; |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame] | 85 | boolean hasReadme = false; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 86 | |
| 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 Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 92 | soyObjects.add( |
| 93 | ImmutableMap.of( |
| 94 | "type", |
| 95 | Constants.TYPE_COMMIT, |
| 96 | "data", |
| 97 | new CommitSoyData() |
| 98 | .setLinkifier(linkifier) |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 99 | .setArchiveFormat(getArchiveFormat(access)) |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 100 | .toSoyData(req, walk, (RevCommit) obj, COMMIT_SOY_FIELDS, df))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 101 | break; |
| 102 | case OBJ_TREE: |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame] | 103 | Map<String, Object> tree = |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 104 | new TreeSoyData( |
| 105 | walk.getObjectReader(), view, cfg, (RevTree) obj, req.getRequestURI()) |
| 106 | .toSoyData(obj); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 107 | soyObjects.add(ImmutableMap.of("type", Constants.TYPE_TREE, "data", tree)); |
| Shawn Pearce | 73e3453 | 2015-02-12 16:27:54 -0800 | [diff] [blame] | 108 | hasReadme = tree.containsKey("readmeHtml"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 109 | break; |
| 110 | case OBJ_BLOB: |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 111 | soyObjects.add( |
| 112 | ImmutableMap.of( |
| 113 | "type", |
| 114 | Constants.TYPE_BLOB, |
| 115 | "data", |
| 116 | new BlobSoyData(walk.getObjectReader(), view).toSoyData(obj))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 117 | hasBlob = true; |
| 118 | break; |
| 119 | case OBJ_TAG: |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 120 | soyObjects.add( |
| 121 | ImmutableMap.of( |
| 122 | "type", |
| 123 | Constants.TYPE_TAG, |
| 124 | "data", |
| Jonathan Nieder | 9e3b1b7 | 2019-03-07 14:38:28 -0800 | [diff] [blame] | 125 | new TagSoyData(linkifier, req).toSoyData(walk, (RevTag) obj, df))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 126 | break; |
| 127 | default: |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame^] | 128 | throw new GitilesRequestFailureException(FailureReason.UNSUPPORTED_OBJECT_TYPE); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 129 | } |
| 130 | } catch (MissingObjectException e) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame^] | 131 | throw new GitilesRequestFailureException(FailureReason.OBJECT_NOT_FOUND, e); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 132 | } catch (IncorrectObjectTypeException e) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame^] | 133 | throw new GitilesRequestFailureException(FailureReason.INCORRECT_OBJECT_TYPE, e); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 137 | renderHtml( |
| 138 | req, |
| 139 | res, |
| 140 | "gitiles.revisionDetail", |
| 141 | ImmutableMap.of( |
| 142 | "title", view.getRevision().getName(), |
| 143 | "objects", soyObjects, |
| 144 | "hasBlob", hasBlob, |
| 145 | "hasReadme", hasReadme)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 149 | @Override |
| Robert Iannucci | 5d93b85 | 2014-09-27 19:48:44 -0700 | [diff] [blame] | 150 | protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 151 | GitilesView view = ViewFilter.getView(req); |
| 152 | Repository repo = ServletUtils.getRepository(req); |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 153 | try (ObjectReader reader = repo.newObjectReader()) { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 154 | ObjectLoader loader = reader.open(view.getRevision().getId()); |
| 155 | if (loader.getType() != OBJ_COMMIT) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame^] | 156 | throw new GitilesRequestFailureException(FailureReason.UNSUPPORTED_OBJECT_TYPE); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 157 | } else { |
| 158 | PathServlet.setTypeHeader(res, loader.getType()); |
| 159 | try (Writer writer = startRenderText(req, res); |
| 160 | OutputStream out = BaseEncoding.base64().encodingStream(writer)) { |
| 161 | loader.copyTo(out); |
| Robert Iannucci | 5d93b85 | 2014-09-27 19:48:44 -0700 | [diff] [blame] | 162 | } |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 163 | } |
| Robert Iannucci | 5d93b85 | 2014-09-27 19:48:44 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | @Override |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 168 | protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 169 | GitilesView view = ViewFilter.getView(req); |
| 170 | Repository repo = ServletUtils.getRepository(req); |
| 171 | |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 172 | try (RevWalk walk = new RevWalk(repo)) { |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 173 | DateFormatter df = new DateFormatter(getAccess(req), Format.DEFAULT); |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 174 | RevObject obj = walk.parseAny(view.getRevision().getId()); |
| 175 | switch (obj.getType()) { |
| 176 | case OBJ_COMMIT: |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 177 | renderJson( |
| 178 | req, |
| 179 | res, |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 180 | new CommitJsonData().toJsonData(req, walk, (RevCommit) obj, COMMIT_JSON_FIELDS, df), |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 181 | Commit.class); |
| 182 | break; |
| 183 | default: |
| 184 | // TODO(dborowitz): Support showing other types. |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame^] | 185 | throw new GitilesRequestFailureException(FailureReason.UNSUPPORTED_OBJECT_TYPE); |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 186 | } |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 190 | // TODO(dborowitz): Extract this. |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 191 | static List<RevObject> listObjects(RevWalk walk, Revision rev) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 192 | throws MissingObjectException, IOException { |
| 193 | List<RevObject> objects = Lists.newArrayListWithExpectedSize(1); |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 194 | ObjectId id = rev.getId(); |
| 195 | RevObject cur; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 196 | while (true) { |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 197 | cur = walk.parseAny(id); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 198 | objects.add(cur); |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 199 | if (cur.getType() != Constants.OBJ_TAG) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 200 | break; |
| 201 | } |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 202 | id = ((RevTag) cur).getObject(); |
| 203 | } |
| 204 | if (cur.getType() == Constants.OBJ_COMMIT) { |
| 205 | objects.add(walk.parseTree(((RevCommit) cur).getTree())); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 206 | } |
| 207 | return objects; |
| 208 | } |
| 209 | } |