| 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.checkArgument; |
| 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 | import static org.eclipse.jgit.lib.Constants.OBJ_BAD; |
| 21 | import static org.eclipse.jgit.lib.Constants.OBJ_TAG; |
| 22 | |
| 23 | import com.google.common.annotations.VisibleForTesting; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 24 | import java.io.IOException; |
| 25 | import java.util.Objects; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 26 | import org.eclipse.jgit.errors.MissingObjectException; |
| 27 | import org.eclipse.jgit.lib.AbbreviatedObjectId; |
| Dave Borowitz | 5530a16 | 2013-06-19 15:14:47 -0700 | [diff] [blame] | 28 | import org.eclipse.jgit.lib.Constants; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 29 | import org.eclipse.jgit.lib.ObjectId; |
| 30 | import org.eclipse.jgit.revwalk.RevObject; |
| 31 | import org.eclipse.jgit.revwalk.RevWalk; |
| 32 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Object encapsulating a single revision as seen by Gitiles. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 35 | * |
| 36 | * <p>A single revision consists of a name, an ID, and a type. Name parsing is done once per request |
| 37 | * by {@link RevisionParser}. |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 38 | */ |
| 39 | public class Revision { |
| 40 | /** Sentinel indicating a missing or empty revision. */ |
| 41 | public static final Revision NULL = peeled("", ObjectId.zeroId(), OBJ_BAD); |
| 42 | |
| 43 | /** Common default branch given to clients. */ |
| 44 | public static final Revision HEAD = named("HEAD"); |
| 45 | |
| Dave Borowitz | 5d5619d | 2014-04-18 17:01:45 -0700 | [diff] [blame] | 46 | public static Revision normalizeParentExpressions(Revision rev) { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 47 | if (rev == null || (rev.name.indexOf('~') < 0 && rev.name.indexOf('^') < 0)) { |
| Dave Borowitz | 5d5619d | 2014-04-18 17:01:45 -0700 | [diff] [blame] | 48 | return rev; |
| 49 | } |
| 50 | return new Revision(rev.id.name(), rev.id, rev.type, rev.peeledId, rev.peeledType); |
| 51 | } |
| 52 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 53 | private final String name; |
| 54 | private final ObjectId id; |
| 55 | private final int type; |
| 56 | private final ObjectId peeledId; |
| 57 | private final int peeledType; |
| 58 | |
| 59 | public static Revision peeled(String name, RevObject obj) { |
| 60 | return peeled(name, obj, obj.getType()); |
| 61 | } |
| 62 | |
| 63 | public static Revision unpeeled(String name, ObjectId id) { |
| 64 | return peeled(name, id, OBJ_BAD); |
| 65 | } |
| 66 | |
| 67 | public static Revision named(String name) { |
| 68 | return peeled(name, null, OBJ_BAD); |
| 69 | } |
| 70 | |
| Dave Borowitz | 48ca670 | 2013-04-09 11:52:41 -0700 | [diff] [blame] | 71 | public static Revision peel(String name, RevObject obj, RevWalk walk) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 72 | throws MissingObjectException, IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 73 | RevObject peeled = walk.peel(obj); |
| 74 | return new Revision(name, obj, obj.getType(), peeled, peeled.getType()); |
| 75 | } |
| 76 | |
| 77 | private static Revision peeled(String name, ObjectId id, int type) { |
| 78 | checkArgument(type != OBJ_TAG, "expected non-tag for %s/%s", name, id); |
| 79 | return new Revision(name, id, type, id, type); |
| 80 | } |
| 81 | |
| 82 | @VisibleForTesting |
| 83 | Revision(String name, ObjectId id, int type, ObjectId peeledId, int peeledType) { |
| 84 | this.name = name; |
| 85 | this.id = id; |
| 86 | this.type = type; |
| 87 | this.peeledId = peeledId; |
| 88 | this.peeledType = peeledType; |
| 89 | } |
| 90 | |
| David Pursehouse | c53de2b | 2019-06-01 15:27:25 +0900 | [diff] [blame] | 91 | @SuppressWarnings("ReferenceEquality") |
| 92 | public static boolean isNull(Revision r) { |
| 93 | return r == NULL; |
| 94 | } |
| 95 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 96 | public String getName() { |
| 97 | return name; |
| 98 | } |
| 99 | |
| 100 | public int getType() { |
| 101 | return type; |
| 102 | } |
| 103 | |
| 104 | public ObjectId getId() { |
| 105 | return id; |
| 106 | } |
| 107 | |
| 108 | public ObjectId getPeeledId() { |
| 109 | return peeledId; |
| 110 | } |
| 111 | |
| 112 | public int getPeeledType() { |
| 113 | return peeledType; |
| 114 | } |
| 115 | |
| Dave Borowitz | 2298cef | 2014-09-23 10:29:46 -0700 | [diff] [blame] | 116 | public boolean matches(ObjectId other) { |
| 117 | return id.equals(other) || nameEqualsAbbreviated(other); |
| 118 | } |
| 119 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 120 | public boolean nameIsId() { |
| Dave Borowitz | 2298cef | 2014-09-23 10:29:46 -0700 | [diff] [blame] | 121 | return nameEqualsAbbreviated(id); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public boolean equals(Object o) { |
| 126 | if (o instanceof Revision) { |
| 127 | Revision r = (Revision) o; |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 128 | return Objects.equals(name, r.name) |
| 129 | && Objects.equals(id, r.id) |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 130 | && type == r.type |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 131 | && Objects.equals(peeledId, r.peeledId) |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 132 | && peeledType == r.peeledType; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public int hashCode() { |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 139 | return hash(name, id, type, peeledId, peeledType); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public String toString() { |
| Dave Borowitz | c410f96 | 2014-09-23 10:49:26 -0700 | [diff] [blame] | 144 | return toStringHelper(this) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 145 | .omitNullValues() |
| 146 | .add("name", name) |
| Dave Borowitz | 5530a16 | 2013-06-19 15:14:47 -0700 | [diff] [blame] | 147 | .add("id", id != null ? id.getName() : null) |
| 148 | .add("type", type > 0 ? Constants.typeString(type) : null) |
| 149 | .add("peeledId", peeledId != null ? peeledId.getName() : null) |
| 150 | .add("peeledType", type > 0 ? Constants.typeString(peeledType) : null) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 151 | .toString(); |
| 152 | } |
| Dave Borowitz | 2298cef | 2014-09-23 10:29:46 -0700 | [diff] [blame] | 153 | |
| 154 | private boolean nameEqualsAbbreviated(ObjectId other) { |
| 155 | return AbbreviatedObjectId.isId(name) |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 156 | && AbbreviatedObjectId.fromString(name).prefixCompare(other) == 0; |
| Dave Borowitz | 2298cef | 2014-09-23 10:29:46 -0700 | [diff] [blame] | 157 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 158 | } |