1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.api;
11
12 import static java.nio.charset.StandardCharsets.UTF_8;
13 import static org.junit.Assert.assertEquals;
14
15 import java.io.File;
16 import java.io.PrintWriter;
17 import java.nio.file.Path;
18 import java.nio.file.Paths;
19 import java.util.Iterator;
20
21 import org.eclipse.jgit.junit.RepositoryTestCase;
22 import org.eclipse.jgit.revwalk.RevCommit;
23 import org.eclipse.jgit.util.FileUtils;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27
28
29
30
31 public class LogFilterTest extends RepositoryTestCase {
32 private Git git;
33
34 @Before
35 public void setup() throws Exception {
36 super.setUp();
37 git = new Git(db);
38
39
40 File file = new File(db.getWorkTree(), "a.txt");
41 FileUtils.createNewFile(file);
42 try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
43 writer.print("content1");
44 }
45
46
47 git.add().addFilepattern("a.txt").call();
48 git.commit().setMessage("commit1").setCommitter(committer).call();
49
50
51 file = new File(db.getWorkTree(), "b.txt");
52 FileUtils.createNewFile(file);
53 try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
54 writer.print("content2");
55 }
56
57
58 git.add().addFilepattern("b.txt").call();
59 git.commit().setMessage("commit2").setCommitter(committer).call();
60
61
62 Path includeSubdir = Paths.get(db.getWorkTree().toString(),
63 "subdir-include");
64 includeSubdir.toFile().mkdirs();
65 file = Paths.get(includeSubdir.toString(), "c.txt").toFile();
66 FileUtils.createNewFile(file);
67 try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
68 writer.print("content3");
69 }
70
71
72 git.add().addFilepattern("subdir-include").call();
73 git.commit().setMessage("commit3").setCommitter(committer).call();
74
75
76 Path excludeSubdir = Paths.get(db.getWorkTree().toString(),
77 "subdir-exclude");
78 excludeSubdir.toFile().mkdirs();
79 file = Paths.get(excludeSubdir.toString(), "d.txt").toFile();
80 FileUtils.createNewFile(file);
81 try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
82 writer.print("content4");
83 }
84
85
86 git.add().addFilepattern("subdir-exclude").call();
87 git.commit().setMessage("commit4").setCommitter(committer).call();
88 }
89
90 @After
91 @Override
92 public void tearDown() throws Exception {
93 git.close();
94 super.tearDown();
95 }
96
97 @Test
98 public void testLogWithFilterCanDistinguishFilesByPath() throws Exception {
99 int count = 0;
100 for (RevCommit c : git.log().addPath("a.txt").call()) {
101 assertEquals("commit1", c.getFullMessage());
102 count++;
103 }
104 assertEquals(1, count);
105
106 count = 0;
107 for (RevCommit c : git.log().addPath("b.txt").call()) {
108 assertEquals("commit2", c.getFullMessage());
109 count++;
110 }
111 assertEquals(1, count);
112 }
113
114 @Test
115 public void testLogWithFilterCanIncludeFilesInDirectory() throws Exception {
116 int count = 0;
117 for (RevCommit c : git.log().addPath("subdir-include").call()) {
118 assertEquals("commit3", c.getFullMessage());
119 count++;
120 }
121 assertEquals(1, count);
122 }
123
124 @Test
125 public void testLogWithFilterCanExcludeFilesInDirectory() throws Exception {
126 int count = 0;
127 Iterator it = git.log().excludePath("subdir-exclude").call().iterator();
128 while (it.hasNext()) {
129 it.next();
130 count++;
131 }
132
133 assertEquals(3, count);
134 }
135
136 @Test
137 public void testLogWithoutFilter() throws Exception {
138 int count = 0;
139 for (RevCommit c : git.log().call()) {
140 assertEquals(committer, c.getCommitterIdent());
141 count++;
142 }
143 assertEquals(4, count);
144 }
145
146 @Test
147 public void testLogWithFilterCanExcludeAndIncludeFilesInDifferentDirectories()
148 throws Exception {
149 int count = 0;
150 Iterator it = git.log().addPath("subdir-include")
151 .excludePath("subdir-exclude").call().iterator();
152 while (it.hasNext()) {
153 it.next();
154 count++;
155 }
156
157 assertEquals(1, count);
158 }
159
160 @Test
161 public void testLogWithFilterExcludeAndIncludeSameFileIncludesNothing()
162 throws Exception {
163 int count = 0;
164 Iterator it = git.log().addPath("subdir-exclude")
165 .excludePath("subdir-exclude").call().iterator();
166
167 while (it.hasNext()) {
168 it.next();
169 count++;
170 }
171
172 assertEquals(0, count);
173 }
174
175 @Test
176 public void testLogWithFilterCanExcludeFileAndDirectory() throws Exception {
177 int count = 0;
178 Iterator it = git.log().excludePath("b.txt")
179 .excludePath("subdir-exclude").call().iterator();
180
181 while (it.hasNext()) {
182 it.next();
183 count++;
184 }
185
186 assertEquals(2, count);
187 }
188 }