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.

CGitIgnoreTest.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. import static org.junit.Assert.assertArrayEquals;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import java.io.BufferedInputStream;
  48. import java.io.BufferedReader;
  49. import java.io.ByteArrayInputStream;
  50. import java.io.File;
  51. import java.io.IOException;
  52. import java.io.InputStreamReader;
  53. import java.util.LinkedHashSet;
  54. import java.util.Set;
  55. import org.eclipse.jgit.junit.RepositoryTestCase;
  56. import org.eclipse.jgit.lib.Constants;
  57. import org.eclipse.jgit.lib.StoredConfig;
  58. import org.eclipse.jgit.treewalk.FileTreeIterator;
  59. import org.eclipse.jgit.treewalk.TreeWalk;
  60. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  61. import org.eclipse.jgit.treewalk.filter.NotIgnoredFilter;
  62. import org.eclipse.jgit.util.FS;
  63. import org.eclipse.jgit.util.FS.ExecutionResult;
  64. import org.eclipse.jgit.util.RawParseUtils;
  65. import org.eclipse.jgit.util.TemporaryBuffer;
  66. import org.junit.Before;
  67. import org.junit.Test;
  68. /**
  69. * Tests that verify that the set of ignore files in a repository is the same in
  70. * JGit and in C-git.
  71. */
  72. public class CGitIgnoreTest extends RepositoryTestCase {
  73. @Before
  74. public void initRepo() throws IOException {
  75. // These tests focus on .gitignore files inside the repository. Because
  76. // we run C-git, we must ensure that global or user exclude files cannot
  77. // influence the tests. So we set core.excludesFile to an empty file
  78. // inside the repository.
  79. File fakeUserGitignore = writeTrashFile(".fake_user_gitignore", "");
  80. StoredConfig config = db.getConfig();
  81. config.setString("core", null, "excludesFile",
  82. fakeUserGitignore.getAbsolutePath());
  83. // Disable case-insensitivity -- JGit doesn't handle that yet.
  84. config.setBoolean("core", null, "ignoreCase", false);
  85. config.save();
  86. }
  87. private void createFiles(String... paths) throws IOException {
  88. for (String path : paths) {
  89. writeTrashFile(path, "x");
  90. }
  91. }
  92. private String toString(TemporaryBuffer b) throws IOException {
  93. return RawParseUtils.decode(b.toByteArray());
  94. }
  95. private String[] cgitIgnored() throws Exception {
  96. FS fs = db.getFS();
  97. ProcessBuilder builder = fs.runInShell("git", new String[] { "ls-files",
  98. "--ignored", "--exclude-standard", "-o" });
  99. builder.directory(db.getWorkTree());
  100. builder.environment().put("HOME", fs.userHome().getAbsolutePath());
  101. ExecutionResult result = fs.execute(builder,
  102. new ByteArrayInputStream(new byte[0]));
  103. String errorOut = toString(result.getStderr());
  104. assertEquals("External git failed", "exit 0\n",
  105. "exit " + result.getRc() + '\n' + errorOut);
  106. try (BufferedReader r = new BufferedReader(new InputStreamReader(
  107. new BufferedInputStream(result.getStdout().openInputStream()),
  108. Constants.CHARSET))) {
  109. return r.lines().toArray(String[]::new);
  110. }
  111. }
  112. private LinkedHashSet<String> jgitIgnored() throws IOException {
  113. // Do a tree walk that does descend into ignored directories and return
  114. // a list of all ignored files
  115. LinkedHashSet<String> result = new LinkedHashSet<>();
  116. try (TreeWalk walk = new TreeWalk(db)) {
  117. walk.addTree(new FileTreeIterator(db));
  118. walk.setRecursive(true);
  119. while (walk.next()) {
  120. if (walk.getTree(WorkingTreeIterator.class).isEntryIgnored()) {
  121. result.add(walk.getPathString());
  122. }
  123. }
  124. }
  125. return result;
  126. }
  127. private void assertNoIgnoredVisited(Set<String> ignored) throws Exception {
  128. // Do a recursive tree walk with a NotIgnoredFilter and verify that none
  129. // of the files visited is in the ignored set
  130. try (TreeWalk walk = new TreeWalk(db)) {
  131. walk.addTree(new FileTreeIterator(db));
  132. walk.setFilter(new NotIgnoredFilter(0));
  133. walk.setRecursive(true);
  134. while (walk.next()) {
  135. String path = walk.getPathString();
  136. assertFalse("File " + path + " is ignored, should not appear",
  137. ignored.contains(path));
  138. }
  139. }
  140. }
  141. private void assertSameAsCGit(String... notIgnored) throws Exception {
  142. LinkedHashSet<String> ignored = jgitIgnored();
  143. String[] cgit = cgitIgnored();
  144. assertArrayEquals(cgit, ignored.toArray());
  145. for (String notExcluded : notIgnored) {
  146. assertFalse("File " + notExcluded + " should not be ignored",
  147. ignored.contains(notExcluded));
  148. }
  149. assertNoIgnoredVisited(ignored);
  150. }
  151. @Test
  152. public void testSimpleIgnored() throws Exception {
  153. createFiles("a.txt", "a.tmp", "src/sub/a.txt", "src/a.tmp",
  154. "src/a.txt/b.tmp", "ignored/a.tmp", "ignored/not_ignored/a.tmp",
  155. "ignored/other/a.tmp");
  156. writeTrashFile(".gitignore",
  157. "*.txt\n" + "/ignored/*\n" + "!/ignored/not_ignored");
  158. assertSameAsCGit("ignored/not_ignored/a.tmp");
  159. }
  160. @Test
  161. public void testDirOnlyMatch() throws Exception {
  162. createFiles("a.txt", "src/foo/a.txt", "src/a.txt", "foo/a.txt");
  163. writeTrashFile(".gitignore", "foo/");
  164. assertSameAsCGit();
  165. }
  166. @Test
  167. public void testDirOnlyMatchDeep() throws Exception {
  168. createFiles("a.txt", "src/foo/a.txt", "src/a.txt", "foo/a.txt");
  169. writeTrashFile(".gitignore", "**/foo/");
  170. assertSameAsCGit();
  171. }
  172. @Test
  173. public void testStarMatchOnSlashNot() throws Exception {
  174. createFiles("sub/a.txt", "foo/sext", "foo/s.txt");
  175. writeTrashFile(".gitignore", "s*xt");
  176. assertSameAsCGit("sub/a.txt");
  177. }
  178. @Test
  179. public void testPrefixMatch() throws Exception {
  180. createFiles("src/new/foo.txt");
  181. writeTrashFile(".gitignore", "src/new");
  182. assertSameAsCGit();
  183. }
  184. @Test
  185. public void testDirectoryMatchSubRecursive() throws Exception {
  186. createFiles("src/new/foo.txt", "foo/src/new/foo.txt", "sub/src/new");
  187. writeTrashFile(".gitignore", "**/src/new/");
  188. assertSameAsCGit();
  189. }
  190. @Test
  191. public void testDirectoryMatchSubRecursiveBacktrack() throws Exception {
  192. createFiles("src/new/foo.txt", "src/src/new/foo.txt");
  193. writeTrashFile(".gitignore", "**/src/new/");
  194. assertSameAsCGit();
  195. }
  196. @Test
  197. public void testDirectoryMatchSubRecursiveBacktrack2() throws Exception {
  198. createFiles("src/new/foo.txt", "src/src/new/foo.txt");
  199. writeTrashFile(".gitignore", "**/**/src/new/");
  200. assertSameAsCGit();
  201. }
  202. @Test
  203. public void testDirectoryMatchSubRecursiveBacktrack3() throws Exception {
  204. createFiles("x/a/a/b/foo.txt");
  205. writeTrashFile(".gitignore", "**/*/a/b/");
  206. assertSameAsCGit();
  207. }
  208. @Test
  209. public void testDirectoryMatchSubRecursiveBacktrack4() throws Exception {
  210. createFiles("x/a/a/b/foo.txt", "x/y/z/b/a/b/foo.txt",
  211. "x/y/a/a/a/a/b/foo.txt", "x/y/a/a/a/a/b/a/b/foo.txt");
  212. writeTrashFile(".gitignore", "**/*/a/b bar\n");
  213. assertSameAsCGit();
  214. }
  215. @Test
  216. public void testDirectoryMatchSubRecursiveBacktrack5() throws Exception {
  217. createFiles("x/a/a/b/foo.txt", "x/y/a/b/a/b/foo.txt");
  218. writeTrashFile(".gitignore", "**/*/**/a/b bar\n");
  219. assertSameAsCGit();
  220. }
  221. @Test
  222. public void testUnescapedBracketsInGroup() throws Exception {
  223. createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]");
  224. writeTrashFile(".gitignore", "[[]]\n");
  225. assertSameAsCGit();
  226. }
  227. @Test
  228. public void testEscapedFirstBracketInGroup() throws Exception {
  229. createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]");
  230. writeTrashFile(".gitignore", "[\\[]]\n");
  231. assertSameAsCGit();
  232. }
  233. @Test
  234. public void testEscapedSecondBracketInGroup() throws Exception {
  235. createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]");
  236. writeTrashFile(".gitignore", "[[\\]]\n");
  237. assertSameAsCGit();
  238. }
  239. @Test
  240. public void testEscapedBothBracketsInGroup() throws Exception {
  241. createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]");
  242. writeTrashFile(".gitignore", "[\\[\\]]\n");
  243. assertSameAsCGit();
  244. }
  245. }