1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.ignore;
11
12 import static java.nio.charset.StandardCharsets.UTF_8;
13 import static org.eclipse.jgit.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25
26 import org.eclipse.jgit.junit.RepositoryTestCase;
27 import org.eclipse.jgit.lib.FileMode;
28 import org.eclipse.jgit.treewalk.FileTreeIterator;
29 import org.eclipse.jgit.treewalk.TreeWalk;
30 import org.eclipse.jgit.treewalk.WorkingTreeIterator;
31 import org.eclipse.jgit.util.FileUtils;
32 import org.eclipse.jgit.util.SystemReader;
33 import org.junit.After;
34 import org.junit.Test;
35
36
37
38
39 public class IgnoreNodeTest extends RepositoryTestCase {
40 private static final FileMode D = FileMode.TREE;
41
42 private static final FileMode F = FileMode.REGULAR_FILE;
43
44 private static final boolean ignored = true;
45
46 private static final boolean tracked = false;
47
48 private TreeWalk walk;
49
50 @After
51 public void closeWalk() {
52 if (walk != null) {
53 walk.close();
54 }
55 }
56
57 @Test
58 public void testSimpleRootGitIgnoreGlobalIgnore() throws IOException {
59 writeIgnoreFile(".gitignore", "x");
60
61 writeTrashFile("a/x/file", "");
62 writeTrashFile("b/x", "");
63 writeTrashFile("x/file", "");
64
65 beginWalk();
66 assertEntry(F, tracked, ".gitignore");
67 assertEntry(D, tracked, "a");
68 assertEntry(D, ignored, "a/x");
69 assertEntry(F, ignored, "a/x/file");
70 assertEntry(D, tracked, "b");
71 assertEntry(F, ignored, "b/x");
72 assertEntry(D, ignored, "x");
73 assertEntry(F, ignored, "x/file");
74 endWalk();
75 }
76
77 @Test
78 public void testSimpleRootGitIgnoreGlobalDirIgnore() throws IOException {
79 writeIgnoreFile(".gitignore", "x/");
80
81 writeTrashFile("a/x/file", "");
82 writeTrashFile("x/file", "");
83
84 beginWalk();
85 assertEntry(F, tracked, ".gitignore");
86 assertEntry(D, tracked, "a");
87 assertEntry(D, ignored, "a/x");
88 assertEntry(F, ignored, "a/x/file");
89 assertEntry(D, ignored, "x");
90 assertEntry(F, ignored, "x/file");
91 endWalk();
92 }
93
94 @Test
95 public void testSimpleRootGitIgnoreWildMatcher() throws IOException {
96 writeIgnoreFile(".gitignore", "**");
97
98 writeTrashFile("a/x", "");
99 writeTrashFile("y", "");
100
101 beginWalk();
102 assertEntry(F, ignored, ".gitignore");
103 assertEntry(D, ignored, "a");
104 assertEntry(F, ignored, "a/x");
105 assertEntry(F, ignored, "y");
106 endWalk();
107 }
108
109 @Test
110 public void testSimpleRootGitIgnoreWildMatcherDirOnly() throws IOException {
111 writeIgnoreFile(".gitignore", "**/");
112
113 writeTrashFile("a/x", "");
114 writeTrashFile("y", "");
115
116 beginWalk();
117 assertEntry(F, tracked, ".gitignore");
118 assertEntry(D, ignored, "a");
119 assertEntry(F, ignored, "a/x");
120 assertEntry(F, tracked, "y");
121 endWalk();
122 }
123
124 @Test
125 public void testSimpleRootGitIgnoreGlobalNegation1() throws IOException {
126 writeIgnoreFile(".gitignore", "*", "!x*");
127 writeTrashFile("x1", "");
128 writeTrashFile("a/x2", "");
129 writeTrashFile("x3/y", "");
130
131 beginWalk();
132 assertEntry(F, ignored, ".gitignore");
133 assertEntry(D, ignored, "a");
134 assertEntry(F, ignored, "a/x2");
135 assertEntry(F, tracked, "x1");
136 assertEntry(D, tracked, "x3");
137 assertEntry(F, ignored, "x3/y");
138 endWalk();
139 }
140
141 @Test
142 public void testSimpleRootGitIgnoreGlobalNegation2() throws IOException {
143 writeIgnoreFile(".gitignore", "*", "!x*", "!/a");
144 writeTrashFile("x1", "");
145 writeTrashFile("a/x2", "");
146 writeTrashFile("x3/y", "");
147
148 beginWalk();
149 assertEntry(F, ignored, ".gitignore");
150 assertEntry(D, tracked, "a");
151 assertEntry(F, tracked, "a/x2");
152 assertEntry(F, tracked, "x1");
153 assertEntry(D, tracked, "x3");
154 assertEntry(F, ignored, "x3/y");
155 endWalk();
156 }
157
158 @Test
159 public void testSimpleRootGitIgnoreGlobalNegation3() throws IOException {
160 writeIgnoreFile(".gitignore", "*", "!x*", "!x*/**");
161 writeTrashFile("x1", "");
162 writeTrashFile("a/x2", "");
163 writeTrashFile("x3/y", "");
164
165 beginWalk();
166 assertEntry(F, ignored, ".gitignore");
167 assertEntry(D, ignored, "a");
168 assertEntry(F, ignored, "a/x2");
169 assertEntry(F, tracked, "x1");
170 assertEntry(D, tracked, "x3");
171 assertEntry(F, tracked, "x3/y");
172 endWalk();
173 }
174
175 @Test
176 public void testSimpleRootGitIgnoreGlobalNegation4() throws IOException {
177 writeIgnoreFile(".gitignore", "*", "!**/");
178 writeTrashFile("x1", "");
179 writeTrashFile("a/x2", "");
180 writeTrashFile("x3/y", "");
181
182 beginWalk();
183 assertEntry(F, ignored, ".gitignore");
184 assertEntry(D, tracked, "a");
185 assertEntry(F, ignored, "a/x2");
186 assertEntry(F, ignored, "x1");
187 assertEntry(D, tracked, "x3");
188 assertEntry(F, ignored, "x3/y");
189 endWalk();
190 }
191
192 @Test
193 public void testRules() throws IOException {
194 writeIgnoreFile(".git/info/exclude", "*~", "/out");
195
196 writeIgnoreFile(".gitignore", "*.o", "/config");
197 writeTrashFile("config/secret", "");
198 writeTrashFile("mylib.c", "");
199 writeTrashFile("mylib.c~", "");
200 writeTrashFile("mylib.o", "");
201
202 writeTrashFile("out/object/foo.exe", "");
203 writeIgnoreFile("src/config/.gitignore", "lex.out");
204 writeTrashFile("src/config/lex.out", "");
205 writeTrashFile("src/config/config.c", "");
206 writeTrashFile("src/config/config.c~", "");
207 writeTrashFile("src/config/old/lex.out", "");
208
209 beginWalk();
210 assertEntry(F, tracked, ".gitignore");
211 assertEntry(D, ignored, "config");
212 assertEntry(F, ignored, "config/secret");
213 assertEntry(F, tracked, "mylib.c");
214 assertEntry(F, ignored, "mylib.c~");
215 assertEntry(F, ignored, "mylib.o");
216
217 assertEntry(D, ignored, "out");
218 assertEntry(D, ignored, "out/object");
219 assertEntry(F, ignored, "out/object/foo.exe");
220
221 assertEntry(D, tracked, "src");
222 assertEntry(D, tracked, "src/config");
223 assertEntry(F, tracked, "src/config/.gitignore");
224 assertEntry(F, tracked, "src/config/config.c");
225 assertEntry(F, ignored, "src/config/config.c~");
226 assertEntry(F, ignored, "src/config/lex.out");
227 assertEntry(D, tracked, "src/config/old");
228 assertEntry(F, ignored, "src/config/old/lex.out");
229 endWalk();
230 }
231
232 @Test
233 public void testNegation() throws IOException {
234
235 writeIgnoreFile(".gitignore", "*.o", "d");
236
237
238 writeIgnoreFile("src/a/b/.gitignore", "!keep.o");
239 writeTrashFile("src/a/b/keep.o", "");
240 writeTrashFile("src/a/b/nothere.o", "");
241
242
243 writeIgnoreFile("src/c/.gitignore", "!d");
244
245 writeIgnoreFile("src/c/d/.gitignore", "!keep.o");
246 writeTrashFile("src/c/d/keep.o", "");
247 writeTrashFile("src/c/d/nothere.o", "");
248
249 beginWalk();
250 assertEntry(F, tracked, ".gitignore");
251 assertEntry(D, tracked, "src");
252 assertEntry(D, tracked, "src/a");
253 assertEntry(D, tracked, "src/a/b");
254 assertEntry(F, tracked, "src/a/b/.gitignore");
255 assertEntry(F, tracked, "src/a/b/keep.o");
256 assertEntry(F, ignored, "src/a/b/nothere.o");
257
258 assertEntry(D, tracked, "src/c");
259 assertEntry(F, tracked, "src/c/.gitignore");
260 assertEntry(D, tracked, "src/c/d");
261 assertEntry(F, tracked, "src/c/d/.gitignore");
262 assertEntry(F, tracked, "src/c/d/keep.o");
263
264 assertEntry(F, ignored, "src/c/d/nothere.o");
265 endWalk();
266 }
267
268
269
270
271 @Test
272 public void testNegateAllExceptJavaInSrc() throws IOException {
273
274 writeIgnoreFile(".gitignore", "/*", "!/src/");
275 writeTrashFile("nothere.o", "");
276
277
278 writeIgnoreFile("src/.gitignore", "*", "!*.java");
279
280 writeTrashFile("src/keep.java", "");
281 writeTrashFile("src/nothere.o", "");
282 writeTrashFile("src/a/nothere.o", "");
283
284 beginWalk();
285 assertEntry(F, ignored, ".gitignore");
286 assertEntry(F, ignored, "nothere.o");
287 assertEntry(D, tracked, "src");
288 assertEntry(F, ignored, "src/.gitignore");
289 assertEntry(D, ignored, "src/a");
290 assertEntry(F, ignored, "src/a/nothere.o");
291 assertEntry(F, tracked, "src/keep.java");
292 assertEntry(F, ignored, "src/nothere.o");
293 endWalk();
294 }
295
296
297
298
299 @Test
300 public void testNegationAllExceptJavaInSrcAndExceptChildDirInSrc()
301 throws IOException {
302
303 writeIgnoreFile(".gitignore", "/*", "!/src/");
304 writeTrashFile("nothere.o", "");
305
306
307
308 writeIgnoreFile("src/.gitignore", "*", "!*.java", "!*/");
309
310 writeTrashFile("src/keep.java", "");
311 writeTrashFile("src/nothere.o", "");
312 writeTrashFile("src/a/keep.java", "");
313 writeTrashFile("src/a/keep.o", "");
314
315 beginWalk();
316 assertEntry(F, ignored, ".gitignore");
317 assertEntry(F, ignored, "nothere.o");
318 assertEntry(D, tracked, "src");
319 assertEntry(F, ignored, "src/.gitignore");
320 assertEntry(D, tracked, "src/a");
321 assertEntry(F, tracked, "src/a/keep.java");
322 assertEntry(F, ignored, "src/a/keep.o");
323 assertEntry(F, tracked, "src/keep.java");
324 assertEntry(F, ignored, "src/nothere.o");
325 endWalk();
326 }
327
328
329
330
331 @Test
332 public void testRepeatedNegation() throws IOException {
333 writeIgnoreFile(".gitignore", "e", "!e", "e", "!e", "e");
334
335 writeTrashFile("e/nothere.o", "");
336
337 beginWalk();
338 assertEntry(F, tracked, ".gitignore");
339 assertEntry(D, ignored, "e");
340 assertEntry(F, ignored, "e/nothere.o");
341 endWalk();
342 }
343
344
345
346
347 @Test
348 public void testRepeatedNegationInDifferentFiles1() throws IOException {
349 writeIgnoreFile(".gitignore", "*.o", "e");
350
351 writeIgnoreFile("e/.gitignore", "!e");
352 writeTrashFile("e/nothere.o", "");
353
354 beginWalk();
355 assertEntry(F, tracked, ".gitignore");
356 assertEntry(D, ignored, "e");
357 assertEntry(F, ignored, "e/.gitignore");
358 assertEntry(F, ignored, "e/nothere.o");
359 endWalk();
360 }
361
362
363
364
365 @Test
366 public void testRepeatedNegationInDifferentFiles2() throws IOException {
367 writeIgnoreFile(".gitignore", "*.o", "e");
368
369 writeIgnoreFile("a/.gitignore", "!e");
370 writeTrashFile("a/e/nothere.o", "");
371
372 beginWalk();
373 assertEntry(F, tracked, ".gitignore");
374 assertEntry(D, tracked, "a");
375 assertEntry(F, tracked, "a/.gitignore");
376 assertEntry(D, tracked, "a/e");
377 assertEntry(F, ignored, "a/e/nothere.o");
378 endWalk();
379 }
380
381
382
383
384 @Test
385 public void testRepeatedNegationInDifferentFiles3() throws IOException {
386 writeIgnoreFile(".gitignore", "*.o");
387
388 writeIgnoreFile("a/.gitignore", "e");
389 writeIgnoreFile("a/b/.gitignore", "!e");
390 writeTrashFile("a/b/e/nothere.o", "");
391
392 beginWalk();
393 assertEntry(F, tracked, ".gitignore");
394 assertEntry(D, tracked, "a");
395 assertEntry(F, tracked, "a/.gitignore");
396 assertEntry(D, tracked, "a/b");
397 assertEntry(F, tracked, "a/b/.gitignore");
398 assertEntry(D, tracked, "a/b/e");
399 assertEntry(F, ignored, "a/b/e/nothere.o");
400 endWalk();
401 }
402
403 @Test
404 public void testRepeatedNegationInDifferentFiles4() throws IOException {
405 writeIgnoreFile(".gitignore", "*.o");
406
407 writeIgnoreFile("a/.gitignore", "e");
408
409
410 writeIgnoreFile("a/b/.gitignore", "#");
411 writeIgnoreFile("a/b/c/.gitignore", "!e");
412 writeTrashFile("a/b/c/e/nothere.o", "");
413
414 beginWalk();
415 assertEntry(F, tracked, ".gitignore");
416 assertEntry(D, tracked, "a");
417 assertEntry(F, tracked, "a/.gitignore");
418 assertEntry(D, tracked, "a/b");
419 assertEntry(F, tracked, "a/b/.gitignore");
420 assertEntry(D, tracked, "a/b/c");
421 assertEntry(F, tracked, "a/b/c/.gitignore");
422 assertEntry(D, tracked, "a/b/c/e");
423 assertEntry(F, ignored, "a/b/c/e/nothere.o");
424 endWalk();
425 }
426
427 @Test
428 public void testRepeatedNegationInDifferentFiles5() throws IOException {
429 writeIgnoreFile(".gitignore", "e");
430 writeIgnoreFile("a/.gitignore", "e");
431 writeIgnoreFile("a/b/.gitignore", "!e");
432 writeTrashFile("a/b/e/nothere.o", "");
433
434 beginWalk();
435 assertEntry(F, tracked, ".gitignore");
436 assertEntry(D, tracked, "a");
437 assertEntry(F, tracked, "a/.gitignore");
438 assertEntry(D, tracked, "a/b");
439 assertEntry(F, tracked, "a/b/.gitignore");
440 assertEntry(D, tracked, "a/b/e");
441 assertEntry(F, tracked, "a/b/e/nothere.o");
442 endWalk();
443 }
444
445 @Test
446 public void testIneffectiveNegationDifferentLevels1() throws IOException {
447 writeIgnoreFile(".gitignore", "a/b/e/", "!a/b/e/*");
448 writeTrashFile("a/b/e/nothere.o", "");
449
450 beginWalk();
451 assertEntry(F, tracked, ".gitignore");
452 assertEntry(D, tracked, "a");
453 assertEntry(D, tracked, "a/b");
454 assertEntry(D, ignored, "a/b/e");
455 assertEntry(F, ignored, "a/b/e/nothere.o");
456 endWalk();
457 }
458
459 @Test
460 public void testIneffectiveNegationDifferentLevels2() throws IOException {
461 writeIgnoreFile(".gitignore", "a/b/e/");
462 writeIgnoreFile("a/.gitignore", "!b/e/*");
463 writeTrashFile("a/b/e/nothere.o", "");
464
465 beginWalk();
466 assertEntry(F, tracked, ".gitignore");
467 assertEntry(D, tracked, "a");
468 assertEntry(F, tracked, "a/.gitignore");
469 assertEntry(D, tracked, "a/b");
470 assertEntry(D, ignored, "a/b/e");
471 assertEntry(F, ignored, "a/b/e/nothere.o");
472 endWalk();
473 }
474
475 @Test
476 public void testIneffectiveNegationDifferentLevels3() throws IOException {
477 writeIgnoreFile(".gitignore", "a/b/e/");
478 writeIgnoreFile("a/b/.gitignore", "!e/*");
479 writeTrashFile("a/b/e/nothere.o", "");
480
481 beginWalk();
482 assertEntry(F, tracked, ".gitignore");
483 assertEntry(D, tracked, "a");
484 assertEntry(D, tracked, "a/b");
485 assertEntry(F, tracked, "a/b/.gitignore");
486 assertEntry(D, ignored, "a/b/e");
487 assertEntry(F, ignored, "a/b/e/nothere.o");
488 endWalk();
489 }
490
491 @Test
492 public void testIneffectiveNegationDifferentLevels4() throws IOException {
493 writeIgnoreFile(".gitignore", "a/b/e/");
494 writeIgnoreFile("a/b/e/.gitignore", "!*");
495 writeTrashFile("a/b/e/nothere.o", "");
496
497 beginWalk();
498 assertEntry(F, tracked, ".gitignore");
499 assertEntry(D, tracked, "a");
500 assertEntry(D, tracked, "a/b");
501 assertEntry(D, ignored, "a/b/e");
502 assertEntry(F, ignored, "a/b/e/.gitignore");
503 assertEntry(F, ignored, "a/b/e/nothere.o");
504 endWalk();
505 }
506
507 @Test
508 public void testIneffectiveNegationDifferentLevels5() throws IOException {
509 writeIgnoreFile("a/.gitignore", "b/e/");
510 writeIgnoreFile("a/b/.gitignore", "!e/*");
511 writeTrashFile("a/b/e/nothere.o", "");
512
513 beginWalk();
514 assertEntry(D, tracked, "a");
515 assertEntry(F, tracked, "a/.gitignore");
516 assertEntry(D, tracked, "a/b");
517 assertEntry(F, tracked, "a/b/.gitignore");
518 assertEntry(D, ignored, "a/b/e");
519 assertEntry(F, ignored, "a/b/e/nothere.o");
520 endWalk();
521 }
522
523 @Test
524 public void testEmptyIgnoreRules() throws IOException {
525 IgnoreNode node = new IgnoreNode();
526 node.parse(writeToString("", "#", "!", "[[=a=]]"));
527 assertEquals(new ArrayList<>(), node.getRules());
528 node.parse(writeToString(" ", " / "));
529 assertEquals(2, node.getRules().size());
530 }
531
532 @Test
533 public void testSlashOnlyMatchesDirectory() throws IOException {
534 writeIgnoreFile(".gitignore", "out/");
535 writeTrashFile("out", "");
536
537 beginWalk();
538 assertEntry(F, tracked, ".gitignore");
539 assertEntry(F, tracked, "out");
540
541 FileUtils.delete(new File(trash, "out"));
542 writeTrashFile("out/foo", "");
543
544 beginWalk();
545 assertEntry(F, tracked, ".gitignore");
546 assertEntry(D, ignored, "out");
547 assertEntry(F, ignored, "out/foo");
548 endWalk();
549 }
550
551 @Test
552 public void testSlashMatchesDirectory() throws IOException {
553 writeIgnoreFile(".gitignore", "out2/");
554
555 writeTrashFile("out1/out1", "");
556 writeTrashFile("out1/out2", "");
557 writeTrashFile("out2/out1", "");
558 writeTrashFile("out2/out2", "");
559
560 beginWalk();
561 assertEntry(F, tracked, ".gitignore");
562 assertEntry(D, tracked, "out1");
563 assertEntry(F, tracked, "out1/out1");
564 assertEntry(F, tracked, "out1/out2");
565 assertEntry(D, ignored, "out2");
566 assertEntry(F, ignored, "out2/out1");
567 assertEntry(F, ignored, "out2/out2");
568 endWalk();
569 }
570
571 @Test
572 public void testWildcardWithSlashMatchesDirectory() throws IOException {
573 writeIgnoreFile(".gitignore", "out2*/");
574
575 writeTrashFile("out1/out1.txt", "");
576 writeTrashFile("out1/out2", "");
577 writeTrashFile("out1/out2.txt", "");
578 writeTrashFile("out1/out2x/a", "");
579 writeTrashFile("out2/out1.txt", "");
580 writeTrashFile("out2/out2.txt", "");
581 writeTrashFile("out2x/out1.txt", "");
582 writeTrashFile("out2x/out2.txt", "");
583
584 beginWalk();
585 assertEntry(F, tracked, ".gitignore");
586 assertEntry(D, tracked, "out1");
587 assertEntry(F, tracked, "out1/out1.txt");
588 assertEntry(F, tracked, "out1/out2");
589 assertEntry(F, tracked, "out1/out2.txt");
590 assertEntry(D, ignored, "out1/out2x");
591 assertEntry(F, ignored, "out1/out2x/a");
592 assertEntry(D, ignored, "out2");
593 assertEntry(F, ignored, "out2/out1.txt");
594 assertEntry(F, ignored, "out2/out2.txt");
595 assertEntry(D, ignored, "out2x");
596 assertEntry(F, ignored, "out2x/out1.txt");
597 assertEntry(F, ignored, "out2x/out2.txt");
598 endWalk();
599 }
600
601 @Test
602 public void testWithSlashDoesNotMatchInSubDirectory() throws IOException {
603 writeIgnoreFile(".gitignore", "a/b");
604 writeTrashFile("a/a", "");
605 writeTrashFile("a/b", "");
606 writeTrashFile("src/a/a", "");
607 writeTrashFile("src/a/b", "");
608
609 beginWalk();
610 assertEntry(F, tracked, ".gitignore");
611 assertEntry(D, tracked, "a");
612 assertEntry(F, tracked, "a/a");
613 assertEntry(F, ignored, "a/b");
614 assertEntry(D, tracked, "src");
615 assertEntry(D, tracked, "src/a");
616 assertEntry(F, tracked, "src/a/a");
617 assertEntry(F, tracked, "src/a/b");
618 endWalk();
619 }
620
621 @Test
622 public void testNoPatterns() throws IOException {
623 writeIgnoreFile(".gitignore", "", " ", "# comment", "/");
624 writeTrashFile("a/a", "");
625
626 beginWalk();
627 assertEntry(F, tracked, ".gitignore");
628 assertEntry(D, tracked, "a");
629 assertEntry(F, tracked, "a/a");
630 endWalk();
631 }
632
633 @Test
634 public void testLeadingSpaces() throws IOException {
635 writeTrashFile(" a/ a", "");
636 writeTrashFile(" a/ a", "");
637 writeTrashFile(" a/a", "");
638 writeTrashFile(" a/ a", "");
639 writeTrashFile(" a/ a", "");
640 writeTrashFile(" a/a", "");
641 writeIgnoreFile(".gitignore", " a", " a");
642 writeTrashFile("a/ a", "");
643 writeTrashFile("a/ a", "");
644 writeTrashFile("a/a", "");
645
646 beginWalk();
647 assertEntry(D, ignored, " a");
648 assertEntry(F, ignored, " a/ a");
649 assertEntry(F, ignored, " a/ a");
650 assertEntry(F, ignored, " a/a");
651 assertEntry(D, ignored, " a");
652 assertEntry(F, ignored, " a/ a");
653 assertEntry(F, ignored, " a/ a");
654 assertEntry(F, ignored, " a/a");
655 assertEntry(F, tracked, ".gitignore");
656 assertEntry(D, tracked, "a");
657 assertEntry(F, ignored, "a/ a");
658 assertEntry(F, ignored, "a/ a");
659 assertEntry(F, tracked, "a/a");
660 endWalk();
661 }
662
663 @Test
664 public void testTrailingSpaces() throws IOException {
665
666
667 org.junit.Assume.assumeFalse(SystemReader.getInstance().isWindows());
668 writeTrashFile("a /a", "");
669 writeTrashFile("a /a ", "");
670 writeTrashFile("a /a ", "");
671 writeTrashFile("a /a", "");
672 writeTrashFile("a /a ", "");
673 writeTrashFile("a /a ", "");
674 writeTrashFile("a/a", "");
675 writeTrashFile("a/a ", "");
676 writeTrashFile("a/a ", "");
677 writeTrashFile("b/c", "");
678
679 writeIgnoreFile(".gitignore", "a\\ ", "a \\ ", "b/ ");
680
681 beginWalk();
682 assertEntry(F, tracked, ".gitignore");
683 assertEntry(D, ignored, "a ");
684 assertEntry(F, ignored, "a /a");
685 assertEntry(F, ignored, "a /a ");
686 assertEntry(F, ignored, "a /a ");
687 assertEntry(D, ignored, "a ");
688 assertEntry(F, ignored, "a /a");
689 assertEntry(F, ignored, "a /a ");
690 assertEntry(F, ignored, "a /a ");
691 assertEntry(D, tracked, "a");
692 assertEntry(F, tracked, "a/a");
693 assertEntry(F, ignored, "a/a ");
694 assertEntry(F, ignored, "a/a ");
695 assertEntry(D, ignored, "b");
696 assertEntry(F, ignored, "b/c");
697 endWalk();
698 }
699
700 @Test
701 public void testToString() throws Exception {
702 assertEquals(Arrays.asList("").toString(), new IgnoreNode().toString());
703 assertEquals(Arrays.asList("hello").toString(),
704 new IgnoreNode(Arrays.asList(new FastIgnoreRule("hello")))
705 .toString());
706 }
707
708 private void beginWalk() {
709 walk = new TreeWalk(db);
710 FileTreeIterator iter = new FileTreeIterator(db);
711 iter.setWalkIgnoredDirectories(true);
712 walk.addTree(iter);
713 }
714
715 private void endWalk() throws IOException {
716 assertFalse("Not all files tested", walk.next());
717 }
718
719 private void assertEntry(FileMode type, boolean entryIgnored,
720 String pathName) throws IOException {
721 assertTrue("walk has entry", walk.next());
722 assertEquals(pathName, walk.getPathString());
723 assertEquals(type, walk.getFileMode(0));
724
725 WorkingTreeIterator itr = walk.getTree(0, WorkingTreeIterator.class);
726 assertNotNull("has tree", itr);
727 assertEquals("is ignored", entryIgnored, itr.isEntryIgnored());
728 if (D.equals(type))
729 walk.enterSubtree();
730 }
731
732 private void writeIgnoreFile(String name, String... rules)
733 throws IOException {
734 StringBuilder data = new StringBuilder();
735 for (String line : rules)
736 data.append(line + "\n");
737 writeTrashFile(name, data.toString());
738 }
739
740 private InputStream writeToString(String... rules) {
741 StringBuilder data = new StringBuilder();
742 for (String line : rules) {
743 data.append(line + "\n");
744 }
745 return new ByteArrayInputStream(data.toString().getBytes(UTF_8));
746 }
747 }