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