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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 2019, John Tipper <John_Tipper@hotmail.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.api;
  11. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import static org.junit.Assert.assertEquals;
  13. import java.io.File;
  14. import java.io.PrintWriter;
  15. import java.nio.file.Path;
  16. import java.nio.file.Paths;
  17. import java.util.Iterator;
  18. import org.eclipse.jgit.junit.RepositoryTestCase;
  19. import org.eclipse.jgit.revwalk.RevCommit;
  20. import org.eclipse.jgit.util.FileUtils;
  21. import org.junit.After;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. /**
  25. * Testing the log command with include and exclude filters
  26. */
  27. public class LogFilterTest extends RepositoryTestCase {
  28. private Git git;
  29. @Before
  30. public void setup() throws Exception {
  31. super.setUp();
  32. git = new Git(db);
  33. // create first file
  34. File file = new File(db.getWorkTree(), "a.txt");
  35. FileUtils.createNewFile(file);
  36. try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
  37. writer.print("content1");
  38. }
  39. // First commit - a.txt file
  40. git.add().addFilepattern("a.txt").call();
  41. git.commit().setMessage("commit1").setCommitter(committer).call();
  42. // create second file
  43. file = new File(db.getWorkTree(), "b.txt");
  44. FileUtils.createNewFile(file);
  45. try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
  46. writer.print("content2");
  47. }
  48. // Second commit - b.txt file
  49. git.add().addFilepattern("b.txt").call();
  50. git.commit().setMessage("commit2").setCommitter(committer).call();
  51. // create third file
  52. Path includeSubdir = Paths.get(db.getWorkTree().toString(),
  53. "subdir-include");
  54. includeSubdir.toFile().mkdirs();
  55. file = Paths.get(includeSubdir.toString(), "c.txt").toFile();
  56. FileUtils.createNewFile(file);
  57. try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
  58. writer.print("content3");
  59. }
  60. // Third commit - c.txt file
  61. git.add().addFilepattern("subdir-include").call();
  62. git.commit().setMessage("commit3").setCommitter(committer).call();
  63. // create fourth file
  64. Path excludeSubdir = Paths.get(db.getWorkTree().toString(),
  65. "subdir-exclude");
  66. excludeSubdir.toFile().mkdirs();
  67. file = Paths.get(excludeSubdir.toString(), "d.txt").toFile();
  68. FileUtils.createNewFile(file);
  69. try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
  70. writer.print("content4");
  71. }
  72. // Fourth commit - d.txt file
  73. git.add().addFilepattern("subdir-exclude").call();
  74. git.commit().setMessage("commit4").setCommitter(committer).call();
  75. }
  76. @After
  77. @Override
  78. public void tearDown() throws Exception {
  79. git.close();
  80. super.tearDown();
  81. }
  82. @Test
  83. public void testLogWithFilterCanDistinguishFilesByPath() throws Exception {
  84. int count = 0;
  85. for (RevCommit c : git.log().addPath("a.txt").call()) {
  86. assertEquals("commit1", c.getFullMessage());
  87. count++;
  88. }
  89. assertEquals(1, count);
  90. count = 0;
  91. for (RevCommit c : git.log().addPath("b.txt").call()) {
  92. assertEquals("commit2", c.getFullMessage());
  93. count++;
  94. }
  95. assertEquals(1, count);
  96. }
  97. @Test
  98. public void testLogWithFilterCanIncludeFilesInDirectory() throws Exception {
  99. int count = 0;
  100. for (RevCommit c : git.log().addPath("subdir-include").call()) {
  101. assertEquals("commit3", c.getFullMessage());
  102. count++;
  103. }
  104. assertEquals(1, count);
  105. }
  106. @Test
  107. public void testLogWithFilterCanExcludeFilesInDirectory() throws Exception {
  108. int count = 0;
  109. Iterator it = git.log().excludePath("subdir-exclude").call().iterator();
  110. while (it.hasNext()) {
  111. it.next();
  112. count++;
  113. }
  114. // of all the commits, we expect to filter out only d.txt
  115. assertEquals(3, count);
  116. }
  117. @Test
  118. public void testLogWithoutFilter() throws Exception {
  119. int count = 0;
  120. for (RevCommit c : git.log().call()) {
  121. assertEquals(committer, c.getCommitterIdent());
  122. count++;
  123. }
  124. assertEquals(4, count);
  125. }
  126. @Test
  127. public void testLogWithFilterCanExcludeAndIncludeFilesInDifferentDirectories()
  128. throws Exception {
  129. int count = 0;
  130. Iterator it = git.log().addPath("subdir-include")
  131. .excludePath("subdir-exclude").call().iterator();
  132. while (it.hasNext()) {
  133. it.next();
  134. count++;
  135. }
  136. // we expect to include c.txt
  137. assertEquals(1, count);
  138. }
  139. @Test
  140. public void testLogWithFilterExcludeAndIncludeSameFileIncludesNothing()
  141. throws Exception {
  142. int count = 0;
  143. Iterator it = git.log().addPath("subdir-exclude")
  144. .excludePath("subdir-exclude").call().iterator();
  145. while (it.hasNext()) {
  146. it.next();
  147. count++;
  148. }
  149. // we expect the exclude to trump everything
  150. assertEquals(0, count);
  151. }
  152. @Test
  153. public void testLogWithFilterCanExcludeFileAndDirectory() throws Exception {
  154. int count = 0;
  155. Iterator it = git.log().excludePath("b.txt")
  156. .excludePath("subdir-exclude").call().iterator();
  157. while (it.hasNext()) {
  158. it.next();
  159. count++;
  160. }
  161. // we expect a.txt and c.txt
  162. assertEquals(2, count);
  163. }
  164. }