| 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; |
| 25 | import com.google.common.collect.Lists; |
| 26 | import com.google.gitiles.CommitSoyData.KeySet; |
| 27 | |
| 28 | import org.eclipse.jgit.errors.IncorrectObjectTypeException; |
| 29 | import org.eclipse.jgit.errors.MissingObjectException; |
| 30 | import org.eclipse.jgit.http.server.ServletUtils; |
| 31 | import org.eclipse.jgit.lib.Constants; |
| 32 | import org.eclipse.jgit.lib.ObjectId; |
| 33 | import org.eclipse.jgit.lib.Repository; |
| 34 | import org.eclipse.jgit.revwalk.RevCommit; |
| 35 | import org.eclipse.jgit.revwalk.RevObject; |
| 36 | import org.eclipse.jgit.revwalk.RevTag; |
| 37 | import org.eclipse.jgit.revwalk.RevWalk; |
| 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
| 41 | import java.io.IOException; |
| 42 | import java.util.List; |
| 43 | import java.util.Map; |
| 44 | |
| 45 | import javax.servlet.http.HttpServletRequest; |
| 46 | import javax.servlet.http.HttpServletResponse; |
| 47 | |
| 48 | /** Serves an HTML page with detailed information about a ref. */ |
| 49 | public class RevisionServlet extends BaseServlet { |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 50 | private static final long serialVersionUID = 1L; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 51 | private static final Logger log = LoggerFactory.getLogger(RevisionServlet.class); |
| 52 | |
| 53 | private final Linkifier linkifier; |
| 54 | |
| 55 | public RevisionServlet(Renderer renderer, Linkifier linkifier) { |
| 56 | super(renderer); |
| 57 | this.linkifier = checkNotNull(linkifier, "linkifier"); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 62 | GitilesView view = ViewFilter.getView(req); |
| 63 | Repository repo = ServletUtils.getRepository(req); |
| 64 | |
| 65 | RevWalk walk = new RevWalk(repo); |
| 66 | try { |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 67 | List<RevObject> objects = listObjects(walk, view.getRevision()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 68 | List<Map<String, ?>> soyObjects = Lists.newArrayListWithCapacity(objects.size()); |
| 69 | boolean hasBlob = false; |
| 70 | |
| 71 | // TODO(sop): Allow caching commits by SHA-1 when no S cookie is sent. |
| 72 | for (RevObject obj : objects) { |
| 73 | try { |
| 74 | switch (obj.getType()) { |
| 75 | case OBJ_COMMIT: |
| 76 | soyObjects.add(ImmutableMap.of( |
| 77 | "type", Constants.TYPE_COMMIT, |
| 78 | "data", new CommitSoyData(linkifier, req, repo, walk, view) |
| 79 | .toSoyData((RevCommit) obj, KeySet.DETAIL_DIFF_TREE))); |
| 80 | break; |
| 81 | case OBJ_TREE: |
| 82 | soyObjects.add(ImmutableMap.of( |
| 83 | "type", Constants.TYPE_TREE, |
| 84 | "data", new TreeSoyData(walk, view).toSoyData(obj))); |
| 85 | break; |
| 86 | case OBJ_BLOB: |
| 87 | soyObjects.add(ImmutableMap.of( |
| 88 | "type", Constants.TYPE_BLOB, |
| 89 | "data", new BlobSoyData(walk, view).toSoyData(obj))); |
| 90 | hasBlob = true; |
| 91 | break; |
| 92 | case OBJ_TAG: |
| 93 | soyObjects.add(ImmutableMap.of( |
| 94 | "type", Constants.TYPE_TAG, |
| 95 | "data", new TagSoyData(linkifier, req).toSoyData((RevTag) obj))); |
| 96 | break; |
| 97 | default: |
| 98 | log.warn("Bad object type for %s: %s", ObjectId.toString(obj.getId()), obj.getType()); |
| 99 | res.setStatus(SC_NOT_FOUND); |
| 100 | return; |
| 101 | } |
| 102 | } catch (MissingObjectException e) { |
| 103 | log.warn("Missing object " + ObjectId.toString(obj.getId()), e); |
| 104 | res.setStatus(SC_NOT_FOUND); |
| 105 | return; |
| 106 | } catch (IncorrectObjectTypeException e) { |
| 107 | log.warn("Incorrect object type for " + ObjectId.toString(obj.getId()), e); |
| 108 | res.setStatus(SC_NOT_FOUND); |
| 109 | return; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | render(req, res, "gitiles.revisionDetail", ImmutableMap.of( |
| 114 | "title", view.getRevision().getName(), |
| 115 | "objects", soyObjects, |
| 116 | "hasBlob", hasBlob)); |
| 117 | } finally { |
| 118 | walk.release(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // TODO(dborowitz): Extract this. |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 123 | static List<RevObject> listObjects(RevWalk walk, Revision rev) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 124 | throws MissingObjectException, IOException { |
| 125 | List<RevObject> objects = Lists.newArrayListWithExpectedSize(1); |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 126 | ObjectId id = rev.getId(); |
| 127 | RevObject cur; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 128 | while (true) { |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 129 | cur = walk.parseAny(id); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 130 | objects.add(cur); |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 131 | if (cur.getType() != Constants.OBJ_TAG) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 132 | break; |
| 133 | } |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 134 | id = ((RevTag) cur).getObject(); |
| 135 | } |
| 136 | if (cur.getType() == Constants.OBJ_COMMIT) { |
| 137 | objects.add(walk.parseTree(((RevCommit) cur).getTree())); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 138 | } |
| 139 | return objects; |
| 140 | } |
| 141 | } |