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.

CommitGraphWriter.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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.GRAPH_CHUNK_LOOKUP_WIDTH;
  17. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_EXTRA_EDGES_NEEDED;
  18. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_LAST_EDGE;
  19. import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_NO_PARENT;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.nio.ByteBuffer;
  23. import java.text.MessageFormat;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import java.util.Set;
  27. import java.util.Stack;
  28. import org.eclipse.jgit.annotations.NonNull;
  29. import org.eclipse.jgit.errors.MissingObjectException;
  30. import org.eclipse.jgit.internal.JGitText;
  31. import org.eclipse.jgit.lib.CommitGraph;
  32. import org.eclipse.jgit.lib.Constants;
  33. import org.eclipse.jgit.lib.NullProgressMonitor;
  34. import org.eclipse.jgit.lib.ObjectId;
  35. import org.eclipse.jgit.lib.ObjectIdOwnerMap;
  36. import org.eclipse.jgit.lib.ObjectReader;
  37. import org.eclipse.jgit.lib.ProgressMonitor;
  38. import org.eclipse.jgit.lib.Repository;
  39. import org.eclipse.jgit.revwalk.ObjectWalk;
  40. import org.eclipse.jgit.revwalk.RevCommit;
  41. import org.eclipse.jgit.revwalk.RevObject;
  42. import org.eclipse.jgit.revwalk.RevSort;
  43. import org.eclipse.jgit.util.BlockList;
  44. import org.eclipse.jgit.util.NB;
  45. /**
  46. * Writes a commit-graph formatted file.
  47. */
  48. public class CommitGraphWriter {
  49. private static final int COMMIT_GRAPH_VERSION_GENERATED = 1;
  50. private static final int OID_HASH_VERSION = 1;
  51. private static final int GENERATION_NUMBER_MAX = 0x3FFFFFFF;
  52. private static final int MAX_NUM_CHUNKS = 5;
  53. private static final int GRAPH_FANOUT_SIZE = 4 * 256;
  54. private static final int OID_HASH_LENGTH = Constants.OBJECT_ID_LENGTH;
  55. private final ObjectWalk walk;
  56. private List<ObjectToCommitData> commitDataList = new BlockList<>();
  57. private List<ObjectToCommitData> commitDataSortedByName;
  58. private ObjectIdOwnerMap<ObjectToCommitData> commitDataMap = new ObjectIdOwnerMap<>();
  59. private int numExtraEdges;
  60. private boolean computeGeneration;
  61. /**
  62. * Create writer for specified repository.
  63. *
  64. * @param repo
  65. * repository where objects are stored.
  66. */
  67. public CommitGraphWriter(Repository repo) {
  68. this(repo, repo.newObjectReader());
  69. }
  70. /**
  71. * Create writer for specified repository.
  72. *
  73. * @param repo
  74. * repository where objects are stored.
  75. * @param reader
  76. * reader to read from the repository with.
  77. */
  78. public CommitGraphWriter(Repository repo, ObjectReader reader) {
  79. this(new CommitGraphConfig(repo), reader);
  80. }
  81. /**
  82. * Create writer with a specified configuration.
  83. *
  84. * @param cfg
  85. * configuration for the commit-graph writer.
  86. * @param reader
  87. * reader to read from the repository with.
  88. */
  89. public CommitGraphWriter(CommitGraphConfig cfg, ObjectReader reader) {
  90. this.walk = new ObjectWalk(reader);
  91. this.computeGeneration = cfg.isComputeGeneration();
  92. }
  93. /**
  94. * Prepare the list of commits to be written to the commit-graph stream.
  95. *
  96. * @param findingMonitor
  97. * progress monitor to report the number of commits found.
  98. * @param computeGenerationMonitor
  99. * progress monitor to report generation computation work.
  100. * @param wants
  101. * the list of wanted objects, writer walks commits starting at
  102. * these. Must not be {@code null}.
  103. * @throws IOException
  104. */
  105. public void prepareCommitGraph(ProgressMonitor findingMonitor,
  106. ProgressMonitor computeGenerationMonitor,
  107. @NonNull Set<? extends ObjectId> wants) throws IOException {
  108. BlockList<RevCommit> commits = findCommits(findingMonitor, walk, wants);
  109. if (computeGeneration) {
  110. computeGenerationNumbers(computeGenerationMonitor, commits);
  111. }
  112. }
  113. /**
  114. * Write the prepared commits to the supplied stream.
  115. *
  116. * @param writeMonitor
  117. * progress monitor to report the number of items written.
  118. * @param commitGraphStream
  119. * output stream of commit-graph data. The stream should be
  120. * buffered by the caller. The caller is responsible for closing
  121. * the stream.
  122. * @throws IOException
  123. */
  124. public void writeCommitGraph(ProgressMonitor writeMonitor,
  125. OutputStream commitGraphStream) throws IOException {
  126. if (writeMonitor == null) {
  127. writeMonitor = NullProgressMonitor.INSTANCE;
  128. }
  129. ChunkInfo[] chunks = new ChunkInfo[MAX_NUM_CHUNKS];
  130. for (int i = 0; i < chunks.length; i++) {
  131. chunks[i] = new ChunkInfo();
  132. }
  133. int numChunks = 3;
  134. int hashsz = OID_HASH_LENGTH;
  135. long writeCount = 0;
  136. long chunkOffset;
  137. CommitGraphOutPutStream out = new CommitGraphOutPutStream(writeMonitor,
  138. commitGraphStream);
  139. chunks[0].id = CHUNK_ID_OID_FANOUT;
  140. chunks[0].size = GRAPH_FANOUT_SIZE;
  141. writeCount += 256;
  142. chunks[1].id = CHUNK_ID_OID_LOOKUP;
  143. chunks[1].size = hashsz * commitDataList.size();
  144. writeCount += commitDataList.size();
  145. chunks[2].id = CHUNK_ID_COMMIT_DATA;
  146. chunks[2].size = (hashsz + 16) * commitDataList.size();
  147. writeCount += commitDataList.size();
  148. if (numExtraEdges > 0) {
  149. chunks[numChunks].id = CHUNK_ID_EXTRA_EDGE_LIST;
  150. chunks[numChunks].size = numExtraEdges * 4;
  151. writeCount += numExtraEdges;
  152. numChunks++;
  153. }
  154. chunks[numChunks].id = 0;
  155. chunks[numChunks].size = 0L;
  156. beginPhase(MessageFormat.format(JGitText.get().writingOutCommitGraph,
  157. Integer.valueOf(numChunks)), writeMonitor, writeCount);
  158. try {
  159. // write header
  160. out.writeFileHeader(getVersion(), OID_HASH_VERSION, numChunks);
  161. out.flush();
  162. // write chunk lookup
  163. chunkOffset = 8 + (numChunks + 1) * GRAPH_CHUNK_LOOKUP_WIDTH;
  164. for (int i = 0; i <= numChunks; i++) {
  165. ChunkInfo chunk = chunks[i];
  166. ByteBuffer buffer = ByteBuffer
  167. .allocate(GRAPH_CHUNK_LOOKUP_WIDTH);
  168. buffer.putInt(chunk.id);
  169. buffer.putLong(chunkOffset);
  170. out.write(buffer.array());
  171. chunkOffset += chunk.size;
  172. }
  173. // write chunks
  174. for (int i = 0; i < numChunks; i++) {
  175. int chunkId = chunks[i].id;
  176. switch (chunkId) {
  177. case CHUNK_ID_OID_FANOUT:
  178. writeFanoutTable(out);
  179. break;
  180. case CHUNK_ID_OID_LOOKUP:
  181. writeOidLookUp(out, hashsz);
  182. break;
  183. case CHUNK_ID_COMMIT_DATA:
  184. writeCommitData(out, hashsz);
  185. break;
  186. case CHUNK_ID_EXTRA_EDGE_LIST:
  187. writeExtraEdges(out);
  188. break;
  189. }
  190. }
  191. // write check sum
  192. out.write(out.getDigest());
  193. out.flush();
  194. } finally {
  195. endPhase(writeMonitor);
  196. }
  197. }
  198. /**
  199. * Returns commits number that was created by this writer.
  200. *
  201. * @return number of commits.
  202. */
  203. public long getCommitCnt() {
  204. return commitDataList.size();
  205. }
  206. /**
  207. * Whether to compute generation numbers.
  208. *
  209. * Default setting: {@value CommitGraphConfig#DEFAULT_COMPUTE_GENERATION}
  210. *
  211. * @return {@code true} if the writer should compute generation numbers.
  212. */
  213. public boolean isComputeGeneration() {
  214. return computeGeneration;
  215. }
  216. /**
  217. * Whether the writer should compute generation numbers.
  218. *
  219. * Default setting: {@value CommitGraphConfig#DEFAULT_COMPUTE_GENERATION}
  220. *
  221. * @param computeGeneration
  222. * if {@code true} the commits in commit-graph will have the
  223. * computed generation number.
  224. */
  225. public void setComputeGeneration(boolean computeGeneration) {
  226. this.computeGeneration = computeGeneration;
  227. }
  228. /**
  229. * Whether to write the extra edge list.
  230. * <p>
  231. * This list of 4-byte values store the second through nth parents for all
  232. * octopus merges.
  233. *
  234. * @return {@code true} if the writer will write the extra edge list.
  235. */
  236. public boolean willWriteExtraEdgeList() {
  237. return numExtraEdges > 0;
  238. }
  239. private void writeFanoutTable(CommitGraphOutPutStream out)
  240. throws IOException {
  241. byte[] tmp = new byte[4];
  242. int[] fanout = new int[256];
  243. for (ObjectToCommitData oc : commitDataList)
  244. fanout[oc.getFirstByte() & 0xff]++;
  245. for (int i = 1; i < 256; i++)
  246. fanout[i] += fanout[i - 1];
  247. for (int n : fanout) {
  248. NB.encodeInt32(tmp, 0, n);
  249. out.write(tmp, 0, 4);
  250. out.updateMonitor();
  251. }
  252. }
  253. private void writeOidLookUp(CommitGraphOutPutStream out, int hashsz)
  254. throws IOException {
  255. byte[] tmp = new byte[4 + hashsz];
  256. List<ObjectToCommitData> sortedByName = commitDataSortByName();
  257. for (int i = 0; i < sortedByName.size(); i++) {
  258. ObjectToCommitData commitData = sortedByName.get(i);
  259. commitData.setOidPosition(i);
  260. commitData.copyRawTo(tmp, 0);
  261. out.write(tmp, 0, hashsz);
  262. out.updateMonitor();
  263. }
  264. commitDataList = sortedByName;
  265. }
  266. private void writeCommitData(CommitGraphOutPutStream out, int hashsz)
  267. throws IOException {
  268. int numExtraEdges = 0;
  269. byte[] tmp = new byte[hashsz + COMMIT_DATA_EXTRA_LENGTH];
  270. for (ObjectToCommitData oc : commitDataList) {
  271. int edgeValue;
  272. int[] packedDate = new int[2];
  273. RevCommit commit = walk.parseCommit(oc);
  274. ObjectId treeId = commit.getTree();
  275. treeId.copyRawTo(tmp, 0);
  276. RevCommit[] parents = commit.getParents();
  277. if (parents.length == 0) {
  278. edgeValue = GRAPH_NO_PARENT;
  279. } else {
  280. RevCommit parent = parents[0];
  281. edgeValue = getCommitOidPosition(parent);
  282. }
  283. NB.encodeInt32(tmp, hashsz, edgeValue);
  284. if (parents.length == 1) {
  285. edgeValue = GRAPH_NO_PARENT;
  286. } else if (parents.length == 2) {
  287. RevCommit parent = parents[1];
  288. edgeValue = getCommitOidPosition(parent);
  289. } else if (parents.length > 2) {
  290. edgeValue = GRAPH_EXTRA_EDGES_NEEDED | numExtraEdges;
  291. numExtraEdges += parents.length - 1;
  292. }
  293. NB.encodeInt32(tmp, hashsz + 4, edgeValue);
  294. packedDate[0] = 0; // commitTime is an int in JGit now
  295. packedDate[0] |= oc.getGeneration() << 2;
  296. packedDate[1] = commit.getCommitTime();
  297. NB.encodeInt32(tmp, hashsz + 8, packedDate[0]);
  298. NB.encodeInt32(tmp, hashsz + 12, packedDate[1]);
  299. out.write(tmp);
  300. out.updateMonitor();
  301. }
  302. }
  303. private void writeExtraEdges(CommitGraphOutPutStream out)
  304. throws IOException {
  305. byte[] tmp = new byte[4];
  306. for (ObjectToCommitData oc : commitDataList) {
  307. RevCommit commit = walk.parseCommit(oc);
  308. RevCommit[] parents = commit.getParents();
  309. if (parents.length > 2) {
  310. int edgeValue;
  311. for (int n = 1; n < parents.length; n++) {
  312. RevCommit parent = parents[n];
  313. edgeValue = getCommitOidPosition(parent);
  314. if (n == parents.length - 1) {
  315. edgeValue |= GRAPH_LAST_EDGE;
  316. }
  317. NB.encodeInt32(tmp, 0, edgeValue);
  318. out.write(tmp);
  319. out.updateMonitor();
  320. }
  321. }
  322. }
  323. }
  324. private BlockList<RevCommit> findCommits(ProgressMonitor findingMonitor,
  325. ObjectWalk walk, Set<? extends ObjectId> wants) throws IOException {
  326. if (findingMonitor == null) {
  327. findingMonitor = NullProgressMonitor.INSTANCE;
  328. }
  329. for (ObjectId id : wants) {
  330. RevObject o = walk.parseAny(id);
  331. if (o instanceof RevCommit) {
  332. walk.markStart((RevCommit) o);
  333. }
  334. }
  335. walk.sort(RevSort.COMMIT_TIME_DESC);
  336. BlockList<RevCommit> commits = new BlockList<>();
  337. RevCommit c;
  338. beginPhase(JGitText.get().findingCommitsForCommitGraph, findingMonitor,
  339. ProgressMonitor.UNKNOWN);
  340. while ((c = walk.next()) != null) {
  341. findingMonitor.update(1);
  342. commits.add(c);
  343. addCommitData(c);
  344. if (c.getParentCount() > 2) {
  345. numExtraEdges += c.getParentCount() - 1;
  346. }
  347. }
  348. endPhase(findingMonitor);
  349. return commits;
  350. }
  351. private void computeGenerationNumbers(
  352. ProgressMonitor computeGenerationMonitor, List<RevCommit> commits)
  353. throws MissingObjectException {
  354. if (computeGenerationMonitor == null) {
  355. computeGenerationMonitor = NullProgressMonitor.INSTANCE;
  356. }
  357. beginPhase(JGitText.get().computingCommitGeneration,
  358. computeGenerationMonitor, commits.size());
  359. for (RevCommit cmit : commits) {
  360. computeGenerationMonitor.update(1);
  361. int generation = getCommitGeneration(cmit);
  362. if (generation != CommitGraph.GENERATION_NUMBER_ZERO
  363. && generation != CommitGraph.GENERATION_NUMBER_INFINITY) {
  364. continue;
  365. }
  366. Stack<RevCommit> commitStack = new Stack<>();
  367. commitStack.push(cmit);
  368. while (!commitStack.empty()) {
  369. int maxGeneration = 0;
  370. boolean allParentComputed = true;
  371. RevCommit current = commitStack.peek();
  372. RevCommit parent;
  373. for (int i = 0; i < current.getParentCount(); i++) {
  374. parent = current.getParent(i);
  375. generation = getCommitGeneration(parent);
  376. if (generation == CommitGraph.GENERATION_NUMBER_ZERO
  377. || generation == CommitGraph.GENERATION_NUMBER_INFINITY) {
  378. allParentComputed = false;
  379. commitStack.push(parent);
  380. break;
  381. } else if (generation > maxGeneration) {
  382. maxGeneration = generation;
  383. }
  384. }
  385. if (allParentComputed) {
  386. RevCommit commit = commitStack.pop();
  387. generation = maxGeneration + 1;
  388. if (generation > GENERATION_NUMBER_MAX) {
  389. generation = GENERATION_NUMBER_MAX;
  390. }
  391. setCommitGeneration(commit, generation);
  392. }
  393. }
  394. }
  395. endPhase(computeGenerationMonitor);
  396. }
  397. private int getVersion() {
  398. return COMMIT_GRAPH_VERSION_GENERATED;
  399. }
  400. private static class ChunkInfo {
  401. int id;
  402. long size;
  403. }
  404. private int getCommitGeneration(RevCommit commit)
  405. throws MissingObjectException {
  406. ObjectToCommitData oc = commitDataMap.get(commit);
  407. if (oc == null) {
  408. throw new MissingObjectException(commit, Constants.OBJ_COMMIT);
  409. }
  410. return oc.getGeneration();
  411. }
  412. private void setCommitGeneration(RevCommit commit, int generation)
  413. throws MissingObjectException {
  414. ObjectToCommitData oc = commitDataMap.get(commit);
  415. if (oc == null) {
  416. throw new MissingObjectException(commit, Constants.OBJ_COMMIT);
  417. }
  418. oc.setGeneration(generation);
  419. }
  420. private int getCommitOidPosition(RevCommit commit)
  421. throws MissingObjectException {
  422. ObjectToCommitData oc = commitDataMap.get(commit);
  423. if (oc == null) {
  424. throw new MissingObjectException(commit, Constants.OBJ_COMMIT);
  425. }
  426. return oc.getOidPosition();
  427. }
  428. private void addCommitData(RevCommit commit) {
  429. ObjectToCommitData otc = new ObjectToCommitData(commit);
  430. commitDataList.add(otc);
  431. commitDataMap.add(otc);
  432. }
  433. private List<ObjectToCommitData> commitDataSortByName() {
  434. if (commitDataSortedByName == null) {
  435. commitDataSortedByName = new BlockList<>(commitDataList.size());
  436. commitDataSortedByName.addAll(commitDataList);
  437. Collections.sort(commitDataSortedByName);
  438. }
  439. return commitDataSortedByName;
  440. }
  441. private void beginPhase(String task, ProgressMonitor monitor, long cnt) {
  442. monitor.beginTask(task, (int) cnt);
  443. }
  444. private void endPhase(ProgressMonitor monitor) {
  445. monitor.endTask();
  446. }
  447. }