Line data Source code
1 : // Copyright (C) 2017 The Android Open Source Project 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.gerrit.index; 16 : 17 : import static com.google.common.base.MoreObjects.firstNonNull; 18 : import static com.google.common.base.Preconditions.checkArgument; 19 : import static java.nio.charset.StandardCharsets.UTF_8; 20 : 21 : import com.google.auto.value.AutoValue; 22 : import com.google.common.base.Splitter; 23 : import com.google.common.collect.ImmutableSetMultimap; 24 : import com.google.gerrit.common.Nullable; 25 : import com.google.gerrit.entities.Project; 26 : import com.google.gerrit.git.ObjectIds; 27 : import java.io.IOException; 28 : import java.util.List; 29 : import org.eclipse.jgit.lib.ObjectId; 30 : import org.eclipse.jgit.lib.Ref; 31 : import org.eclipse.jgit.lib.Repository; 32 : 33 : @AutoValue 34 144 : public abstract class RefState { 35 : public static ImmutableSetMultimap<Project.NameKey, RefState> parseStates( 36 : Iterable<byte[]> states) { 37 100 : RefState.check(states != null, null); 38 100 : ImmutableSetMultimap.Builder<Project.NameKey, RefState> result = ImmutableSetMultimap.builder(); 39 100 : for (byte[] b : states) { 40 100 : RefState.check(b != null, null); 41 100 : String s = new String(b, UTF_8); 42 100 : List<String> parts = Splitter.on(':').splitToList(s); 43 100 : RefState.check(parts.size() == 3 && !parts.get(0).isEmpty() && !parts.get(1).isEmpty(), s); 44 100 : result.put(Project.nameKey(parts.get(0)), RefState.create(parts.get(1), parts.get(2))); 45 100 : } 46 100 : return result.build(); 47 : } 48 : 49 : public static RefState create(String ref, String sha) { 50 102 : return new AutoValue_RefState(ref, ObjectId.fromString(sha)); 51 : } 52 : 53 : public static RefState create(String ref, @Nullable ObjectId id) { 54 144 : return new AutoValue_RefState(ref, firstNonNull(id, ObjectId.zeroId())); 55 : } 56 : 57 : public static RefState of(Ref ref) { 58 6 : return new AutoValue_RefState(ref.getName(), ref.getObjectId()); 59 : } 60 : 61 : public byte[] toByteArray(Project.NameKey project) { 62 105 : byte[] a = (project.toString() + ':' + ref() + ':').getBytes(UTF_8); 63 105 : byte[] b = new byte[a.length + ObjectIds.STR_LEN]; 64 105 : System.arraycopy(a, 0, b, 0, a.length); 65 105 : id().copyTo(b, a.length); 66 105 : return b; 67 : } 68 : 69 : public static void check(boolean condition, String str) { 70 100 : checkArgument(condition, "invalid RefState: %s", str); 71 100 : } 72 : 73 : public abstract String ref(); 74 : 75 : public abstract ObjectId id(); 76 : 77 : public boolean match(Repository repo) throws IOException { 78 136 : Ref ref = repo.exactRef(ref()); 79 136 : ObjectId expected = ref != null ? ref.getObjectId() : ObjectId.zeroId(); 80 136 : return id().equals(expected); 81 : } 82 : }