Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RefDatabase.java 13KB

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