-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRevision.java
More file actions
158 lines (133 loc) · 4.66 KB
/
Revision.java
File metadata and controls
158 lines (133 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gitiles;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.hash;
import static org.eclipse.jgit.lib.Constants.OBJ_BAD;
import static org.eclipse.jgit.lib.Constants.OBJ_TAG;
import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.util.Objects;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevWalk;
/**
* Object encapsulating a single revision as seen by Gitiles.
*
* <p>A single revision consists of a name, an ID, and a type. Name parsing is done once per request
* by {@link RevisionParser}.
*/
public class Revision {
/** Sentinel indicating a missing or empty revision. */
public static final Revision NULL = peeled("", ObjectId.zeroId(), OBJ_BAD);
/** Common default branch given to clients. */
public static final Revision HEAD = named("HEAD");
public static Revision normalizeParentExpressions(Revision rev) {
if (rev == null || (rev.name.indexOf('~') < 0 && rev.name.indexOf('^') < 0)) {
return rev;
}
return new Revision(rev.id.name(), rev.id, rev.type, rev.peeledId, rev.peeledType);
}
private final String name;
private final ObjectId id;
private final int type;
private final ObjectId peeledId;
private final int peeledType;
public static Revision peeled(String name, RevObject obj) {
return peeled(name, obj, obj.getType());
}
public static Revision unpeeled(String name, ObjectId id) {
return peeled(name, id, OBJ_BAD);
}
public static Revision named(String name) {
return peeled(name, null, OBJ_BAD);
}
public static Revision peel(String name, RevObject obj, RevWalk walk)
throws MissingObjectException, IOException {
RevObject peeled = walk.peel(obj);
return new Revision(name, obj, obj.getType(), peeled, peeled.getType());
}
private static Revision peeled(String name, ObjectId id, int type) {
checkArgument(type != OBJ_TAG, "expected non-tag for %s/%s", name, id);
return new Revision(name, id, type, id, type);
}
@VisibleForTesting
Revision(String name, ObjectId id, int type, ObjectId peeledId, int peeledType) {
this.name = name;
this.id = id;
this.type = type;
this.peeledId = peeledId;
this.peeledType = peeledType;
}
@SuppressWarnings("ReferenceEquality")
public static boolean isNull(Revision r) {
return r == NULL;
}
public String getName() {
return name;
}
public int getType() {
return type;
}
public ObjectId getId() {
return id;
}
public ObjectId getPeeledId() {
return peeledId;
}
public int getPeeledType() {
return peeledType;
}
public boolean matches(ObjectId other) {
return id.equals(other) || nameEqualsAbbreviated(other);
}
public boolean nameIsId() {
return nameEqualsAbbreviated(id);
}
@Override
public boolean equals(Object o) {
if (o instanceof Revision) {
Revision r = (Revision) o;
return Objects.equals(name, r.name)
&& Objects.equals(id, r.id)
&& type == r.type
&& Objects.equals(peeledId, r.peeledId)
&& peeledType == r.peeledType;
}
return false;
}
@Override
public int hashCode() {
return hash(name, id, type, peeledId, peeledType);
}
@Override
public String toString() {
return toStringHelper(this)
.omitNullValues()
.add("name", name)
.add("id", id != null ? id.getName() : null)
.add("type", type > 0 ? Constants.typeString(type) : null)
.add("peeledId", peeledId != null ? peeledId.getName() : null)
.add("peeledType", type > 0 ? Constants.typeString(peeledType) : null)
.toString();
}
private boolean nameEqualsAbbreviated(ObjectId other) {
return AbbreviatedObjectId.isId(name)
&& AbbreviatedObjectId.fromString(name).prefixCompare(other) == 0;
}
}