Beispiel #1
0
  /**
   * 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;
  }
Beispiel #2
0
 /**
  * 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;
 }