| 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 | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 18 | |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 19 | import com.google.common.base.Strings; |
| 20 | import com.google.common.collect.Iterables; |
| 21 | import com.google.common.collect.ListMultimap; |
| 22 | import com.google.common.collect.Lists; |
| Dave Borowitz | ef05581 | 2013-11-01 12:05:55 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Maps; |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 24 | import com.google.common.collect.Sets; |
| Dave Borowitz | ef05581 | 2013-11-01 12:05:55 -0700 | [diff] [blame] | 25 | import com.google.common.primitives.Longs; |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 26 | import com.google.gitiles.CommitData.Field; |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 27 | import com.google.gitiles.DateFormatter.Format; |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 28 | import com.google.gitiles.GitilesRequestFailureException.FailureReason; |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 29 | import com.google.gson.reflect.TypeToken; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 30 | import java.io.IOException; |
| 31 | import java.io.OutputStream; |
| 32 | import java.io.Writer; |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.Collection; |
| 35 | import java.util.List; |
| 36 | import java.util.Map; |
| David Pursehouse | 7a7f547 | 2016-10-14 09:59:20 +0900 | [diff] [blame] | 37 | import java.util.Optional; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 38 | import java.util.Set; |
| 39 | import javax.servlet.http.HttpServletRequest; |
| 40 | import javax.servlet.http.HttpServletResponse; |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 41 | import org.eclipse.jgit.diff.DiffConfig; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 42 | import org.eclipse.jgit.errors.IncorrectObjectTypeException; |
| 43 | import org.eclipse.jgit.errors.MissingObjectException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 44 | import org.eclipse.jgit.http.server.ServletUtils; |
| 45 | import org.eclipse.jgit.lib.AbbreviatedObjectId; |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 46 | import org.eclipse.jgit.lib.Constants; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 47 | import org.eclipse.jgit.lib.ObjectId; |
| 48 | import org.eclipse.jgit.lib.ObjectReader; |
| 49 | import org.eclipse.jgit.lib.Ref; |
| 50 | import org.eclipse.jgit.lib.Repository; |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 51 | import org.eclipse.jgit.revwalk.FollowFilter; |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 52 | import org.eclipse.jgit.revwalk.RevCommit; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 53 | import org.eclipse.jgit.revwalk.RevObject; |
| Shawn Pearce | 3103767 | 2016-08-18 21:44:43 -0700 | [diff] [blame] | 54 | import org.eclipse.jgit.revwalk.RevSort; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 55 | import org.eclipse.jgit.revwalk.RevTag; |
| 56 | import org.eclipse.jgit.revwalk.RevWalk; |
| Shawn Pearce | 20b4229 | 2015-03-17 21:53:55 -0700 | [diff] [blame] | 57 | import org.eclipse.jgit.revwalk.filter.AndRevFilter; |
| 58 | import org.eclipse.jgit.revwalk.filter.RevFilter; |
| Jonathan Nieder | cb4d91c | 2013-12-04 16:59:15 -0800 | [diff] [blame] | 59 | import org.eclipse.jgit.treewalk.filter.AndTreeFilter; |
| Dave Borowitz | 1b08aea | 2014-05-05 12:02:21 -0700 | [diff] [blame] | 60 | import org.eclipse.jgit.treewalk.filter.PathFilterGroup; |
| Jonathan Nieder | cb4d91c | 2013-12-04 16:59:15 -0800 | [diff] [blame] | 61 | import org.eclipse.jgit.treewalk.filter.TreeFilter; |
| Shawn Pearce | 20b4229 | 2015-03-17 21:53:55 -0700 | [diff] [blame] | 62 | import org.eclipse.jgit.util.StringUtils; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 63 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 64 | /** Serves an HTML page with a shortlog for commits and paths. */ |
| 65 | public class LogServlet extends BaseServlet { |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 66 | private static final long serialVersionUID = 1L; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 67 | |
| Dave Borowitz | ef05581 | 2013-11-01 12:05:55 -0700 | [diff] [blame] | 68 | static final String LIMIT_PARAM = "n"; |
| Dave Borowitz | b772cce | 2012-12-28 13:57:22 -0800 | [diff] [blame] | 69 | static final String START_PARAM = "s"; |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 70 | |
| 71 | private static final String FOLLOW_PARAM = "follow"; |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 72 | private static final String NAME_STATUS_PARAM = "name-status"; |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 73 | private static final String PRETTY_PARAM = "pretty"; |
| Alex Spradlin | 284a35a | 2019-07-09 16:02:55 -0700 | [diff] [blame] | 74 | private static final String TOPO_ORDER_PARAM = "topo-order"; |
| 75 | private static final String REVERSE_PARAM = "reverse"; |
| 76 | private static final String FIRST_PARENT_PARAM = "first-parent"; |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 77 | |
| Dave Borowitz | ef05581 | 2013-11-01 12:05:55 -0700 | [diff] [blame] | 78 | private static final int DEFAULT_LIMIT = 100; |
| 79 | private static final int MAX_LIMIT = 10000; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 80 | |
| 81 | private final Linkifier linkifier; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 82 | |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 83 | public LogServlet(GitilesAccess.Factory accessFactory, Renderer renderer, Linkifier linkifier) { |
| Dave Borowitz | 8d6d687 | 2014-03-16 15:18:14 -0700 | [diff] [blame] | 84 | super(renderer, accessFactory); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 85 | this.linkifier = checkNotNull(linkifier, "linkifier"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 89 | protected void doGetHtml(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 90 | Repository repo = ServletUtils.getRepository(req); |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 91 | GitilesView view = getView(req, repo); |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 92 | |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 93 | Paginator paginator = null; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 94 | try { |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 95 | GitilesAccess access = getAccess(req); |
| Shawn Pearce | 3103767 | 2016-08-18 21:44:43 -0700 | [diff] [blame] | 96 | paginator = newPaginator(repo, view, access); |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 97 | if (paginator == null) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 98 | throw new GitilesRequestFailureException(FailureReason.OBJECT_NOT_FOUND); |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 99 | } |
| Dave Borowitz | 2b2f34b | 2014-04-29 16:47:20 -0700 | [diff] [blame] | 100 | DateFormatter df = new DateFormatter(access, Format.DEFAULT); |
| Michael Moss | 558f864 | 2014-04-15 09:29:21 -0700 | [diff] [blame] | 101 | |
| 102 | // Allow the user to select a logView variant with the "pretty" param. |
| 103 | String pretty = Iterables.getFirst(view.getParameters().get(PRETTY_PARAM), "default"); |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 104 | Map<String, Object> data = Maps.newHashMapWithExpectedSize(2); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 105 | |
| 106 | if (!view.getRevision().nameIsId()) { |
| 107 | List<Map<String, Object>> tags = Lists.newArrayListWithExpectedSize(1); |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 108 | for (RevObject o : RevisionServlet.listObjects(paginator.getWalk(), view.getRevision())) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 109 | if (o instanceof RevTag) { |
| Jonathan Nieder | 9e3b1b7 | 2019-03-07 14:38:28 -0800 | [diff] [blame] | 110 | tags.add(new TagSoyData(linkifier, req).toSoyData(paginator.getWalk(), (RevTag) o, df)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | if (!tags.isEmpty()) { |
| 114 | data.put("tags", tags); |
| 115 | } |
| 116 | } |
| 117 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 118 | String title = "Log - "; |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 119 | if (!Revision.isNull(view.getOldRevision())) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 120 | title += view.getRevisionRange(); |
| 121 | } else { |
| 122 | title += view.getRevision().getName(); |
| 123 | } |
| 124 | |
| 125 | data.put("title", title); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 126 | |
| Shawn Pearce | 4c2eb85 | 2014-08-26 15:35:33 -0700 | [diff] [blame] | 127 | try (OutputStream out = startRenderStreamingHtml(req, res, "gitiles.logDetail", data)) { |
| 128 | Writer w = newWriter(out, res); |
| Dave Borowitz | 571b7a0 | 2018-02-09 15:18:10 -0500 | [diff] [blame] | 129 | new LogSoyData(req, access, pretty) |
| 130 | .renderStreaming(paginator, null, renderer, w, df, LogSoyData.FooterBehavior.NEXT); |
| Shawn Pearce | 4c2eb85 | 2014-08-26 15:35:33 -0700 | [diff] [blame] | 131 | w.flush(); |
| Dave Borowitz | f6dcf7a | 2014-07-30 10:26:58 -0700 | [diff] [blame] | 132 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 133 | } finally { |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 134 | if (paginator != null) { |
| 135 | paginator.getWalk().close(); |
| 136 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 140 | @Override |
| 141 | protected void doGetJson(HttpServletRequest req, HttpServletResponse res) throws IOException { |
| 142 | Repository repo = ServletUtils.getRepository(req); |
| 143 | GitilesView view = getView(req, repo); |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 144 | |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 145 | Set<Field> fs = Sets.newEnumSet(CommitJsonData.DEFAULT_FIELDS, Field.class); |
| 146 | String nameStatus = Iterables.getFirst(view.getParameters().get(NAME_STATUS_PARAM), null); |
| 147 | if ("1".equals(nameStatus) || "".equals(nameStatus)) { |
| 148 | fs.add(Field.DIFF_TREE); |
| 149 | } |
| 150 | |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 151 | Paginator paginator = null; |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 152 | try { |
| Shawn Pearce | 3103767 | 2016-08-18 21:44:43 -0700 | [diff] [blame] | 153 | GitilesAccess access = getAccess(req); |
| 154 | paginator = newPaginator(repo, view, access); |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 155 | if (paginator == null) { |
| Masaya Suzuki | 5cecb86 | 2019-03-25 17:35:44 -0700 | [diff] [blame] | 156 | throw new GitilesRequestFailureException(FailureReason.OBJECT_NOT_FOUND); |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 157 | } |
| Shawn Pearce | 3103767 | 2016-08-18 21:44:43 -0700 | [diff] [blame] | 158 | DateFormatter df = new DateFormatter(access, Format.DEFAULT); |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 159 | CommitJsonData.Log result = new CommitJsonData.Log(); |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 160 | List<CommitJsonData.Commit> entries = Lists.newArrayListWithCapacity(paginator.getLimit()); |
| 161 | for (RevCommit c : paginator) { |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 162 | entries.add(new CommitJsonData().toJsonData(req, paginator.getWalk(), c, fs, df)); |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 163 | } |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 164 | result.log = entries; |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 165 | if (paginator.getPreviousStart() != null) { |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 166 | result.previous = paginator.getPreviousStart().name(); |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 167 | } |
| 168 | if (paginator.getNextStart() != null) { |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 169 | result.next = paginator.getNextStart().name(); |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 170 | } |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 171 | renderJson(req, res, result, new TypeToken<CommitJsonData.Log>() {}.getType()); |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 172 | } finally { |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 173 | if (paginator != null) { |
| 174 | paginator.getWalk().close(); |
| 175 | } |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 179 | private static GitilesView getView(HttpServletRequest req, Repository repo) throws IOException { |
| 180 | GitilesView view = ViewFilter.getView(req); |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 181 | if (!Revision.isNull(view.getRevision())) { |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 182 | return view; |
| 183 | } |
| Dave Borowitz | 14cad73 | 2016-05-26 17:34:19 -0400 | [diff] [blame] | 184 | Ref headRef = repo.exactRef(Constants.HEAD); |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 185 | if (headRef == null) { |
| 186 | return null; |
| 187 | } |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 188 | try (RevWalk walk = new RevWalk(repo)) { |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 189 | return GitilesView.log() |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 190 | .copyFrom(view) |
| 191 | .setRevision(Revision.peel(Constants.HEAD, walk.parseAny(headRef.getObjectId()), walk)) |
| 192 | .build(); |
| Dave Borowitz | 80334b2 | 2013-01-11 14:19:11 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| David Pursehouse | 17c09fe | 2016-08-26 15:41:01 +0900 | [diff] [blame] | 196 | private static class InvalidStartValueException extends IllegalArgumentException { |
| 197 | private static final long serialVersionUID = 1L; |
| 198 | |
| 199 | InvalidStartValueException() { |
| 200 | super(); |
| 201 | } |
| 202 | } |
| 203 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 204 | private static Optional<ObjectId> getStart( |
| David Pursehouse | 17c09fe | 2016-08-26 15:41:01 +0900 | [diff] [blame] | 205 | ListMultimap<String, String> params, ObjectReader reader) |
| David Pursehouse | ccaa85d | 2017-05-30 10:47:27 +0900 | [diff] [blame] | 206 | throws IOException, InvalidStartValueException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 207 | List<String> values = params.get(START_PARAM); |
| 208 | switch (values.size()) { |
| 209 | case 0: |
| David Pursehouse | 7a7f547 | 2016-10-14 09:59:20 +0900 | [diff] [blame] | 210 | return Optional.empty(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 211 | case 1: |
| Dave Borowitz | 95ef58a | 2016-05-26 17:30:10 -0400 | [diff] [blame] | 212 | String id = values.get(0); |
| 213 | if (!AbbreviatedObjectId.isId(id)) { |
| David Pursehouse | 17c09fe | 2016-08-26 15:41:01 +0900 | [diff] [blame] | 214 | throw new InvalidStartValueException(); |
| Dave Borowitz | 95ef58a | 2016-05-26 17:30:10 -0400 | [diff] [blame] | 215 | } |
| 216 | Collection<ObjectId> ids = reader.resolve(AbbreviatedObjectId.fromString(id)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 217 | if (ids.size() != 1) { |
| David Pursehouse | 17c09fe | 2016-08-26 15:41:01 +0900 | [diff] [blame] | 218 | throw new InvalidStartValueException(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 219 | } |
| 220 | return Optional.of(Iterables.getOnlyElement(ids)); |
| 221 | default: |
| David Pursehouse | 17c09fe | 2016-08-26 15:41:01 +0900 | [diff] [blame] | 222 | throw new InvalidStartValueException(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 226 | private static RevWalk newWalk(Repository repo, GitilesView view, GitilesAccess access) |
| David Pursehouse | c3e772a | 2016-06-15 21:49:35 +0900 | [diff] [blame] | 227 | throws MissingObjectException, IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 228 | RevWalk walk = new RevWalk(repo); |
| Alex Spradlin | 284a35a | 2019-07-09 16:02:55 -0700 | [diff] [blame] | 229 | if (isTrue(view, FIRST_PARENT_PARAM)) { |
| 230 | walk.setFirstParent(true); |
| 231 | } |
| 232 | if (isTrue(view, TOPO_ORDER_PARAM)) { |
| 233 | walk.sort(RevSort.TOPO, true); |
| 234 | } |
| 235 | if (isTrue(view, REVERSE_PARAM)) { |
| 236 | walk.sort(RevSort.REVERSE, true); |
| 237 | } |
| David Pursehouse | c3e772a | 2016-06-15 21:49:35 +0900 | [diff] [blame] | 238 | try { |
| 239 | walk.markStart(walk.parseCommit(view.getRevision().getId())); |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 240 | if (!Revision.isNull(view.getOldRevision())) { |
| David Pursehouse | c3e772a | 2016-06-15 21:49:35 +0900 | [diff] [blame] | 241 | walk.markUninteresting(walk.parseCommit(view.getOldRevision().getId())); |
| 242 | } |
| 243 | } catch (IncorrectObjectTypeException iote) { |
| 244 | return null; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 245 | } |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 246 | setTreeFilter(walk, view, access); |
| Shawn Pearce | 3103767 | 2016-08-18 21:44:43 -0700 | [diff] [blame] | 247 | setRevFilter(walk, view); |
| Shawn Pearce | 3103767 | 2016-08-18 21:44:43 -0700 | [diff] [blame] | 248 | return walk; |
| 249 | } |
| 250 | |
| 251 | private static void setRevFilter(RevWalk walk, GitilesView view) { |
| Shawn Pearce | 20b4229 | 2015-03-17 21:53:55 -0700 | [diff] [blame] | 252 | List<RevFilter> filters = new ArrayList<>(3); |
| Shawn Pearce | 3103767 | 2016-08-18 21:44:43 -0700 | [diff] [blame] | 253 | if (isTrue(view, "no-merges")) { |
| Shawn Pearce | 20b4229 | 2015-03-17 21:53:55 -0700 | [diff] [blame] | 254 | filters.add(RevFilter.NO_MERGES); |
| 255 | } |
| Shawn Pearce | 3103767 | 2016-08-18 21:44:43 -0700 | [diff] [blame] | 256 | |
| Benjamin Kalman | bcc19fa | 2014-08-21 17:24:41 -0700 | [diff] [blame] | 257 | String author = Iterables.getFirst(view.getParameters().get("author"), null); |
| 258 | if (author != null) { |
| Shawn Pearce | 20b4229 | 2015-03-17 21:53:55 -0700 | [diff] [blame] | 259 | filters.add(IdentRevFilter.author(author)); |
| Dave Borowitz | 8646ff5 | 2014-09-05 16:18:56 -0700 | [diff] [blame] | 260 | } |
| Shawn Pearce | 3103767 | 2016-08-18 21:44:43 -0700 | [diff] [blame] | 261 | |
| Dave Borowitz | 8646ff5 | 2014-09-05 16:18:56 -0700 | [diff] [blame] | 262 | String committer = Iterables.getFirst(view.getParameters().get("committer"), null); |
| 263 | if (committer != null) { |
| Shawn Pearce | 20b4229 | 2015-03-17 21:53:55 -0700 | [diff] [blame] | 264 | filters.add(IdentRevFilter.committer(committer)); |
| 265 | } |
| Shawn Pearce | 3103767 | 2016-08-18 21:44:43 -0700 | [diff] [blame] | 266 | |
| Shawn Pearce | 20b4229 | 2015-03-17 21:53:55 -0700 | [diff] [blame] | 267 | if (filters.size() > 1) { |
| 268 | walk.setRevFilter(AndRevFilter.create(filters)); |
| 269 | } else if (filters.size() == 1) { |
| 270 | walk.setRevFilter(filters.get(0)); |
| Benjamin Kalman | bcc19fa | 2014-08-21 17:24:41 -0700 | [diff] [blame] | 271 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 272 | } |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 273 | |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 274 | private static void setTreeFilter(RevWalk walk, GitilesView view, GitilesAccess access) |
| 275 | throws IOException { |
| 276 | if (Strings.isNullOrEmpty(view.getPathPart())) { |
| 277 | return; |
| 278 | } |
| 279 | walk.setRewriteParents(false); |
| 280 | String path = view.getPathPart(); |
| Dave Borowitz | 45dde17 | 2016-03-22 10:10:58 -0400 | [diff] [blame] | 281 | |
| 282 | List<String> followParams = view.getParameters().get(FOLLOW_PARAM); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 283 | boolean follow = |
| 284 | !followParams.isEmpty() |
| 285 | ? isTrue(followParams.get(0)) |
| 286 | : access.getConfig().getBoolean("log", null, "follow", true); |
| Dave Borowitz | 45dde17 | 2016-03-22 10:10:58 -0400 | [diff] [blame] | 287 | if (follow) { |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 288 | walk.setTreeFilter(FollowFilter.create(path, access.getConfig().get(DiffConfig.KEY))); |
| 289 | } else { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 290 | walk.setTreeFilter( |
| 291 | AndTreeFilter.create(PathFilterGroup.createFromStrings(path), TreeFilter.ANY_DIFF)); |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| Shawn Pearce | 3103767 | 2016-08-18 21:44:43 -0700 | [diff] [blame] | 295 | private static boolean isTrue(GitilesView view, String param) { |
| 296 | return isTrue(Iterables.getFirst(view.getParameters().get(param), null)); |
| 297 | } |
| 298 | |
| Shawn Pearce | 20b4229 | 2015-03-17 21:53:55 -0700 | [diff] [blame] | 299 | private static boolean isTrue(String v) { |
| 300 | if (v == null) { |
| 301 | return false; |
| 302 | } else if (v.isEmpty()) { |
| 303 | return true; |
| 304 | } |
| 305 | return Boolean.TRUE.equals(StringUtils.toBooleanOrNull(v)); |
| 306 | } |
| 307 | |
| Dave Borowitz | 344c4dd | 2015-10-26 11:02:13 -0400 | [diff] [blame] | 308 | private static Paginator newPaginator(Repository repo, GitilesView view, GitilesAccess access) |
| 309 | throws IOException { |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 310 | if (view == null) { |
| 311 | return null; |
| 312 | } |
| 313 | |
| David Pursehouse | c3e772a | 2016-06-15 21:49:35 +0900 | [diff] [blame] | 314 | try (RevWalk walk = newWalk(repo, view, access)) { |
| 315 | if (walk == null) { |
| 316 | return null; |
| 317 | } |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 318 | |
| David Pursehouse | 17c09fe | 2016-08-26 15:41:01 +0900 | [diff] [blame] | 319 | try { |
| 320 | Optional<ObjectId> start = getStart(view.getParameters(), walk.getObjectReader()); |
| David Pursehouse | 7a7f547 | 2016-10-14 09:59:20 +0900 | [diff] [blame] | 321 | return new Paginator(walk, getLimit(view), start.orElse(null)); |
| David Pursehouse | 17c09fe | 2016-08-26 15:41:01 +0900 | [diff] [blame] | 322 | } catch (InvalidStartValueException e) { |
| David Pursehouse | c3e772a | 2016-06-15 21:49:35 +0900 | [diff] [blame] | 323 | return null; |
| 324 | } |
| David Pursehouse | c3e772a | 2016-06-15 21:49:35 +0900 | [diff] [blame] | 325 | } |
| Dave Borowitz | ef05581 | 2013-11-01 12:05:55 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | private static int getLimit(GitilesView view) { |
| 329 | List<String> values = view.getParameters().get(LIMIT_PARAM); |
| 330 | if (values.isEmpty()) { |
| 331 | return DEFAULT_LIMIT; |
| 332 | } |
| 333 | Long limit = Longs.tryParse(values.get(0)); |
| 334 | if (limit == null) { |
| 335 | return DEFAULT_LIMIT; |
| 336 | } |
| 337 | return (int) Math.min(limit, MAX_LIMIT); |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 338 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 339 | } |