| 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 | 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 | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame^] | 71 | public LogServlet(GitilesAccess.Factory accessFactory, Renderer renderer, Linkifier linkifier) { |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 72 | super(renderer, accessFactory); |
| 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 | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame^] | 87 | GitilesAccess access = getAccess(req); |
| 88 | DateFormatter df = new DateFormatter(access, Format.DEFAULT); |
| Dave Borowitz | a03760a | 2014-01-29 16:17:28 -0800 | [diff] [blame] | 89 | Map<String, Object> data = new LogSoyData(req, view).toSoyData(paginator, null, df); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 90 | |
| 91 | if (!view.getRevision().nameIsId()) { |
| 92 | List<Map<String, Object>> tags = Lists.newArrayListWithExpectedSize(1); |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 93 | for (RevObject o : RevisionServlet.listObjects(paginator.getWalk(), view.getRevision())) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 94 | if (o instanceof RevTag) { |
| Dave Borowitz | a03760a | 2014-01-29 16:17:28 -0800 | [diff] [blame] | 95 | tags.add(new TagSoyData(linkifier, req).toSoyData((RevTag) o, df)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | if (!tags.isEmpty()) { |
| 99 | data.put("tags", tags); |
| 100 | } |
| 101 | } |
| 102 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 103 | String title = "Log - "; |
| 104 | if (view.getOldRevision() != Revision.NULL) { |
| 105 | title += view.getRevisionRange(); |
| 106 | } else { |
| 107 | title += view.getRevision().getName(); |
| 108 | } |
| 109 | |
| 110 | data.put("title", title); |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame^] | 111 | GitilesConfig.putVariant(access.getConfig(), "logEntry", "logEntryVariant", data); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 112 | |
| Dave Borowitz | b1c628f | 2013-01-11 11:28:20 -0800 | [diff] [blame] | 113 | renderHtml(req, res, "gitiles.logDetail", data); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 114 | } catch (RevWalkException e) { |
| 115 | log.warn("Error in rev walk", e); |
| 116 | res.setStatus(SC_INTERNAL_SERVER_ERROR); |
| 117 | return; |
| 118 | } finally { |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 119 | paginator.getWalk().release(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 123 | @Override |
| 124 | protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 125 | Repository repo = ServletUtils.getRepository(req); |
| 126 | GitilesView view = getView(req, repo); |
| 127 | Paginator paginator = newPaginator(repo, view); |
| 128 | if (paginator == null) { |
| 129 | res.setStatus(SC_NOT_FOUND); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | try { |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame^] | 134 | DateFormatter df = new DateFormatter(getAccess(req), Format.DEFAULT); |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 135 | Map<String, Object> result = Maps.newLinkedHashMap(); |
| 136 | List<CommitJsonData.Commit> entries = Lists.newArrayListWithCapacity(paginator.getLimit()); |
| 137 | for (RevCommit c : paginator) { |
| 138 | paginator.getWalk().parseBody(c); |
| Dave Borowitz | ee2d77c | 2014-03-16 13:49:37 -0700 | [diff] [blame] | 139 | entries.add(new CommitJsonData().setRevWalk(paginator.getWalk()).toJsonData(req, c, df)); |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 140 | } |
| 141 | result.put("log", entries); |
| 142 | if (paginator.getPreviousStart() != null) { |
| 143 | result.put("previous", paginator.getPreviousStart().name()); |
| 144 | } |
| 145 | if (paginator.getNextStart() != null) { |
| 146 | result.put("next", paginator.getNextStart().name()); |
| 147 | } |
| 148 | renderJson(req, res, result, new TypeToken<Map<String, Object>>() {}.getType()); |
| 149 | } finally { |
| 150 | paginator.getWalk().release(); |
| 151 | } |
| 152 | } |
| 153 | |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 154 | private static GitilesView getView(HttpServletRequest req, Repository repo) throws IOException { |
| 155 | GitilesView view = ViewFilter.getView(req); |
| 156 | if (view.getRevision() != Revision.NULL) { |
| 157 | return view; |
| 158 | } |
| 159 | Ref headRef = repo.getRef(Constants.HEAD); |
| 160 | if (headRef == null) { |
| 161 | return null; |
| 162 | } |
| 163 | RevWalk walk = new RevWalk(repo); |
| 164 | try { |
| 165 | return GitilesView.log() |
| 166 | .copyFrom(view) |
| Dave Borowitz | 48ca670 | 2013-04-09 11:52:41 -0700 | [diff] [blame] | 167 | .setRevision(Revision.peel(Constants.HEAD, walk.parseAny(headRef.getObjectId()), walk)) |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 168 | .build(); |
| 169 | } finally { |
| 170 | walk.release(); |
| 171 | } |
| 172 | } |
| 173 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 174 | private static Optional<ObjectId> getStart(ListMultimap<String, String> params, |
| 175 | ObjectReader reader) throws IOException { |
| 176 | List<String> values = params.get(START_PARAM); |
| 177 | switch (values.size()) { |
| 178 | case 0: |
| 179 | return Optional.absent(); |
| 180 | case 1: |
| 181 | Collection<ObjectId> ids = reader.resolve(AbbreviatedObjectId.fromString(values.get(0))); |
| 182 | if (ids.size() != 1) { |
| 183 | return null; |
| 184 | } |
| 185 | return Optional.of(Iterables.getOnlyElement(ids)); |
| 186 | default: |
| 187 | return null; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | private static RevWalk newWalk(Repository repo, GitilesView view) |
| 192 | throws MissingObjectException, IncorrectObjectTypeException, IOException { |
| 193 | RevWalk walk = new RevWalk(repo); |
| 194 | walk.markStart(walk.parseCommit(view.getRevision().getId())); |
| 195 | if (view.getOldRevision() != Revision.NULL) { |
| 196 | walk.markUninteresting(walk.parseCommit(view.getOldRevision().getId())); |
| 197 | } |
| Dave Borowitz | dd3c3d9 | 2013-03-11 16:38:41 -0700 | [diff] [blame] | 198 | if (!Strings.isNullOrEmpty(view.getPathPart())) { |
| Jonathan Nieder | cb4d91c | 2013-12-04 16:59:15 -0800 | [diff] [blame] | 199 | walk.setTreeFilter(AndTreeFilter.create( |
| 200 | PathFilter.create(view.getPathPart()), |
| 201 | TreeFilter.ANY_DIFF)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 202 | } |
| 203 | return walk; |
| 204 | } |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 205 | |
| 206 | private static Paginator newPaginator(Repository repo, GitilesView view) throws IOException { |
| 207 | if (view == null) { |
| 208 | return null; |
| 209 | } |
| 210 | |
| 211 | RevWalk walk = null; |
| 212 | try { |
| 213 | walk = newWalk(repo, view); |
| 214 | } catch (IncorrectObjectTypeException e) { |
| 215 | return null; |
| 216 | } |
| 217 | |
| 218 | Optional<ObjectId> start; |
| 219 | try { |
| 220 | start = getStart(view.getParameters(), walk.getObjectReader()); |
| 221 | } catch (IOException e) { |
| 222 | walk.release(); |
| 223 | throw e; |
| 224 | } |
| 225 | if (start == null) { |
| 226 | return null; |
| 227 | } |
| Dave Borowitz | ef05581 | 2013-11-01 12:05:55 -0700 | [diff] [blame] | 228 | |
| 229 | return new Paginator(walk, getLimit(view), start.orNull()); |
| 230 | } |
| 231 | |
| 232 | private static int getLimit(GitilesView view) { |
| 233 | List<String> values = view.getParameters().get(LIMIT_PARAM); |
| 234 | if (values.isEmpty()) { |
| 235 | return DEFAULT_LIMIT; |
| 236 | } |
| 237 | Long limit = Longs.tryParse(values.get(0)); |
| 238 | if (limit == null) { |
| 239 | return DEFAULT_LIMIT; |
| 240 | } |
| 241 | return (int) Math.min(limit, MAX_LIMIT); |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 242 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 243 | } |