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 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. import org.eclipse.jgit.annotations.NonNull;
  45. import org.eclipse.jgit.annotations.Nullable;
  46. /**
  47. * Pairing of a name and the {@link org.eclipse.jgit.lib.ObjectId} it currently
  48. * has.
  49. * <p>
  50. * A ref in Git is (more or less) a variable that holds a single object
  51. * identifier. The object identifier can be any valid Git object (blob, tree,
  52. * commit, annotated tag, ...).
  53. * <p>
  54. * The ref name has the attributes of the ref that was asked for as well as the
  55. * ref it was resolved to for symbolic refs plus the object id it points to and
  56. * (for tags) the peeled target object id, i.e. the tag resolved recursively
  57. * until a non-tag object is referenced.
  58. */
  59. public interface Ref {
  60. /** Location where a {@link Ref} is stored. */
  61. enum Storage {
  62. /**
  63. * The ref does not exist yet, updating it may create it.
  64. * <p>
  65. * Creation is likely to choose {@link #LOOSE} storage.
  66. */
  67. NEW(true, false),
  68. /**
  69. * The ref is stored in a file by itself.
  70. * <p>
  71. * Updating this ref affects only this ref.
  72. */
  73. LOOSE(true, false),
  74. /**
  75. * The ref is stored in the <code>packed-refs</code> file, with others.
  76. * <p>
  77. * Updating this ref requires rewriting the file, with perhaps many
  78. * other refs being included at the same time.
  79. */
  80. PACKED(false, true),
  81. /**
  82. * The ref is both {@link #LOOSE} and {@link #PACKED}.
  83. * <p>
  84. * Updating this ref requires only updating the loose file, but deletion
  85. * requires updating both the loose file and the packed refs file.
  86. */
  87. LOOSE_PACKED(true, true),
  88. /**
  89. * The ref came from a network advertisement and storage is unknown.
  90. * <p>
  91. * This ref cannot be updated without Git-aware support on the remote
  92. * side, as Git-aware code consolidate the remote refs and reported them
  93. * to this process.
  94. */
  95. NETWORK(false, false);
  96. private final boolean loose;
  97. private final boolean packed;
  98. private Storage(boolean l, boolean p) {
  99. loose = l;
  100. packed = p;
  101. }
  102. /**
  103. * @return true if this storage has a loose file.
  104. */
  105. public boolean isLoose() {
  106. return loose;
  107. }
  108. /**
  109. * @return true if this storage is inside the packed file.
  110. */
  111. public boolean isPacked() {
  112. return packed;
  113. }
  114. }
  115. /**
  116. * What this ref is called within the repository.
  117. *
  118. * @return name of this ref.
  119. */
  120. @NonNull
  121. String getName();
  122. /**
  123. * Test if this reference is a symbolic reference.
  124. * <p>
  125. * A symbolic reference does not have its own
  126. * {@link org.eclipse.jgit.lib.ObjectId} value, but instead points to
  127. * another {@code Ref} in the same database and always uses that other
  128. * reference's value as its own.
  129. *
  130. * @return true if this is a symbolic reference; false if this reference
  131. * contains its own ObjectId.
  132. */
  133. boolean isSymbolic();
  134. /**
  135. * Traverse target references until {@link #isSymbolic()} is false.
  136. * <p>
  137. * If {@link #isSymbolic()} is false, returns {@code this}.
  138. * <p>
  139. * If {@link #isSymbolic()} is true, this method recursively traverses
  140. * {@link #getTarget()} until {@link #isSymbolic()} returns false.
  141. * <p>
  142. * This method is effectively
  143. *
  144. * <pre>
  145. * return isSymbolic() ? getTarget().getLeaf() : this;
  146. * </pre>
  147. *
  148. * @return the reference that actually stores the ObjectId value.
  149. */
  150. @NonNull
  151. Ref getLeaf();
  152. /**
  153. * Get the reference this reference points to, or {@code this}.
  154. * <p>
  155. * If {@link #isSymbolic()} is true this method returns the reference it
  156. * directly names, which might not be the leaf reference, but could be
  157. * another symbolic reference.
  158. * <p>
  159. * If this is a leaf level reference that contains its own ObjectId,this
  160. * method returns {@code this}.
  161. *
  162. * @return the target reference, or {@code this}.
  163. */
  164. @NonNull
  165. Ref getTarget();
  166. /**
  167. * Cached value of this ref.
  168. *
  169. * @return the value of this ref at the last time we read it. May be
  170. * {@code null} to indicate a ref that does not exist yet or a
  171. * symbolic ref pointing to an unborn branch.
  172. */
  173. @Nullable
  174. ObjectId getObjectId();
  175. /**
  176. * Cached value of <code>ref^{}</code> (the ref peeled to commit).
  177. *
  178. * @return if this ref is an annotated tag the id of the commit (or tree or
  179. * blob) that the annotated tag refers to; {@code null} if this ref
  180. * does not refer to an annotated tag.
  181. */
  182. @Nullable
  183. ObjectId getPeeledObjectId();
  184. /**
  185. * Whether the Ref represents a peeled tag.
  186. *
  187. * @return whether the Ref represents a peeled tag.
  188. */
  189. boolean isPeeled();
  190. /**
  191. * How was this ref obtained?
  192. * <p>
  193. * The current storage model of a Ref may influence how the ref must be
  194. * updated or deleted from the repository.
  195. *
  196. * @return type of ref.
  197. */
  198. @NonNull
  199. Storage getStorage();
  200. /**
  201. * Indicator of the relative order between updates of a specific reference
  202. * name. A number that increases when a reference is updated.
  203. * <p>
  204. * With symbolic references, the update index refers to updates of the
  205. * symbolic reference itself. For example, if HEAD points to
  206. * refs/heads/master, then the update index for exactRef("HEAD") will only
  207. * increase when HEAD changes to point to another ref, regardless of how
  208. * many times refs/heads/master is updated.
  209. * <p>
  210. * Should not be used unless the {@code RefDatabase} that instantiated the
  211. * ref supports versioning (see {@link RefDatabase#hasVersioning()})
  212. *
  213. * @return the update index (i.e. version) of this reference.
  214. * @throws UnsupportedOperationException
  215. * if the creator of the instance (e.g. {@link RefDatabase})
  216. * doesn't support versioning and doesn't override this method
  217. * @since 5.3
  218. */
  219. default long getUpdateIndex() {
  220. throw new UnsupportedOperationException();
  221. }
  222. }