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