Ref.java

  1. /*
  2.  * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org> 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.  * Pairing of a name and the {@link org.eclipse.jgit.lib.ObjectId} it currently
  15.  * has.
  16.  * <p>
  17.  * A ref in Git is (more or less) a variable that holds a single object
  18.  * identifier. The object identifier can be any valid Git object (blob, tree,
  19.  * commit, annotated tag, ...).
  20.  * <p>
  21.  * The ref name has the attributes of the ref that was asked for as well as the
  22.  * ref it was resolved to for symbolic refs plus the object id it points to and
  23.  * (for tags) the peeled target object id, i.e. the tag resolved recursively
  24.  * until a non-tag object is referenced.
  25.  */
  26. public interface Ref {
  27.     /** Location where a {@link Ref} is stored. */
  28.     enum Storage {
  29.         /**
  30.          * The ref does not exist yet, updating it may create it.
  31.          * <p>
  32.          * Creation is likely to choose {@link #LOOSE} storage.
  33.          */
  34.         NEW(true, false),

  35.         /**
  36.          * The ref is stored in a file by itself.
  37.          * <p>
  38.          * Updating this ref affects only this ref.
  39.          */
  40.         LOOSE(true, false),

  41.         /**
  42.          * The ref is stored in the <code>packed-refs</code> file, with others.
  43.          * <p>
  44.          * Updating this ref requires rewriting the file, with perhaps many
  45.          * other refs being included at the same time.
  46.          */
  47.         PACKED(false, true),

  48.         /**
  49.          * The ref is both {@link #LOOSE} and {@link #PACKED}.
  50.          * <p>
  51.          * Updating this ref requires only updating the loose file, but deletion
  52.          * requires updating both the loose file and the packed refs file.
  53.          */
  54.         LOOSE_PACKED(true, true),

  55.         /**
  56.          * The ref came from a network advertisement and storage is unknown.
  57.          * <p>
  58.          * This ref cannot be updated without Git-aware support on the remote
  59.          * side, as Git-aware code consolidate the remote refs and reported them
  60.          * to this process.
  61.          */
  62.         NETWORK(false, false);

  63.         private final boolean loose;

  64.         private final boolean packed;

  65.         private Storage(boolean l, boolean p) {
  66.             loose = l;
  67.             packed = p;
  68.         }

  69.         /**
  70.          * @return true if this storage has a loose file.
  71.          */
  72.         public boolean isLoose() {
  73.             return loose;
  74.         }

  75.         /**
  76.          * @return true if this storage is inside the packed file.
  77.          */
  78.         public boolean isPacked() {
  79.             return packed;
  80.         }
  81.     }

  82.     /**
  83.      * Update index value when a reference doesn't have one
  84.      *
  85.      * @since 5.4
  86.      */
  87.     long UNDEFINED_UPDATE_INDEX = -1L;

  88.     /**
  89.      * What this ref is called within the repository.
  90.      *
  91.      * @return name of this ref.
  92.      */
  93.     @NonNull
  94.     String getName();

  95.     /**
  96.      * Test if this reference is a symbolic reference.
  97.      * <p>
  98.      * A symbolic reference does not have its own
  99.      * {@link org.eclipse.jgit.lib.ObjectId} value, but instead points to
  100.      * another {@code Ref} in the same database and always uses that other
  101.      * reference's value as its own.
  102.      *
  103.      * @return true if this is a symbolic reference; false if this reference
  104.      *         contains its own ObjectId.
  105.      */
  106.     boolean isSymbolic();

  107.     /**
  108.      * Traverse target references until {@link #isSymbolic()} is false.
  109.      * <p>
  110.      * If {@link #isSymbolic()} is false, returns {@code this}.
  111.      * <p>
  112.      * If {@link #isSymbolic()} is true, this method recursively traverses
  113.      * {@link #getTarget()} until {@link #isSymbolic()} returns false.
  114.      * <p>
  115.      * This method is effectively
  116.      *
  117.      * <pre>
  118.      * return isSymbolic() ? getTarget().getLeaf() : this;
  119.      * </pre>
  120.      *
  121.      * @return the reference that actually stores the ObjectId value.
  122.      */
  123.     @NonNull
  124.     Ref getLeaf();

  125.     /**
  126.      * Get the reference this reference points to, or {@code this}.
  127.      * <p>
  128.      * If {@link #isSymbolic()} is true this method returns the reference it
  129.      * directly names, which might not be the leaf reference, but could be
  130.      * another symbolic reference.
  131.      * <p>
  132.      * If this is a leaf level reference that contains its own ObjectId,this
  133.      * method returns {@code this}.
  134.      *
  135.      * @return the target reference, or {@code this}.
  136.      */
  137.     @NonNull
  138.     Ref getTarget();

  139.     /**
  140.      * Cached value of this ref.
  141.      *
  142.      * @return the value of this ref at the last time we read it. May be
  143.      *         {@code null} to indicate a ref that does not exist yet or a
  144.      *         symbolic ref pointing to an unborn branch.
  145.      */
  146.     @Nullable
  147.     ObjectId getObjectId();

  148.     /**
  149.      * Cached value of <code>ref^{}</code> (the ref peeled to commit).
  150.      *
  151.      * @return if this ref is an annotated tag the id of the commit (or tree or
  152.      *         blob) that the annotated tag refers to; {@code null} if this ref
  153.      *         does not refer to an annotated tag.
  154.      */
  155.     @Nullable
  156.     ObjectId getPeeledObjectId();

  157.     /**
  158.      * Whether the Ref represents a peeled tag.
  159.      *
  160.      * @return whether the Ref represents a peeled tag.
  161.      */
  162.     boolean isPeeled();

  163.     /**
  164.      * How was this ref obtained?
  165.      * <p>
  166.      * The current storage model of a Ref may influence how the ref must be
  167.      * updated or deleted from the repository.
  168.      *
  169.      * @return type of ref.
  170.      */
  171.     @NonNull
  172.     Storage getStorage();

  173.     /**
  174.      * Indicator of the relative order between updates of a specific reference
  175.      * name. A number that increases when a reference is updated.
  176.      * <p>
  177.      * With symbolic references, the update index refers to updates of the
  178.      * symbolic reference itself. For example, if HEAD points to
  179.      * refs/heads/master, then the update index for exactRef("HEAD") will only
  180.      * increase when HEAD changes to point to another ref, regardless of how
  181.      * many times refs/heads/master is updated.
  182.      * <p>
  183.      * Should not be used unless the {@code RefDatabase} that instantiated the
  184.      * ref supports versioning (see {@link RefDatabase#hasVersioning()})
  185.      *
  186.      * @return the update index (i.e. version) of this reference.
  187.      * @throws UnsupportedOperationException
  188.      *             if the creator of the instance (e.g. {@link RefDatabase})
  189.      *             doesn't support versioning and doesn't override this method
  190.      * @since 5.3
  191.      */
  192.     default long getUpdateIndex() {
  193.         throw new UnsupportedOperationException();
  194.     }
  195. }