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.

RevWalkShallowTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2012, Marc Strapetz <marc.strapetz@syntevo.com> and others
  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.revwalk;
  11. import static org.junit.Assert.assertNull;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import org.eclipse.jgit.junit.JGitTestUtil;
  15. import org.eclipse.jgit.lib.ObjectId;
  16. import org.junit.Test;
  17. public class RevWalkShallowTest extends RevWalkTestCase {
  18. // Accessing ==============================================================
  19. @Test
  20. public void testDepth1() throws Exception {
  21. RevCommit[] commits = setupLinearChain();
  22. createShallowFile(commits[3]);
  23. updateCommits(commits);
  24. rw.markStart(commits[3]);
  25. assertCommit(commits[3], rw.next());
  26. assertNull(rw.next());
  27. }
  28. @Test
  29. public void testDepth2() throws Exception {
  30. RevCommit[] commits = setupLinearChain();
  31. createShallowFile(commits[2]);
  32. updateCommits(commits);
  33. rw.markStart(commits[3]);
  34. assertCommit(commits[3], rw.next());
  35. assertCommit(commits[2], rw.next());
  36. assertNull(rw.next());
  37. }
  38. @Test
  39. public void testDepth3() throws Exception {
  40. RevCommit[] commits = setupLinearChain();
  41. createShallowFile(commits[1]);
  42. updateCommits(commits);
  43. rw.markStart(commits[3]);
  44. assertCommit(commits[3], rw.next());
  45. assertCommit(commits[2], rw.next());
  46. assertCommit(commits[1], rw.next());
  47. assertNull(rw.next());
  48. }
  49. @Test
  50. public void testObjectDirectorySnapshot() throws Exception {
  51. RevCommit[] commits = setupLinearChain();
  52. createShallowFile(commits[3]);
  53. updateCommits(commits);
  54. markStart(commits[3]);
  55. assertCommit(commits[3], rw.next());
  56. assertNull(rw.next());
  57. createShallowFile(commits[2]);
  58. updateCommits(commits);
  59. markStart(commits[3]);
  60. assertCommit(commits[3], rw.next());
  61. assertCommit(commits[2], rw.next());
  62. assertNull(rw.next());
  63. }
  64. private RevCommit[] setupLinearChain() throws Exception {
  65. RevCommit[] commits = new RevCommit[4];
  66. RevCommit parent = null;
  67. for (int i = 0; i < commits.length; i++) {
  68. commits[i] = parent != null ? commit(parent) : commit();
  69. parent = commits[i];
  70. }
  71. return commits;
  72. }
  73. @Test
  74. public void testMergeCommitOneParentShallow() throws Exception {
  75. RevCommit[] commits = setupMergeChain();
  76. createShallowFile(commits[4]);
  77. updateCommits(commits);
  78. markStart(commits[5]);
  79. assertCommit(commits[5], rw.next());
  80. assertCommit(commits[4], rw.next());
  81. assertCommit(commits[2], rw.next());
  82. assertCommit(commits[1], rw.next());
  83. assertCommit(commits[0], rw.next());
  84. assertNull(rw.next());
  85. }
  86. @Test
  87. public void testMergeCommitEntirelyShallow() throws Exception {
  88. RevCommit[] commits = setupMergeChain();
  89. createShallowFile(commits[2], commits[4]);
  90. updateCommits(commits);
  91. markStart(commits[5]);
  92. assertCommit(commits[5], rw.next());
  93. assertCommit(commits[4], rw.next());
  94. assertCommit(commits[2], rw.next());
  95. assertNull(rw.next());
  96. }
  97. private RevCommit[] setupMergeChain() throws Exception {
  98. /*-
  99. * Create a history like this, diverging at 1 and merging at 5:
  100. *
  101. * ---o--o commits 3,4
  102. * / \
  103. * o--o--o------o commits 0,1,2,5
  104. */
  105. RevCommit[] commits = new RevCommit[6];
  106. commits[0] = commit();
  107. commits[1] = commit(commits[0]);
  108. commits[2] = commit(commits[1]);
  109. commits[3] = commit(commits[1]);
  110. commits[4] = commit(commits[3]);
  111. commits[5] = commit(commits[2], commits[4]);
  112. return commits;
  113. }
  114. private void updateCommits(RevCommit[] commits) {
  115. // Relookup commits using the new RevWalk
  116. for (int i = 0; i < commits.length; i++) {
  117. commits[i] = rw.lookupCommit(commits[i].getId());
  118. }
  119. }
  120. private void createShallowFile(ObjectId... shallowCommits)
  121. throws IOException {
  122. // Reset the RevWalk since the new shallow file invalidates the existing
  123. // RevWalk's shallow state.
  124. rw.close();
  125. rw = createRevWalk();
  126. StringBuilder builder = new StringBuilder();
  127. for (ObjectId commit : shallowCommits) {
  128. builder.append(commit.getName() + "\n");
  129. }
  130. JGitTestUtil.write(new File(db.getDirectory(), "shallow"),
  131. builder.toString());
  132. }
  133. @Test
  134. public void testShallowCommitParse() throws Exception {
  135. RevCommit a = commit();
  136. RevCommit b = commit(a);
  137. createShallowFile(b);
  138. rw.close();
  139. rw = createRevWalk();
  140. b = rw.parseCommit(b);
  141. markStart(b);
  142. assertCommit(b, rw.next());
  143. assertNull(rw.next());
  144. }
  145. }