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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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
  98. .format(JGitText.get().unsupportedCommitGraphVersion, v));
  99. }
  100. return new CommitGraphDataV1(fd, hdr);
  101. }
  102. /**
  103. * Finds the position in the commit-graph of the object.
  104. *
  105. * @param objId
  106. * the id for which the commit-graph position will be found.
  107. * @return the commit-graph id or -1 if the object was not found.
  108. */
  109. public abstract int findGraphPosition(AnyObjectId objId);
  110. /**
  111. * Get the object at the commit-graph position.
  112. *
  113. * @param graphPos
  114. * the position in the commit-graph of the object.
  115. * @return the ObjectId or null if the object was not found.
  116. */
  117. public abstract ObjectId getObjectId(int graphPos);
  118. /**
  119. * Get the metadata of a commit。
  120. *
  121. * @param graphPos
  122. * the position in the commit-graph of the object.
  123. * @return the metadata of a commit or null if it's not found.
  124. */
  125. public abstract CommitGraph.CommitData getCommitData(int graphPos);
  126. /**
  127. * Obtain the total number of commits described by this commit-graph.
  128. *
  129. * @return number of commits in this commit-graph
  130. */
  131. public abstract long getCommitCnt();
  132. /**
  133. * Get the hash length of this commit-graph
  134. *
  135. * @return object hash length
  136. */
  137. public abstract int getHashLength();
  138. static class CommitDataImpl extends CommitGraph.CommitData {
  139. ObjectId tree;
  140. int[] parents;
  141. long commitTime;
  142. int generation;
  143. @Override
  144. public ObjectId getTree() {
  145. return tree;
  146. }
  147. @Override
  148. public int[] getParents() {
  149. return parents;
  150. }
  151. @Override
  152. public long getCommitTime() {
  153. return commitTime;
  154. }
  155. @Override
  156. public int getGeneration() {
  157. return generation;
  158. }
  159. }
  160. }