SymbolicRef.java

  1. /*
  2.  * Copyright (C) 2010, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.lib;

  11. import org.eclipse.jgit.annotations.NonNull;
  12. import org.eclipse.jgit.annotations.Nullable;

  13. /**
  14.  * A reference that indirectly points at another
  15.  * {@link org.eclipse.jgit.lib.Ref}.
  16.  * <p>
  17.  * A symbolic reference always derives its current value from the target
  18.  * reference.
  19.  */
  20. public class SymbolicRef implements Ref {
  21.     private final String name;

  22.     private final Ref target;

  23.     private final long updateIndex;

  24.     /**
  25.      * Create a new ref pairing.
  26.      *
  27.      * @param refName
  28.      *            name of this ref.
  29.      * @param target
  30.      *            the ref we reference and derive our value from.
  31.      */
  32.     public SymbolicRef(@NonNull String refName, @NonNull Ref target) {
  33.         this.name = refName;
  34.         this.target = target;
  35.         this.updateIndex = UNDEFINED_UPDATE_INDEX;
  36.     }

  37.     /**
  38.      * Create a new ref pairing.
  39.      *
  40.      * @param refName
  41.      *            name of this ref.
  42.      * @param target
  43.      *            the ref we reference and derive our value from.
  44.      * @param updateIndex
  45.      *            index that increases with each update of the reference
  46.      * @since 5.3
  47.      */
  48.     public SymbolicRef(@NonNull String refName, @NonNull Ref target,
  49.             long updateIndex) {
  50.         this.name = refName;
  51.         this.target = target;
  52.         this.updateIndex = updateIndex;
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     @NonNull
  57.     public String getName() {
  58.         return name;
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public boolean isSymbolic() {
  63.         return true;
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     @NonNull
  68.     public Ref getLeaf() {
  69.         Ref dst = getTarget();
  70.         while (dst.isSymbolic())
  71.             dst = dst.getTarget();
  72.         return dst;
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     @NonNull
  77.     public Ref getTarget() {
  78.         return target;
  79.     }

  80.     /** {@inheritDoc} */
  81.     @Override
  82.     @Nullable
  83.     public ObjectId getObjectId() {
  84.         return getLeaf().getObjectId();
  85.     }

  86.     /** {@inheritDoc} */
  87.     @Override
  88.     @NonNull
  89.     public Storage getStorage() {
  90.         return Storage.LOOSE;
  91.     }

  92.     /** {@inheritDoc} */
  93.     @Override
  94.     @Nullable
  95.     public ObjectId getPeeledObjectId() {
  96.         return getLeaf().getPeeledObjectId();
  97.     }

  98.     /** {@inheritDoc} */
  99.     @Override
  100.     public boolean isPeeled() {
  101.         return getLeaf().isPeeled();
  102.     }

  103.     /**
  104.      * {@inheritDoc}
  105.      * @since 5.3
  106.      */
  107.     @Override
  108.     public long getUpdateIndex() {
  109.         if (updateIndex == UNDEFINED_UPDATE_INDEX) {
  110.             throw new UnsupportedOperationException();
  111.         }
  112.         return updateIndex;
  113.     }

  114.     /** {@inheritDoc} */
  115.     @SuppressWarnings("nls")
  116.     @Override
  117.     public String toString() {
  118.         StringBuilder r = new StringBuilder();
  119.         r.append("SymbolicRef[");
  120.         Ref cur = this;
  121.         while (cur.isSymbolic()) {
  122.             r.append(cur.getName());
  123.             r.append(" -> ");
  124.             cur = cur.getTarget();
  125.         }
  126.         r.append(cur.getName());
  127.         r.append('=');
  128.         r.append(ObjectId.toString(cur.getObjectId()));
  129.         r.append("(");
  130.         r.append(updateIndex); // Print value, even if -1
  131.         r.append(")]");
  132.         return r.toString();
  133.     }
  134. }