View Javadoc
1   /*
2    * Copyright (C) 2017 Thomas Wolf <thomas.wolf@paranor.ch>
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.ignore;
44  
45  import static java.nio.charset.StandardCharsets.UTF_8;
46  import static org.junit.Assert.assertArrayEquals;
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.assertFalse;
49  
50  import java.io.BufferedInputStream;
51  import java.io.BufferedReader;
52  import java.io.ByteArrayInputStream;
53  import java.io.File;
54  import java.io.IOException;
55  import java.io.InputStreamReader;
56  import java.util.LinkedHashSet;
57  import java.util.Set;
58  
59  import org.eclipse.jgit.junit.RepositoryTestCase;
60  import org.eclipse.jgit.lib.StoredConfig;
61  import org.eclipse.jgit.treewalk.FileTreeIterator;
62  import org.eclipse.jgit.treewalk.TreeWalk;
63  import org.eclipse.jgit.treewalk.WorkingTreeIterator;
64  import org.eclipse.jgit.treewalk.filter.NotIgnoredFilter;
65  import org.eclipse.jgit.util.FS;
66  import org.eclipse.jgit.util.FS.ExecutionResult;
67  import org.eclipse.jgit.util.RawParseUtils;
68  import org.eclipse.jgit.util.TemporaryBuffer;
69  import org.junit.Before;
70  import org.junit.Test;
71  
72  /**
73   * Tests that verify that the set of ignore files in a repository is the same in
74   * JGit and in C-git.
75   */
76  public class CGitIgnoreTest extends RepositoryTestCase {
77  
78  	@Before
79  	public void initRepo() throws IOException {
80  		// These tests focus on .gitignore files inside the repository. Because
81  		// we run C-git, we must ensure that global or user exclude files cannot
82  		// influence the tests. So we set core.excludesFile to an empty file
83  		// inside the repository.
84  		File fakeUserGitignore = writeTrashFile(".fake_user_gitignore", "");
85  		StoredConfig config = db.getConfig();
86  		config.setString("core", null, "excludesFile",
87  				fakeUserGitignore.getAbsolutePath());
88  		// Disable case-insensitivity -- JGit doesn't handle that yet.
89  		config.setBoolean("core", null, "ignoreCase", false);
90  		config.save();
91  	}
92  
93  	private void createFiles(String... paths) throws IOException {
94  		for (String path : paths) {
95  			writeTrashFile(path, "x");
96  		}
97  	}
98  
99  	private String toString(TemporaryBuffer b) throws IOException {
100 		return RawParseUtils.decode(b.toByteArray());
101 	}
102 
103 	private String[] cgitIgnored() throws Exception {
104 		FS fs = db.getFS();
105 		ProcessBuilder builder = fs.runInShell("git", new String[] { "ls-files",
106 				"--ignored", "--exclude-standard", "-o" });
107 		builder.directory(db.getWorkTree());
108 		builder.environment().put("HOME", fs.userHome().getAbsolutePath());
109 		ExecutionResult result = fs.execute(builder,
110 				new ByteArrayInputStream(new byte[0]));
111 		String errorOut = toString(result.getStderr());
112 		assertEquals("External git failed", "exit 0\n",
113 				"exit " + result.getRc() + '\n' + errorOut);
114 		try (BufferedReader r = new BufferedReader(new InputStreamReader(
115 				new BufferedInputStream(result.getStdout().openInputStream()),
116 				UTF_8))) {
117 			return r.lines().toArray(String[]::new);
118 		}
119 	}
120 
121 	private String[] cgitUntracked() throws Exception {
122 		FS fs = db.getFS();
123 		ProcessBuilder builder = fs.runInShell("git",
124 				new String[] { "ls-files", "--exclude-standard", "-o" });
125 		builder.directory(db.getWorkTree());
126 		builder.environment().put("HOME", fs.userHome().getAbsolutePath());
127 		ExecutionResult result = fs.execute(builder,
128 				new ByteArrayInputStream(new byte[0]));
129 		String errorOut = toString(result.getStderr());
130 		assertEquals("External git failed", "exit 0\n",
131 				"exit " + result.getRc() + '\n' + errorOut);
132 		try (BufferedReader r = new BufferedReader(new InputStreamReader(
133 				new BufferedInputStream(result.getStdout().openInputStream()),
134 				UTF_8))) {
135 			return r.lines().toArray(String[]::new);
136 		}
137 	}
138 
139 	private void jgitIgnoredAndUntracked(LinkedHashSet<String> ignored,
140 			LinkedHashSet<String> untracked) throws IOException {
141 		// Do a tree walk that does descend into ignored directories and return
142 		// a list of all ignored files
143 		try (TreeWalk walk = new TreeWalk(db)) {
144 			FileTreeIterator iter = new FileTreeIterator(db);
145 			iter.setWalkIgnoredDirectories(true);
146 			walk.addTree(iter);
147 			walk.setRecursive(true);
148 			while (walk.next()) {
149 				if (walk.getTree(WorkingTreeIterator.class).isEntryIgnored()) {
150 					ignored.add(walk.getPathString());
151 				} else {
152 					// tests of this class won't add any files to the index,
153 					// hence everything what is not ignored is untracked
154 					untracked.add(walk.getPathString());
155 				}
156 			}
157 		}
158 	}
159 
160 	private void assertNoIgnoredVisited(Set<String> ignored) throws Exception {
161 		// Do a recursive tree walk with a NotIgnoredFilter and verify that none
162 		// of the files visited is in the ignored set
163 		try (TreeWalk walk = new TreeWalk(db)) {
164 			walk.addTree(new FileTreeIterator(db));
165 			walk.setFilter(new NotIgnoredFilter(0));
166 			walk.setRecursive(true);
167 			while (walk.next()) {
168 				String path = walk.getPathString();
169 				assertFalse("File " + path + " is ignored, should not appear",
170 						ignored.contains(path));
171 			}
172 		}
173 	}
174 
175 	private void assertSameAsCGit(String... notIgnored) throws Exception {
176 		LinkedHashSet<String> ignored = new LinkedHashSet<>();
177 		LinkedHashSet<String> untracked = new LinkedHashSet<>();
178 		jgitIgnoredAndUntracked(ignored, untracked);
179 		String[] cgit = cgitIgnored();
180 		String[] cgitUntracked = cgitUntracked();
181 		assertArrayEquals(cgit, ignored.toArray());
182 		assertArrayEquals(cgitUntracked, untracked.toArray());
183 		for (String notExcluded : notIgnored) {
184 			assertFalse("File " + notExcluded + " should not be ignored",
185 					ignored.contains(notExcluded));
186 		}
187 		assertNoIgnoredVisited(ignored);
188 	}
189 
190 	@Test
191 	public void testSimpleIgnored() throws Exception {
192 		createFiles("a.txt", "a.tmp", "src/sub/a.txt", "src/a.tmp",
193 				"src/a.txt/b.tmp", "ignored/a.tmp", "ignored/not_ignored/a.tmp",
194 				"ignored/other/a.tmp");
195 		writeTrashFile(".gitignore",
196 				"*.txt\n" + "/ignored/*\n" + "!/ignored/not_ignored");
197 		assertSameAsCGit("ignored/not_ignored/a.tmp");
198 	}
199 
200 	@Test
201 	public void testDirOnlyMatch() throws Exception {
202 		createFiles("a.txt", "src/foo/a.txt", "src/a.txt", "foo/a.txt");
203 		writeTrashFile(".gitignore", "foo/");
204 		assertSameAsCGit();
205 	}
206 
207 	@Test
208 	public void testDirOnlyMatchDeep() throws Exception {
209 		createFiles("a.txt", "src/foo/a.txt", "src/a.txt", "foo/a.txt");
210 		writeTrashFile(".gitignore", "**/foo/");
211 		assertSameAsCGit();
212 	}
213 
214 	@Test
215 	public void testStarMatchOnSlashNot() throws Exception {
216 		createFiles("sub/a.txt", "foo/sext", "foo/s.txt");
217 		writeTrashFile(".gitignore", "s*xt");
218 		assertSameAsCGit("sub/a.txt");
219 	}
220 
221 	@Test
222 	public void testPrefixMatch() throws Exception {
223 		createFiles("src/new/foo.txt");
224 		writeTrashFile(".gitignore", "src/new");
225 		assertSameAsCGit();
226 	}
227 
228 	@Test
229 	public void testDirectoryMatchSubRecursive() throws Exception {
230 		createFiles("src/new/foo.txt", "foo/src/new/foo.txt", "sub/src/new");
231 		writeTrashFile(".gitignore", "**/src/new/");
232 		assertSameAsCGit();
233 	}
234 
235 	@Test
236 	public void testDirectoryMatchSubRecursiveBacktrack() throws Exception {
237 		createFiles("src/new/foo.txt", "src/src/new/foo.txt");
238 		writeTrashFile(".gitignore", "**/src/new/");
239 		assertSameAsCGit();
240 	}
241 
242 	@Test
243 	public void testDirectoryMatchSubRecursiveBacktrack2() throws Exception {
244 		createFiles("src/new/foo.txt", "src/src/new/foo.txt");
245 		writeTrashFile(".gitignore", "**/**/src/new/");
246 		assertSameAsCGit();
247 	}
248 
249 	@Test
250 	public void testDirectoryMatchSubRecursiveBacktrack3() throws Exception {
251 		createFiles("x/a/a/b/foo.txt");
252 		writeTrashFile(".gitignore", "**/*/a/b/");
253 		assertSameAsCGit();
254 	}
255 
256 	@Test
257 	public void testDirectoryMatchSubRecursiveBacktrack4() throws Exception {
258 		createFiles("x/a/a/b/foo.txt", "x/y/z/b/a/b/foo.txt",
259 				"x/y/a/a/a/a/b/foo.txt", "x/y/a/a/a/a/b/a/b/foo.txt");
260 		writeTrashFile(".gitignore", "**/*/a/b bar\n");
261 		assertSameAsCGit();
262 	}
263 
264 	@Test
265 	public void testDirectoryMatchSubRecursiveBacktrack5() throws Exception {
266 		createFiles("x/a/a/b/foo.txt", "x/y/a/b/a/b/foo.txt");
267 		writeTrashFile(".gitignore", "**/*/**/a/b bar\n");
268 		assertSameAsCGit();
269 	}
270 
271 	@Test
272 	public void testDirectoryWildmatchDoesNotMatchFiles1() throws Exception {
273 		createFiles("a", "dir/b", "dir/sub/c");
274 		writeTrashFile(".gitignore", "**/\n");
275 		assertSameAsCGit();
276 	}
277 
278 	@Test
279 	public void testDirectoryWildmatchDoesNotMatchFiles2() throws Exception {
280 		createFiles("a", "dir/b", "dir/sub/c");
281 		writeTrashFile(".gitignore", "**/**/\n");
282 		assertSameAsCGit();
283 	}
284 
285 	@Test
286 	public void testDirectoryWildmatchDoesNotMatchFiles3() throws Exception {
287 		createFiles("a", "x/b", "sub/x/c", "sub/x/d/e");
288 		writeTrashFile(".gitignore", "x/**/\n");
289 		assertSameAsCGit();
290 	}
291 
292 	@Test
293 	public void testDirectoryWildmatchDoesNotMatchFiles4() throws Exception {
294 		createFiles("a", "dir/x", "dir/sub1/x", "dir/sub2/x/y");
295 		writeTrashFile(".gitignore", "**/x/\n");
296 		assertSameAsCGit();
297 	}
298 
299 	@Test
300 	public void testUnescapedBracketsInGroup() throws Exception {
301 		createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]");
302 		writeTrashFile(".gitignore", "[[]]\n");
303 		assertSameAsCGit();
304 	}
305 
306 	@Test
307 	public void testEscapedFirstBracketInGroup() throws Exception {
308 		createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]");
309 		writeTrashFile(".gitignore", "[\\[]]\n");
310 		assertSameAsCGit();
311 	}
312 
313 	@Test
314 	public void testEscapedSecondBracketInGroup() throws Exception {
315 		createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]");
316 		writeTrashFile(".gitignore", "[[\\]]\n");
317 		assertSameAsCGit();
318 	}
319 
320 	@Test
321 	public void testEscapedBothBracketsInGroup() throws Exception {
322 		createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]");
323 		writeTrashFile(".gitignore", "[\\[\\]]\n");
324 		assertSameAsCGit();
325 	}
326 
327 	@Test
328 	public void testSimpleRootGitIgnoreGlobalNegation1() throws Exception {
329 		// see IgnoreNodeTest.testSimpleRootGitIgnoreGlobalNegation1
330 		createFiles("x1", "a/x2", "x3/y");
331 		writeTrashFile(".gitignore", "*\n!x*");
332 		assertSameAsCGit();
333 	}
334 
335 	@Test
336 	public void testRepeatedNegationInDifferentFiles5() throws Exception {
337 		// see IgnoreNodeTest.testRepeatedNegationInDifferentFiles5
338 		createFiles("a/b/e/nothere.o");
339 		writeTrashFile(".gitignore", "e");
340 		writeTrashFile("a/.gitignore", "e");
341 		writeTrashFile("a/b/.gitignore", "!e");
342 		assertSameAsCGit();
343 	}
344 
345 	@Test
346 	public void testRepeatedNegationInDifferentFilesWithWildmatcher1()
347 			throws Exception {
348 		createFiles("e", "x/e/f", "a/e/x1", "a/e/x2", "a/e/y", "a/e/sub/y");
349 		writeTrashFile(".gitignore", "a/e/**");
350 		writeTrashFile("a/.gitignore", "!e/x*");
351 		assertSameAsCGit();
352 	}
353 
354 	@Test
355 	public void testRepeatedNegationInDifferentFilesWithWildmatcher2()
356 			throws Exception {
357 		createFiles("e", "dir/f", "dir/g/h", "a/dir/i", "a/dir/j/k",
358 				"a/b/dir/l", "a/b/dir/m/n", "a/b/dir/m/o/p", "a/q/dir/r",
359 				"a/q/dir/dir/s", "c/d/dir/x", "c/d/dir/y");
360 		writeTrashFile(".gitignore", "**/dir/*");
361 		writeTrashFile("a/.gitignore", "!dir/*");
362 		writeTrashFile("a/b/.gitignore", "!**/dir/*");
363 		writeTrashFile("c/.gitignore", "!d/dir/x");
364 		assertSameAsCGit();
365 	}
366 
367 	@Test
368 	public void testNegationForSubDirectoryWithinIgnoredDirectoryHasNoEffect1()
369 			throws Exception {
370 		createFiles("e", "a/f", "a/b/g", "a/b/h/i");
371 		writeTrashFile(".gitignore", "a/b");
372 		writeTrashFile("a/.gitignore", "!b/*");
373 		assertSameAsCGit();
374 	}
375 
376 	/*
377 	 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=407475
378 	 */
379 	@Test
380 	public void testNegationAllExceptJavaInSrcAndExceptChildDirInSrc()
381 			throws Exception {
382 		// see
383 		// IgnoreNodeTest.testNegationAllExceptJavaInSrcAndExceptChildDirInSrc
384 		createFiles("nothere.o", "src/keep.java", "src/nothere.o",
385 				"src/a/keep.java", "src/a/keep.o");
386 		writeTrashFile(".gitignore", "/*\n!/src/");
387 		writeTrashFile("src/.gitignore", "*\n!*.java\n!*/");
388 		assertSameAsCGit();
389 	}
390 }