Line data Source code
1 : // Copyright (C) 2019 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.git; 16 : 17 : import static com.google.common.base.Preconditions.checkArgument; 18 : import static java.util.Objects.requireNonNull; 19 : 20 : import com.google.gerrit.common.Nullable; 21 : import java.io.IOException; 22 : import org.eclipse.jgit.lib.AnyObjectId; 23 : import org.eclipse.jgit.lib.Constants; 24 : import org.eclipse.jgit.lib.ObjectId; 25 : import org.eclipse.jgit.lib.ObjectReader; 26 : 27 : /** Static utilities for working with {@code ObjectId}s. */ 28 : public class ObjectIds { 29 : /** Length of a binary SHA-1 byte array. */ 30 : public static final int LEN = Constants.OBJECT_ID_LENGTH; 31 : 32 : /** Length of a hex SHA-1 string. */ 33 : public static final int STR_LEN = Constants.OBJECT_ID_STRING_LENGTH; 34 : 35 : /** Default abbreviated length of a hex SHA-1 string. */ 36 : public static final int ABBREV_STR_LEN = 7; 37 : 38 : /** 39 : * Abbreviate an ID's hex string representation to 7 chars. 40 : * 41 : * @param id object ID. 42 : * @return abbreviated hex string representation, exactly 7 chars. 43 : */ 44 : public static String abbreviateName(AnyObjectId id) { 45 19 : return abbreviateName(id, ABBREV_STR_LEN); 46 : } 47 : 48 : /** 49 : * Abbreviate an ID's hex string representation to {@code n} chars. 50 : * 51 : * @param id object ID. 52 : * @param n number of hex chars, 1 to 40. 53 : * @return abbreviated hex string representation, exactly {@code n} chars. 54 : */ 55 : public static String abbreviateName(AnyObjectId id, int n) { 56 31 : checkValidLength(n); 57 31 : return requireNonNull(id).abbreviate(n).name(); 58 : } 59 : 60 : /** 61 : * Abbreviate an ID's hex string representation uniquely to at least 7 chars. 62 : * 63 : * @param id object ID. 64 : * @param reader object reader for determining uniqueness. 65 : * @return abbreviated hex string representation, unique according to {@code reader} at least 7 66 : * chars. 67 : * @throws IOException if an error occurs while looking for ambiguous objects. 68 : */ 69 : public static String abbreviateName(AnyObjectId id, ObjectReader reader) throws IOException { 70 22 : return abbreviateName(id, ABBREV_STR_LEN, reader); 71 : } 72 : 73 : /** 74 : * Abbreviate an ID's hex string representation uniquely to at least {@code n} chars. 75 : * 76 : * @param id object ID. 77 : * @param n minimum number of hex chars, 1 to 40. 78 : * @param reader object reader for determining uniqueness. 79 : * @return abbreviated hex string representation, unique according to {@code reader} at least 80 : * {@code n} chars. 81 : * @throws IOException if an error occurs while looking for ambiguous objects. 82 : */ 83 : public static String abbreviateName(AnyObjectId id, int n, ObjectReader reader) 84 : throws IOException { 85 103 : checkValidLength(n); 86 103 : return reader.abbreviate(id, n).name(); 87 : } 88 : 89 : /** 90 : * Copy a nullable ID, preserving null. 91 : * 92 : * @param id object ID. 93 : * @return {@link AnyObjectId#copy} of {@code id}, or {@code null} if {@code id} is null. 94 : */ 95 : @Nullable 96 : public static ObjectId copyOrNull(@Nullable AnyObjectId id) { 97 152 : return id != null ? id.copy() : null; 98 : } 99 : 100 : /** 101 : * Copy a nullable ID, converting null to {@code zeroId}. 102 : * 103 : * @param id object ID. 104 : * @return {@link AnyObjectId#copy} of {@code id}, or {@link ObjectId#zeroId} if {@code id} is 105 : * null. 106 : */ 107 : public static ObjectId copyOrZero(@Nullable AnyObjectId id) { 108 153 : return id != null ? id.copy() : ObjectId.zeroId(); 109 : } 110 : 111 : /** 112 : * Return whether the given ID matches the given abbreviation. 113 : * 114 : * @param id object ID. 115 : * @param abbreviation abbreviated hex object ID. May not be null, but may be an invalid hex SHA-1 116 : * abbreviation string. 117 : * @return true if {@code id} is not null and {@code abbreviation} is a valid hex SHA-1 118 : * abbreviation that matches {@code id}, false otherwise. 119 : */ 120 : public static boolean matchesAbbreviation(@Nullable AnyObjectId id, String abbreviation) { 121 30 : requireNonNull(abbreviation); 122 30 : return id != null && id.name().startsWith(abbreviation); 123 : } 124 : 125 : private static void checkValidLength(int n) { 126 103 : checkArgument(n > 0); 127 103 : checkArgument(n <= STR_LEN); 128 103 : } 129 : 130 : private ObjectIds() {} 131 : }