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.

RefDatabase.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  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 java.io.IOException;
  45. import java.util.List;
  46. import java.util.Map;
  47. /**
  48. * Abstraction of name to {@link ObjectId} mapping.
  49. * <p>
  50. * A reference database stores a mapping of reference names to {@link ObjectId}.
  51. * Every {@link Repository} has a single reference database, mapping names to
  52. * the tips of the object graph contained by the {@link ObjectDatabase}.
  53. */
  54. public abstract class RefDatabase {
  55. /**
  56. * Order of prefixes to search when using non-absolute references.
  57. * <p>
  58. * The implementation's {@link #getRef(String)} method must take this search
  59. * space into consideration when locating a reference by name. The first
  60. * entry in the path is always {@code ""}, ensuring that absolute references
  61. * are resolved without further mangling.
  62. */
  63. protected static final String[] SEARCH_PATH = { "", //$NON-NLS-1$
  64. Constants.R_REFS, //
  65. Constants.R_TAGS, //
  66. Constants.R_HEADS, //
  67. Constants.R_REMOTES //
  68. };
  69. /**
  70. * Maximum number of times a {@link SymbolicRef} can be traversed.
  71. * <p>
  72. * If the reference is nested deeper than this depth, the implementation
  73. * should either fail, or at least claim the reference does not exist.
  74. */
  75. protected static final int MAX_SYMBOLIC_REF_DEPTH = 5;
  76. /** Magic value for {@link #getRefs(String)} to return all references. */
  77. public static final String ALL = "";//$NON-NLS-1$
  78. /**
  79. * Initialize a new reference database at this location.
  80. *
  81. * @throws IOException
  82. * the database could not be created.
  83. */
  84. public abstract void create() throws IOException;
  85. /** Close any resources held by this database. */
  86. public abstract void close();
  87. /**
  88. * Determine if a proposed reference name overlaps with an existing one.
  89. * <p>
  90. * Reference names use '/' as a component separator, and may be stored in a
  91. * hierarchical storage such as a directory on the local filesystem.
  92. * <p>
  93. * If the reference "refs/heads/foo" exists then "refs/heads/foo/bar" must
  94. * not exist, as a reference cannot have a value and also be a container for
  95. * other references at the same time.
  96. * <p>
  97. * If the reference "refs/heads/foo/bar" exists than the reference
  98. * "refs/heads/foo" cannot exist, for the same reason.
  99. *
  100. * @param name
  101. * proposed name.
  102. * @return true if the name overlaps with an existing reference; false if
  103. * using this name right now would be safe.
  104. * @throws IOException
  105. * the database could not be read to check for conflicts.
  106. */
  107. public abstract boolean isNameConflicting(String name) throws IOException;
  108. /**
  109. * Create a new update command to create, modify or delete a reference.
  110. *
  111. * @param name
  112. * the name of the reference.
  113. * @param detach
  114. * if {@code true} and {@code name} is currently a
  115. * {@link SymbolicRef}, the update will replace it with an
  116. * {@link ObjectIdRef}. Otherwise, the update will recursively
  117. * traverse {@link SymbolicRef}s and operate on the leaf
  118. * {@link ObjectIdRef}.
  119. * @return a new update for the requested name; never null.
  120. * @throws IOException
  121. * the reference space cannot be accessed.
  122. */
  123. public abstract RefUpdate newUpdate(String name, boolean detach)
  124. throws IOException;
  125. /**
  126. * Create a new update command to rename a reference.
  127. *
  128. * @param fromName
  129. * name of reference to rename from
  130. * @param toName
  131. * name of reference to rename to
  132. * @return an update command that knows how to rename a branch to another.
  133. * @throws IOException
  134. * the reference space cannot be accessed.
  135. */
  136. public abstract RefRename newRename(String fromName, String toName)
  137. throws IOException;
  138. /**
  139. * Read a single reference.
  140. * <p>
  141. * Aside from taking advantage of {@link #SEARCH_PATH}, this method may be
  142. * able to more quickly resolve a single reference name than obtaining the
  143. * complete namespace by {@code getRefs(ALL).get(name)}.
  144. *
  145. * @param name
  146. * the name of the reference. May be a short name which must be
  147. * searched for using the standard {@link #SEARCH_PATH}.
  148. * @return the reference (if it exists); else {@code null}.
  149. * @throws IOException
  150. * the reference space cannot be accessed.
  151. */
  152. public abstract Ref getRef(String name) throws IOException;
  153. /**
  154. * Get a section of the reference namespace.
  155. *
  156. * @param prefix
  157. * prefix to search the namespace with; must end with {@code /}.
  158. * If the empty string ({@link #ALL}), obtain a complete snapshot
  159. * of all references.
  160. * @return modifiable map that is a complete snapshot of the current
  161. * reference namespace, with {@code prefix} removed from the start
  162. * of each key. The map can be an unsorted map.
  163. * @throws IOException
  164. * the reference space cannot be accessed.
  165. */
  166. public abstract Map<String, Ref> getRefs(String prefix) throws IOException;
  167. /**
  168. * Get the additional reference-like entities from the repository.
  169. * <p>
  170. * The result list includes non-ref items such as MERGE_HEAD and
  171. * FETCH_RESULT cast to be refs. The names of these refs are not returned by
  172. * <code>getRefs(ALL)</code> but are accepted by {@link #getRef(String)}
  173. *
  174. * @return a list of additional refs
  175. * @throws IOException
  176. * the reference space cannot be accessed.
  177. */
  178. public abstract List<Ref> getAdditionalRefs() throws IOException;
  179. /**
  180. * Peel a possibly unpeeled reference by traversing the annotated tags.
  181. * <p>
  182. * If the reference cannot be peeled (as it does not refer to an annotated
  183. * tag) the peeled id stays null, but {@link Ref#isPeeled()} will be true.
  184. * <p>
  185. * Implementors should check {@link Ref#isPeeled()} before performing any
  186. * additional work effort.
  187. *
  188. * @param ref
  189. * The reference to peel
  190. * @return {@code ref} if {@code ref.isPeeled()} is true; otherwise a new
  191. * Ref object representing the same data as Ref, but isPeeled() will
  192. * be true and getPeeledObjectId() will contain the peeled object
  193. * (or null).
  194. * @throws IOException
  195. * the reference space or object space cannot be accessed.
  196. */
  197. public abstract Ref peel(Ref ref) throws IOException;
  198. /**
  199. * Triggers a refresh of all internal data structures.
  200. * <p>
  201. * In case the RefDatabase implementation has internal caches this method
  202. * will trigger that all these caches are cleared.
  203. * <p>
  204. * Implementors should overwrite this method if they use any kind of caches.
  205. */
  206. public void refresh() {
  207. // nothing
  208. }
  209. }