blob: b388e368f6f23eff96a191c7bc91138c8b42cccc [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 Borowitz9de65952012-08-13 16:09:45 -070024
25import org.eclipse.jgit.errors.MissingObjectException;
26import org.eclipse.jgit.lib.AbbreviatedObjectId;
Dave Borowitz5530a162013-06-19 15:14:47 -070027import org.eclipse.jgit.lib.Constants;
Dave Borowitz9de65952012-08-13 16:09:45 -070028import org.eclipse.jgit.lib.ObjectId;
29import org.eclipse.jgit.revwalk.RevObject;
30import org.eclipse.jgit.revwalk.RevWalk;
31
32import java.io.IOException;
Dave Borowitzc410f962014-09-23 10:49:26 -070033import java.util.Objects;
Dave Borowitz9de65952012-08-13 16:09:45 -070034
35/**
36 * Object encapsulating a single revision as seen by Gitiles.
37 * <p>
38 * A single revision consists of a name, an ID, and a type. Name parsing is done
39 * once per request by {@link RevisionParser}.
40 */
41public class Revision {
42 /** Sentinel indicating a missing or empty revision. */
43 public static final Revision NULL = peeled("", ObjectId.zeroId(), OBJ_BAD);
44
45 /** Common default branch given to clients. */
46 public static final Revision HEAD = named("HEAD");
47
Dave Borowitz5d5619d2014-04-18 17:01:45 -070048 public static Revision normalizeParentExpressions(Revision rev) {
49 if (rev == null
50 || (rev.name.indexOf("~") < 0 && rev.name.indexOf("^") < 0)) {
51 return rev;
52 }
53 return new Revision(rev.id.name(), rev.id, rev.type, rev.peeledId, rev.peeledType);
54 }
55
Dave Borowitz9de65952012-08-13 16:09:45 -070056 private final String name;
57 private final ObjectId id;
58 private final int type;
59 private final ObjectId peeledId;
60 private final int peeledType;
61
62 public static Revision peeled(String name, RevObject obj) {
63 return peeled(name, obj, obj.getType());
64 }
65
66 public static Revision unpeeled(String name, ObjectId id) {
67 return peeled(name, id, OBJ_BAD);
68 }
69
70 public static Revision named(String name) {
71 return peeled(name, null, OBJ_BAD);
72 }
73
Dave Borowitz48ca6702013-04-09 11:52:41 -070074 public static Revision peel(String name, RevObject obj, RevWalk walk)
Dave Borowitz9de65952012-08-13 16:09:45 -070075 throws MissingObjectException, IOException {
Dave Borowitz9de65952012-08-13 16:09:45 -070076 RevObject peeled = walk.peel(obj);
77 return new Revision(name, obj, obj.getType(), peeled, peeled.getType());
78 }
79
80 private static Revision peeled(String name, ObjectId id, int type) {
81 checkArgument(type != OBJ_TAG, "expected non-tag for %s/%s", name, id);
82 return new Revision(name, id, type, id, type);
83 }
84
85 @VisibleForTesting
86 Revision(String name, ObjectId id, int type, ObjectId peeledId, int peeledType) {
87 this.name = name;
88 this.id = id;
89 this.type = type;
90 this.peeledId = peeledId;
91 this.peeledType = peeledType;
92 }
93
94 public String getName() {
95 return name;
96 }
97
98 public int getType() {
99 return type;
100 }
101
102 public ObjectId getId() {
103 return id;
104 }
105
106 public ObjectId getPeeledId() {
107 return peeledId;
108 }
109
110 public int getPeeledType() {
111 return peeledType;
112 }
113
Dave Borowitz2298cef2014-09-23 10:29:46 -0700114 public boolean matches(ObjectId other) {
115 return id.equals(other) || nameEqualsAbbreviated(other);
116 }
117
Dave Borowitz9de65952012-08-13 16:09:45 -0700118 public boolean nameIsId() {
Dave Borowitz2298cef2014-09-23 10:29:46 -0700119 return nameEqualsAbbreviated(id);
Dave Borowitz9de65952012-08-13 16:09:45 -0700120 }
121
122 @Override
123 public boolean equals(Object o) {
124 if (o instanceof Revision) {
125 Revision r = (Revision) o;
Dave Borowitzc410f962014-09-23 10:49:26 -0700126 return Objects.equals(name, r.name)
127 && Objects.equals(id, r.id)
128 && Objects.equals(type, r.type)
129 && Objects.equals(peeledId, r.peeledId)
130 && Objects.equals(peeledType, r.peeledType);
Dave Borowitz9de65952012-08-13 16:09:45 -0700131 }
132 return false;
133 }
134
135 @Override
136 public int hashCode() {
Dave Borowitzc410f962014-09-23 10:49:26 -0700137 return hash(name, id, type, peeledId, peeledType);
Dave Borowitz9de65952012-08-13 16:09:45 -0700138 }
139
140 @Override
141 public String toString() {
Dave Borowitzc410f962014-09-23 10:49:26 -0700142 return toStringHelper(this)
Dave Borowitz9de65952012-08-13 16:09:45 -0700143 .omitNullValues()
144 .add("name", name)
Dave Borowitz5530a162013-06-19 15:14:47 -0700145 .add("id", id != null ? id.getName() : null)
146 .add("type", type > 0 ? Constants.typeString(type) : null)
147 .add("peeledId", peeledId != null ? peeledId.getName() : null)
148 .add("peeledType", type > 0 ? Constants.typeString(peeledType) : null)
Dave Borowitz9de65952012-08-13 16:09:45 -0700149 .toString();
150 }
Dave Borowitz2298cef2014-09-23 10:29:46 -0700151
152 private boolean nameEqualsAbbreviated(ObjectId other) {
153 return AbbreviatedObjectId.isId(name)
154 ? AbbreviatedObjectId.fromString(name).prefixCompare(other) == 0
155 : false;
156 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700157}