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.

CommitGraphDataV1.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.CHUNK_ID_COMMIT_DATA;
  12. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.CHUNK_ID_EXTRA_EDGE_LIST;
  13. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.CHUNK_ID_OID_FANOUT;
  14. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.CHUNK_ID_OID_LOOKUP;
  15. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.COMMIT_DATA_EXTRA_LENGTH;
  16. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.COMMIT_GRAPH_MAGIC;
  17. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_CHUNK_LOOKUP_WIDTH;
  18. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_EDGE_LAST_MASK;
  19. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_EXTRA_EDGES_NEEDED;
  20. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_LAST_EDGE;
  21. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_NO_PARENT;
  22. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.text.MessageFormat;
  26. import java.util.Arrays;
  27. import org.eclipse.jgit.errors.CommitGraphFormatException;
  28. import org.eclipse.jgit.internal.JGitText;
  29. import org.eclipse.jgit.lib.AnyObjectId;
  30. import org.eclipse.jgit.lib.CommitGraph;
  31. import org.eclipse.jgit.lib.ObjectId;
  32. import org.eclipse.jgit.util.IO;
  33. import org.eclipse.jgit.util.NB;
  34. /**
  35. * Support for the commit-graph v1 format.
  36. *
  37. * @see CommitGraphData
  38. */
  39. public class CommitGraphDataV1 extends CommitGraphData {
  40. private static final int FANOUT = 256;
  41. private final long commitCnt;
  42. private final int hashLength;
  43. private final int commitDataLength;
  44. private long[] oidFanout;
  45. private byte[][] oidLookup;
  46. private byte[][] commitData;
  47. private byte[] extraEdgeList;
  48. CommitGraphDataV1(InputStream fd, byte[] hdr) throws IOException {
  49. int magic = NB.decodeInt32(hdr, 0);
  50. if (magic != COMMIT_GRAPH_MAGIC) {
  51. throw new CommitGraphFormatException(
  52. JGitText.get().notACommitGraph);
  53. }
  54. // Read the hash version (1 byte)
  55. // 1 => SHA-1
  56. // 2 => SHA-256 nonsupport now
  57. int hashVersion = hdr[5];
  58. if (hashVersion != 1) {
  59. throw new CommitGraphFormatException(
  60. JGitText.get().incorrectOBJECT_ID_LENGTH);
  61. }
  62. hashLength = OBJECT_ID_LENGTH;
  63. commitDataLength = hashLength + COMMIT_DATA_EXTRA_LENGTH;
  64. // Read the number of "chunkOffsets" (1 byte)
  65. int numberOfChunks = hdr[6];
  66. byte[] chunkLookup = new byte[GRAPH_CHUNK_LOOKUP_WIDTH
  67. * (numberOfChunks + 1)];
  68. IO.readFully(fd, chunkLookup, 0, chunkLookup.length);
  69. int[] chunkId = new int[numberOfChunks + 1];
  70. long[] chunkOffset = new long[numberOfChunks + 1];
  71. for (int i = 0; i <= numberOfChunks; i++) {
  72. chunkId[i] = NB.decodeInt32(chunkLookup, i * 12);
  73. for (int j = 0; j < i; j++) {
  74. if (chunkId[i] == chunkId[j]) {
  75. throw new CommitGraphFormatException(MessageFormat.format(
  76. JGitText.get().commitGraphChunkRepeated,
  77. Integer.toHexString(chunkId[i])));
  78. }
  79. }
  80. chunkOffset[i] = NB.decodeInt64(chunkLookup, i * 12 + 4);
  81. }
  82. oidLookup = new byte[FANOUT][];
  83. commitData = new byte[FANOUT][];
  84. for (int i = 0; i < numberOfChunks; i++) {
  85. long length = chunkOffset[i + 1] - chunkOffset[i];
  86. long lengthReaded;
  87. if (chunkOffset[i] < 0
  88. || chunkOffset[i] > chunkOffset[numberOfChunks]) {
  89. throw new CommitGraphFormatException(MessageFormat.format(
  90. JGitText.get().commitGraphChunkImproperOffset,
  91. Integer.toHexString(chunkId[i]),
  92. Long.valueOf(chunkOffset[i])));
  93. }
  94. switch (chunkId[i]) {
  95. case CHUNK_ID_OID_FANOUT:
  96. lengthReaded = loadChunkOidFanout(fd);
  97. break;
  98. case CHUNK_ID_OID_LOOKUP:
  99. lengthReaded = loadChunkDataBasedOnFanout(fd, hashLength,
  100. oidLookup);
  101. break;
  102. case CHUNK_ID_COMMIT_DATA:
  103. lengthReaded = loadChunkDataBasedOnFanout(fd, commitDataLength,
  104. commitData);
  105. break;
  106. case CHUNK_ID_EXTRA_EDGE_LIST:
  107. lengthReaded = loadChunkExtraEdgeList(fd, length);
  108. break;
  109. default:
  110. throw new CommitGraphFormatException(MessageFormat.format(
  111. JGitText.get().commitGraphChunkUnknown,
  112. Integer.toHexString(chunkId[i])));
  113. }
  114. if (length != lengthReaded) {
  115. throw new CommitGraphFormatException(MessageFormat.format(
  116. JGitText.get().commitGraphChunkImproperOffset,
  117. Integer.toHexString(chunkId[i + 1]),
  118. Long.valueOf(chunkOffset[i + 1])));
  119. }
  120. }
  121. if (oidFanout == null) {
  122. throw new CommitGraphFormatException(
  123. JGitText.get().commitGraphOidFanoutNeeded);
  124. }
  125. commitCnt = oidFanout[FANOUT - 1];
  126. }
  127. private long loadChunkOidFanout(InputStream fd) throws IOException {
  128. int fanoutLen = FANOUT * 4;
  129. byte[] fanoutTable = new byte[fanoutLen];
  130. IO.readFully(fd, fanoutTable, 0, fanoutLen);
  131. oidFanout = new long[FANOUT];
  132. for (int k = 0; k < oidFanout.length; k++) {
  133. oidFanout[k] = NB.decodeUInt32(fanoutTable, k * 4);
  134. }
  135. return fanoutLen;
  136. }
  137. private long loadChunkDataBasedOnFanout(InputStream fd, int itemLength,
  138. byte[][] chunkData) throws IOException {
  139. if (oidFanout == null) {
  140. throw new CommitGraphFormatException(
  141. JGitText.get().commitGraphOidFanoutNeeded);
  142. }
  143. long readedLength = 0;
  144. for (int k = 0; k < oidFanout.length; k++) {
  145. long n;
  146. if (k == 0) {
  147. n = oidFanout[k];
  148. } else {
  149. n = oidFanout[k] - oidFanout[k - 1];
  150. }
  151. if (n > 0) {
  152. long len = n * itemLength;
  153. if (len > Integer.MAX_VALUE - 8) { // http://stackoverflow.com/a/8381338
  154. throw new CommitGraphFormatException(
  155. JGitText.get().commitGraphFileIsTooLargeForJgit);
  156. }
  157. chunkData[k] = new byte[(int) len];
  158. IO.readFully(fd, chunkData[k], 0, chunkData[k].length);
  159. readedLength += len;
  160. }
  161. }
  162. return readedLength;
  163. }
  164. private long loadChunkExtraEdgeList(InputStream fd, long len)
  165. throws IOException {
  166. if (len > Integer.MAX_VALUE - 8) { // http://stackoverflow.com/a/8381338
  167. throw new CommitGraphFormatException(
  168. JGitText.get().commitGraphFileIsTooLargeForJgit);
  169. }
  170. extraEdgeList = new byte[(int) len];
  171. IO.readFully(fd, extraEdgeList, 0, extraEdgeList.length);
  172. return len;
  173. }
  174. /** {@inheritDoc} */
  175. @Override
  176. public int findGraphPosition(AnyObjectId objId) {
  177. int levelOne = objId.getFirstByte();
  178. byte[] data = oidLookup[levelOne];
  179. if (data == null) {
  180. return -1;
  181. }
  182. int high = data.length / (hashLength);
  183. int low = 0;
  184. do {
  185. int mid = (low + high) >>> 1;
  186. int pos = objIdOffset(mid);
  187. int cmp = objId.compareTo(data, pos);
  188. if (cmp < 0) {
  189. high = mid;
  190. } else if (cmp == 0) {
  191. if (levelOne == 0) {
  192. return mid;
  193. }
  194. return (int) (mid + oidFanout[levelOne - 1]);
  195. } else {
  196. low = mid + 1;
  197. }
  198. } while (low < high);
  199. return -1;
  200. }
  201. /** {@inheritDoc} */
  202. @Override
  203. public ObjectId getObjectId(int graphPos) {
  204. if (graphPos < 0 || graphPos > commitCnt) {
  205. return null;
  206. }
  207. int levelOne = findLevelOne(graphPos);
  208. int p = getLevelTwo(graphPos, levelOne);
  209. int dataIdx = objIdOffset(p);
  210. return ObjectId.fromRaw(oidLookup[levelOne], dataIdx);
  211. }
  212. /** {@inheritDoc} */
  213. @Override
  214. public CommitGraph.CommitData getCommitData(int graphPos) {
  215. int levelOne = findLevelOne(graphPos);
  216. int p = getLevelTwo(graphPos, levelOne);
  217. int dataIdx = commitDataOffset(p);
  218. byte[] data = this.commitData[levelOne];
  219. if (graphPos < 0) {
  220. return null;
  221. }
  222. CommitDataImpl commit = new CommitDataImpl();
  223. // parse tree
  224. commit.tree = ObjectId.fromRaw(data, dataIdx);
  225. // parse date
  226. long dateHigh = NB.decodeInt32(data, dataIdx + hashLength + 8) & 0x3;
  227. long dateLow = NB.decodeInt32(data, dataIdx + hashLength + 12);
  228. commit.commitTime = dateHigh << 32 | dateLow;
  229. // parse generation
  230. commit.generation = NB.decodeInt32(data, dataIdx + hashLength + 8) >> 2;
  231. boolean noParents = false;
  232. int[] pList = new int[0];
  233. int edgeValue = NB.decodeInt32(data, dataIdx + hashLength);
  234. if (edgeValue == GRAPH_NO_PARENT) {
  235. noParents = true;
  236. }
  237. // parse parents
  238. if (!noParents) {
  239. pList = new int[1];
  240. int parent = edgeValue;
  241. pList[0] = parent;
  242. edgeValue = NB.decodeInt32(data, dataIdx + hashLength + 4);
  243. if (edgeValue != GRAPH_NO_PARENT) {
  244. if ((edgeValue & GRAPH_EXTRA_EDGES_NEEDED) != 0) {
  245. int pptr = edgeValue & GRAPH_EDGE_LAST_MASK;
  246. int[] eList = findExtraEdgeList(pptr);
  247. if (eList == null) {
  248. return null;
  249. }
  250. int[] old = pList;
  251. pList = new int[eList.length + 1];
  252. pList[0] = old[0];
  253. for (int i = 0; i < eList.length; i++) {
  254. parent = eList[i];
  255. pList[i + 1] = parent;
  256. }
  257. } else {
  258. parent = edgeValue;
  259. pList = new int[] { pList[0], parent };
  260. }
  261. }
  262. }
  263. commit.parents = pList;
  264. return commit;
  265. }
  266. /**
  267. * Find the list of commit-graph position in extra edge list chunk.
  268. * <p>
  269. * The extra edge list chunk store the second through nth parents for all
  270. * octopus merges.
  271. *
  272. * @param pptr
  273. * the start position to iterate of extra edge list chunk
  274. * @return the list of commit-graph position or null if not found
  275. */
  276. int[] findExtraEdgeList(int pptr) {
  277. if (extraEdgeList == null) {
  278. return null;
  279. }
  280. int maxOffset = extraEdgeList.length - 4;
  281. int offset = pptr * 4;
  282. if (offset < 0 || offset > maxOffset) {
  283. return null;
  284. }
  285. int[] pList = new int[32];
  286. int count = 0;
  287. int parentPosition;
  288. for (;;) {
  289. if (count >= pList.length) {
  290. int[] old = pList;
  291. pList = new int[pList.length + 32];
  292. System.arraycopy(old, 0, pList, 0, count);
  293. }
  294. if (offset > maxOffset) {
  295. return null;
  296. }
  297. parentPosition = NB.decodeInt32(extraEdgeList, offset);
  298. if ((parentPosition & GRAPH_LAST_EDGE) != 0) {
  299. pList[count] = parentPosition & GRAPH_EDGE_LAST_MASK;
  300. count++;
  301. break;
  302. }
  303. pList[count++] = parentPosition;
  304. offset += 4;
  305. }
  306. int[] old = pList;
  307. pList = new int[count];
  308. System.arraycopy(old, 0, pList, 0, count);
  309. return pList;
  310. }
  311. /** {@inheritDoc} */
  312. @Override
  313. public long getCommitCnt() {
  314. return commitCnt;
  315. }
  316. /** {@inheritDoc} */
  317. @Override
  318. public int getHashLength() {
  319. return hashLength;
  320. }
  321. private int findLevelOne(long nthPosition) {
  322. int levelOne = Arrays.binarySearch(oidFanout, nthPosition + 1);
  323. if (levelOne >= 0) {
  324. // If we hit the bucket exactly the item is in the bucket, or
  325. // any bucket before it which has the same object count.
  326. //
  327. long base = oidFanout[levelOne];
  328. while (levelOne > 0 && base == oidFanout[levelOne - 1]) {
  329. levelOne--;
  330. }
  331. } else {
  332. // The item is in the bucket we would insert it into.
  333. //
  334. levelOne = -(levelOne + 1);
  335. }
  336. return levelOne;
  337. }
  338. private int getLevelTwo(long nthPosition, int levelOne) {
  339. long base = levelOne > 0 ? oidFanout[levelOne - 1] : 0;
  340. return (int) (nthPosition - base);
  341. }
  342. private int objIdOffset(int mid) {
  343. return hashLength * mid;
  344. }
  345. private int commitDataOffset(int mid) {
  346. return commitDataLength * mid;
  347. }
  348. }