| 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 | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 17 | import static com.google.common.base.MoreObjects.toStringHelper; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static com.google.common.base.Predicates.not; |
| 20 | import static com.google.common.collect.Collections2.filter; |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 21 | import static java.util.Objects.hash; |
| Dave Borowitz | 227ce70 | 2013-06-10 10:49:27 -0700 | [diff] [blame] | 22 | import static org.eclipse.jgit.lib.Constants.R_HEADS; |
| 23 | import static org.eclipse.jgit.lib.Constants.R_TAGS; |
| 24 | |
| Dave Borowitz | a55017e | 2013-06-19 10:53:26 -0700 | [diff] [blame] | 25 | import com.google.common.base.Function; |
| Dave Borowitz | a55017e | 2013-06-19 10:53:26 -0700 | [diff] [blame] | 26 | import com.google.common.base.Predicate; |
| 27 | import com.google.common.base.Predicates; |
| 28 | import com.google.common.base.Throwables; |
| 29 | import com.google.common.cache.Cache; |
| 30 | import com.google.common.cache.CacheBuilder; |
| 31 | import com.google.common.collect.Collections2; |
| 32 | import com.google.common.util.concurrent.ExecutionError; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 33 | import java.io.IOException; |
| 34 | import java.util.Arrays; |
| 35 | import java.util.Collection; |
| 36 | import java.util.Objects; |
| 37 | import java.util.concurrent.Callable; |
| 38 | import java.util.concurrent.ExecutionException; |
| 39 | import java.util.concurrent.TimeUnit; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 40 | import org.eclipse.jgit.errors.IncorrectObjectTypeException; |
| 41 | import org.eclipse.jgit.errors.MissingObjectException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 42 | import org.eclipse.jgit.lib.ObjectId; |
| 43 | import org.eclipse.jgit.lib.Ref; |
| 44 | import org.eclipse.jgit.lib.RefDatabase; |
| 45 | import org.eclipse.jgit.lib.Repository; |
| 46 | import org.eclipse.jgit.revwalk.RevCommit; |
| 47 | import org.eclipse.jgit.revwalk.RevSort; |
| 48 | import org.eclipse.jgit.revwalk.RevWalk; |
| 49 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 50 | /** Cache of per-user object visibility. */ |
| 51 | public class VisibilityCache { |
| 52 | private static class Key { |
| 53 | private final Object user; |
| 54 | private final String repositoryName; |
| 55 | private final ObjectId objectId; |
| 56 | |
| 57 | private Key(Object user, String repositoryName, ObjectId objectId) { |
| 58 | this.user = checkNotNull(user, "user"); |
| 59 | this.repositoryName = checkNotNull(repositoryName, "repositoryName"); |
| Shawn Pearce | 13e5cc6 | 2013-02-06 11:32:20 -0800 | [diff] [blame] | 60 | this.objectId = checkNotNull(objectId, "objectId").copy(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public boolean equals(Object o) { |
| 65 | if (o instanceof Key) { |
| 66 | Key k = (Key) o; |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 67 | return Objects.equals(user, k.user) |
| 68 | && Objects.equals(repositoryName, k.repositoryName) |
| 69 | && Objects.equals(objectId, k.objectId); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public int hashCode() { |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 76 | return hash(user, repositoryName, objectId); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public String toString() { |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 81 | return toStringHelper(this) |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 82 | .add("user", user) |
| 83 | .add("repositoryName", repositoryName) |
| 84 | .add("objectId", objectId) |
| 85 | .toString(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | private final Cache<Key, Boolean> cache; |
| 90 | private final boolean topoSort; |
| 91 | |
| Dave Borowitz | d71f376 | 2014-04-18 11:05:20 -0700 | [diff] [blame] | 92 | public static CacheBuilder<Object, Object> defaultBuilder() { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 93 | return CacheBuilder.newBuilder().maximumSize(1 << 10).expireAfterWrite(30, TimeUnit.MINUTES); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | public VisibilityCache(boolean topoSort) { |
| Dave Borowitz | d71f376 | 2014-04-18 11:05:20 -0700 | [diff] [blame] | 97 | this(topoSort, defaultBuilder()); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | public VisibilityCache(boolean topoSort, CacheBuilder<Object, Object> builder) { |
| 101 | this.cache = builder.build(); |
| 102 | this.topoSort = topoSort; |
| 103 | } |
| 104 | |
| 105 | public Cache<?, Boolean> getCache() { |
| 106 | return cache; |
| 107 | } |
| 108 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 109 | boolean isVisible( |
| 110 | final Repository repo, |
| 111 | final RevWalk walk, |
| 112 | GitilesAccess access, |
| 113 | final ObjectId id, |
| 114 | final ObjectId... knownReachable) |
| 115 | throws IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 116 | try { |
| 117 | return cache.get( |
| 118 | new Key(access.getUserKey(), access.getRepositoryName(), id), |
| 119 | new Callable<Boolean>() { |
| 120 | @Override |
| 121 | public Boolean call() throws IOException { |
| Dave Borowitz | df363d2 | 2013-06-10 11:18:04 -0700 | [diff] [blame] | 122 | return isVisible(repo, walk, id, Arrays.asList(knownReachable)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 123 | } |
| 124 | }); |
| 125 | } catch (ExecutionException e) { |
| 126 | Throwables.propagateIfInstanceOf(e.getCause(), IOException.class); |
| 127 | throw new IOException(e); |
| Dave Borowitz | 3d67ed5 | 2013-06-10 11:06:12 -0700 | [diff] [blame] | 128 | } catch (ExecutionError e) { |
| 129 | // markUninteresting may overflow on pathological repos with very long |
| 130 | // merge chains. Play it safe and return false rather than letting the |
| 131 | // error propagate. |
| 132 | if (e.getCause() instanceof StackOverflowError) { |
| 133 | return false; |
| 134 | } |
| 135 | throw e; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 139 | private boolean isVisible( |
| 140 | Repository repo, RevWalk walk, ObjectId id, Collection<ObjectId> knownReachable) |
| 141 | throws IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 142 | RevCommit commit; |
| 143 | try { |
| 144 | commit = walk.parseCommit(id); |
| 145 | } catch (IncorrectObjectTypeException e) { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | // If any reference directly points at the requested object, permit display. |
| 150 | // Common for displays of pending patch sets in Gerrit Code Review, or |
| 151 | // bookmarks to the commit a tag points at. |
| 152 | Collection<Ref> allRefs = repo.getRefDatabase().getRefs(RefDatabase.ALL).values(); |
| 153 | for (Ref ref : allRefs) { |
| 154 | ref = repo.getRefDatabase().peel(ref); |
| 155 | if (id.equals(ref.getObjectId()) || id.equals(ref.getPeeledObjectId())) { |
| 156 | return true; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Check heads first under the assumption that most requests are for refs |
| 161 | // close to a head. Tags tend to be much further back in history and just |
| 162 | // clutter up the priority queue in the common case. |
| Dave Borowitz | df363d2 | 2013-06-10 11:18:04 -0700 | [diff] [blame] | 163 | return isReachableFrom(walk, commit, knownReachable) |
| 164 | || isReachableFromRefs(walk, commit, filter(allRefs, refStartsWith(R_HEADS))) |
| 165 | || isReachableFromRefs(walk, commit, filter(allRefs, refStartsWith(R_TAGS))) |
| 166 | || isReachableFromRefs(walk, commit, filter(allRefs, otherRefs())); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | private static Predicate<Ref> refStartsWith(final String prefix) { |
| 170 | return new Predicate<Ref>() { |
| 171 | @Override |
| 172 | public boolean apply(Ref ref) { |
| 173 | return ref.getName().startsWith(prefix); |
| 174 | } |
| 175 | }; |
| 176 | } |
| 177 | |
| Dave Borowitz | 227ce70 | 2013-06-10 10:49:27 -0700 | [diff] [blame] | 178 | @SuppressWarnings("unchecked") |
| 179 | private static Predicate<Ref> otherRefs() { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 180 | return not( |
| 181 | Predicates.<Ref>or( |
| 182 | refStartsWith(R_HEADS), refStartsWith(R_TAGS), refStartsWith("refs/changes/"))); |
| Dave Borowitz | 227ce70 | 2013-06-10 10:49:27 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 185 | private boolean isReachableFromRefs(RevWalk walk, RevCommit commit, Collection<Ref> refs) |
| 186 | throws IOException { |
| 187 | return isReachableFrom( |
| 188 | walk, |
| 189 | commit, |
| 190 | Collections2.transform( |
| 191 | refs, |
| 192 | new Function<Ref, ObjectId>() { |
| 193 | @Override |
| 194 | public ObjectId apply(Ref ref) { |
| 195 | if (ref.getPeeledObjectId() != null) { |
| 196 | return ref.getPeeledObjectId(); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 197 | } |
| David Pursehouse | b3b630f | 2016-06-15 21:51:18 +0900 | [diff] [blame] | 198 | return ref.getObjectId(); |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 199 | } |
| 200 | })); |
| Dave Borowitz | df363d2 | 2013-06-10 11:18:04 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | private boolean isReachableFrom(RevWalk walk, RevCommit commit, Collection<ObjectId> ids) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 204 | throws IOException { |
| Dave Borowitz | df363d2 | 2013-06-10 11:18:04 -0700 | [diff] [blame] | 205 | if (ids.isEmpty()) { |
| Dave Borowitz | 227ce70 | 2013-06-10 10:49:27 -0700 | [diff] [blame] | 206 | return false; |
| 207 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 208 | walk.reset(); |
| 209 | if (topoSort) { |
| 210 | walk.sort(RevSort.TOPO); |
| 211 | } |
| 212 | walk.markStart(commit); |
| Dave Borowitz | df363d2 | 2013-06-10 11:18:04 -0700 | [diff] [blame] | 213 | for (ObjectId id : ids) { |
| 214 | markUninteresting(walk, id); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 215 | } |
| Dave Borowitz | df363d2 | 2013-06-10 11:18:04 -0700 | [diff] [blame] | 216 | // If the commit is reachable from any given tip, it will appear to be |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 217 | // uninteresting to the RevWalk and no output will be produced. |
| 218 | return walk.next() == null; |
| 219 | } |
| 220 | |
| 221 | private static void markUninteresting(RevWalk walk, ObjectId id) throws IOException { |
| 222 | if (id == null) { |
| 223 | return; |
| 224 | } |
| 225 | try { |
| 226 | walk.markUninteresting(walk.parseCommit(id)); |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 227 | } catch (IncorrectObjectTypeException | MissingObjectException e) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 228 | // Do nothing, doesn't affect reachability. |
| 229 | } |
| 230 | } |
| 231 | } |