| 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 | |
| 17 | import static com.google.common.base.Preconditions.checkArgument; |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static com.google.common.base.Preconditions.checkState; |
| 20 | |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 21 | import java.io.IOException; |
| 22 | import java.util.ArrayDeque; |
| 23 | import java.util.Deque; |
| 24 | import java.util.HashMap; |
| 25 | import java.util.Iterator; |
| 26 | import java.util.Map; |
| 27 | import javax.annotation.Nullable; |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 28 | import org.eclipse.jgit.diff.DiffEntry; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 29 | import org.eclipse.jgit.errors.IncorrectObjectTypeException; |
| 30 | import org.eclipse.jgit.errors.MissingObjectException; |
| 31 | import org.eclipse.jgit.errors.RevWalkException; |
| 32 | import org.eclipse.jgit.lib.ObjectId; |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 33 | import org.eclipse.jgit.revwalk.FollowFilter; |
| 34 | import org.eclipse.jgit.revwalk.RenameCallback; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 35 | import org.eclipse.jgit.revwalk.RevCommit; |
| 36 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 37 | import org.eclipse.jgit.treewalk.filter.TreeFilter; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 38 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Wrapper around {@link RevWalk} that paginates for Gitiles. |
| 41 | * |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame^] | 42 | * <p>A single page of a shortlog is defined by a revision range, such as "master" or |
| 43 | * "master..next", a page size, and a start commit, such as "c0ffee". The distance between the first |
| 44 | * commit in the walk ("next") and the first commit in the page may be arbitrarily long, but in |
| 45 | * order to present the commit list in a stable way, we must always start from the first commit in |
| 46 | * the walk. This is because there may be arbitrary merge commits between "c0ffee" and "next" that |
| 47 | * effectively insert arbitrary commits into the history starting from "c0ffee". |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 48 | */ |
| 49 | class Paginator implements Iterable<RevCommit> { |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 50 | private static class RenameWatcher extends RenameCallback { |
| 51 | private DiffEntry entry; |
| 52 | |
| 53 | @Override |
| 54 | public void renamed(DiffEntry entry) { |
| 55 | this.entry = entry; |
| 56 | } |
| 57 | |
| 58 | private DiffEntry getAndClear() { |
| 59 | DiffEntry e = entry; |
| 60 | entry = null; |
| 61 | return e; |
| 62 | } |
| 63 | } |
| 64 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 65 | private final RevWalk walk; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 66 | private final int limit; |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 67 | private final ObjectId prevStart; |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 68 | private final RenameWatcher renameWatcher; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 69 | |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 70 | private RevCommit first; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 71 | private boolean done; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 72 | private int n; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 73 | private ObjectId nextStart; |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 74 | private Map<ObjectId, DiffEntry> renamed; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 77 | * Construct a paginator and walk eagerly to the first returned commit. |
| 78 | * |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 79 | * @param walk revision walk; must be fully initialized before calling. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 80 | * @param limit page size. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame^] | 81 | * @param start commit at which to start the walk, or null to start at the beginning. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 82 | */ |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 83 | Paginator(RevWalk walk, int limit, @Nullable ObjectId start) |
| 84 | throws MissingObjectException, IncorrectObjectTypeException, IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 85 | this.walk = checkNotNull(walk, "walk"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 86 | checkArgument(limit > 0, "limit must be positive: %s", limit); |
| 87 | this.limit = limit; |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 88 | |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 89 | TreeFilter filter = walk.getTreeFilter(); |
| 90 | if (filter instanceof FollowFilter) { |
| 91 | renameWatcher = new RenameWatcher(); |
| 92 | ((FollowFilter) filter).setRenameCallback(renameWatcher); |
| 93 | } else { |
| 94 | renameWatcher = null; |
| 95 | } |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 96 | |
| Dave Borowitz | 96a6f47 | 2014-11-04 16:38:20 -0800 | [diff] [blame] | 97 | Deque<ObjectId> prevBuffer = new ArrayDeque<>(start != null ? limit : 0); |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 98 | while (true) { |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 99 | RevCommit commit = nextWithRename(); |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 100 | if (commit == null) { |
| 101 | done = true; |
| 102 | break; |
| 103 | } |
| 104 | if (start == null || start.equals(commit)) { |
| 105 | first = commit; |
| 106 | break; |
| 107 | } |
| 108 | if (prevBuffer.size() == limit) { |
| 109 | prevBuffer.remove(); |
| 110 | } |
| 111 | prevBuffer.add(commit); |
| 112 | } |
| 113 | prevStart = prevBuffer.pollFirst(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get the next element in this page of the walk. |
| 118 | * |
| 119 | * @return the next element, or null if the walk is finished. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 120 | * @throws MissingObjectException See {@link RevWalk#next()}. |
| 121 | * @throws IncorrectObjectTypeException See {@link RevWalk#next()}. |
| 122 | * @throws IOException See {@link RevWalk#next()}. |
| 123 | */ |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 124 | public RevCommit next() throws MissingObjectException, IncorrectObjectTypeException, IOException { |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 125 | if (done) { |
| 126 | return null; |
| 127 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 128 | RevCommit commit; |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 129 | if (first != null) { |
| 130 | commit = first; |
| 131 | first = null; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 132 | } else { |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 133 | commit = nextWithRename(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 134 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 135 | if (++n == limit) { |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 136 | nextStart = nextWithRename(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 137 | done = true; |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 138 | } else if (commit == null) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 139 | done = true; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 140 | } |
| 141 | return commit; |
| 142 | } |
| 143 | |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 144 | private RevCommit nextWithRename() throws IOException { |
| 145 | RevCommit next = walk.next(); |
| 146 | if (renameWatcher != null) { |
| 147 | // The commit that triggered the rename isn't available to RenameWatcher, |
| 148 | // so we can't populate the map from the callback directly. Instead, we |
| 149 | // need to check after each call to walk.next() whether a rename occurred |
| 150 | // due to this commit. |
| 151 | DiffEntry entry = renameWatcher.getAndClear(); |
| 152 | if (entry != null) { |
| 153 | if (renamed == null) { |
| 154 | renamed = new HashMap<>(); |
| 155 | } |
| 156 | renamed.put(next.copy(), entry); |
| 157 | } |
| 158 | } |
| 159 | return next; |
| 160 | } |
| 161 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 162 | /** |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame^] | 163 | * @return the ID at the start of the page of results preceding this one, or null if this is the |
| 164 | * first page. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 165 | */ |
| 166 | public ObjectId getPreviousStart() { |
| Dave Borowitz | 7756efb | 2014-07-30 08:45:20 -0700 | [diff] [blame] | 167 | return prevStart; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame^] | 171 | * @return the ID at the start of the page of results after this one, or null if this is the last |
| 172 | * page. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 173 | */ |
| 174 | public ObjectId getNextStart() { |
| 175 | checkState(done, "getNextStart() invalid before walk done"); |
| 176 | return nextStart; |
| 177 | } |
| 178 | |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame^] | 179 | /** @return entry corresponding to a rename or copy at the given commit. */ |
| Dave Borowitz | cb88d4d | 2015-10-26 13:58:17 -0400 | [diff] [blame] | 180 | public DiffEntry getRename(ObjectId commitId) { |
| 181 | return renamed != null ? renamed.get(commitId) : null; |
| 182 | } |
| 183 | |
| 184 | /** |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 185 | * @return an iterator over the commits in this walk. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame^] | 186 | * @throws RevWalkException if an error occurred, wrapping the checked exception from {@link |
| 187 | * #next()}. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 188 | */ |
| 189 | @Override |
| 190 | public Iterator<RevCommit> iterator() { |
| 191 | return new Iterator<RevCommit>() { |
| 192 | RevCommit next = nextUnchecked(); |
| 193 | |
| 194 | @Override |
| 195 | public boolean hasNext() { |
| 196 | return next != null; |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public RevCommit next() { |
| 201 | RevCommit r = next; |
| 202 | next = nextUnchecked(); |
| 203 | return r; |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void remove() { |
| 208 | throw new UnsupportedOperationException(); |
| 209 | } |
| 210 | }; |
| 211 | } |
| 212 | |
| Dave Borowitz | 27fada4 | 2013-11-01 11:09:49 -0700 | [diff] [blame] | 213 | public int getLimit() { |
| 214 | return limit; |
| 215 | } |
| 216 | |
| 217 | public RevWalk getWalk() { |
| 218 | return walk; |
| 219 | } |
| 220 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 221 | private RevCommit nextUnchecked() { |
| 222 | try { |
| 223 | return next(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 224 | } catch (IOException e) { |
| 225 | throw new RevWalkException(e); |
| 226 | } |
| 227 | } |
| 228 | } |