| 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 | |
| 17 | import static com.google.common.base.Preconditions.checkArgument; |
| 18 | import static org.eclipse.jgit.lib.Constants.OBJ_BAD; |
| 19 | import static org.eclipse.jgit.lib.Constants.OBJ_TAG; |
| 20 | |
| 21 | import com.google.common.annotations.VisibleForTesting; |
| 22 | import com.google.common.base.Objects; |
| 23 | |
| 24 | import org.eclipse.jgit.errors.MissingObjectException; |
| 25 | import org.eclipse.jgit.lib.AbbreviatedObjectId; |
| Dave Borowitz | 5530a16 | 2013-06-19 15:14:47 -0700 | [diff] [blame] | 26 | import org.eclipse.jgit.lib.Constants; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 27 | import org.eclipse.jgit.lib.ObjectId; |
| 28 | import org.eclipse.jgit.revwalk.RevObject; |
| 29 | import org.eclipse.jgit.revwalk.RevWalk; |
| 30 | |
| 31 | import java.io.IOException; |
| 32 | |
| 33 | /** |
| 34 | * Object encapsulating a single revision as seen by Gitiles. |
| 35 | * <p> |
| 36 | * A single revision consists of a name, an ID, and a type. Name parsing is done |
| 37 | * once per request by {@link RevisionParser}. |
| 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 | |
| 46 | private final String name; |
| 47 | private final ObjectId id; |
| 48 | private final int type; |
| 49 | private final ObjectId peeledId; |
| 50 | private final int peeledType; |
| 51 | |
| 52 | public static Revision peeled(String name, RevObject obj) { |
| 53 | return peeled(name, obj, obj.getType()); |
| 54 | } |
| 55 | |
| 56 | public static Revision unpeeled(String name, ObjectId id) { |
| 57 | return peeled(name, id, OBJ_BAD); |
| 58 | } |
| 59 | |
| 60 | public static Revision named(String name) { |
| 61 | return peeled(name, null, OBJ_BAD); |
| 62 | } |
| 63 | |
| Dave Borowitz | 48ca670 | 2013-04-09 11:52:41 -0700 | [diff] [blame] | 64 | public static Revision peel(String name, RevObject obj, RevWalk walk) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 65 | throws MissingObjectException, IOException { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 66 | RevObject peeled = walk.peel(obj); |
| 67 | return new Revision(name, obj, obj.getType(), peeled, peeled.getType()); |
| 68 | } |
| 69 | |
| 70 | private static Revision peeled(String name, ObjectId id, int type) { |
| 71 | checkArgument(type != OBJ_TAG, "expected non-tag for %s/%s", name, id); |
| 72 | return new Revision(name, id, type, id, type); |
| 73 | } |
| 74 | |
| 75 | @VisibleForTesting |
| 76 | Revision(String name, ObjectId id, int type, ObjectId peeledId, int peeledType) { |
| 77 | this.name = name; |
| 78 | this.id = id; |
| 79 | this.type = type; |
| 80 | this.peeledId = peeledId; |
| 81 | this.peeledType = peeledType; |
| 82 | } |
| 83 | |
| 84 | public String getName() { |
| 85 | return name; |
| 86 | } |
| 87 | |
| 88 | public int getType() { |
| 89 | return type; |
| 90 | } |
| 91 | |
| 92 | public ObjectId getId() { |
| 93 | return id; |
| 94 | } |
| 95 | |
| 96 | public ObjectId getPeeledId() { |
| 97 | return peeledId; |
| 98 | } |
| 99 | |
| 100 | public int getPeeledType() { |
| 101 | return peeledType; |
| 102 | } |
| 103 | |
| 104 | public boolean nameIsId() { |
| 105 | return AbbreviatedObjectId.isId(name) |
| 106 | && (AbbreviatedObjectId.fromString(name).prefixCompare(id) == 0); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public boolean equals(Object o) { |
| 111 | if (o instanceof Revision) { |
| 112 | Revision r = (Revision) o; |
| 113 | return Objects.equal(name, r.name) |
| 114 | && Objects.equal(id, r.id) |
| 115 | && Objects.equal(type, r.type) |
| 116 | && Objects.equal(peeledId, r.peeledId) |
| 117 | && Objects.equal(peeledType, r.peeledType); |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public int hashCode() { |
| 124 | return Objects.hashCode(name, id, type, peeledId, peeledType); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public String toString() { |
| 129 | return Objects.toStringHelper(this) |
| 130 | .omitNullValues() |
| 131 | .add("name", name) |
| Dave Borowitz | 5530a16 | 2013-06-19 15:14:47 -0700 | [diff] [blame] | 132 | .add("id", id != null ? id.getName() : null) |
| 133 | .add("type", type > 0 ? Constants.typeString(type) : null) |
| 134 | .add("peeledId", peeledId != null ? peeledId.getName() : null) |
| 135 | .add("peeledType", type > 0 ? Constants.typeString(peeledType) : null) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 136 | .toString(); |
| 137 | } |
| 138 | } |