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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (C) 2010, 2013 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.ArrayList;
  46. import java.util.Collection;
  47. import java.util.Collections;
  48. import java.util.List;
  49. import java.util.Map;
  50. /**
  51. * Abstraction of name to {@link ObjectId} mapping.
  52. * <p>
  53. * A reference database stores a mapping of reference names to {@link ObjectId}.
  54. * Every {@link Repository} has a single reference database, mapping names to
  55. * the tips of the object graph contained by the {@link ObjectDatabase}.
  56. */
  57. public abstract class RefDatabase {
  58. /**
  59. * Order of prefixes to search when using non-absolute references.
  60. * <p>
  61. * The implementation's {@link #getRef(String)} method must take this search
  62. * space into consideration when locating a reference by name. The first
  63. * entry in the path is always {@code ""}, ensuring that absolute references
  64. * are resolved without further mangling.
  65. */
  66. protected static final String[] SEARCH_PATH = { "", //$NON-NLS-1$
  67. Constants.R_REFS, //
  68. Constants.R_TAGS, //
  69. Constants.R_HEADS, //
  70. Constants.R_REMOTES //
  71. };
  72. /**
  73. * Maximum number of times a {@link SymbolicRef} can be traversed.
  74. * <p>
  75. * If the reference is nested deeper than this depth, the implementation
  76. * should either fail, or at least claim the reference does not exist.
  77. */
  78. protected static final int MAX_SYMBOLIC_REF_DEPTH = 5;
  79. /** Magic value for {@link #getRefs(String)} to return all references. */
  80. public static final String ALL = "";//$NON-NLS-1$
  81. /**
  82. * Initialize a new reference database at this location.
  83. *
  84. * @throws IOException
  85. * the database could not be created.
  86. */
  87. public abstract void create() throws IOException;
  88. /** Close any resources held by this database. */
  89. public abstract void close();
  90. /**
  91. * Determine if a proposed reference name overlaps with an existing one.
  92. * <p>
  93. * Reference names use '/' as a component separator, and may be stored in a
  94. * hierarchical storage such as a directory on the local filesystem.
  95. * <p>
  96. * If the reference "refs/heads/foo" exists then "refs/heads/foo/bar" must
  97. * not exist, as a reference cannot have a value and also be a container for
  98. * other references at the same time.
  99. * <p>
  100. * If the reference "refs/heads/foo/bar" exists than the reference
  101. * "refs/heads/foo" cannot exist, for the same reason.
  102. *
  103. * @param name
  104. * proposed name.
  105. * @return true if the name overlaps with an existing reference; false if
  106. * using this name right now would be safe.
  107. * @throws IOException
  108. * the database could not be read to check for conflicts.
  109. * @see #getConflictingNames(String)
  110. */
  111. public abstract boolean isNameConflicting(String name) throws IOException;
  112. /**
  113. * Determine if a proposed reference cannot coexist with existing ones. If
  114. * the passed name already exists, it's not considered a conflict.
  115. *
  116. * @param name
  117. * proposed name to check for conflicts against
  118. * @return a collection of full names of existing refs which would conflict
  119. * with the passed ref name; empty collection when there are no
  120. * conflicts
  121. * @throws IOException
  122. * @since 2.3
  123. * @see #isNameConflicting(String)
  124. */
  125. public Collection<String> getConflictingNames(String name)
  126. throws IOException {
  127. Map<String, Ref> allRefs = getRefs(ALL);
  128. // Cannot be nested within an existing reference.
  129. int lastSlash = name.lastIndexOf('/');
  130. while (0 < lastSlash) {
  131. String needle = name.substring(0, lastSlash);
  132. if (allRefs.containsKey(needle))
  133. return Collections.singletonList(needle);
  134. lastSlash = name.lastIndexOf('/', lastSlash - 1);
  135. }
  136. List<String> conflicting = new ArrayList<String>();
  137. // Cannot be the container of an existing reference.
  138. String prefix = name + '/';
  139. for (String existing : allRefs.keySet())
  140. if (existing.startsWith(prefix))
  141. conflicting.add(existing);
  142. return conflicting;
  143. }
  144. /**
  145. * Create a new update command to create, modify or delete a reference.
  146. *
  147. * @param name
  148. * the name of the reference.
  149. * @param detach
  150. * if {@code true} and {@code name} is currently a
  151. * {@link SymbolicRef}, the update will replace it with an
  152. * {@link ObjectIdRef}. Otherwise, the update will recursively
  153. * traverse {@link SymbolicRef}s and operate on the leaf
  154. * {@link ObjectIdRef}.
  155. * @return a new update for the requested name; never null.
  156. * @throws IOException
  157. * the reference space cannot be accessed.
  158. */
  159. public abstract RefUpdate newUpdate(String name, boolean detach)
  160. throws IOException;
  161. /**
  162. * Create a new update command to rename a reference.
  163. *
  164. * @param fromName
  165. * name of reference to rename from
  166. * @param toName
  167. * name of reference to rename to
  168. * @return an update command that knows how to rename a branch to another.
  169. * @throws IOException
  170. * the reference space cannot be accessed.
  171. */
  172. public abstract RefRename newRename(String fromName, String toName)
  173. throws IOException;
  174. /**
  175. * Create a new batch update to attempt on this database.
  176. * <p>
  177. * The default implementation performs a sequential update of each command.
  178. *
  179. * @return a new batch update object.
  180. */
  181. public BatchRefUpdate newBatchUpdate() {
  182. return new BatchRefUpdate(this);
  183. }
  184. /**
  185. * Read a single reference.
  186. * <p>
  187. * Aside from taking advantage of {@link #SEARCH_PATH}, this method may be
  188. * able to more quickly resolve a single reference name than obtaining the
  189. * complete namespace by {@code getRefs(ALL).get(name)}.
  190. *
  191. * @param name
  192. * the name of the reference. May be a short name which must be
  193. * searched for using the standard {@link #SEARCH_PATH}.
  194. * @return the reference (if it exists); else {@code null}.
  195. * @throws IOException
  196. * the reference space cannot be accessed.
  197. */
  198. public abstract Ref getRef(String name) throws IOException;
  199. /**
  200. * Get a section of the reference namespace.
  201. *
  202. * @param prefix
  203. * prefix to search the namespace with; must end with {@code /}.
  204. * If the empty string ({@link #ALL}), obtain a complete snapshot
  205. * of all references.
  206. * @return modifiable map that is a complete snapshot of the current
  207. * reference namespace, with {@code prefix} removed from the start
  208. * of each key. The map can be an unsorted map.
  209. * @throws IOException
  210. * the reference space cannot be accessed.
  211. */
  212. public abstract Map<String, Ref> getRefs(String prefix) throws IOException;
  213. /**
  214. * Get the additional reference-like entities from the repository.
  215. * <p>
  216. * The result list includes non-ref items such as MERGE_HEAD and
  217. * FETCH_RESULT cast to be refs. The names of these refs are not returned by
  218. * <code>getRefs(ALL)</code> but are accepted by {@link #getRef(String)}
  219. *
  220. * @return a list of additional refs
  221. * @throws IOException
  222. * the reference space cannot be accessed.
  223. */
  224. public abstract List<Ref> getAdditionalRefs() throws IOException;
  225. /**
  226. * Peel a possibly unpeeled reference by traversing the annotated tags.
  227. * <p>
  228. * If the reference cannot be peeled (as it does not refer to an annotated
  229. * tag) the peeled id stays null, but {@link Ref#isPeeled()} will be true.
  230. * <p>
  231. * Implementors should check {@link Ref#isPeeled()} before performing any
  232. * additional work effort.
  233. *
  234. * @param ref
  235. * The reference to peel
  236. * @return {@code ref} if {@code ref.isPeeled()} is true; otherwise a new
  237. * Ref object representing the same data as Ref, but isPeeled() will
  238. * be true and getPeeledObjectId() will contain the peeled object
  239. * (or null).
  240. * @throws IOException
  241. * the reference space or object space cannot be accessed.
  242. */
  243. public abstract Ref peel(Ref ref) throws IOException;
  244. /**
  245. * Triggers a refresh of all internal data structures.
  246. * <p>
  247. * In case the RefDatabase implementation has internal caches this method
  248. * will trigger that all these caches are cleared.
  249. * <p>
  250. * Implementors should overwrite this method if they use any kind of caches.
  251. */
  252. public void refresh() {
  253. // nothing
  254. }
  255. /**
  256. * Try to find the specified name in the ref map using {@link #SEARCH_PATH}.
  257. *
  258. * @param map
  259. * map of refs to search within. Names should be fully qualified,
  260. * e.g. "refs/heads/master".
  261. * @param name
  262. * short name of ref to find, e.g. "master" to find
  263. * "refs/heads/master" in map.
  264. * @return The first ref matching the name, or null if not found.
  265. * @since 3.4
  266. */
  267. public static Ref findRef(Map<String, Ref> map, String name) {
  268. for (String prefix : SEARCH_PATH) {
  269. String fullname = prefix + name;
  270. Ref ref = map.get(fullname);
  271. if (ref != null)
  272. return ref;
  273. }
  274. return null;
  275. }
  276. }