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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.nio.ByteBuffer;
  24. import java.text.MessageFormat;
  25. import java.util.Collections;
  26. import java.util.List;
  27. import java.util.Set;
  28. import java.util.Stack;
  29. import org.eclipse.jgit.annotations.NonNull;
  30. import org.eclipse.jgit.errors.MissingObjectException;
  31. import org.eclipse.jgit.internal.JGitText;
  32. import org.eclipse.jgit.lib.CommitGraph;
  33. import org.eclipse.jgit.lib.Constants;
  34. import org.eclipse.jgit.lib.NullProgressMonitor;
  35. import org.eclipse.jgit.lib.ObjectId;
  36. import org.eclipse.jgit.lib.ObjectIdOwnerMap;
  37. import org.eclipse.jgit.lib.ObjectReader;
  38. import org.eclipse.jgit.lib.ProgressMonitor;
  39. import org.eclipse.jgit.lib.Repository;
  40. import org.eclipse.jgit.revwalk.ObjectWalk;
  41. import org.eclipse.jgit.revwalk.RevCommit;
  42. import org.eclipse.jgit.revwalk.RevObject;
  43. import org.eclipse.jgit.revwalk.RevSort;
  44. import org.eclipse.jgit.util.BlockList;
  45. import org.eclipse.jgit.util.NB;
  46. /**
  47. * Writes a commit-graph formatted file.
  48. */
  49. public class CommitGraphWriter {
  50. private static final int COMMIT_GRAPH_VERSION_GENERATED = 1;
  51. private static final int OID_HASH_VERSION = 1;
  52. private static final int GENERATION_NUMBER_MAX = 0x3FFFFFFF;
  53. private static final int MAX_NUM_CHUNKS = 5;
  54. private static final int GRAPH_FANOUT_SIZE = 4 * 256;
  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, 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 = OBJECT_ID_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. }
  246. for (int i = 1; i < fanout.length; i++) {
  247. fanout[i] += fanout[i - 1];
  248. }
  249. for (int n : fanout) {
  250. NB.encodeInt32(tmp, 0, n);
  251. out.write(tmp, 0, 4);
  252. out.updateMonitor();
  253. }
  254. }
  255. private void writeOidLookUp(CommitGraphOutPutStream out, int hashsz)
  256. throws IOException {
  257. byte[] tmp = new byte[4 + hashsz];
  258. List<ObjectToCommitData> sortedByName = commitDataSortByName();
  259. for (int i = 0; i < sortedByName.size(); i++) {
  260. ObjectToCommitData commitData = sortedByName.get(i);
  261. commitData.setOidPosition(i);
  262. commitData.copyRawTo(tmp, 0);
  263. out.write(tmp, 0, hashsz);
  264. out.updateMonitor();
  265. }
  266. commitDataList = sortedByName;
  267. }
  268. private void writeCommitData(CommitGraphOutPutStream out, int hashsz)
  269. throws IOException {
  270. int num = 0;
  271. byte[] tmp = new byte[hashsz + COMMIT_DATA_EXTRA_LENGTH];
  272. for (ObjectToCommitData oc : commitDataList) {
  273. int edgeValue;
  274. int[] packedDate = new int[2];
  275. RevCommit commit = walk.parseCommit(oc);
  276. ObjectId treeId = commit.getTree();
  277. treeId.copyRawTo(tmp, 0);
  278. RevCommit[] parents = commit.getParents();
  279. if (parents.length == 0) {
  280. edgeValue = GRAPH_NO_PARENT;
  281. } else {
  282. RevCommit parent = parents[0];
  283. edgeValue = getCommitOidPosition(parent);
  284. }
  285. NB.encodeInt32(tmp, hashsz, edgeValue);
  286. if (parents.length == 1) {
  287. edgeValue = GRAPH_NO_PARENT;
  288. } else if (parents.length == 2) {
  289. RevCommit parent = parents[1];
  290. edgeValue = getCommitOidPosition(parent);
  291. } else if (parents.length > 2) {
  292. edgeValue = GRAPH_EXTRA_EDGES_NEEDED | num;
  293. num += parents.length - 1;
  294. }
  295. NB.encodeInt32(tmp, hashsz + 4, edgeValue);
  296. packedDate[0] = 0; // commitTime is an int in JGit now
  297. packedDate[0] |= oc.getGeneration() << 2;
  298. packedDate[1] = commit.getCommitTime();
  299. NB.encodeInt32(tmp, hashsz + 8, packedDate[0]);
  300. NB.encodeInt32(tmp, hashsz + 12, packedDate[1]);
  301. out.write(tmp);
  302. out.updateMonitor();
  303. }
  304. }
  305. private void writeExtraEdges(CommitGraphOutPutStream out)
  306. throws IOException {
  307. byte[] tmp = new byte[4];
  308. for (ObjectToCommitData oc : commitDataList) {
  309. RevCommit commit = walk.parseCommit(oc);
  310. RevCommit[] parents = commit.getParents();
  311. if (parents.length > 2) {
  312. int edgeValue;
  313. for (int n = 1; n < parents.length; n++) {
  314. RevCommit parent = parents[n];
  315. edgeValue = getCommitOidPosition(parent);
  316. if (n == parents.length - 1) {
  317. edgeValue |= GRAPH_LAST_EDGE;
  318. }
  319. NB.encodeInt32(tmp, 0, edgeValue);
  320. out.write(tmp);
  321. out.updateMonitor();
  322. }
  323. }
  324. }
  325. }
  326. private BlockList<RevCommit> findCommits(ProgressMonitor findingMonitor,
  327. Set<? extends ObjectId> wants) throws IOException {
  328. if (findingMonitor == null) {
  329. findingMonitor = NullProgressMonitor.INSTANCE;
  330. }
  331. for (ObjectId id : wants) {
  332. RevObject o = walk.parseAny(id);
  333. if (o instanceof RevCommit) {
  334. walk.markStart((RevCommit) o);
  335. }
  336. }
  337. walk.sort(RevSort.COMMIT_TIME_DESC);
  338. BlockList<RevCommit> commits = new BlockList<>();
  339. RevCommit c;
  340. beginPhase(JGitText.get().findingCommitsForCommitGraph, findingMonitor,
  341. ProgressMonitor.UNKNOWN);
  342. while ((c = walk.next()) != null) {
  343. findingMonitor.update(1);
  344. commits.add(c);
  345. addCommitData(c);
  346. if (c.getParentCount() > 2) {
  347. numExtraEdges += c.getParentCount() - 1;
  348. }
  349. }
  350. endPhase(findingMonitor);
  351. return commits;
  352. }
  353. private void computeGenerationNumbers(
  354. ProgressMonitor computeGenerationMonitor, List<RevCommit> commits)
  355. throws MissingObjectException {
  356. if (computeGenerationMonitor == null) {
  357. computeGenerationMonitor = NullProgressMonitor.INSTANCE;
  358. }
  359. beginPhase(JGitText.get().computingCommitGeneration,
  360. computeGenerationMonitor, commits.size());
  361. for (RevCommit cmit : commits) {
  362. computeGenerationMonitor.update(1);
  363. int generation = getCommitGeneration(cmit);
  364. if (generation != CommitGraph.GENERATION_NUMBER_ZERO
  365. && generation != CommitGraph.GENERATION_NUMBER_INFINITY) {
  366. continue;
  367. }
  368. Stack<RevCommit> commitStack = new Stack<>();
  369. commitStack.push(cmit);
  370. while (!commitStack.empty()) {
  371. int maxGeneration = 0;
  372. boolean allParentComputed = true;
  373. RevCommit current = commitStack.peek();
  374. RevCommit parent;
  375. for (int i = 0; i < current.getParentCount(); i++) {
  376. parent = current.getParent(i);
  377. generation = getCommitGeneration(parent);
  378. if (generation == CommitGraph.GENERATION_NUMBER_ZERO
  379. || generation == CommitGraph.GENERATION_NUMBER_INFINITY) {
  380. allParentComputed = false;
  381. commitStack.push(parent);
  382. break;
  383. } else if (generation > maxGeneration) {
  384. maxGeneration = generation;
  385. }
  386. }
  387. if (allParentComputed) {
  388. RevCommit commit = commitStack.pop();
  389. generation = maxGeneration + 1;
  390. if (generation > GENERATION_NUMBER_MAX) {
  391. generation = GENERATION_NUMBER_MAX;
  392. }
  393. setCommitGeneration(commit, generation);
  394. }
  395. }
  396. }
  397. endPhase(computeGenerationMonitor);
  398. }
  399. private int getVersion() {
  400. return COMMIT_GRAPH_VERSION_GENERATED;
  401. }
  402. private static class ChunkInfo {
  403. int id;
  404. long size;
  405. }
  406. private int getCommitGeneration(RevCommit commit)
  407. throws MissingObjectException {
  408. ObjectToCommitData oc = commitDataMap.get(commit);
  409. if (oc == null) {
  410. throw new MissingObjectException(commit, Constants.OBJ_COMMIT);
  411. }
  412. return oc.getGeneration();
  413. }
  414. private void setCommitGeneration(RevCommit commit, int generation)
  415. throws MissingObjectException {
  416. ObjectToCommitData oc = commitDataMap.get(commit);
  417. if (oc == null) {
  418. throw new MissingObjectException(commit, Constants.OBJ_COMMIT);
  419. }
  420. oc.setGeneration(generation);
  421. }
  422. private int getCommitOidPosition(RevCommit commit)
  423. throws MissingObjectException {
  424. ObjectToCommitData oc = commitDataMap.get(commit);
  425. if (oc == null) {
  426. throw new MissingObjectException(commit, Constants.OBJ_COMMIT);
  427. }
  428. return oc.getOidPosition();
  429. }
  430. private void addCommitData(RevCommit commit) {
  431. ObjectToCommitData otc = new ObjectToCommitData(commit);
  432. commitDataList.add(otc);
  433. commitDataMap.add(otc);
  434. }
  435. private List<ObjectToCommitData> commitDataSortByName() {
  436. if (commitDataSortedByName == null) {
  437. commitDataSortedByName = new BlockList<>(commitDataList.size());
  438. commitDataSortedByName.addAll(commitDataList);
  439. Collections.sort(commitDataSortedByName);
  440. }
  441. return commitDataSortedByName;
  442. }
  443. private void beginPhase(String task, ProgressMonitor monitor, long cnt) {
  444. monitor.beginTask(task, (int) cnt);
  445. }
  446. private void endPhase(ProgressMonitor monitor) {
  447. monitor.endTask();
  448. }
  449. }