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 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (C) 2011, GitHub Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import java.util.ArrayList;
  48. import java.util.Iterator;
  49. import java.util.List;
  50. import org.eclipse.jgit.junit.RepositoryTestCase;
  51. import org.eclipse.jgit.lib.PersonIdent;
  52. import org.eclipse.jgit.lib.Ref;
  53. import org.eclipse.jgit.merge.MergeStrategy;
  54. import org.eclipse.jgit.revwalk.RevCommit;
  55. import org.eclipse.jgit.revwalk.filter.RevFilter;
  56. import org.junit.Test;
  57. public class LogCommandTest extends RepositoryTestCase {
  58. @Test
  59. public void logAllCommits() throws Exception {
  60. List<RevCommit> commits = new ArrayList<>();
  61. Git git = Git.wrap(db);
  62. writeTrashFile("Test.txt", "Hello world");
  63. git.add().addFilepattern("Test.txt").call();
  64. commits.add(git.commit().setMessage("initial commit").call());
  65. git.branchCreate().setName("branch1").call();
  66. Ref checkedOut = git.checkout().setName("branch1").call();
  67. assertEquals("refs/heads/branch1", checkedOut.getName());
  68. writeTrashFile("Test1.txt", "Hello world!");
  69. git.add().addFilepattern("Test1.txt").call();
  70. commits.add(git.commit().setMessage("branch1 commit").call());
  71. checkedOut = git.checkout().setName("master").call();
  72. assertEquals("refs/heads/master", checkedOut.getName());
  73. writeTrashFile("Test2.txt", "Hello world!!");
  74. git.add().addFilepattern("Test2.txt").call();
  75. commits.add(git.commit().setMessage("branch1 commit").call());
  76. Iterator<RevCommit> log = git.log().all().call().iterator();
  77. assertTrue(log.hasNext());
  78. assertTrue(commits.contains(log.next()));
  79. assertTrue(log.hasNext());
  80. assertTrue(commits.contains(log.next()));
  81. assertTrue(log.hasNext());
  82. assertTrue(commits.contains(log.next()));
  83. assertFalse(log.hasNext());
  84. }
  85. @Test
  86. public void logAllCommitsWithTag() throws Exception {
  87. List<RevCommit> commits = new ArrayList<>();
  88. Git git = Git.wrap(db);
  89. writeTrashFile("Test.txt", "Hello world");
  90. git.add().addFilepattern("Test.txt").call();
  91. commits.add(git.commit().setMessage("initial commit").call());
  92. TagCommand tagCmd = git.tag();
  93. tagCmd.setName("tagcommit");
  94. tagCmd.setObjectId(commits.get(0));
  95. tagCmd.setTagger(new PersonIdent(db));
  96. Ref tag = tagCmd.call();
  97. tagCmd = git.tag();
  98. tagCmd.setName("tagtree");
  99. tagCmd.setObjectId(commits.get(0).getTree());
  100. tagCmd.setTagger(new PersonIdent(db));
  101. tagCmd.call();
  102. Iterator<RevCommit> log = git.log().all().call().iterator();
  103. assertTrue(log.hasNext());
  104. RevCommit commit = log.next();
  105. tag = db.peel(tag);
  106. assertEquals(commit.getName(), tag.getPeeledObjectId().getName());
  107. assertTrue(commits.contains(commit));
  108. }
  109. private List<RevCommit> createCommits(Git git) throws Exception {
  110. List<RevCommit> commits = new ArrayList<>();
  111. writeTrashFile("Test.txt", "Hello world");
  112. git.add().addFilepattern("Test.txt").call();
  113. commits.add(git.commit().setMessage("commit#1").call());
  114. writeTrashFile("Test.txt", "Hello world!");
  115. git.add().addFilepattern("Test.txt").call();
  116. commits.add(git.commit().setMessage("commit#2").call());
  117. writeTrashFile("Test1.txt", "Hello world!!");
  118. git.add().addFilepattern("Test1.txt").call();
  119. commits.add(git.commit().setMessage("commit#3").call());
  120. return commits;
  121. }
  122. @Test
  123. public void logAllCommitsWithMaxCount() throws Exception {
  124. Git git = Git.wrap(db);
  125. List<RevCommit> commits = createCommits(git);
  126. Iterator<RevCommit> log = git.log().all().setMaxCount(2).call()
  127. .iterator();
  128. assertTrue(log.hasNext());
  129. RevCommit commit = log.next();
  130. assertTrue(commits.contains(commit));
  131. assertEquals("commit#3", commit.getShortMessage());
  132. assertTrue(log.hasNext());
  133. commit = log.next();
  134. assertTrue(commits.contains(commit));
  135. assertEquals("commit#2", commit.getShortMessage());
  136. assertFalse(log.hasNext());
  137. }
  138. @Test
  139. public void logPathWithMaxCount() throws Exception {
  140. Git git = Git.wrap(db);
  141. List<RevCommit> commits = createCommits(git);
  142. Iterator<RevCommit> log = git.log().addPath("Test.txt").setMaxCount(1)
  143. .call().iterator();
  144. assertTrue(log.hasNext());
  145. RevCommit commit = log.next();
  146. assertTrue(commits.contains(commit));
  147. assertEquals("commit#2", commit.getShortMessage());
  148. assertFalse(log.hasNext());
  149. }
  150. @Test
  151. public void logPathWithSkip() throws Exception {
  152. Git git = Git.wrap(db);
  153. List<RevCommit> commits = createCommits(git);
  154. Iterator<RevCommit> log = git.log().addPath("Test.txt").setSkip(1)
  155. .call().iterator();
  156. assertTrue(log.hasNext());
  157. RevCommit commit = log.next();
  158. assertTrue(commits.contains(commit));
  159. assertEquals("commit#1", commit.getShortMessage());
  160. assertFalse(log.hasNext());
  161. }
  162. @Test
  163. public void logAllCommitsWithSkip() throws Exception {
  164. Git git = Git.wrap(db);
  165. List<RevCommit> commits = createCommits(git);
  166. Iterator<RevCommit> log = git.log().all().setSkip(1).call().iterator();
  167. assertTrue(log.hasNext());
  168. RevCommit commit = log.next();
  169. assertTrue(commits.contains(commit));
  170. assertEquals("commit#2", commit.getShortMessage());
  171. assertTrue(log.hasNext());
  172. commit = log.next();
  173. assertTrue(commits.contains(commit));
  174. assertEquals("commit#1", commit.getShortMessage());
  175. assertFalse(log.hasNext());
  176. }
  177. @Test
  178. public void logAllCommitsWithSkipAndMaxCount() throws Exception {
  179. Git git = Git.wrap(db);
  180. List<RevCommit> commits = createCommits(git);
  181. Iterator<RevCommit> log = git.log().all().setSkip(1).setMaxCount(1).call()
  182. .iterator();
  183. assertTrue(log.hasNext());
  184. RevCommit commit = log.next();
  185. assertTrue(commits.contains(commit));
  186. assertEquals("commit#2", commit.getShortMessage());
  187. assertFalse(log.hasNext());
  188. }
  189. @Test
  190. public void logOnlyMergeCommits() throws Exception {
  191. setCommitsAndMerge();
  192. Git git = Git.wrap(db);
  193. Iterable<RevCommit> commits = git.log().all().call();
  194. Iterator<RevCommit> i = commits.iterator();
  195. RevCommit commit = i.next();
  196. assertEquals("merge s0 with m1", commit.getFullMessage());
  197. commit = i.next();
  198. assertEquals("s0", commit.getFullMessage());
  199. commit = i.next();
  200. assertEquals("m1", commit.getFullMessage());
  201. commit = i.next();
  202. assertEquals("m0", commit.getFullMessage());
  203. assertFalse(i.hasNext());
  204. commits = git.log().setRevFilter(RevFilter.ONLY_MERGES).call();
  205. i = commits.iterator();
  206. commit = i.next();
  207. assertEquals("merge s0 with m1", commit.getFullMessage());
  208. assertFalse(i.hasNext());
  209. }
  210. @Test
  211. public void logNoMergeCommits() throws Exception {
  212. setCommitsAndMerge();
  213. Git git = Git.wrap(db);
  214. Iterable<RevCommit> commits = git.log().all().call();
  215. Iterator<RevCommit> i = commits.iterator();
  216. RevCommit commit = i.next();
  217. assertEquals("merge s0 with m1", commit.getFullMessage());
  218. commit = i.next();
  219. assertEquals("s0", commit.getFullMessage());
  220. commit = i.next();
  221. assertEquals("m1", commit.getFullMessage());
  222. commit = i.next();
  223. assertEquals("m0", commit.getFullMessage());
  224. assertFalse(i.hasNext());
  225. commits = git.log().setRevFilter(RevFilter.NO_MERGES).call();
  226. i = commits.iterator();
  227. commit = i.next();
  228. assertEquals("m1", commit.getFullMessage());
  229. commit = i.next();
  230. assertEquals("s0", commit.getFullMessage());
  231. commit = i.next();
  232. assertEquals("m0", commit.getFullMessage());
  233. assertFalse(i.hasNext());
  234. }
  235. private void setCommitsAndMerge() throws Exception {
  236. Git git = Git.wrap(db);
  237. writeTrashFile("file1", "1\n2\n3\n4\n");
  238. git.add().addFilepattern("file1").call();
  239. RevCommit masterCommit0 = git.commit().setMessage("m0").call();
  240. createBranch(masterCommit0, "refs/heads/side");
  241. checkoutBranch("refs/heads/side");
  242. writeTrashFile("file2", "1\n2\n3\n4\n5\n6\n7\n8\n");
  243. git.add().addFilepattern("file2").call();
  244. RevCommit c = git.commit().setMessage("s0").call();
  245. checkoutBranch("refs/heads/master");
  246. writeTrashFile("file3", "1\n2\n");
  247. git.add().addFilepattern("file3").call();
  248. git.commit().setMessage("m1").call();
  249. git.merge().include(c.getId())
  250. .setStrategy(MergeStrategy.RESOLVE)
  251. .setMessage("merge s0 with m1").call();
  252. }
  253. }