| 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; |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 19 | import static java.util.Objects.hash; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 20 | |
| Dave Borowitz | fe8fdab | 2014-11-04 16:19:33 -0800 | [diff] [blame] | 21 | import com.google.common.annotations.VisibleForTesting; |
| 22 | import com.google.common.base.CharMatcher; |
| Dave Borowitz | fe8fdab | 2014-11-04 16:19:33 -0800 | [diff] [blame] | 23 | import com.google.common.base.Splitter; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 24 | import java.io.IOException; |
| 25 | import java.util.Objects; |
| Dave Borowitz | 93c1fca | 2013-09-25 16:37:43 -0700 | [diff] [blame] | 26 | import org.eclipse.jgit.errors.AmbiguousObjectException; |
| Dave Borowitz | 48ca670 | 2013-04-09 11:52:41 -0700 | [diff] [blame] | 27 | import org.eclipse.jgit.errors.MissingObjectException; |
| Dave Borowitz | 5f7e8b7 | 2013-01-07 09:31:41 -0800 | [diff] [blame] | 28 | import org.eclipse.jgit.errors.RevisionSyntaxException; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 29 | import org.eclipse.jgit.lib.ObjectId; |
| 30 | import org.eclipse.jgit.lib.Repository; |
| 31 | import org.eclipse.jgit.revwalk.RevCommit; |
| Dave Borowitz | 48ca670 | 2013-04-09 11:52:41 -0700 | [diff] [blame] | 32 | import org.eclipse.jgit.revwalk.RevObject; |
| 33 | import org.eclipse.jgit.revwalk.RevTag; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 34 | import org.eclipse.jgit.revwalk.RevWalk; |
| 35 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 36 | /** Object to parse revisions out of Gitiles paths. */ |
| 37 | class RevisionParser { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 38 | private static final Splitter OPERATOR_SPLITTER = Splitter.on(CharMatcher.anyOf("^~")); |
| 39 | |
| 40 | static class Result { |
| 41 | private final Revision revision; |
| 42 | private final Revision oldRevision; |
| Dave Borowitz | 06b88d2 | 2013-06-19 15:19:14 -0700 | [diff] [blame] | 43 | private final String path; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 44 | |
| 45 | @VisibleForTesting |
| 46 | Result(Revision revision) { |
| Dave Borowitz | 06b88d2 | 2013-06-19 15:19:14 -0700 | [diff] [blame] | 47 | this(revision, null, ""); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @VisibleForTesting |
| Dave Borowitz | 06b88d2 | 2013-06-19 15:19:14 -0700 | [diff] [blame] | 51 | Result(Revision revision, Revision oldRevision, String path) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 52 | this.revision = revision; |
| 53 | this.oldRevision = oldRevision; |
| Dave Borowitz | 06b88d2 | 2013-06-19 15:19:14 -0700 | [diff] [blame] | 54 | this.path = path; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | public Revision getRevision() { |
| 58 | return revision; |
| 59 | } |
| 60 | |
| 61 | public Revision getOldRevision() { |
| 62 | return oldRevision; |
| 63 | } |
| 64 | |
| Dave Borowitz | 06b88d2 | 2013-06-19 15:19:14 -0700 | [diff] [blame] | 65 | public String getPath() { |
| 66 | return path; |
| 67 | } |
| 68 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 69 | @Override |
| 70 | public boolean equals(Object o) { |
| 71 | if (o instanceof Result) { |
| 72 | Result r = (Result) o; |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 73 | return Objects.equals(revision, r.revision) |
| 74 | && Objects.equals(oldRevision, r.oldRevision) |
| 75 | && Objects.equals(path, r.path); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int hashCode() { |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 82 | return hash(revision, oldRevision, path); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public String toString() { |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 87 | return toStringHelper(this) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 88 | .omitNullValues() |
| Dave Borowitz | 06b88d2 | 2013-06-19 15:19:14 -0700 | [diff] [blame] | 89 | .add("revision", revision.getName()) |
| 90 | .add("oldRevision", oldRevision != null ? oldRevision.getName() : null) |
| 91 | .add("path", path) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 92 | .toString(); |
| 93 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | private final Repository repo; |
| 97 | private final GitilesAccess access; |
| 98 | private final VisibilityCache cache; |
| 99 | |
| 100 | RevisionParser(Repository repo, GitilesAccess access, VisibilityCache cache) { |
| 101 | this.repo = checkNotNull(repo, "repo"); |
| 102 | this.access = checkNotNull(access, "access"); |
| 103 | this.cache = checkNotNull(cache, "cache"); |
| 104 | } |
| 105 | |
| 106 | Result parse(String path) throws IOException { |
| Dave Borowitz | 06b88d2 | 2013-06-19 15:19:14 -0700 | [diff] [blame] | 107 | if (path.startsWith("/")) { |
| 108 | path = path.substring(1); |
| 109 | } |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 110 | try (RevWalk walk = new RevWalk(repo)) { |
| Jonathan Nieder | c63c059 | 2019-03-07 14:40:49 -0800 | [diff] [blame] | 111 | walk.setRetainBody(false); |
| 112 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 113 | Revision oldRevision = null; |
| 114 | |
| 115 | StringBuilder b = new StringBuilder(); |
| 116 | boolean first = true; |
| Dave Borowitz | cfc1c53 | 2015-02-18 13:41:19 -0800 | [diff] [blame] | 117 | for (String part : PathUtil.SPLITTER.split(path)) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 118 | if (part.isEmpty()) { |
| 119 | return null; // No valid revision contains empty segments. |
| 120 | } |
| 121 | if (!first) { |
| 122 | b.append('/'); |
| 123 | } |
| 124 | |
| 125 | if (oldRevision == null) { |
| 126 | int dots = part.indexOf(".."); |
| 127 | int firstParent = part.indexOf("^!"); |
| 128 | if (dots == 0 || firstParent == 0) { |
| 129 | return null; |
| 130 | } else if (dots > 0) { |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 131 | b.append(part, 0, dots); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 132 | String oldName = b.toString(); |
| 133 | if (!isValidRevision(oldName)) { |
| 134 | return null; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 135 | } |
| David Pursehouse | b3b630f | 2016-06-15 21:51:18 +0900 | [diff] [blame] | 136 | RevObject old = resolve(oldName, walk); |
| 137 | if (old == null) { |
| 138 | return null; |
| 139 | } |
| 140 | oldRevision = Revision.peel(oldName, old, walk); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 141 | part = part.substring(dots + 2); |
| 142 | b = new StringBuilder(); |
| 143 | } else if (firstParent > 0) { |
| 144 | if (firstParent != part.length() - 2) { |
| 145 | return null; |
| 146 | } |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 147 | b.append(part, 0, part.length() - 2); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 148 | String name = b.toString(); |
| 149 | if (!isValidRevision(name)) { |
| 150 | return null; |
| 151 | } |
| Dave Borowitz | 48ca670 | 2013-04-09 11:52:41 -0700 | [diff] [blame] | 152 | RevObject obj = resolve(name, walk); |
| 153 | if (obj == null) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 154 | return null; |
| 155 | } |
| Dave Borowitz | 48ca670 | 2013-04-09 11:52:41 -0700 | [diff] [blame] | 156 | while (obj instanceof RevTag) { |
| 157 | obj = ((RevTag) obj).getObject(); |
| 158 | walk.parseHeaders(obj); |
| 159 | } |
| 160 | if (!(obj instanceof RevCommit)) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 161 | return null; // Not a commit, ^! is invalid. |
| 162 | } |
| Dave Borowitz | 48ca670 | 2013-04-09 11:52:41 -0700 | [diff] [blame] | 163 | RevCommit c = (RevCommit) obj; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 164 | if (c.getParentCount() > 0) { |
| 165 | oldRevision = Revision.peeled(name + "^", c.getParent(0)); |
| 166 | } else { |
| 167 | oldRevision = Revision.NULL; |
| 168 | } |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 169 | Result result = |
| 170 | new Result( |
| 171 | Revision.peeled(name, c), oldRevision, path.substring(name.length() + 2)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 172 | return isVisible(walk, result) ? result : null; |
| 173 | } |
| 174 | } |
| 175 | b.append(part); |
| 176 | |
| 177 | String name = b.toString(); |
| 178 | if (!isValidRevision(name)) { |
| 179 | return null; |
| 180 | } |
| Dave Borowitz | 48ca670 | 2013-04-09 11:52:41 -0700 | [diff] [blame] | 181 | RevObject obj = resolve(name, walk); |
| 182 | if (obj != null) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 183 | int pathStart; |
| 184 | if (oldRevision == null) { |
| 185 | pathStart = name.length(); // foo |
| 186 | } else { |
| 187 | // foo..bar (foo may be empty) |
| 188 | pathStart = oldRevision.getName().length() + 2 + name.length(); |
| 189 | } |
| Dave Borowitz | e360d5c | 2015-09-16 16:53:30 -0400 | [diff] [blame] | 190 | Result result = |
| 191 | new Result(Revision.peel(name, obj, walk), oldRevision, path.substring(pathStart)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 192 | return isVisible(walk, result) ? result : null; |
| 193 | } |
| 194 | first = false; |
| 195 | } |
| 196 | return null; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| Dave Borowitz | 48ca670 | 2013-04-09 11:52:41 -0700 | [diff] [blame] | 200 | private RevObject resolve(String name, RevWalk walk) throws IOException { |
| Dave Borowitz | 5f7e8b7 | 2013-01-07 09:31:41 -0800 | [diff] [blame] | 201 | try { |
| Dave Borowitz | 48ca670 | 2013-04-09 11:52:41 -0700 | [diff] [blame] | 202 | ObjectId id = repo.resolve(name); |
| 203 | return id != null ? walk.parseAny(id) : null; |
| Dave Borowitz | 93c1fca | 2013-09-25 16:37:43 -0700 | [diff] [blame] | 204 | } catch (AmbiguousObjectException e) { |
| 205 | // TODO(dborowitz): Render a helpful disambiguation page. |
| 206 | return null; |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 207 | } catch (RevisionSyntaxException | MissingObjectException e) { |
| Dave Borowitz | 48ca670 | 2013-04-09 11:52:41 -0700 | [diff] [blame] | 208 | return null; |
| Dave Borowitz | 5f7e8b7 | 2013-01-07 09:31:41 -0800 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 212 | private static boolean isValidRevision(String revision) { |
| 213 | // Disallow some uncommon but valid revision expressions that either we |
| 214 | // don't support or we represent differently in our URLs. |
| Jonathan Nieder | 4a7dac0 | 2019-07-01 16:15:03 -0700 | [diff] [blame] | 215 | return !revision.contains(":") |
| 216 | && !revision.contains("^{") |
| 217 | && !revision.contains("@{") |
| 218 | && !revision.equals("@"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | private boolean isVisible(RevWalk walk, Result result) throws IOException { |
| 222 | String maybeRef = OPERATOR_SPLITTER.split(result.getRevision().getName()).iterator().next(); |
| Dave Borowitz | 14cad73 | 2016-05-26 17:34:19 -0400 | [diff] [blame] | 223 | if (repo.findRef(maybeRef) != null) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 224 | // Name contains a visible ref; skip expensive reachability check. |
| 225 | return true; |
| 226 | } |
| Dave Borowitz | df363d2 | 2013-06-10 11:18:04 -0700 | [diff] [blame] | 227 | ObjectId id = result.getRevision().getId(); |
| 228 | if (!cache.isVisible(repo, walk, access, id)) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 229 | return false; |
| 230 | } |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 231 | if (result.getOldRevision() != null && !Revision.isNull(result.getOldRevision())) { |
| Dave Borowitz | df363d2 | 2013-06-10 11:18:04 -0700 | [diff] [blame] | 232 | return cache.isVisible(repo, walk, access, result.getOldRevision().getId(), id); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 233 | } |
| David Pursehouse | b3b630f | 2016-06-15 21:51:18 +0900 | [diff] [blame] | 234 | return true; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 235 | } |
| 236 | } |