You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbbreviatedObjectId.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.lib;
  44. import org.eclipse.jgit.errors.InvalidObjectIdException;
  45. import org.eclipse.jgit.util.NB;
  46. import org.eclipse.jgit.util.RawParseUtils;
  47. /**
  48. * A prefix abbreviation of an {@link ObjectId}.
  49. * <p>
  50. * Sometimes Git produces abbreviated SHA-1 strings, using sufficient leading
  51. * digits from the ObjectId name to still be unique within the repository the
  52. * string was generated from. These ids are likely to be unique for a useful
  53. * period of time, especially if they contain at least 6-10 hex digits.
  54. * <p>
  55. * This class converts the hex string into a binary form, to make it more
  56. * efficient for matching against an object.
  57. */
  58. public final class AbbreviatedObjectId {
  59. /**
  60. * Convert an AbbreviatedObjectId from hex characters (US-ASCII).
  61. *
  62. * @param buf
  63. * the US-ASCII buffer to read from.
  64. * @param offset
  65. * position to read the first character from.
  66. * @param end
  67. * one past the last position to read (<code>end-offset</code> is
  68. * the length of the string).
  69. * @return the converted object id.
  70. */
  71. public static final AbbreviatedObjectId fromString(final byte[] buf,
  72. final int offset, final int end) {
  73. if (end - offset > Constants.OBJECT_ID_STRING_LENGTH)
  74. throw new IllegalArgumentException("Invalid id");
  75. return fromHexString(buf, offset, end);
  76. }
  77. /**
  78. * Convert an AbbreviatedObjectId from hex characters.
  79. *
  80. * @param str
  81. * the string to read from. Must be &lt;= 40 characters.
  82. * @return the converted object id.
  83. */
  84. public static final AbbreviatedObjectId fromString(final String str) {
  85. if (str.length() > Constants.OBJECT_ID_STRING_LENGTH)
  86. throw new IllegalArgumentException("Invalid id: " + str);
  87. final byte[] b = Constants.encodeASCII(str);
  88. return fromHexString(b, 0, b.length);
  89. }
  90. private static final AbbreviatedObjectId fromHexString(final byte[] bs,
  91. int ptr, final int end) {
  92. try {
  93. final int a = hexUInt32(bs, ptr, end);
  94. final int b = hexUInt32(bs, ptr + 8, end);
  95. final int c = hexUInt32(bs, ptr + 16, end);
  96. final int d = hexUInt32(bs, ptr + 24, end);
  97. final int e = hexUInt32(bs, ptr + 32, end);
  98. return new AbbreviatedObjectId(end - ptr, a, b, c, d, e);
  99. } catch (ArrayIndexOutOfBoundsException e1) {
  100. throw new InvalidObjectIdException(bs, ptr, end - ptr);
  101. }
  102. }
  103. private static final int hexUInt32(final byte[] bs, int p, final int end) {
  104. if (8 <= end - p)
  105. return RawParseUtils.parseHexInt32(bs, p);
  106. int r = 0, n = 0;
  107. while (n < 8 && p < end) {
  108. r <<= 4;
  109. r |= RawParseUtils.parseHexInt4(bs[p++]);
  110. n++;
  111. }
  112. return r << (8 - n) * 4;
  113. }
  114. static int mask(final int nibbles, final int word, final int v) {
  115. final int b = (word - 1) * 8;
  116. if (b + 8 <= nibbles) {
  117. // We have all of the bits required for this word.
  118. //
  119. return v;
  120. }
  121. if (nibbles <= b) {
  122. // We have none of the bits required for this word.
  123. //
  124. return 0;
  125. }
  126. final int s = 32 - (nibbles - b) * 4;
  127. return (v >>> s) << s;
  128. }
  129. /** Number of half-bytes used by this id. */
  130. final int nibbles;
  131. final int w1;
  132. final int w2;
  133. final int w3;
  134. final int w4;
  135. final int w5;
  136. AbbreviatedObjectId(final int n, final int new_1, final int new_2,
  137. final int new_3, final int new_4, final int new_5) {
  138. nibbles = n;
  139. w1 = new_1;
  140. w2 = new_2;
  141. w3 = new_3;
  142. w4 = new_4;
  143. w5 = new_5;
  144. }
  145. /** @return number of hex digits appearing in this id */
  146. public int length() {
  147. return nibbles;
  148. }
  149. /** @return true if this ObjectId is actually a complete id. */
  150. public boolean isComplete() {
  151. return length() == Constants.OBJECT_ID_STRING_LENGTH;
  152. }
  153. /** @return a complete ObjectId; null if {@link #isComplete()} is false */
  154. public ObjectId toObjectId() {
  155. return isComplete() ? new ObjectId(w1, w2, w3, w4, w5) : null;
  156. }
  157. /**
  158. * Compares this abbreviation to a full object id.
  159. *
  160. * @param other
  161. * the other object id.
  162. * @return &lt;0 if this abbreviation names an object that is less than
  163. * <code>other</code>; 0 if this abbreviation exactly matches the
  164. * first {@link #length()} digits of <code>other.name()</code>;
  165. * &gt;0 if this abbreviation names an object that is after
  166. * <code>other</code>.
  167. */
  168. public int prefixCompare(final AnyObjectId other) {
  169. int cmp;
  170. cmp = NB.compareUInt32(w1, mask(1, other.w1));
  171. if (cmp != 0)
  172. return cmp;
  173. cmp = NB.compareUInt32(w2, mask(2, other.w2));
  174. if (cmp != 0)
  175. return cmp;
  176. cmp = NB.compareUInt32(w3, mask(3, other.w3));
  177. if (cmp != 0)
  178. return cmp;
  179. cmp = NB.compareUInt32(w4, mask(4, other.w4));
  180. if (cmp != 0)
  181. return cmp;
  182. return NB.compareUInt32(w5, mask(5, other.w5));
  183. }
  184. private int mask(final int word, final int v) {
  185. return mask(nibbles, word, v);
  186. }
  187. @Override
  188. public int hashCode() {
  189. return w2;
  190. }
  191. @Override
  192. public boolean equals(final Object o) {
  193. if (o instanceof AbbreviatedObjectId) {
  194. final AbbreviatedObjectId b = (AbbreviatedObjectId) o;
  195. return nibbles == b.nibbles && w1 == b.w1 && w2 == b.w2
  196. && w3 == b.w3 && w4 == b.w4 && w5 == b.w5;
  197. }
  198. return false;
  199. }
  200. /**
  201. * @return string form of the abbreviation, in lower case hexadecimal.
  202. */
  203. public final String name() {
  204. final char[] b = new char[Constants.OBJECT_ID_STRING_LENGTH];
  205. AnyObjectId.formatHexChar(b, 0, w1);
  206. if (nibbles <= 8)
  207. return new String(b, 0, nibbles);
  208. AnyObjectId.formatHexChar(b, 8, w2);
  209. if (nibbles <= 16)
  210. return new String(b, 0, nibbles);
  211. AnyObjectId.formatHexChar(b, 16, w3);
  212. if (nibbles <= 24)
  213. return new String(b, 0, nibbles);
  214. AnyObjectId.formatHexChar(b, 24, w4);
  215. if (nibbles <= 32)
  216. return new String(b, 0, nibbles);
  217. AnyObjectId.formatHexChar(b, 32, w5);
  218. return new String(b, 0, nibbles);
  219. }
  220. @Override
  221. public String toString() {
  222. return "AbbreviatedObjectId[" + name() + "]";
  223. }
  224. }