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.

IgnoreNodeTest.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2010, Red Hat Inc.
  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. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotNull;
  46. import static org.junit.Assert.assertTrue;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import org.eclipse.jgit.errors.CorruptObjectException;
  50. import org.eclipse.jgit.lib.FileMode;
  51. import org.eclipse.jgit.lib.RepositoryTestCase;
  52. import org.eclipse.jgit.treewalk.FileTreeIterator;
  53. import org.eclipse.jgit.treewalk.TreeWalk;
  54. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  55. import org.eclipse.jgit.util.FileUtils;
  56. import org.junit.Test;
  57. /**
  58. * Tests ignore node behavior on the local filesystem.
  59. */
  60. public class IgnoreNodeTest extends RepositoryTestCase {
  61. private static final FileMode D = FileMode.TREE;
  62. private static final FileMode F = FileMode.REGULAR_FILE;
  63. private static final boolean ignored = true;
  64. private static final boolean tracked = false;
  65. private TreeWalk walk;
  66. @Test
  67. public void testRules() throws IOException {
  68. writeIgnoreFile(".git/info/exclude", "*~", "/out");
  69. writeIgnoreFile(".gitignore", "*.o", "/config");
  70. writeTrashFile("config/secret", "");
  71. writeTrashFile("mylib.c", "");
  72. writeTrashFile("mylib.c~", "");
  73. writeTrashFile("mylib.o", "");
  74. writeTrashFile("out/object/foo.exe", "");
  75. writeIgnoreFile("src/config/.gitignore", "lex.out");
  76. writeTrashFile("src/config/lex.out", "");
  77. writeTrashFile("src/config/config.c", "");
  78. writeTrashFile("src/config/config.c~", "");
  79. writeTrashFile("src/config/old/lex.out", "");
  80. beginWalk();
  81. assertEntry(F, tracked, ".gitignore");
  82. assertEntry(D, ignored, "config");
  83. assertEntry(F, ignored, "config/secret");
  84. assertEntry(F, tracked, "mylib.c");
  85. assertEntry(F, ignored, "mylib.c~");
  86. assertEntry(F, ignored, "mylib.o");
  87. assertEntry(D, ignored, "out");
  88. assertEntry(D, ignored, "out/object");
  89. assertEntry(F, ignored, "out/object/foo.exe");
  90. assertEntry(D, tracked, "src");
  91. assertEntry(D, tracked, "src/config");
  92. assertEntry(F, tracked, "src/config/.gitignore");
  93. assertEntry(F, tracked, "src/config/config.c");
  94. assertEntry(F, ignored, "src/config/config.c~");
  95. assertEntry(F, ignored, "src/config/lex.out");
  96. assertEntry(D, tracked, "src/config/old");
  97. assertEntry(F, ignored, "src/config/old/lex.out");
  98. }
  99. @Test
  100. public void testNegation() throws IOException {
  101. writeIgnoreFile(".gitignore", "*.o");
  102. writeIgnoreFile("src/a/b/.gitignore", "!keep.o");
  103. writeTrashFile("src/a/b/keep.o", "");
  104. writeTrashFile("src/a/b/nothere.o", "");
  105. beginWalk();
  106. assertEntry(F, tracked, ".gitignore");
  107. assertEntry(D, tracked, "src");
  108. assertEntry(D, tracked, "src/a");
  109. assertEntry(D, tracked, "src/a/b");
  110. assertEntry(F, tracked, "src/a/b/.gitignore");
  111. assertEntry(F, tracked, "src/a/b/keep.o");
  112. assertEntry(F, ignored, "src/a/b/nothere.o");
  113. }
  114. @Test
  115. public void testSlashOnlyMatchesDirectory() throws IOException {
  116. writeIgnoreFile(".gitignore", "out/");
  117. writeTrashFile("out", "");
  118. beginWalk();
  119. assertEntry(F, tracked, ".gitignore");
  120. assertEntry(F, tracked, "out");
  121. FileUtils.delete(new File(trash, "out"));
  122. writeTrashFile("out/foo", "");
  123. beginWalk();
  124. assertEntry(F, tracked, ".gitignore");
  125. assertEntry(D, ignored, "out");
  126. assertEntry(F, ignored, "out/foo");
  127. }
  128. @Test
  129. public void testWithSlashDoesNotMatchInSubDirectory() throws IOException {
  130. writeIgnoreFile(".gitignore", "a/b");
  131. writeTrashFile("a/a", "");
  132. writeTrashFile("a/b", "");
  133. writeTrashFile("src/a/a", "");
  134. writeTrashFile("src/a/b", "");
  135. beginWalk();
  136. assertEntry(F, tracked, ".gitignore");
  137. assertEntry(D, tracked, "a");
  138. assertEntry(F, tracked, "a/a");
  139. assertEntry(F, ignored, "a/b");
  140. assertEntry(D, tracked, "src");
  141. assertEntry(D, tracked, "src/a");
  142. assertEntry(F, tracked, "src/a/a");
  143. assertEntry(F, tracked, "src/a/b");
  144. }
  145. private void beginWalk() throws CorruptObjectException {
  146. walk = new TreeWalk(db);
  147. walk.addTree(new FileTreeIterator(db));
  148. }
  149. private void assertEntry(FileMode type, boolean entryIgnored,
  150. String pathName) throws IOException {
  151. assertTrue("walk has entry", walk.next());
  152. assertEquals(pathName, walk.getPathString());
  153. assertEquals(type, walk.getFileMode(0));
  154. WorkingTreeIterator itr = walk.getTree(0, WorkingTreeIterator.class);
  155. assertNotNull("has tree", itr);
  156. assertEquals("is ignored", entryIgnored, itr.isEntryIgnored());
  157. if (D.equals(type))
  158. walk.enterSubtree();
  159. }
  160. private void writeIgnoreFile(String name, String... rules)
  161. throws IOException {
  162. StringBuilder data = new StringBuilder();
  163. for (String line : rules)
  164. data.append(line + "\n");
  165. writeTrashFile(name, data.toString());
  166. }
  167. }