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