| 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 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 17 | import static com.google.common.base.Preconditions.checkNotNull; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame^] | 18 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 19 | import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; |
| 20 | import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; |
| 21 | |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame^] | 22 | import java.io.IOException; |
| 23 | import java.util.Collection; |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import javax.servlet.http.HttpServletRequest; |
| 29 | import javax.servlet.http.HttpServletResponse; |
| 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.errors.RevWalkException; |
| 34 | import org.eclipse.jgit.http.server.ServletUtils; |
| 35 | import org.eclipse.jgit.lib.AbbreviatedObjectId; |
| 36 | import org.eclipse.jgit.lib.AnyObjectId; |
| 37 | import org.eclipse.jgit.lib.ObjectId; |
| 38 | import org.eclipse.jgit.lib.ObjectReader; |
| 39 | import org.eclipse.jgit.lib.Ref; |
| 40 | import org.eclipse.jgit.lib.Repository; |
| 41 | import org.eclipse.jgit.revwalk.FollowFilter; |
| 42 | import org.eclipse.jgit.revwalk.RevCommit; |
| 43 | import org.eclipse.jgit.revwalk.RevObject; |
| 44 | import org.eclipse.jgit.revwalk.RevTag; |
| 45 | import org.eclipse.jgit.revwalk.RevWalk; |
| 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame^] | 49 | import com.google.common.base.Optional; |
| 50 | import com.google.common.base.Strings; |
| 51 | import com.google.common.collect.Iterables; |
| 52 | import com.google.common.collect.ListMultimap; |
| 53 | import com.google.common.collect.Lists; |
| 54 | import com.google.gitiles.CommitSoyData.KeySet; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 55 | |
| 56 | /** Serves an HTML page with a shortlog for commits and paths. */ |
| 57 | public class LogServlet extends BaseServlet { |
| 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(LogServlet.class); |
| 60 | |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame^] | 61 | static final String START_PARAM = "s"; |
| 62 | private static final int LIMIT = 100; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 63 | |
| 64 | private final Linkifier linkifier; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 65 | |
| 66 | public LogServlet(Renderer renderer, Linkifier linkifier) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 67 | super(renderer); |
| 68 | this.linkifier = checkNotNull(linkifier, "linkifier"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 73 | GitilesView view = ViewFilter.getView(req); |
| 74 | Repository repo = ServletUtils.getRepository(req); |
| 75 | RevWalk walk = null; |
| 76 | try { |
| 77 | try { |
| 78 | walk = newWalk(repo, view); |
| 79 | } catch (IncorrectObjectTypeException e) { |
| 80 | res.setStatus(SC_NOT_FOUND); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | Optional<ObjectId> start = getStart(view.getParameters(), walk.getObjectReader()); |
| 85 | if (start == null) { |
| 86 | res.setStatus(SC_NOT_FOUND); |
| 87 | return; |
| 88 | } |
| 89 | |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame^] | 90 | Map<String, Object> data = new LogSoyData(req, repo, view) |
| 91 | .toSoyData(walk, LIMIT, null, start.orNull()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 92 | |
| 93 | if (!view.getRevision().nameIsId()) { |
| 94 | List<Map<String, Object>> tags = Lists.newArrayListWithExpectedSize(1); |
| Dave Borowitz | 558005d | 2012-12-20 15:48:08 -0800 | [diff] [blame] | 95 | for (RevObject o : RevisionServlet.listObjects(walk, view.getRevision())) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 96 | if (o instanceof RevTag) { |
| 97 | tags.add(new TagSoyData(linkifier, req).toSoyData((RevTag) o)); |
| 98 | } |
| 99 | } |
| 100 | if (!tags.isEmpty()) { |
| 101 | data.put("tags", tags); |
| 102 | } |
| 103 | } |
| 104 | |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame^] | 105 | Paginator paginator = new Paginator(walk, LIMIT, start.orNull()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 106 | Map<AnyObjectId, Set<Ref>> refsById = repo.getAllRefsByPeeledObjectId(); |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame^] | 107 | List<Map<String, Object>> entries = Lists.newArrayListWithCapacity(LIMIT); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 108 | for (RevCommit c : paginator) { |
| 109 | entries.add(new CommitSoyData(null, req, repo, walk, view, refsById) |
| 110 | .toSoyData(c, KeySet.SHORTLOG)); |
| 111 | } |
| 112 | |
| 113 | String title = "Log - "; |
| 114 | if (view.getOldRevision() != Revision.NULL) { |
| 115 | title += view.getRevisionRange(); |
| 116 | } else { |
| 117 | title += view.getRevision().getName(); |
| 118 | } |
| 119 | |
| 120 | data.put("title", title); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 121 | |
| 122 | render(req, res, "gitiles.logDetail", data); |
| 123 | } catch (RevWalkException e) { |
| 124 | log.warn("Error in rev walk", e); |
| 125 | res.setStatus(SC_INTERNAL_SERVER_ERROR); |
| 126 | return; |
| 127 | } finally { |
| 128 | if (walk != null) { |
| 129 | walk.release(); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 134 | private static Optional<ObjectId> getStart(ListMultimap<String, String> params, |
| 135 | ObjectReader reader) throws IOException { |
| 136 | List<String> values = params.get(START_PARAM); |
| 137 | switch (values.size()) { |
| 138 | case 0: |
| 139 | return Optional.absent(); |
| 140 | case 1: |
| 141 | Collection<ObjectId> ids = reader.resolve(AbbreviatedObjectId.fromString(values.get(0))); |
| 142 | if (ids.size() != 1) { |
| 143 | return null; |
| 144 | } |
| 145 | return Optional.of(Iterables.getOnlyElement(ids)); |
| 146 | default: |
| 147 | return null; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | private static RevWalk newWalk(Repository repo, GitilesView view) |
| 152 | throws MissingObjectException, IncorrectObjectTypeException, IOException { |
| 153 | RevWalk walk = new RevWalk(repo); |
| 154 | walk.markStart(walk.parseCommit(view.getRevision().getId())); |
| 155 | if (view.getOldRevision() != Revision.NULL) { |
| 156 | walk.markUninteresting(walk.parseCommit(view.getOldRevision().getId())); |
| 157 | } |
| 158 | if (!Strings.isNullOrEmpty(view.getTreePath())) { |
| 159 | walk.setTreeFilter(FollowFilter.create(view.getTreePath())); |
| 160 | } |
| 161 | return walk; |
| 162 | } |
| 163 | } |