You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Ref.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.lib;
  44. /**
  45. * Pairing of a name and the {@link ObjectId} it currently has.
  46. * <p>
  47. * A ref in Git is (more or less) a variable that holds a single object
  48. * identifier. The object identifier can be any valid Git object (blob, tree,
  49. * commit, annotated tag, ...).
  50. * <p>
  51. * The ref name has the attributes of the ref that was asked for as well as the
  52. * ref it was resolved to for symbolic refs plus the object id it points to and
  53. * (for tags) the peeled target object id, i.e. the tag resolved recursively
  54. * until a non-tag object is referenced.
  55. */
  56. public interface Ref {
  57. /** Location where a {@link Ref} is stored. */
  58. public static enum Storage {
  59. /**
  60. * The ref does not exist yet, updating it may create it.
  61. * <p>
  62. * Creation is likely to choose {@link #LOOSE} storage.
  63. */
  64. NEW(true, false),
  65. /**
  66. * The ref is stored in a file by itself.
  67. * <p>
  68. * Updating this ref affects only this ref.
  69. */
  70. LOOSE(true, false),
  71. /**
  72. * The ref is stored in the <code>packed-refs</code> file, with others.
  73. * <p>
  74. * Updating this ref requires rewriting the file, with perhaps many
  75. * other refs being included at the same time.
  76. */
  77. PACKED(false, true),
  78. /**
  79. * The ref is both {@link #LOOSE} and {@link #PACKED}.
  80. * <p>
  81. * Updating this ref requires only updating the loose file, but deletion
  82. * requires updating both the loose file and the packed refs file.
  83. */
  84. LOOSE_PACKED(true, true),
  85. /**
  86. * The ref came from a network advertisement and storage is unknown.
  87. * <p>
  88. * This ref cannot be updated without Git-aware support on the remote
  89. * side, as Git-aware code consolidate the remote refs and reported them
  90. * to this process.
  91. */
  92. NETWORK(false, false);
  93. private final boolean loose;
  94. private final boolean packed;
  95. private Storage(final boolean l, final boolean p) {
  96. loose = l;
  97. packed = p;
  98. }
  99. /**
  100. * @return true if this storage has a loose file.
  101. */
  102. public boolean isLoose() {
  103. return loose;
  104. }
  105. /**
  106. * @return true if this storage is inside the packed file.
  107. */
  108. public boolean isPacked() {
  109. return packed;
  110. }
  111. }
  112. /**
  113. * What this ref is called within the repository.
  114. *
  115. * @return name of this ref.
  116. */
  117. public String getName();
  118. /**
  119. * Test if this reference is a symbolic reference.
  120. * <p>
  121. * A symbolic reference does not have its own {@link ObjectId} value, but
  122. * instead points to another {@code Ref} in the same database and always
  123. * uses that other reference's value as its own.
  124. *
  125. * @return true if this is a symbolic reference; false if this reference
  126. * contains its own ObjectId.
  127. */
  128. public abstract boolean isSymbolic();
  129. /**
  130. * Traverse target references until {@link #isSymbolic()} is false.
  131. * <p>
  132. * If {@link #isSymbolic()} is false, returns {@code this}.
  133. * <p>
  134. * If {@link #isSymbolic()} is true, this method recursively traverses
  135. * {@link #getTarget()} until {@link #isSymbolic()} returns false.
  136. * <p>
  137. * This method is effectively
  138. *
  139. * <pre>
  140. * return isSymbolic() ? getTarget().getLeaf() : this;
  141. * </pre>
  142. *
  143. * @return the reference that actually stores the ObjectId value.
  144. */
  145. public abstract Ref getLeaf();
  146. /**
  147. * Get the reference this reference points to, or {@code this}.
  148. * <p>
  149. * If {@link #isSymbolic()} is true this method returns the reference it
  150. * directly names, which might not be the leaf reference, but could be
  151. * another symbolic reference.
  152. * <p>
  153. * If this is a leaf level reference that contains its own ObjectId,this
  154. * method returns {@code this}.
  155. *
  156. * @return the target reference, or {@code this}.
  157. */
  158. public abstract Ref getTarget();
  159. /**
  160. * Cached value of this ref.
  161. *
  162. * @return the value of this ref at the last time we read it.
  163. */
  164. public abstract ObjectId getObjectId();
  165. /**
  166. * Cached value of <code>ref^{}</code> (the ref peeled to commit).
  167. *
  168. * @return if this ref is an annotated tag the id of the commit (or tree or
  169. * blob) that the annotated tag refers to; null if this ref does not
  170. * refer to an annotated tag.
  171. */
  172. public abstract ObjectId getPeeledObjectId();
  173. /**
  174. * @return whether the Ref represents a peeled tag
  175. */
  176. public abstract boolean isPeeled();
  177. /**
  178. * How was this ref obtained?
  179. * <p>
  180. * The current storage model of a Ref may influence how the ref must be
  181. * updated or deleted from the repository.
  182. *
  183. * @return type of ref.
  184. */
  185. public abstract Storage getStorage();
  186. }