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