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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.revwalk.RevCommit;
  54. import org.junit.Test;
  55. public class LogCommandTest extends RepositoryTestCase {
  56. @Test
  57. public void logAllCommits() throws Exception {
  58. List<RevCommit> commits = new ArrayList<RevCommit>();
  59. Git git = Git.wrap(db);
  60. writeTrashFile("Test.txt", "Hello world");
  61. git.add().addFilepattern("Test.txt").call();
  62. commits.add(git.commit().setMessage("initial commit").call());
  63. git.branchCreate().setName("branch1").call();
  64. Ref checkedOut = git.checkout().setName("branch1").call();
  65. assertEquals("refs/heads/branch1", checkedOut.getName());
  66. writeTrashFile("Test1.txt", "Hello world!");
  67. git.add().addFilepattern("Test1.txt").call();
  68. commits.add(git.commit().setMessage("branch1 commit").call());
  69. checkedOut = git.checkout().setName("master").call();
  70. assertEquals("refs/heads/master", checkedOut.getName());
  71. writeTrashFile("Test2.txt", "Hello world!!");
  72. git.add().addFilepattern("Test2.txt").call();
  73. commits.add(git.commit().setMessage("branch1 commit").call());
  74. Iterator<RevCommit> log = git.log().all().call().iterator();
  75. assertTrue(log.hasNext());
  76. assertTrue(commits.contains(log.next()));
  77. assertTrue(log.hasNext());
  78. assertTrue(commits.contains(log.next()));
  79. assertTrue(log.hasNext());
  80. assertTrue(commits.contains(log.next()));
  81. assertFalse(log.hasNext());
  82. }
  83. @Test
  84. public void logAllCommitsWithTag() throws Exception {
  85. List<RevCommit> commits = new ArrayList<RevCommit>();
  86. Git git = Git.wrap(db);
  87. writeTrashFile("Test.txt", "Hello world");
  88. git.add().addFilepattern("Test.txt").call();
  89. commits.add(git.commit().setMessage("initial commit").call());
  90. TagCommand tagCmd = git.tag();
  91. tagCmd.setName("tagcommit");
  92. tagCmd.setObjectId(commits.get(0));
  93. tagCmd.setTagger(new PersonIdent(db));
  94. Ref tag = tagCmd.call();
  95. tagCmd = git.tag();
  96. tagCmd.setName("tagtree");
  97. tagCmd.setObjectId(commits.get(0).getTree());
  98. tagCmd.setTagger(new PersonIdent(db));
  99. tagCmd.call();
  100. Iterator<RevCommit> log = git.log().all().call().iterator();
  101. assertTrue(log.hasNext());
  102. RevCommit commit = log.next();
  103. tag = db.peel(tag);
  104. assertEquals(commit.getName(), tag.getPeeledObjectId().getName());
  105. assertTrue(commits.contains(commit));
  106. }
  107. private List<RevCommit> createCommits(Git git) throws Exception {
  108. List<RevCommit> commits = new ArrayList<RevCommit>();
  109. writeTrashFile("Test.txt", "Hello world");
  110. git.add().addFilepattern("Test.txt").call();
  111. commits.add(git.commit().setMessage("commit#1").call());
  112. writeTrashFile("Test.txt", "Hello world!");
  113. git.add().addFilepattern("Test.txt").call();
  114. commits.add(git.commit().setMessage("commit#2").call());
  115. writeTrashFile("Test1.txt", "Hello world!!");
  116. git.add().addFilepattern("Test1.txt").call();
  117. commits.add(git.commit().setMessage("commit#3").call());
  118. return commits;
  119. }
  120. @Test
  121. public void logAllCommitsWithMaxCount() throws Exception {
  122. Git git = Git.wrap(db);
  123. List<RevCommit> commits = createCommits(git);
  124. Iterator<RevCommit> log = git.log().all().setMaxCount(2).call()
  125. .iterator();
  126. assertTrue(log.hasNext());
  127. RevCommit commit = log.next();
  128. assertTrue(commits.contains(commit));
  129. assertEquals("commit#3", commit.getShortMessage());
  130. assertTrue(log.hasNext());
  131. commit = log.next();
  132. assertTrue(commits.contains(commit));
  133. assertEquals("commit#2", commit.getShortMessage());
  134. assertFalse(log.hasNext());
  135. }
  136. @Test
  137. public void logPathWithMaxCount() throws Exception {
  138. Git git = Git.wrap(db);
  139. List<RevCommit> commits = createCommits(git);
  140. Iterator<RevCommit> log = git.log().addPath("Test.txt").setMaxCount(1)
  141. .call().iterator();
  142. assertTrue(log.hasNext());
  143. RevCommit commit = log.next();
  144. assertTrue(commits.contains(commit));
  145. assertEquals("commit#2", commit.getShortMessage());
  146. assertFalse(log.hasNext());
  147. }
  148. @Test
  149. public void logPathWithSkip() throws Exception {
  150. Git git = Git.wrap(db);
  151. List<RevCommit> commits = createCommits(git);
  152. Iterator<RevCommit> log = git.log().addPath("Test.txt").setSkip(1)
  153. .call().iterator();
  154. assertTrue(log.hasNext());
  155. RevCommit commit = log.next();
  156. assertTrue(commits.contains(commit));
  157. assertEquals("commit#1", commit.getShortMessage());
  158. assertFalse(log.hasNext());
  159. }
  160. @Test
  161. public void logAllCommitsWithSkip() throws Exception {
  162. Git git = Git.wrap(db);
  163. List<RevCommit> commits = createCommits(git);
  164. Iterator<RevCommit> log = git.log().all().setSkip(1).call().iterator();
  165. assertTrue(log.hasNext());
  166. RevCommit commit = log.next();
  167. assertTrue(commits.contains(commit));
  168. assertEquals("commit#2", commit.getShortMessage());
  169. assertTrue(log.hasNext());
  170. commit = log.next();
  171. assertTrue(commits.contains(commit));
  172. assertEquals("commit#1", commit.getShortMessage());
  173. assertFalse(log.hasNext());
  174. }
  175. @Test
  176. public void logAllCommitsWithSkipAndMaxCount() throws Exception {
  177. Git git = Git.wrap(db);
  178. List<RevCommit> commits = createCommits(git);
  179. Iterator<RevCommit> log = git.log().all().setSkip(1).setMaxCount(1).call()
  180. .iterator();
  181. assertTrue(log.hasNext());
  182. RevCommit commit = log.next();
  183. assertTrue(commits.contains(commit));
  184. assertEquals("commit#2", commit.getShortMessage());
  185. assertFalse(log.hasNext());
  186. }
  187. }