| 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; |
| 18 | import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; |
| 19 | import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; |
| 20 | |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 21 | import com.google.common.base.Optional; |
| 22 | import com.google.common.base.Strings; |
| 23 | import com.google.common.collect.Iterables; |
| 24 | import com.google.common.collect.ListMultimap; |
| 25 | import com.google.common.collect.Lists; |
| Dave Borowitz | ef05581 | 2013-11-01 12:05:55 -0700 | [diff] [blame] | 26 | import com.google.common.collect.Maps; |
| 27 | import com.google.common.primitives.Longs; |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 28 | import com.google.gson.reflect.TypeToken; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 29 | |
| 30 | import org.eclipse.jgit.errors.IncorrectObjectTypeException; |
| 31 | import org.eclipse.jgit.errors.MissingObjectException; |
| 32 | import org.eclipse.jgit.errors.RevWalkException; |
| 33 | import org.eclipse.jgit.http.server.ServletUtils; |
| 34 | import org.eclipse.jgit.lib.AbbreviatedObjectId; |
| Dave Borowitz | 5427146 | 2013-11-11 11:43:11 -0800 | [diff] [blame] | 35 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 36 | import org.eclipse.jgit.lib.Constants; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 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; |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 41 | import org.eclipse.jgit.revwalk.RevCommit; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 42 | import org.eclipse.jgit.revwalk.RevObject; |
| 43 | import org.eclipse.jgit.revwalk.RevTag; |
| 44 | import org.eclipse.jgit.revwalk.RevWalk; |
| Jonathan Nieder | cb4d91c | 2013-12-04 16:59:15 -0800 | [diff] [blame^] | 45 | import org.eclipse.jgit.treewalk.filter.AndTreeFilter; |
| Dave Borowitz | 775f222 | 2013-11-05 10:36:12 -0800 | [diff] [blame] | 46 | import org.eclipse.jgit.treewalk.filter.PathFilter; |
| Jonathan Nieder | cb4d91c | 2013-12-04 16:59:15 -0800 | [diff] [blame^] | 47 | import org.eclipse.jgit.treewalk.filter.TreeFilter; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
| 50 | |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 51 | import java.io.IOException; |
| 52 | import java.util.Collection; |
| 53 | import java.util.List; |
| 54 | import java.util.Map; |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 55 | |
| 56 | import javax.servlet.http.HttpServletRequest; |
| 57 | import javax.servlet.http.HttpServletResponse; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 58 | |
| 59 | /** Serves an HTML page with a shortlog for commits and paths. */ |
| 60 | public class LogServlet extends BaseServlet { |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 61 | private static final long serialVersionUID = 1L; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 62 | private static final Logger log = LoggerFactory.getLogger(LogServlet.class); |
| 63 | |
| Dave Borowitz | ef05581 | 2013-11-01 12:05:55 -0700 | [diff] [blame] | 64 | static final String LIMIT_PARAM = "n"; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 65 | static final String START_PARAM = "s"; |
| Dave Borowitz | ef05581 | 2013-11-01 12:05:55 -0700 | [diff] [blame] | 66 | private static final int DEFAULT_LIMIT = 100; |
| 67 | private static final int MAX_LIMIT = 10000; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 68 | |
| 69 | private final Linkifier linkifier; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 70 | |
| Dave Borowitz | 5427146 | 2013-11-11 11:43:11 -0800 | [diff] [blame] | 71 | public LogServlet(Config cfg, Renderer renderer, Linkifier linkifier) { |
| 72 | super(cfg, renderer); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 73 | this.linkifier = checkNotNull(linkifier, "linkifier"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 77 | protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 78 | Repository repo = ServletUtils.getRepository(req); |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 79 | GitilesView view = getView(req, repo); |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 80 | Paginator paginator = newPaginator(repo, view); |
| 81 | if (paginator == null) { |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 82 | res.setStatus(SC_NOT_FOUND); |
| 83 | return; |
| 84 | } |
| 85 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 86 | try { |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 87 | Map<String, Object> data = new LogSoyData(req, view).toSoyData(paginator, null); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 88 | |
| 89 | if (!view.getRevision().nameIsId()) { |
| 90 | List<Map<String, Object>> tags = Lists.newArrayListWithExpectedSize(1); |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 91 | for (RevObject o : RevisionServlet.listObjects(paginator.getWalk(), view.getRevision())) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 92 | if (o instanceof RevTag) { |
| 93 | tags.add(new TagSoyData(linkifier, req).toSoyData((RevTag) o)); |
| 94 | } |
| 95 | } |
| 96 | if (!tags.isEmpty()) { |
| 97 | data.put("tags", tags); |
| 98 | } |
| 99 | } |
| 100 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 101 | String title = "Log - "; |
| 102 | if (view.getOldRevision() != Revision.NULL) { |
| 103 | title += view.getRevisionRange(); |
| 104 | } else { |
| 105 | title += view.getRevision().getName(); |
| 106 | } |
| 107 | |
| 108 | data.put("title", title); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 109 | |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 110 | renderHtml(req, res, "gitiles.logDetail", data); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 111 | } catch (RevWalkException e) { |
| 112 | log.warn("Error in rev walk", e); |
| 113 | res.setStatus(SC_INTERNAL_SERVER_ERROR); |
| 114 | return; |
| 115 | } finally { |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 116 | paginator.getWalk().release(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 120 | @Override |
| 121 | protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 122 | Repository repo = ServletUtils.getRepository(req); |
| 123 | GitilesView view = getView(req, repo); |
| 124 | Paginator paginator = newPaginator(repo, view); |
| 125 | if (paginator == null) { |
| 126 | res.setStatus(SC_NOT_FOUND); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | try { |
| 131 | Map<String, Object> result = Maps.newLinkedHashMap(); |
| 132 | List<CommitJsonData.Commit> entries = Lists.newArrayListWithCapacity(paginator.getLimit()); |
| 133 | for (RevCommit c : paginator) { |
| 134 | paginator.getWalk().parseBody(c); |
| 135 | entries.add(CommitJsonData.toJsonData(c)); |
| 136 | } |
| 137 | result.put("log", entries); |
| 138 | if (paginator.getPreviousStart() != null) { |
| 139 | result.put("previous", paginator.getPreviousStart().name()); |
| 140 | } |
| 141 | if (paginator.getNextStart() != null) { |
| 142 | result.put("next", paginator.getNextStart().name()); |
| 143 | } |
| 144 | renderJson(req, res, result, new TypeToken<Map<String, Object>>() {}.getType()); |
| 145 | } finally { |
| 146 | paginator.getWalk().release(); |
| 147 | } |
| 148 | } |
| 149 | |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 150 | private static GitilesView getView(HttpServletRequest req, Repository repo) throws IOException { |
| 151 | GitilesView view = ViewFilter.getView(req); |
| 152 | if (view.getRevision() != Revision.NULL) { |
| 153 | return view; |
| 154 | } |
| 155 | Ref headRef = repo.getRef(Constants.HEAD); |
| 156 | if (headRef == null) { |
| 157 | return null; |
| 158 | } |
| 159 | RevWalk walk = new RevWalk(repo); |
| 160 | try { |
| 161 | return GitilesView.log() |
| 162 | .copyFrom(view) |
| Dave Borowitz | 48ca670 | 2013-04-09 11:52:41 -0700 | [diff] [blame] | 163 | .setRevision(Revision.peel(Constants.HEAD, walk.parseAny(headRef.getObjectId()), walk)) |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 164 | .build(); |
| 165 | } finally { |
| 166 | walk.release(); |
| 167 | } |
| 168 | } |
| 169 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 170 | private static Optional<ObjectId> getStart(ListMultimap<String, String> params, |
| 171 | ObjectReader reader) throws IOException { |
| 172 | List<String> values = params.get(START_PARAM); |
| 173 | switch (values.size()) { |
| 174 | case 0: |
| 175 | return Optional.absent(); |
| 176 | case 1: |
| 177 | Collection<ObjectId> ids = reader.resolve(AbbreviatedObjectId.fromString(values.get(0))); |
| 178 | if (ids.size() != 1) { |
| 179 | return null; |
| 180 | } |
| 181 | return Optional.of(Iterables.getOnlyElement(ids)); |
| 182 | default: |
| 183 | return null; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | private static RevWalk newWalk(Repository repo, GitilesView view) |
| 188 | throws MissingObjectException, IncorrectObjectTypeException, IOException { |
| 189 | RevWalk walk = new RevWalk(repo); |
| 190 | walk.markStart(walk.parseCommit(view.getRevision().getId())); |
| 191 | if (view.getOldRevision() != Revision.NULL) { |
| 192 | walk.markUninteresting(walk.parseCommit(view.getOldRevision().getId())); |
| 193 | } |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 194 | if (!Strings.isNullOrEmpty(view.getPathPart())) { |
| Jonathan Nieder | cb4d91c | 2013-12-04 16:59:15 -0800 | [diff] [blame^] | 195 | walk.setTreeFilter(AndTreeFilter.create( |
| 196 | PathFilter.create(view.getPathPart()), |
| 197 | TreeFilter.ANY_DIFF)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 198 | } |
| 199 | return walk; |
| 200 | } |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 201 | |
| 202 | private static Paginator newPaginator(Repository repo, GitilesView view) throws IOException { |
| 203 | if (view == null) { |
| 204 | return null; |
| 205 | } |
| 206 | |
| 207 | RevWalk walk = null; |
| 208 | try { |
| 209 | walk = newWalk(repo, view); |
| 210 | } catch (IncorrectObjectTypeException e) { |
| 211 | return null; |
| 212 | } |
| 213 | |
| 214 | Optional<ObjectId> start; |
| 215 | try { |
| 216 | start = getStart(view.getParameters(), walk.getObjectReader()); |
| 217 | } catch (IOException e) { |
| 218 | walk.release(); |
| 219 | throw e; |
| 220 | } |
| 221 | if (start == null) { |
| 222 | return null; |
| 223 | } |
| Dave Borowitz | ef05581 | 2013-11-01 12:05:55 -0700 | [diff] [blame] | 224 | |
| 225 | return new Paginator(walk, getLimit(view), start.orNull()); |
| 226 | } |
| 227 | |
| 228 | private static int getLimit(GitilesView view) { |
| 229 | List<String> values = view.getParameters().get(LIMIT_PARAM); |
| 230 | if (values.isEmpty()) { |
| 231 | return DEFAULT_LIMIT; |
| 232 | } |
| 233 | Long limit = Longs.tryParse(values.get(0)); |
| 234 | if (limit == null) { |
| 235 | return DEFAULT_LIMIT; |
| 236 | } |
| 237 | return (int) Math.min(limit, MAX_LIMIT); |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 238 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 239 | } |