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.

CommitGraphData.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2021, Tencent.
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.storage.commitgraph;
  11. import java.io.File;
  12. import java.io.FileNotFoundException;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.text.MessageFormat;
  16. import org.eclipse.jgit.errors.CommitGraphFormatException;
  17. import org.eclipse.jgit.internal.JGitText;
  18. import org.eclipse.jgit.lib.AnyObjectId;
  19. import org.eclipse.jgit.lib.CommitGraph;
  20. import org.eclipse.jgit.lib.ObjectId;
  21. import org.eclipse.jgit.util.IO;
  22. import org.eclipse.jgit.util.io.SilentFileInputStream;
  23. /**
  24. * <p>
  25. * The commit-graph stores a list of commit OIDs and some associated metadata,
  26. * including:
  27. * <ol>
  28. * <li>The generation number of the commit. Commits with no parents have
  29. * generation number 1; commits with parents have generation number one more
  30. * than the maximum generation number of its parents. We reserve zero as
  31. * special, and can be used to mark a generation number invalid or as "not
  32. * computed".</li>
  33. * <li>The root tree OID.</li>
  34. * <li>The commit date.</li>
  35. * <li>The parents of the commit, stored using positional references within the
  36. * graph file.</li>
  37. * </ol>
  38. * </p>
  39. */
  40. public abstract class CommitGraphData {
  41. /**
  42. * Open an existing commit-graph file for reading.
  43. * <p>
  44. * The format of the file will be automatically detected and a proper access
  45. * implementation for that format will be constructed and returned to the
  46. * caller. The file may or may not be held open by the returned instance.
  47. *
  48. * @param graphFile
  49. * existing commit-graph to read.
  50. * @return a copy of the commit-graph file in memory
  51. * @throws FileNotFoundException
  52. * the file does not exist.
  53. * @throws CommitGraphFormatException
  54. * commit-graph file's format is different than we expected.
  55. * @throws java.io.IOException
  56. * the file exists but could not be read due to security errors
  57. * or unexpected data corruption.
  58. */
  59. public static CommitGraphData open(File graphFile)
  60. throws FileNotFoundException, CommitGraphFormatException,
  61. IOException {
  62. try (SilentFileInputStream fd = new SilentFileInputStream(graphFile)) {
  63. try {
  64. return read(fd);
  65. } catch (CommitGraphFormatException fe) {
  66. throw fe;
  67. } catch (IOException ioe) {
  68. throw new IOException(MessageFormat.format(
  69. JGitText.get().unreadableCommitGraph,
  70. graphFile.getAbsolutePath()), ioe);
  71. }
  72. }
  73. }
  74. /**
  75. * Read an existing commit-graph file from a buffered stream.
  76. * <p>
  77. * The format of the file will be automatically detected and a proper access
  78. * implementation for that format will be constructed and returned to the
  79. * caller. The file may or may not be held open by the returned instance.
  80. *
  81. * @param fd
  82. * stream to read the commit-graph file from. The stream must be
  83. * buffered as some small IOs are performed against the stream.
  84. * The caller is responsible for closing the stream.
  85. *
  86. * @return a copy of the commit-graph file in memory
  87. * @throws CommitGraphFormatException
  88. * the commit-graph file's format is different than we expected.
  89. * @throws java.io.IOException
  90. * the stream cannot be read.
  91. */
  92. public static CommitGraphData read(InputStream fd) throws IOException {
  93. byte[] hdr = new byte[8];
  94. IO.readFully(fd, hdr, 0, hdr.length);
  95. int v = hdr[4];
  96. if (v != 1) {
  97. throw new CommitGraphFormatException(MessageFormat.format(
  98. JGitText.get().unsupportedCommitGraphVersion,
  99. Integer.valueOf(v)));
  100. }
  101. return new CommitGraphDataV1(fd, hdr);
  102. }
  103. /**
  104. * Finds the position in the commit-graph of the object.
  105. *
  106. * @param objId
  107. * the id for which the commit-graph position will be found.
  108. * @return the commit-graph id or -1 if the object was not found.
  109. */
  110. public abstract int findGraphPosition(AnyObjectId objId);
  111. /**
  112. * Get the object at the commit-graph position.
  113. *
  114. * @param graphPos
  115. * the position in the commit-graph of the object.
  116. * @return the ObjectId or null if the object was not found.
  117. */
  118. public abstract ObjectId getObjectId(int graphPos);
  119. /**
  120. * Get the metadata of a commit。
  121. *
  122. * @param graphPos
  123. * the position in the commit-graph of the object.
  124. * @return the metadata of a commit or null if it's not found.
  125. */
  126. public abstract CommitGraph.CommitData getCommitData(int graphPos);
  127. /**
  128. * Obtain the total number of commits described by this commit-graph.
  129. *
  130. * @return number of commits in this commit-graph
  131. */
  132. public abstract long getCommitCnt();
  133. /**
  134. * Get the hash length of this commit-graph
  135. *
  136. * @return object hash length
  137. */
  138. public abstract int getHashLength();
  139. static class CommitDataImpl implements CommitGraph.CommitData {
  140. ObjectId tree;
  141. int[] parents;
  142. long commitTime;
  143. int generation;
  144. @Override
  145. public ObjectId getTree() {
  146. return tree;
  147. }
  148. @Override
  149. public int[] getParents() {
  150. return parents;
  151. }
  152. @Override
  153. public long getCommitTime() {
  154. return commitTime;
  155. }
  156. @Override
  157. public int getGeneration() {
  158. return generation;
  159. }
  160. }
  161. }