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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 log entries that appear in this
  95. * reftable.
  96. *
  97. * @return the maximum update index for log entries that appear in this
  98. * reftable. This should be 1 higher than the prior reftable's
  99. * {@code maxUpdateIndex} if this table is used in a stack.
  100. * @throws java.io.IOException
  101. * file cannot be read.
  102. */
  103. public abstract long maxUpdateIndex() throws IOException;
  104. /**
  105. * Seek to the first reference, to iterate in order.
  106. *
  107. * @return cursor to iterate.
  108. * @throws java.io.IOException
  109. * if references cannot be read.
  110. */
  111. public abstract RefCursor allRefs() throws IOException;
  112. /**
  113. * Seek to a reference.
  114. * <p>
  115. * This method will seek to the reference {@code refName}. If present, the
  116. * returned cursor will iterate exactly one entry. If not found, an empty
  117. * cursor is returned.
  118. *
  119. * @param refName
  120. * reference name.
  121. * @return cursor to iterate; empty cursor if no references match.
  122. * @throws java.io.IOException
  123. * if references cannot be read.
  124. */
  125. public abstract RefCursor seekRef(String refName) throws IOException;
  126. /**
  127. * Seek references with prefix.
  128. * <p>
  129. * The method will seek all the references starting with {@code prefix} as a
  130. * prefix. If no references start with this prefix, an empty cursor is
  131. * returned.
  132. *
  133. * @param prefix
  134. * prefix to find.
  135. * @return cursor to iterate; empty cursor if no references match.
  136. * @throws java.io.IOException
  137. * if references cannot be read.
  138. */
  139. public abstract RefCursor seekRefsWithPrefix(String prefix) throws IOException;
  140. /**
  141. * Match references pointing to a specific object.
  142. *
  143. * @param id
  144. * object to find.
  145. * @return cursor to iterate; empty cursor if no references match.
  146. * @throws java.io.IOException
  147. * if references cannot be read.
  148. */
  149. public abstract RefCursor byObjectId(AnyObjectId id) throws IOException;
  150. /**
  151. * Seek reader to read log records.
  152. *
  153. * @return cursor to iterate; empty cursor if no logs are present.
  154. * @throws java.io.IOException
  155. * if logs cannot be read.
  156. */
  157. public abstract LogCursor allLogs() throws IOException;
  158. /**
  159. * Read a single reference's log.
  160. *
  161. * @param refName
  162. * exact name of the reference whose log to read.
  163. * @return cursor to iterate; empty cursor if no logs match.
  164. * @throws java.io.IOException
  165. * if logs cannot be read.
  166. */
  167. public LogCursor seekLog(String refName) throws IOException {
  168. return seekLog(refName, Long.MAX_VALUE);
  169. }
  170. /**
  171. * Seek to an update index in a reference's log.
  172. *
  173. * @param refName
  174. * exact name of the reference whose log to read.
  175. * @param updateIndex
  176. * most recent index to return first in the log cursor. Log
  177. * records at or before {@code updateIndex} will be returned.
  178. * @return cursor to iterate; empty cursor if no logs match.
  179. * @throws java.io.IOException
  180. * if logs cannot be read.
  181. */
  182. public abstract LogCursor seekLog(String refName, long updateIndex)
  183. throws IOException;
  184. /**
  185. * Lookup a reference, or null if not found.
  186. *
  187. * @param refName
  188. * reference name to find.
  189. * @return the reference, or {@code null} if not found.
  190. * @throws java.io.IOException
  191. * if references cannot be read.
  192. */
  193. @Nullable
  194. public Ref exactRef(String refName) throws IOException {
  195. try (RefCursor rc = seekRef(refName)) {
  196. return rc.next() ? rc.getRef() : null;
  197. }
  198. }
  199. /**
  200. * Test if a reference exists.
  201. *
  202. * @param refName
  203. * reference name or subtree to find.
  204. * @return {@code true} if the reference exists.
  205. * @throws java.io.IOException
  206. * if references cannot be read.
  207. */
  208. public boolean hasRef(String refName) throws IOException {
  209. try (RefCursor rc = seekRef(refName)) {
  210. return rc.next();
  211. }
  212. }
  213. /**
  214. * Test if any reference starts with {@code prefix} as a prefix.
  215. *
  216. * @param prefix
  217. * prefix to find.
  218. * @return {@code true} if at least one reference exists with prefix.
  219. * @throws java.io.IOException
  220. * if references cannot be read.
  221. */
  222. public boolean hasRefsWithPrefix(String prefix) throws IOException {
  223. try (RefCursor rc = seekRefsWithPrefix(prefix)) {
  224. return rc.next();
  225. }
  226. }
  227. /**
  228. * Test if any reference directly refers to the object.
  229. *
  230. * @param id
  231. * ObjectId to find.
  232. * @return {@code true} if any reference exists directly referencing
  233. * {@code id}, or a annotated tag that peels to {@code id}.
  234. * @throws java.io.IOException
  235. * if references cannot be read.
  236. */
  237. public boolean hasId(AnyObjectId id) throws IOException {
  238. try (RefCursor rc = byObjectId(id)) {
  239. return rc.next();
  240. }
  241. }
  242. /**
  243. * Resolve a symbolic reference to populate its value.
  244. *
  245. * @param symref
  246. * reference to resolve.
  247. * @return resolved {@code symref}, or {@code null}.
  248. * @throws java.io.IOException
  249. * if references cannot be read.
  250. */
  251. @Nullable
  252. public Ref resolve(Ref symref) throws IOException {
  253. return resolve(symref, 0);
  254. }
  255. private Ref resolve(Ref ref, int depth) throws IOException {
  256. if (!ref.isSymbolic()) {
  257. return ref;
  258. }
  259. Ref dst = ref.getTarget();
  260. if (MAX_SYMBOLIC_REF_DEPTH <= depth) {
  261. return null; // claim it doesn't exist
  262. }
  263. dst = exactRef(dst.getName());
  264. if (dst == null) {
  265. return ref;
  266. }
  267. dst = resolve(dst, depth + 1);
  268. if (dst == null) {
  269. return null; // claim it doesn't exist
  270. }
  271. return new SymbolicRef(ref.getName(), dst, ref.getUpdateIndex());
  272. }
  273. }