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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 java.io.Serializable;
  45. import java.text.MessageFormat;
  46. import org.eclipse.jgit.errors.InvalidObjectIdException;
  47. import org.eclipse.jgit.internal.JGitText;
  48. import org.eclipse.jgit.util.NB;
  49. import org.eclipse.jgit.util.RawParseUtils;
  50. /**
  51. * A prefix abbreviation of an {@link ObjectId}.
  52. * <p>
  53. * Sometimes Git produces abbreviated SHA-1 strings, using sufficient leading
  54. * digits from the ObjectId name to still be unique within the repository the
  55. * string was generated from. These ids are likely to be unique for a useful
  56. * period of time, especially if they contain at least 6-10 hex digits.
  57. * <p>
  58. * This class converts the hex string into a binary form, to make it more
  59. * efficient for matching against an object.
  60. */
  61. public final class AbbreviatedObjectId implements Serializable {
  62. private static final long serialVersionUID = 1L;
  63. /**
  64. * Test a string of characters to verify it is a hex format.
  65. * <p>
  66. * If true the string can be parsed with {@link #fromString(String)}.
  67. *
  68. * @param id
  69. * the string to test.
  70. * @return true if the string can converted into an AbbreviatedObjectId.
  71. */
  72. public static final boolean isId(final String id) {
  73. if (id.length() < 2 || Constants.OBJECT_ID_STRING_LENGTH < id.length())
  74. return false;
  75. try {
  76. for (int i = 0; i < id.length(); i++)
  77. RawParseUtils.parseHexInt4((byte) id.charAt(i));
  78. return true;
  79. } catch (ArrayIndexOutOfBoundsException e) {
  80. return false;
  81. }
  82. }
  83. /**
  84. * Convert an AbbreviatedObjectId from hex characters (US-ASCII).
  85. *
  86. * @param buf
  87. * the US-ASCII buffer to read from.
  88. * @param offset
  89. * position to read the first character from.
  90. * @param end
  91. * one past the last position to read (<code>end-offset</code> is
  92. * the length of the string).
  93. * @return the converted object id.
  94. */
  95. public static final AbbreviatedObjectId fromString(final byte[] buf,
  96. final int offset, final int end) {
  97. if (end - offset > Constants.OBJECT_ID_STRING_LENGTH)
  98. throw new IllegalArgumentException(MessageFormat.format(
  99. JGitText.get().invalidIdLength,
  100. Integer.valueOf(end - offset),
  101. Integer.valueOf(Constants.OBJECT_ID_STRING_LENGTH)));
  102. return fromHexString(buf, offset, end);
  103. }
  104. /**
  105. * Convert an AbbreviatedObjectId from an {@link AnyObjectId}.
  106. * <p>
  107. * This method copies over all bits of the Id, and is therefore complete
  108. * (see {@link #isComplete()}).
  109. *
  110. * @param id
  111. * the {@link ObjectId} to convert from.
  112. * @return the converted object id.
  113. */
  114. public static final AbbreviatedObjectId fromObjectId(AnyObjectId id) {
  115. return new AbbreviatedObjectId(Constants.OBJECT_ID_STRING_LENGTH,
  116. id.w1, id.w2, id.w3, id.w4, id.w5);
  117. }
  118. /**
  119. * Convert an AbbreviatedObjectId from hex characters.
  120. *
  121. * @param str
  122. * the string to read from. Must be &lt;= 40 characters.
  123. * @return the converted object id.
  124. */
  125. public static final AbbreviatedObjectId fromString(final String str) {
  126. if (str.length() > Constants.OBJECT_ID_STRING_LENGTH)
  127. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().invalidId, str));
  128. final byte[] b = Constants.encodeASCII(str);
  129. return fromHexString(b, 0, b.length);
  130. }
  131. private static final AbbreviatedObjectId fromHexString(final byte[] bs,
  132. int ptr, final int end) {
  133. try {
  134. final int a = hexUInt32(bs, ptr, end);
  135. final int b = hexUInt32(bs, ptr + 8, end);
  136. final int c = hexUInt32(bs, ptr + 16, end);
  137. final int d = hexUInt32(bs, ptr + 24, end);
  138. final int e = hexUInt32(bs, ptr + 32, end);
  139. return new AbbreviatedObjectId(end - ptr, a, b, c, d, e);
  140. } catch (ArrayIndexOutOfBoundsException e1) {
  141. throw new InvalidObjectIdException(bs, ptr, end - ptr);
  142. }
  143. }
  144. private static final int hexUInt32(final byte[] bs, int p, final int end) {
  145. if (8 <= end - p)
  146. return RawParseUtils.parseHexInt32(bs, p);
  147. int r = 0, n = 0;
  148. while (n < 8 && p < end) {
  149. r <<= 4;
  150. r |= RawParseUtils.parseHexInt4(bs[p++]);
  151. n++;
  152. }
  153. return r << (8 - n) * 4;
  154. }
  155. static int mask(final int nibbles, final int word, final int v) {
  156. final int b = (word - 1) * 8;
  157. if (b + 8 <= nibbles) {
  158. // We have all of the bits required for this word.
  159. //
  160. return v;
  161. }
  162. if (nibbles <= b) {
  163. // We have none of the bits required for this word.
  164. //
  165. return 0;
  166. }
  167. final int s = 32 - (nibbles - b) * 4;
  168. return (v >>> s) << s;
  169. }
  170. /** Number of half-bytes used by this id. */
  171. final int nibbles;
  172. final int w1;
  173. final int w2;
  174. final int w3;
  175. final int w4;
  176. final int w5;
  177. AbbreviatedObjectId(final int n, final int new_1, final int new_2,
  178. final int new_3, final int new_4, final int new_5) {
  179. nibbles = n;
  180. w1 = new_1;
  181. w2 = new_2;
  182. w3 = new_3;
  183. w4 = new_4;
  184. w5 = new_5;
  185. }
  186. /** @return number of hex digits appearing in this id */
  187. public int length() {
  188. return nibbles;
  189. }
  190. /** @return true if this ObjectId is actually a complete id. */
  191. public boolean isComplete() {
  192. return length() == Constants.OBJECT_ID_STRING_LENGTH;
  193. }
  194. /** @return a complete ObjectId; null if {@link #isComplete()} is false */
  195. public ObjectId toObjectId() {
  196. return isComplete() ? new ObjectId(w1, w2, w3, w4, w5) : null;
  197. }
  198. /**
  199. * Compares this abbreviation to a full object id.
  200. *
  201. * @param other
  202. * the other object id.
  203. * @return &lt;0 if this abbreviation names an object that is less than
  204. * <code>other</code>; 0 if this abbreviation exactly matches the
  205. * first {@link #length()} digits of <code>other.name()</code>;
  206. * &gt;0 if this abbreviation names an object that is after
  207. * <code>other</code>.
  208. */
  209. public final int prefixCompare(final AnyObjectId other) {
  210. int cmp;
  211. cmp = NB.compareUInt32(w1, mask(1, other.w1));
  212. if (cmp != 0)
  213. return cmp;
  214. cmp = NB.compareUInt32(w2, mask(2, other.w2));
  215. if (cmp != 0)
  216. return cmp;
  217. cmp = NB.compareUInt32(w3, mask(3, other.w3));
  218. if (cmp != 0)
  219. return cmp;
  220. cmp = NB.compareUInt32(w4, mask(4, other.w4));
  221. if (cmp != 0)
  222. return cmp;
  223. return NB.compareUInt32(w5, mask(5, other.w5));
  224. }
  225. /**
  226. * Compare this abbreviation to a network-byte-order ObjectId.
  227. *
  228. * @param bs
  229. * array containing the other ObjectId in network byte order.
  230. * @param p
  231. * position within {@code bs} to start the compare at. At least
  232. * 20 bytes, starting at this position are required.
  233. * @return &lt;0 if this abbreviation names an object that is less than
  234. * <code>other</code>; 0 if this abbreviation exactly matches the
  235. * first {@link #length()} digits of <code>other.name()</code>;
  236. * &gt;0 if this abbreviation names an object that is after
  237. * <code>other</code>.
  238. */
  239. public final int prefixCompare(final byte[] bs, final int p) {
  240. int cmp;
  241. cmp = NB.compareUInt32(w1, mask(1, NB.decodeInt32(bs, p)));
  242. if (cmp != 0)
  243. return cmp;
  244. cmp = NB.compareUInt32(w2, mask(2, NB.decodeInt32(bs, p + 4)));
  245. if (cmp != 0)
  246. return cmp;
  247. cmp = NB.compareUInt32(w3, mask(3, NB.decodeInt32(bs, p + 8)));
  248. if (cmp != 0)
  249. return cmp;
  250. cmp = NB.compareUInt32(w4, mask(4, NB.decodeInt32(bs, p + 12)));
  251. if (cmp != 0)
  252. return cmp;
  253. return NB.compareUInt32(w5, mask(5, NB.decodeInt32(bs, p + 16)));
  254. }
  255. /**
  256. * Compare this abbreviation to a network-byte-order ObjectId.
  257. *
  258. * @param bs
  259. * array containing the other ObjectId in network byte order.
  260. * @param p
  261. * position within {@code bs} to start the compare at. At least 5
  262. * ints, starting at this position are required.
  263. * @return &lt;0 if this abbreviation names an object that is less than
  264. * <code>other</code>; 0 if this abbreviation exactly matches the
  265. * first {@link #length()} digits of <code>other.name()</code>;
  266. * &gt;0 if this abbreviation names an object that is after
  267. * <code>other</code>.
  268. */
  269. public final int prefixCompare(final int[] bs, final int p) {
  270. int cmp;
  271. cmp = NB.compareUInt32(w1, mask(1, bs[p]));
  272. if (cmp != 0)
  273. return cmp;
  274. cmp = NB.compareUInt32(w2, mask(2, bs[p + 1]));
  275. if (cmp != 0)
  276. return cmp;
  277. cmp = NB.compareUInt32(w3, mask(3, bs[p + 2]));
  278. if (cmp != 0)
  279. return cmp;
  280. cmp = NB.compareUInt32(w4, mask(4, bs[p + 3]));
  281. if (cmp != 0)
  282. return cmp;
  283. return NB.compareUInt32(w5, mask(5, bs[p + 4]));
  284. }
  285. /** @return value for a fan-out style map, only valid of length >= 2. */
  286. public final int getFirstByte() {
  287. return w1 >>> 24;
  288. }
  289. private int mask(final int word, final int v) {
  290. return mask(nibbles, word, v);
  291. }
  292. @Override
  293. public int hashCode() {
  294. return w2;
  295. }
  296. @Override
  297. public boolean equals(final Object o) {
  298. if (o instanceof AbbreviatedObjectId) {
  299. final AbbreviatedObjectId b = (AbbreviatedObjectId) o;
  300. return nibbles == b.nibbles && w1 == b.w1 && w2 == b.w2
  301. && w3 == b.w3 && w4 == b.w4 && w5 == b.w5;
  302. }
  303. return false;
  304. }
  305. /**
  306. * @return string form of the abbreviation, in lower case hexadecimal.
  307. */
  308. public final String name() {
  309. final char[] b = new char[Constants.OBJECT_ID_STRING_LENGTH];
  310. AnyObjectId.formatHexChar(b, 0, w1);
  311. if (nibbles <= 8)
  312. return new String(b, 0, nibbles);
  313. AnyObjectId.formatHexChar(b, 8, w2);
  314. if (nibbles <= 16)
  315. return new String(b, 0, nibbles);
  316. AnyObjectId.formatHexChar(b, 16, w3);
  317. if (nibbles <= 24)
  318. return new String(b, 0, nibbles);
  319. AnyObjectId.formatHexChar(b, 24, w4);
  320. if (nibbles <= 32)
  321. return new String(b, 0, nibbles);
  322. AnyObjectId.formatHexChar(b, 32, w5);
  323. return new String(b, 0, nibbles);
  324. }
  325. @SuppressWarnings("nls")
  326. @Override
  327. public String toString() {
  328. return "AbbreviatedObjectId[" + name() + "]"; //$NON-NLS-1$
  329. }
  330. }