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.

Reftable.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (C) 2017, 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.internal.storage.reftable;
  44. import static org.eclipse.jgit.lib.RefDatabase.MAX_SYMBOLIC_REF_DEPTH;
  45. import java.io.ByteArrayOutputStream;
  46. import java.io.IOException;
  47. import java.util.Collection;
  48. import org.eclipse.jgit.annotations.Nullable;
  49. import org.eclipse.jgit.internal.storage.io.BlockSource;
  50. import org.eclipse.jgit.lib.AnyObjectId;
  51. import org.eclipse.jgit.lib.Ref;
  52. import org.eclipse.jgit.lib.SymbolicRef;
  53. /**
  54. * Abstract table of references.
  55. */
  56. public abstract class Reftable {
  57. /**
  58. * References to convert into a reftable
  59. *
  60. * @param refs
  61. * references to convert into a reftable; may be empty.
  62. * @return a reader for the supplied references.
  63. */
  64. public static Reftable from(Collection<Ref> refs) {
  65. try {
  66. ReftableConfig cfg = new ReftableConfig();
  67. cfg.setIndexObjects(false);
  68. cfg.setAlignBlocks(false);
  69. ByteArrayOutputStream buf = new ByteArrayOutputStream();
  70. new ReftableWriter(buf)
  71. .setConfig(cfg)
  72. .begin()
  73. .sortAndWriteRefs(refs)
  74. .finish();
  75. return new ReftableReader(BlockSource.from(buf.toByteArray()));
  76. } catch (IOException e) {
  77. throw new RuntimeException(e);
  78. }
  79. }
  80. /** {@code true} if deletions should be included in results. */
  81. protected boolean includeDeletes;
  82. /**
  83. * Whether deleted references will be returned.
  84. *
  85. * @param deletes
  86. * if {@code true} deleted references will be returned. If
  87. * {@code false} (default behavior), deleted references will be
  88. * skipped, and not returned.
  89. */
  90. public void setIncludeDeletes(boolean deletes) {
  91. includeDeletes = deletes;
  92. }
  93. /**
  94. * Get the maximum update index for ref entries that appear in this
  95. * reftable.
  96. *
  97. * @return the maximum update index for ref entries that appear in this
  98. * reftable.
  99. * @throws java.io.IOException
  100. * file cannot be read.
  101. */
  102. public abstract long maxUpdateIndex() throws IOException;
  103. /**
  104. * Get the minimum update index for ref entries that appear in this
  105. * reftable.
  106. *
  107. * @return the minimum update index for ref entries that appear in this
  108. * reftable.
  109. * @throws java.io.IOException
  110. * file cannot be read.
  111. */
  112. public abstract long minUpdateIndex() throws IOException;
  113. /**
  114. * Seek to the first reference, to iterate in order.
  115. *
  116. * @return cursor to iterate.
  117. * @throws java.io.IOException
  118. * if references cannot be read.
  119. */
  120. public abstract RefCursor allRefs() throws IOException;
  121. /**
  122. * Seek to a reference.
  123. * <p>
  124. * This method will seek to the reference {@code refName}. If present, the
  125. * returned cursor will iterate exactly one entry. If not found, an empty
  126. * cursor is returned.
  127. *
  128. * @param refName
  129. * reference name.
  130. * @return cursor to iterate; empty cursor if no references match.
  131. * @throws java.io.IOException
  132. * if references cannot be read.
  133. */
  134. public abstract RefCursor seekRef(String refName) throws IOException;
  135. /**
  136. * Seek references with prefix.
  137. * <p>
  138. * The method will seek all the references starting with {@code prefix} as a
  139. * prefix. If no references start with this prefix, an empty cursor is
  140. * returned.
  141. *
  142. * @param prefix
  143. * prefix to find.
  144. * @return cursor to iterate; empty cursor if no references match.
  145. * @throws java.io.IOException
  146. * if references cannot be read.
  147. */
  148. public abstract RefCursor seekRefsWithPrefix(String prefix) throws IOException;
  149. /**
  150. * Match references pointing to a specific object.
  151. *
  152. * @param id
  153. * object to find.
  154. * @return cursor to iterate; empty cursor if no references match.
  155. * @throws java.io.IOException
  156. * if references cannot be read.
  157. */
  158. public abstract RefCursor byObjectId(AnyObjectId id) throws IOException;
  159. /**
  160. * @return whether this reftable can do a fast SHA1 => ref lookup.
  161. * @throws IOException on I/O problems.
  162. */
  163. public abstract boolean hasObjectMap() throws IOException;
  164. /**
  165. * Seek reader to read log records.
  166. *
  167. * @return cursor to iterate; empty cursor if no logs are present.
  168. * @throws java.io.IOException
  169. * if logs cannot be read.
  170. */
  171. public abstract LogCursor allLogs() throws IOException;
  172. /**
  173. * Read a single reference's log.
  174. *
  175. * @param refName
  176. * exact name of the reference whose log to read.
  177. * @return cursor to iterate; empty cursor if no logs match.
  178. * @throws java.io.IOException
  179. * if logs cannot be read.
  180. */
  181. public LogCursor seekLog(String refName) throws IOException {
  182. return seekLog(refName, Long.MAX_VALUE);
  183. }
  184. /**
  185. * Seek to an update index in a reference's log.
  186. *
  187. * @param refName
  188. * exact name of the reference whose log to read.
  189. * @param updateIndex
  190. * most recent index to return first in the log cursor. Log
  191. * records at or before {@code updateIndex} will be returned.
  192. * @return cursor to iterate; empty cursor if no logs match.
  193. * @throws java.io.IOException
  194. * if logs cannot be read.
  195. */
  196. public abstract LogCursor seekLog(String refName, long updateIndex)
  197. throws IOException;
  198. /**
  199. * Lookup a reference, or null if not found.
  200. *
  201. * @param refName
  202. * reference name to find.
  203. * @return the reference, or {@code null} if not found.
  204. * @throws java.io.IOException
  205. * if references cannot be read.
  206. */
  207. @Nullable
  208. public Ref exactRef(String refName) throws IOException {
  209. try (RefCursor rc = seekRef(refName)) {
  210. return rc.next() ? rc.getRef() : null;
  211. }
  212. }
  213. /**
  214. * Test if a reference exists.
  215. *
  216. * @param refName
  217. * reference name or subtree to find.
  218. * @return {@code true} if the reference exists.
  219. * @throws java.io.IOException
  220. * if references cannot be read.
  221. */
  222. public boolean hasRef(String refName) throws IOException {
  223. try (RefCursor rc = seekRef(refName)) {
  224. return rc.next();
  225. }
  226. }
  227. /**
  228. * Test if any reference starts with {@code prefix} as a prefix.
  229. *
  230. * @param prefix
  231. * prefix to find.
  232. * @return {@code true} if at least one reference exists with prefix.
  233. * @throws java.io.IOException
  234. * if references cannot be read.
  235. */
  236. public boolean hasRefsWithPrefix(String prefix) throws IOException {
  237. try (RefCursor rc = seekRefsWithPrefix(prefix)) {
  238. return rc.next();
  239. }
  240. }
  241. /**
  242. * Test if any reference directly refers to the object.
  243. *
  244. * @param id
  245. * ObjectId to find.
  246. * @return {@code true} if any reference exists directly referencing
  247. * {@code id}, or a annotated tag that peels to {@code id}.
  248. * @throws java.io.IOException
  249. * if references cannot be read.
  250. */
  251. public boolean hasId(AnyObjectId id) throws IOException {
  252. try (RefCursor rc = byObjectId(id)) {
  253. return rc.next();
  254. }
  255. }
  256. /**
  257. * Resolve a symbolic reference to populate its value.
  258. *
  259. * @param symref
  260. * reference to resolve.
  261. * @return resolved {@code symref}, or {@code null}.
  262. * @throws java.io.IOException
  263. * if references cannot be read.
  264. */
  265. @Nullable
  266. public Ref resolve(Ref symref) throws IOException {
  267. return resolve(symref, 0);
  268. }
  269. private Ref resolve(Ref ref, int depth) throws IOException {
  270. if (!ref.isSymbolic()) {
  271. return ref;
  272. }
  273. Ref dst = ref.getTarget();
  274. if (MAX_SYMBOLIC_REF_DEPTH <= depth) {
  275. return null; // claim it doesn't exist
  276. }
  277. dst = exactRef(dst.getName());
  278. if (dst == null) {
  279. return ref;
  280. }
  281. dst = resolve(dst, depth + 1);
  282. if (dst == null) {
  283. return null; // claim it doesn't exist
  284. }
  285. return new SymbolicRef(ref.getName(), dst, ref.getUpdateIndex());
  286. }
  287. }