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.

CommitGraph.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.lib;
  11. /**
  12. * The CommitGraph is a supplemental data structure that accelerates commit
  13. * graph walks.
  14. * <p>
  15. * If a user downgrades or disables the <code>core.commitGraph</code> config
  16. * setting, then the existing ODB is sufficient.
  17. * </p>
  18. * <p>
  19. * It stores the commit graph structure along with some extra metadata to speed
  20. * up graph walks. By listing commit OIDs in lexicographic order, we can
  21. * identify an integer position for each commit and refer to the parents of a
  22. * commit using those integer positions. We use binary search to find initial
  23. * commits and then use the integer positions for fast lookups during the walk.
  24. * </p>
  25. *
  26. * @since 5.13
  27. */
  28. public abstract class CommitGraph {
  29. /**
  30. * We use GENERATION_NUMBER_INFINITY(-1) to mark commits not in the
  31. * commit-graph file.
  32. */
  33. public static final int GENERATION_NUMBER_INFINITY = -1;
  34. /**
  35. * If a commit-graph file was written by a version of Git that did not
  36. * compute generation numbers, then those commits will have generation
  37. * number represented by GENERATION_NUMBER_ZERO(0).
  38. */
  39. public static final int GENERATION_NUMBER_ZERO = 0;
  40. /**
  41. * Get the metadata of a commit.
  42. *
  43. * @param commit
  44. * the commit object id to inspect.
  45. * @return the metadata of a commit or null if it's not found.
  46. */
  47. public abstract CommitData getCommitData(AnyObjectId commit);
  48. /**
  49. * Get the metadata of a commit。
  50. *
  51. * @param graphPos
  52. * the position in the commit-graph of the object.
  53. * @return the metadata of a commit or null if it's not found.
  54. */
  55. public abstract CommitData getCommitData(int graphPos);
  56. /**
  57. * Get the object at the commit-graph position.
  58. *
  59. * @param graphPos
  60. * the position in the commit-graph of the object.
  61. * @return the ObjectId or null if it's not found.
  62. */
  63. public abstract ObjectId getObjectId(int graphPos);
  64. /**
  65. * Obtain the total number of commits described by this commit-graph.
  66. *
  67. * @return number of commits in this commit-graph
  68. */
  69. public abstract long getCommitCnt();
  70. /**
  71. * Metadata of a commit in commit data chunk.
  72. */
  73. public abstract static class CommitData {
  74. /**
  75. * Get a reference to this commit's tree.
  76. *
  77. * @return tree of this commit.
  78. */
  79. public abstract ObjectId getTree();
  80. /**
  81. * Obtain an array of all parents.
  82. * <p>
  83. * The method only provides the positions of parents in commit-graph,
  84. * call {@link CommitGraph#getObjectId(int)} to get the real objectId.
  85. *
  86. * @return the array of parents.
  87. */
  88. public abstract int[] getParents();
  89. /**
  90. * Time from the "committer" line.
  91. *
  92. * @return commit time
  93. */
  94. public abstract long getCommitTime();
  95. /**
  96. * Get the generation number of the commit.
  97. * <p>
  98. * If A and B are commits with generation numbers N and M, respectively,
  99. * and N <= M, then A cannot reach B. That is, we know without searching
  100. * that B is not an ancestor of A because it is further from a root
  101. * commit than A.
  102. * <p>
  103. * Conversely, when checking if A is an ancestor of B, then we only need
  104. * to walk commits until all commits on the walk boundary have
  105. * generation number at most N. If we walk commits using a priority
  106. * queue seeded by generation numbers, then we always expand the
  107. * boundary commit with highest generation number and can easily detect
  108. * the stopping condition.
  109. * <p>
  110. * We use {@value #GENERATION_NUMBER_INFINITY} to mark commits not in the
  111. * commit-graph file. If a commit-graph file was written without
  112. * computing generation numbers, then those commits will have generation
  113. * number represented by {@value #GENERATION_NUMBER_ZERO}.
  114. *
  115. * @return the generation number
  116. */
  117. public abstract int getGeneration();
  118. }
  119. }