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.2KB

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