blob: 02a04f869329c8e71af82924bb89b6b2e7510bc6 [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// 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
15package com.google.gitiles;
16
Dave Borowitzc410f962014-09-23 10:49:26 -070017import static com.google.common.base.MoreObjects.toStringHelper;
Dave Borowitz9de65952012-08-13 16:09:45 -070018import static com.google.common.base.Preconditions.checkArgument;
Dave Borowitzc410f962014-09-23 10:49:26 -070019import static java.util.Objects.hash;
Dave Borowitz9de65952012-08-13 16:09:45 -070020import static org.eclipse.jgit.lib.Constants.OBJ_BAD;
21import static org.eclipse.jgit.lib.Constants.OBJ_TAG;
22
23import com.google.common.annotations.VisibleForTesting;
Dave Borowitz3b744b12016-08-19 16:11:10 -040024import java.io.IOException;
25import java.util.Objects;
Dave Borowitz9de65952012-08-13 16:09:45 -070026import org.eclipse.jgit.errors.MissingObjectException;
27import org.eclipse.jgit.lib.AbbreviatedObjectId;
Dave Borowitz5530a162013-06-19 15:14:47 -070028import org.eclipse.jgit.lib.Constants;
Dave Borowitz9de65952012-08-13 16:09:45 -070029import org.eclipse.jgit.lib.ObjectId;
30import org.eclipse.jgit.revwalk.RevObject;
31import org.eclipse.jgit.revwalk.RevWalk;
32
Dave Borowitz9de65952012-08-13 16:09:45 -070033/**
34 * Object encapsulating a single revision as seen by Gitiles.
Dave Borowitz40255d52016-08-19 16:16:22 -040035 *
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 Borowitz9de65952012-08-13 16:09:45 -070038 */
39public 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 Borowitz5d5619d2014-04-18 17:01:45 -070046 public static Revision normalizeParentExpressions(Revision rev) {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020047 if (rev == null || (rev.name.indexOf('~') < 0 && rev.name.indexOf('^') < 0)) {
Dave Borowitz5d5619d2014-04-18 17:01:45 -070048 return rev;
49 }
50 return new Revision(rev.id.name(), rev.id, rev.type, rev.peeledId, rev.peeledType);
51 }
52
Dave Borowitz9de65952012-08-13 16:09:45 -070053 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 Borowitz48ca6702013-04-09 11:52:41 -070071 public static Revision peel(String name, RevObject obj, RevWalk walk)
Dave Borowitz9de65952012-08-13 16:09:45 -070072 throws MissingObjectException, IOException {
Dave Borowitz9de65952012-08-13 16:09:45 -070073 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 Pursehousec53de2b2019-06-01 15:27:25 +090091 @SuppressWarnings("ReferenceEquality")
92 public static boolean isNull(Revision r) {
93 return r == NULL;
94 }
95
Dave Borowitz9de65952012-08-13 16:09:45 -070096 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 Borowitz2298cef2014-09-23 10:29:46 -0700116 public boolean matches(ObjectId other) {
117 return id.equals(other) || nameEqualsAbbreviated(other);
118 }
119
Dave Borowitz9de65952012-08-13 16:09:45 -0700120 public boolean nameIsId() {
Dave Borowitz2298cef2014-09-23 10:29:46 -0700121 return nameEqualsAbbreviated(id);
Dave Borowitz9de65952012-08-13 16:09:45 -0700122 }
123
124 @Override
125 public boolean equals(Object o) {
126 if (o instanceof Revision) {
127 Revision r = (Revision) o;
Dave Borowitzc410f962014-09-23 10:49:26 -0700128 return Objects.equals(name, r.name)
129 && Objects.equals(id, r.id)
Dave Borowitz27058932014-12-03 15:44:46 -0800130 && type == r.type
Dave Borowitzc410f962014-09-23 10:49:26 -0700131 && Objects.equals(peeledId, r.peeledId)
Dave Borowitz27058932014-12-03 15:44:46 -0800132 && peeledType == r.peeledType;
Dave Borowitz9de65952012-08-13 16:09:45 -0700133 }
134 return false;
135 }
136
137 @Override
138 public int hashCode() {
Dave Borowitzc410f962014-09-23 10:49:26 -0700139 return hash(name, id, type, peeledId, peeledType);
Dave Borowitz9de65952012-08-13 16:09:45 -0700140 }
141
142 @Override
143 public String toString() {
Dave Borowitzc410f962014-09-23 10:49:26 -0700144 return toStringHelper(this)
Dave Borowitz9de65952012-08-13 16:09:45 -0700145 .omitNullValues()
146 .add("name", name)
Dave Borowitz5530a162013-06-19 15:14:47 -0700147 .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 Borowitz9de65952012-08-13 16:09:45 -0700151 .toString();
152 }
Dave Borowitz2298cef2014-09-23 10:29:46 -0700153
154 private boolean nameEqualsAbbreviated(ObjectId other) {
155 return AbbreviatedObjectId.isId(name)
Dave Borowitz27058932014-12-03 15:44:46 -0800156 && AbbreviatedObjectId.fromString(name).prefixCompare(other) == 0;
Dave Borowitz2298cef2014-09-23 10:29:46 -0700157 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700158}