| 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; |
| 18 | import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; |
| 19 | import static org.eclipse.jgit.lib.Constants.OBJ_BLOB; |
| 20 | import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT; |
| 21 | import static org.eclipse.jgit.lib.Constants.OBJ_TAG; |
| 22 | import static org.eclipse.jgit.lib.Constants.OBJ_TREE; |
| 23 | |
| 24 | import com.google.common.collect.ImmutableMap; |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableSet; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 26 | import com.google.common.collect.Lists; |
| 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; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 30 | |
| 31 | import org.eclipse.jgit.errors.IncorrectObjectTypeException; |
| 32 | import org.eclipse.jgit.errors.MissingObjectException; |
| 33 | import org.eclipse.jgit.http.server.ServletUtils; |
| 34 | import org.eclipse.jgit.lib.Constants; |
| 35 | import org.eclipse.jgit.lib.ObjectId; |
| 36 | import org.eclipse.jgit.lib.Repository; |
| 37 | import org.eclipse.jgit.revwalk.RevCommit; |
| 38 | import org.eclipse.jgit.revwalk.RevObject; |
| 39 | import org.eclipse.jgit.revwalk.RevTag; |
| 40 | import org.eclipse.jgit.revwalk.RevWalk; |
| 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
| 43 | |
| 44 | import java.io.IOException; |
| 45 | import java.util.List; |
| 46 | import java.util.Map; |
| 47 | |
| 48 | import javax.servlet.http.HttpServletRequest; |
| 49 | import javax.servlet.http.HttpServletResponse; |
| 50 | |
| 51 | /** Serves an HTML page with detailed information about a ref. */ |
| 52 | public class RevisionServlet extends BaseServlet { |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 53 | private static final ImmutableSet<Field> COMMIT_SOY_FIELDS = |
| 54 | Field.setOf(CommitSoyData.DEFAULT_FIELDS, Field.DIFF_TREE); |
| 55 | private static final ImmutableSet<Field> COMMIT_JSON_FIELDS = |
| 56 | Field.setOf(CommitJsonData.DEFAULT_FIELDS, Field.DIFF_TREE); |
| Dave Borowitz | 0155127 | 2014-03-16 13:43:16 -0700 | [diff] [blame] | 57 | |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 58 | private static final long serialVersionUID = 1L; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 59 | private static final Logger log = LoggerFactory.getLogger(RevisionServlet.class); |
| 60 | |
| 61 | private final Linkifier linkifier; |
| 62 | |
| Dave Borowitz | ded109a | 2014-03-03 15:25:39 -0500 | [diff] [blame] | 63 | public RevisionServlet(GitilesAccess.Factory accessFactory, Renderer renderer, |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 64 | Linkifier linkifier) { |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 65 | super(renderer, accessFactory); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 66 | this.linkifier = checkNotNull(linkifier, "linkifier"); |
| 67 | } |
| 68 | |
| 69 | @Override |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 70 | protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 71 | GitilesView view = ViewFilter.getView(req); |
| 72 | Repository repo = ServletUtils.getRepository(req); |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 73 | GitilesAccess access = getAccess(req); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 74 | |
| 75 | RevWalk walk = new RevWalk(repo); |
| 76 | try { |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 77 | DateFormatter df = new DateFormatter(access, Format.DEFAULT); |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 78 | List<RevObject> objects = listObjects(walk, view.getRevision()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 79 | List<Map<String, ?>> soyObjects = Lists.newArrayListWithCapacity(objects.size()); |
| 80 | boolean hasBlob = false; |
| 81 | |
| 82 | // TODO(sop): Allow caching commits by SHA-1 when no S cookie is sent. |
| 83 | for (RevObject obj : objects) { |
| 84 | try { |
| 85 | switch (obj.getType()) { |
| 86 | case OBJ_COMMIT: |
| 87 | soyObjects.add(ImmutableMap.of( |
| 88 | "type", Constants.TYPE_COMMIT, |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 89 | "data", new CommitSoyData() |
| 90 | .setLinkifier(linkifier) |
| 91 | .setRevWalk(walk) |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 92 | .setArchiveFormat(getArchiveFormat(access)) |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 93 | .toSoyData(req, (RevCommit) obj, COMMIT_SOY_FIELDS, df))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 94 | break; |
| 95 | case OBJ_TREE: |
| 96 | soyObjects.add(ImmutableMap.of( |
| 97 | "type", Constants.TYPE_TREE, |
| Dave Borowitz | 6e79748 | 2014-05-01 11:10:01 -0700 | [diff] [blame] | 98 | "data", new TreeSoyData(walk.getObjectReader(), view).toSoyData(obj))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 99 | break; |
| 100 | case OBJ_BLOB: |
| 101 | soyObjects.add(ImmutableMap.of( |
| 102 | "type", Constants.TYPE_BLOB, |
| Dave Borowitz | 6e79748 | 2014-05-01 11:10:01 -0700 | [diff] [blame] | 103 | "data", new BlobSoyData(walk.getObjectReader(), view).toSoyData(obj))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 104 | hasBlob = true; |
| 105 | break; |
| 106 | case OBJ_TAG: |
| 107 | soyObjects.add(ImmutableMap.of( |
| 108 | "type", Constants.TYPE_TAG, |
| Dave Borowitz | a03760a | 2014-01-29 16:17:28 -0800 | [diff] [blame] | 109 | "data", new TagSoyData(linkifier, req).toSoyData((RevTag) obj, df))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 110 | break; |
| 111 | default: |
| Dave Borowitz | fd25c3a | 2013-01-11 14:37:11 -0800 | [diff] [blame] | 112 | log.warn("Bad object type for {}: {}", ObjectId.toString(obj.getId()), obj.getType()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 113 | res.setStatus(SC_NOT_FOUND); |
| 114 | return; |
| 115 | } |
| 116 | } catch (MissingObjectException e) { |
| 117 | log.warn("Missing object " + ObjectId.toString(obj.getId()), e); |
| 118 | res.setStatus(SC_NOT_FOUND); |
| 119 | return; |
| 120 | } catch (IncorrectObjectTypeException e) { |
| 121 | log.warn("Incorrect object type for " + ObjectId.toString(obj.getId()), e); |
| 122 | res.setStatus(SC_NOT_FOUND); |
| 123 | return; |
| 124 | } |
| 125 | } |
| 126 | |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 127 | renderHtml(req, res, "gitiles.revisionDetail", ImmutableMap.of( |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 128 | "title", view.getRevision().getName(), |
| 129 | "objects", soyObjects, |
| 130 | "hasBlob", hasBlob)); |
| 131 | } finally { |
| 132 | walk.release(); |
| 133 | } |
| 134 | } |
| 135 | |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 136 | @Override |
| 137 | protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 138 | GitilesView view = ViewFilter.getView(req); |
| 139 | Repository repo = ServletUtils.getRepository(req); |
| 140 | |
| 141 | RevWalk walk = new RevWalk(repo); |
| 142 | try { |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 143 | DateFormatter df = new DateFormatter(getAccess(req), Format.DEFAULT); |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 144 | RevObject obj = walk.parseAny(view.getRevision().getId()); |
| 145 | switch (obj.getType()) { |
| 146 | case OBJ_COMMIT: |
| 147 | renderJson(req, res, new CommitJsonData() |
| 148 | .setRevWalk(walk) |
| 149 | .toJsonData(req, (RevCommit) obj, COMMIT_JSON_FIELDS, df), |
| 150 | Commit.class); |
| 151 | break; |
| 152 | default: |
| 153 | // TODO(dborowitz): Support showing other types. |
| 154 | res.setStatus(SC_NOT_FOUND); |
| 155 | break; |
| 156 | } |
| 157 | } finally { |
| 158 | walk.release(); |
| 159 | } |
| 160 | } |
| 161 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 162 | // TODO(dborowitz): Extract this. |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 163 | static List<RevObject> listObjects(RevWalk walk, Revision rev) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 164 | throws MissingObjectException, IOException { |
| 165 | List<RevObject> objects = Lists.newArrayListWithExpectedSize(1); |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 166 | ObjectId id = rev.getId(); |
| 167 | RevObject cur; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 168 | while (true) { |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 169 | cur = walk.parseAny(id); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 170 | objects.add(cur); |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 171 | if (cur.getType() != Constants.OBJ_TAG) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 172 | break; |
| 173 | } |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 174 | id = ((RevTag) cur).getObject(); |
| 175 | } |
| 176 | if (cur.getType() == Constants.OBJ_COMMIT) { |
| 177 | objects.add(walk.parseTree(((RevCommit) cur).getTree())); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 178 | } |
| 179 | return objects; |
| 180 | } |
| 181 | } |