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