Ejemplo n.º 1
0
  /**
   * Convert push remote refs update specification from {@link RefSpec} form to {@link
   * RemoteRefUpdate}. Conversion expands wildcards by matching source part to local refs.
   * expectedOldObjectId in RemoteRefUpdate is always set as null. Tracking branch is configured if
   * RefSpec destination matches source of any fetch ref spec for this transport remote
   * configuration.
   *
   * @param db local database.
   * @param specs collection of RefSpec to convert.
   * @param fetchSpecs fetch specifications used for finding localtracking refs. May be null or
   *     empty collection.
   * @return collection of set up {@link RemoteRefUpdate}.
   * @throws IOException when problem occurred during conversion or specification set up: most
   *     probably, missing objects or refs.
   */
  public static Collection<RemoteRefUpdate> findRemoteRefUpdatesFor(
      final Repository db, final Collection<RefSpec> specs, Collection<RefSpec> fetchSpecs)
      throws IOException {
    if (fetchSpecs == null) fetchSpecs = Collections.emptyList();
    final List<RemoteRefUpdate> result = new LinkedList<RemoteRefUpdate>();
    final Collection<RefSpec> procRefs = expandPushWildcardsFor(db, specs);

    for (final RefSpec spec : procRefs) {
      String srcSpec = spec.getSource();
      final Ref srcRef = db.getRef(srcSpec);
      if (srcRef != null) srcSpec = srcRef.getName();

      String destSpec = spec.getDestination();
      if (destSpec == null) {
        // No destination (no-colon in ref-spec), DWIMery assumes src
        //
        destSpec = srcSpec;
      }

      if (srcRef != null && !destSpec.startsWith(Constants.R_REFS)) {
        // Assume the same kind of ref at the destination, e.g.
        // "refs/heads/foo:master", DWIMery assumes master is also
        // under "refs/heads/".
        //
        final String n = srcRef.getName();
        final int kindEnd = n.indexOf('/', Constants.R_REFS.length());
        destSpec = n.substring(0, kindEnd + 1) + destSpec;
      }

      final boolean forceUpdate = spec.isForceUpdate();
      final String localName = findTrackingRefName(destSpec, fetchSpecs);
      final RemoteRefUpdate rru =
          new RemoteRefUpdate(db, srcSpec, destSpec, forceUpdate, localName, null);
      result.add(rru);
    }
    return result;
  }