| 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; |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 31 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 32 | import org.eclipse.jgit.lib.Constants; |
| 33 | import org.eclipse.jgit.lib.ObjectId; |
| 34 | import org.eclipse.jgit.lib.Repository; |
| 35 | import org.eclipse.jgit.revwalk.RevCommit; |
| 36 | import org.eclipse.jgit.revwalk.RevObject; |
| 37 | import org.eclipse.jgit.revwalk.RevTag; |
| 38 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | a03760a | 2014-01-29 16:17:28 -0800 | [diff] [blame] | 39 | import org.eclipse.jgit.util.GitDateFormatter; |
| 40 | import org.eclipse.jgit.util.GitDateFormatter.Format; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 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 { |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 53 | private static final long serialVersionUID = 1L; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 54 | private static final Logger log = LoggerFactory.getLogger(RevisionServlet.class); |
| 55 | |
| 56 | private final Linkifier linkifier; |
| 57 | |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 58 | public RevisionServlet(Config cfg, Renderer renderer, Linkifier linkifier) { |
| Dave Borowitz | 5427146 | 2013-11-11 11:43:11 -0800 | [diff] [blame] | 59 | super(cfg, renderer); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 60 | this.linkifier = checkNotNull(linkifier, "linkifier"); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 65 | GitilesView view = ViewFilter.getView(req); |
| 66 | Repository repo = ServletUtils.getRepository(req); |
| 67 | |
| 68 | RevWalk walk = new RevWalk(repo); |
| 69 | try { |
| Dave Borowitz | a03760a | 2014-01-29 16:17:28 -0800 | [diff] [blame] | 70 | GitDateFormatter df = new GitDateFormatter(Format.DEFAULT); |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 71 | List<RevObject> objects = listObjects(walk, view.getRevision()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 72 | List<Map<String, ?>> soyObjects = Lists.newArrayListWithCapacity(objects.size()); |
| 73 | boolean hasBlob = false; |
| 74 | |
| 75 | // TODO(sop): Allow caching commits by SHA-1 when no S cookie is sent. |
| 76 | for (RevObject obj : objects) { |
| 77 | try { |
| 78 | switch (obj.getType()) { |
| 79 | case OBJ_COMMIT: |
| 80 | soyObjects.add(ImmutableMap.of( |
| 81 | "type", Constants.TYPE_COMMIT, |
| Dave Borowitz | 3b086a7 | 2013-07-02 15:03:03 -0700 | [diff] [blame] | 82 | "data", new CommitSoyData() |
| 83 | .setLinkifier(linkifier) |
| 84 | .setRevWalk(walk) |
| Dave Borowitz | c8a1568 | 2013-07-02 14:33:08 -0700 | [diff] [blame] | 85 | .setArchiveFormat(archiveFormat) |
| Dave Borowitz | a03760a | 2014-01-29 16:17:28 -0800 | [diff] [blame] | 86 | .toSoyData(req, (RevCommit) obj, KeySet.DETAIL_DIFF_TREE, df))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 87 | break; |
| 88 | case OBJ_TREE: |
| 89 | soyObjects.add(ImmutableMap.of( |
| 90 | "type", Constants.TYPE_TREE, |
| 91 | "data", new TreeSoyData(walk, view).toSoyData(obj))); |
| 92 | break; |
| 93 | case OBJ_BLOB: |
| 94 | soyObjects.add(ImmutableMap.of( |
| 95 | "type", Constants.TYPE_BLOB, |
| 96 | "data", new BlobSoyData(walk, view).toSoyData(obj))); |
| 97 | hasBlob = true; |
| 98 | break; |
| 99 | case OBJ_TAG: |
| 100 | soyObjects.add(ImmutableMap.of( |
| 101 | "type", Constants.TYPE_TAG, |
| Dave Borowitz | a03760a | 2014-01-29 16:17:28 -0800 | [diff] [blame] | 102 | "data", new TagSoyData(linkifier, req).toSoyData((RevTag) obj, df))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 103 | break; |
| 104 | default: |
| Dave Borowitz | fd25c3a | 2013-01-11 14:37:11 -0800 | [diff] [blame] | 105 | log.warn("Bad object type for {}: {}", ObjectId.toString(obj.getId()), obj.getType()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 106 | res.setStatus(SC_NOT_FOUND); |
| 107 | return; |
| 108 | } |
| 109 | } catch (MissingObjectException e) { |
| 110 | log.warn("Missing object " + ObjectId.toString(obj.getId()), e); |
| 111 | res.setStatus(SC_NOT_FOUND); |
| 112 | return; |
| 113 | } catch (IncorrectObjectTypeException e) { |
| 114 | log.warn("Incorrect object type for " + ObjectId.toString(obj.getId()), e); |
| 115 | res.setStatus(SC_NOT_FOUND); |
| 116 | return; |
| 117 | } |
| 118 | } |
| 119 | |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 120 | renderHtml(req, res, "gitiles.revisionDetail", ImmutableMap.of( |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 121 | "title", view.getRevision().getName(), |
| 122 | "objects", soyObjects, |
| 123 | "hasBlob", hasBlob)); |
| 124 | } finally { |
| 125 | walk.release(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // TODO(dborowitz): Extract this. |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 130 | static List<RevObject> listObjects(RevWalk walk, Revision rev) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 131 | throws MissingObjectException, IOException { |
| 132 | List<RevObject> objects = Lists.newArrayListWithExpectedSize(1); |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 133 | ObjectId id = rev.getId(); |
| 134 | RevObject cur; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 135 | while (true) { |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 136 | cur = walk.parseAny(id); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 137 | objects.add(cur); |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 138 | if (cur.getType() != Constants.OBJ_TAG) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 139 | break; |
| 140 | } |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 141 | id = ((RevTag) cur).getObject(); |
| 142 | } |
| 143 | if (cur.getType() == Constants.OBJ_COMMIT) { |
| 144 | objects.add(walk.parseTree(((RevCommit) cur).getTree())); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 145 | } |
| 146 | return objects; |
| 147 | } |
| 148 | } |