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.

LogCommandTest.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (C) 2011, GitHub Inc. 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.api;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertTrue;
  14. import java.util.ArrayList;
  15. import java.util.Iterator;
  16. import java.util.List;
  17. import org.eclipse.jgit.junit.RepositoryTestCase;
  18. import org.eclipse.jgit.lib.PersonIdent;
  19. import org.eclipse.jgit.lib.Ref;
  20. import org.eclipse.jgit.merge.MergeStrategy;
  21. import org.eclipse.jgit.revwalk.RevCommit;
  22. import org.eclipse.jgit.revwalk.filter.RevFilter;
  23. import org.junit.Test;
  24. public class LogCommandTest extends RepositoryTestCase {
  25. @Test
  26. public void logAllCommits() throws Exception {
  27. List<RevCommit> commits = new ArrayList<>();
  28. Git git = Git.wrap(db);
  29. writeTrashFile("Test.txt", "Hello world");
  30. git.add().addFilepattern("Test.txt").call();
  31. commits.add(git.commit().setMessage("initial commit").call());
  32. git.branchCreate().setName("branch1").call();
  33. Ref checkedOut = git.checkout().setName("branch1").call();
  34. assertEquals("refs/heads/branch1", checkedOut.getName());
  35. writeTrashFile("Test1.txt", "Hello world!");
  36. git.add().addFilepattern("Test1.txt").call();
  37. commits.add(git.commit().setMessage("branch1 commit").call());
  38. checkedOut = git.checkout().setName("master").call();
  39. assertEquals("refs/heads/master", checkedOut.getName());
  40. writeTrashFile("Test2.txt", "Hello world!!");
  41. git.add().addFilepattern("Test2.txt").call();
  42. commits.add(git.commit().setMessage("branch1 commit").call());
  43. Iterator<RevCommit> log = git.log().all().call().iterator();
  44. assertTrue(log.hasNext());
  45. assertTrue(commits.contains(log.next()));
  46. assertTrue(log.hasNext());
  47. assertTrue(commits.contains(log.next()));
  48. assertTrue(log.hasNext());
  49. assertTrue(commits.contains(log.next()));
  50. assertFalse(log.hasNext());
  51. }
  52. @Test
  53. public void logAllCommitsWithTag() throws Exception {
  54. List<RevCommit> commits = new ArrayList<>();
  55. Git git = Git.wrap(db);
  56. writeTrashFile("Test.txt", "Hello world");
  57. git.add().addFilepattern("Test.txt").call();
  58. commits.add(git.commit().setMessage("initial commit").call());
  59. TagCommand tagCmd = git.tag();
  60. tagCmd.setName("tagcommit");
  61. tagCmd.setObjectId(commits.get(0));
  62. tagCmd.setTagger(new PersonIdent(db));
  63. Ref tag = tagCmd.call();
  64. tagCmd = git.tag();
  65. tagCmd.setName("tagtree");
  66. tagCmd.setObjectId(commits.get(0).getTree());
  67. tagCmd.setTagger(new PersonIdent(db));
  68. tagCmd.call();
  69. Iterator<RevCommit> log = git.log().all().call().iterator();
  70. assertTrue(log.hasNext());
  71. RevCommit commit = log.next();
  72. tag = db.getRefDatabase().peel(tag);
  73. assertEquals(commit.getName(), tag.getPeeledObjectId().getName());
  74. assertTrue(commits.contains(commit));
  75. }
  76. private List<RevCommit> createCommits(Git git) throws Exception {
  77. List<RevCommit> commits = new ArrayList<>();
  78. writeTrashFile("Test.txt", "Hello world");
  79. git.add().addFilepattern("Test.txt").call();
  80. commits.add(git.commit().setMessage("commit#1").call());
  81. writeTrashFile("Test.txt", "Hello world!");
  82. git.add().addFilepattern("Test.txt").call();
  83. commits.add(git.commit().setMessage("commit#2").call());
  84. writeTrashFile("Test1.txt", "Hello world!!");
  85. git.add().addFilepattern("Test1.txt").call();
  86. commits.add(git.commit().setMessage("commit#3").call());
  87. return commits;
  88. }
  89. @Test
  90. public void logAllCommitsWithMaxCount() throws Exception {
  91. Git git = Git.wrap(db);
  92. List<RevCommit> commits = createCommits(git);
  93. Iterator<RevCommit> log = git.log().all().setMaxCount(2).call()
  94. .iterator();
  95. assertTrue(log.hasNext());
  96. RevCommit commit = log.next();
  97. assertTrue(commits.contains(commit));
  98. assertEquals("commit#3", commit.getShortMessage());
  99. assertTrue(log.hasNext());
  100. commit = log.next();
  101. assertTrue(commits.contains(commit));
  102. assertEquals("commit#2", commit.getShortMessage());
  103. assertFalse(log.hasNext());
  104. }
  105. @Test
  106. public void logPathWithMaxCount() throws Exception {
  107. Git git = Git.wrap(db);
  108. List<RevCommit> commits = createCommits(git);
  109. Iterator<RevCommit> log = git.log().addPath("Test.txt").setMaxCount(1)
  110. .call().iterator();
  111. assertTrue(log.hasNext());
  112. RevCommit commit = log.next();
  113. assertTrue(commits.contains(commit));
  114. assertEquals("commit#2", commit.getShortMessage());
  115. assertFalse(log.hasNext());
  116. }
  117. @Test
  118. public void logPathWithSkip() throws Exception {
  119. Git git = Git.wrap(db);
  120. List<RevCommit> commits = createCommits(git);
  121. Iterator<RevCommit> log = git.log().addPath("Test.txt").setSkip(1)
  122. .call().iterator();
  123. assertTrue(log.hasNext());
  124. RevCommit commit = log.next();
  125. assertTrue(commits.contains(commit));
  126. assertEquals("commit#1", commit.getShortMessage());
  127. assertFalse(log.hasNext());
  128. }
  129. @Test
  130. public void logAllCommitsWithSkip() throws Exception {
  131. Git git = Git.wrap(db);
  132. List<RevCommit> commits = createCommits(git);
  133. Iterator<RevCommit> log = git.log().all().setSkip(1).call().iterator();
  134. assertTrue(log.hasNext());
  135. RevCommit commit = log.next();
  136. assertTrue(commits.contains(commit));
  137. assertEquals("commit#2", commit.getShortMessage());
  138. assertTrue(log.hasNext());
  139. commit = log.next();
  140. assertTrue(commits.contains(commit));
  141. assertEquals("commit#1", commit.getShortMessage());
  142. assertFalse(log.hasNext());
  143. }
  144. @Test
  145. public void logAllCommitsWithSkipAndMaxCount() throws Exception {
  146. Git git = Git.wrap(db);
  147. List<RevCommit> commits = createCommits(git);
  148. Iterator<RevCommit> log = git.log().all().setSkip(1).setMaxCount(1).call()
  149. .iterator();
  150. assertTrue(log.hasNext());
  151. RevCommit commit = log.next();
  152. assertTrue(commits.contains(commit));
  153. assertEquals("commit#2", commit.getShortMessage());
  154. assertFalse(log.hasNext());
  155. }
  156. @Test
  157. public void logOnlyMergeCommits() throws Exception {
  158. setCommitsAndMerge();
  159. Git git = Git.wrap(db);
  160. Iterable<RevCommit> commits = git.log().all().call();
  161. Iterator<RevCommit> i = commits.iterator();
  162. RevCommit commit = i.next();
  163. assertEquals("merge s0 with m1", commit.getFullMessage());
  164. commit = i.next();
  165. assertEquals("s0", commit.getFullMessage());
  166. commit = i.next();
  167. assertEquals("m1", commit.getFullMessage());
  168. commit = i.next();
  169. assertEquals("m0", commit.getFullMessage());
  170. assertFalse(i.hasNext());
  171. commits = git.log().setRevFilter(RevFilter.ONLY_MERGES).call();
  172. i = commits.iterator();
  173. commit = i.next();
  174. assertEquals("merge s0 with m1", commit.getFullMessage());
  175. assertFalse(i.hasNext());
  176. }
  177. @Test
  178. public void logNoMergeCommits() throws Exception {
  179. setCommitsAndMerge();
  180. Git git = Git.wrap(db);
  181. Iterable<RevCommit> commits = git.log().all().call();
  182. Iterator<RevCommit> i = commits.iterator();
  183. RevCommit commit = i.next();
  184. assertEquals("merge s0 with m1", commit.getFullMessage());
  185. commit = i.next();
  186. assertEquals("s0", commit.getFullMessage());
  187. commit = i.next();
  188. assertEquals("m1", commit.getFullMessage());
  189. commit = i.next();
  190. assertEquals("m0", commit.getFullMessage());
  191. assertFalse(i.hasNext());
  192. commits = git.log().setRevFilter(RevFilter.NO_MERGES).call();
  193. i = commits.iterator();
  194. commit = i.next();
  195. assertEquals("m1", commit.getFullMessage());
  196. commit = i.next();
  197. assertEquals("s0", commit.getFullMessage());
  198. commit = i.next();
  199. assertEquals("m0", commit.getFullMessage());
  200. assertFalse(i.hasNext());
  201. }
  202. private void setCommitsAndMerge() throws Exception {
  203. Git git = Git.wrap(db);
  204. writeTrashFile("file1", "1\n2\n3\n4\n");
  205. git.add().addFilepattern("file1").call();
  206. RevCommit masterCommit0 = git.commit().setMessage("m0").call();
  207. createBranch(masterCommit0, "refs/heads/side");
  208. checkoutBranch("refs/heads/side");
  209. writeTrashFile("file2", "1\n2\n3\n4\n5\n6\n7\n8\n");
  210. git.add().addFilepattern("file2").call();
  211. RevCommit c = git.commit().setMessage("s0").call();
  212. checkoutBranch("refs/heads/master");
  213. writeTrashFile("file3", "1\n2\n");
  214. git.add().addFilepattern("file3").call();
  215. git.commit().setMessage("m1").call();
  216. git.merge().include(c.getId())
  217. .setStrategy(MergeStrategy.RESOLVE)
  218. .setMessage("merge s0 with m1").call();
  219. }
  220. }