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.

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