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.

LogFilterTest.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (C) 2019, John Tipper <John_Tipper@hotmail.com>
  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 java.nio.charset.StandardCharsets.UTF_8;
  45. import static org.junit.Assert.assertEquals;
  46. import java.io.File;
  47. import java.io.PrintWriter;
  48. import java.nio.file.Path;
  49. import java.nio.file.Paths;
  50. import java.util.Iterator;
  51. import org.eclipse.jgit.junit.RepositoryTestCase;
  52. import org.eclipse.jgit.revwalk.RevCommit;
  53. import org.eclipse.jgit.util.FileUtils;
  54. import org.junit.After;
  55. import org.junit.Before;
  56. import org.junit.Test;
  57. /**
  58. * Testing the log command with include and exclude filters
  59. */
  60. public class LogFilterTest extends RepositoryTestCase {
  61. private Git git;
  62. @Before
  63. public void setup() throws Exception {
  64. super.setUp();
  65. git = new Git(db);
  66. // create first file
  67. File file = new File(db.getWorkTree(), "a.txt");
  68. FileUtils.createNewFile(file);
  69. try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
  70. writer.print("content1");
  71. }
  72. // First commit - a.txt file
  73. git.add().addFilepattern("a.txt").call();
  74. git.commit().setMessage("commit1").setCommitter(committer).call();
  75. // create second file
  76. file = new File(db.getWorkTree(), "b.txt");
  77. FileUtils.createNewFile(file);
  78. try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
  79. writer.print("content2");
  80. }
  81. // Second commit - b.txt file
  82. git.add().addFilepattern("b.txt").call();
  83. git.commit().setMessage("commit2").setCommitter(committer).call();
  84. // create third file
  85. Path includeSubdir = Paths.get(db.getWorkTree().toString(),
  86. "subdir-include");
  87. includeSubdir.toFile().mkdirs();
  88. file = Paths.get(includeSubdir.toString(), "c.txt").toFile();
  89. FileUtils.createNewFile(file);
  90. try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
  91. writer.print("content3");
  92. }
  93. // Third commit - c.txt file
  94. git.add().addFilepattern("subdir-include").call();
  95. git.commit().setMessage("commit3").setCommitter(committer).call();
  96. // create fourth file
  97. Path excludeSubdir = Paths.get(db.getWorkTree().toString(),
  98. "subdir-exclude");
  99. excludeSubdir.toFile().mkdirs();
  100. file = Paths.get(excludeSubdir.toString(), "d.txt").toFile();
  101. FileUtils.createNewFile(file);
  102. try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
  103. writer.print("content4");
  104. }
  105. // Fourth commit - d.txt file
  106. git.add().addFilepattern("subdir-exclude").call();
  107. git.commit().setMessage("commit4").setCommitter(committer).call();
  108. }
  109. @After
  110. @Override
  111. public void tearDown() throws Exception {
  112. git.close();
  113. super.tearDown();
  114. }
  115. @Test
  116. public void testLogWithFilterCanDistinguishFilesByPath() throws Exception {
  117. int count = 0;
  118. for (RevCommit c : git.log().addPath("a.txt").call()) {
  119. assertEquals("commit1", c.getFullMessage());
  120. count++;
  121. }
  122. assertEquals(1, count);
  123. count = 0;
  124. for (RevCommit c : git.log().addPath("b.txt").call()) {
  125. assertEquals("commit2", c.getFullMessage());
  126. count++;
  127. }
  128. assertEquals(1, count);
  129. }
  130. @Test
  131. public void testLogWithFilterCanIncludeFilesInDirectory() throws Exception {
  132. int count = 0;
  133. for (RevCommit c : git.log().addPath("subdir-include").call()) {
  134. assertEquals("commit3", c.getFullMessage());
  135. count++;
  136. }
  137. assertEquals(1, count);
  138. }
  139. @Test
  140. public void testLogWithFilterCanExcludeFilesInDirectory() throws Exception {
  141. int count = 0;
  142. Iterator it = git.log().excludePath("subdir-exclude").call().iterator();
  143. while (it.hasNext()) {
  144. it.next();
  145. count++;
  146. }
  147. // of all the commits, we expect to filter out only d.txt
  148. assertEquals(3, count);
  149. }
  150. @Test
  151. public void testLogWithoutFilter() throws Exception {
  152. int count = 0;
  153. for (RevCommit c : git.log().call()) {
  154. assertEquals(committer, c.getCommitterIdent());
  155. count++;
  156. }
  157. assertEquals(4, count);
  158. }
  159. @Test
  160. public void testLogWithFilterCanExcludeAndIncludeFilesInDifferentDirectories()
  161. throws Exception {
  162. int count = 0;
  163. Iterator it = git.log().addPath("subdir-include")
  164. .excludePath("subdir-exclude").call().iterator();
  165. while (it.hasNext()) {
  166. it.next();
  167. count++;
  168. }
  169. // we expect to include c.txt
  170. assertEquals(1, count);
  171. }
  172. @Test
  173. public void testLogWithFilterExcludeAndIncludeSameFileIncludesNothing()
  174. throws Exception {
  175. int count = 0;
  176. Iterator it = git.log().addPath("subdir-exclude")
  177. .excludePath("subdir-exclude").call().iterator();
  178. while (it.hasNext()) {
  179. it.next();
  180. count++;
  181. }
  182. // we expect the exclude to trump everything
  183. assertEquals(0, count);
  184. }
  185. @Test
  186. public void testLogWithFilterCanExcludeFileAndDirectory() throws Exception {
  187. int count = 0;
  188. Iterator it = git.log().excludePath("b.txt")
  189. .excludePath("subdir-exclude").call().iterator();
  190. while (it.hasNext()) {
  191. it.next();
  192. count++;
  193. }
  194. // we expect a.txt and c.txt
  195. assertEquals(2, count);
  196. }
  197. }