/** * Return an abbreviation (prefix) of this object SHA-1. * * <p>This implementation does not guaranteeing uniqueness. Callers should instead use {@link * ObjectReader#abbreviate(AnyObjectId, int)} to obtain a unique abbreviation within the scope of * a particular object database. * * @param len length of the abbreviated string. * @return SHA-1 abbreviation. */ public AbbreviatedObjectId abbreviate(final int len) { final int a = AbbreviatedObjectId.mask(len, 1, w1); final int b = AbbreviatedObjectId.mask(len, 2, w2); final int c = AbbreviatedObjectId.mask(len, 3, w3); final int d = AbbreviatedObjectId.mask(len, 4, w4); final int e = AbbreviatedObjectId.mask(len, 5, w5); return new AbbreviatedObjectId(len, a, b, c, d, e); }
/** * Obtain a unique abbreviation (prefix) of an object SHA-1. * * <p>The returned abbreviation would expand back to the argument ObjectId when passed to {@link * #resolve(AbbreviatedObjectId)}, assuming no new objects are added to this repository between * calls. * * <p>The default implementation of this method abbreviates the id to the minimum length, then * resolves it to see if there are multiple results. When multiple results are found, the length * is extended by 1 and resolve is tried again. * * @param objectId object identity that needs to be abbreviated. * @param len minimum length of the abbreviated string. Must be in the range [2, {@value * Constants#OBJECT_ID_STRING_LENGTH}]. * @return SHA-1 abbreviation. If no matching objects exist in the repository, the abbreviation * will match the minimum length. * @throws IOException the object store cannot be read. */ public AbbreviatedObjectId abbreviate(AnyObjectId objectId, int len) throws IOException { if (len == Constants.OBJECT_ID_STRING_LENGTH) return AbbreviatedObjectId.fromObjectId(objectId); AbbreviatedObjectId abbrev = objectId.abbreviate(len); Collection<ObjectId> matches = resolve(abbrev); while (1 < matches.size() && len < Constants.OBJECT_ID_STRING_LENGTH) { abbrev = objectId.abbreviate(++len); List<ObjectId> n = new ArrayList<ObjectId>(8); for (ObjectId candidate : matches) { if (abbrev.prefixCompare(candidate) == 0) n.add(candidate); } if (1 < n.size()) matches = n; else matches = resolve(abbrev); } return abbrev; }
/** * Tests if this ObjectId starts with the given abbreviation. * * @param abbr the abbreviation. * @return true if this ObjectId begins with the abbreviation; else false. */ public boolean startsWith(final AbbreviatedObjectId abbr) { return abbr.prefixCompare(this) == 0; }