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

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