123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- /*
- * Copyright (C) 2017 Thomas Wolf <thomas.wolf@paranor.ch>
- * and other copyright owners as documented in the project's IP log.
- *
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Distribution License v1.0 which
- * accompanies this distribution, is reproduced below, and is
- * available at http://www.eclipse.org/org/documents/edl-v10.php
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or
- * without modification, are permitted provided that the following
- * conditions are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- *
- * - Neither the name of the Eclipse Foundation, Inc. nor the
- * names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- package org.eclipse.jgit.ignore;
-
- import static org.junit.Assert.assertArrayEquals;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertFalse;
-
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.LinkedHashSet;
- import java.util.Set;
-
- import org.eclipse.jgit.junit.RepositoryTestCase;
- import org.eclipse.jgit.lib.Constants;
- import org.eclipse.jgit.lib.StoredConfig;
- import org.eclipse.jgit.treewalk.FileTreeIterator;
- import org.eclipse.jgit.treewalk.TreeWalk;
- import org.eclipse.jgit.treewalk.WorkingTreeIterator;
- import org.eclipse.jgit.treewalk.filter.NotIgnoredFilter;
- import org.eclipse.jgit.util.FS;
- import org.eclipse.jgit.util.FS.ExecutionResult;
- import org.eclipse.jgit.util.RawParseUtils;
- import org.eclipse.jgit.util.TemporaryBuffer;
- import org.junit.Before;
- import org.junit.Test;
-
- /**
- * Tests that verify that the set of ignore files in a repository is the same in
- * JGit and in C-git.
- */
- public class CGitIgnoreTest extends RepositoryTestCase {
-
- @Before
- public void initRepo() throws IOException {
- // These tests focus on .gitignore files inside the repository. Because
- // we run C-git, we must ensure that global or user exclude files cannot
- // influence the tests. So we set core.excludesFile to an empty file
- // inside the repository.
- File fakeUserGitignore = writeTrashFile(".fake_user_gitignore", "");
- StoredConfig config = db.getConfig();
- config.setString("core", null, "excludesFile",
- fakeUserGitignore.getAbsolutePath());
- // Disable case-insensitivity -- JGit doesn't handle that yet.
- config.setBoolean("core", null, "ignoreCase", false);
- config.save();
- }
-
- private void createFiles(String... paths) throws IOException {
- for (String path : paths) {
- writeTrashFile(path, "x");
- }
- }
-
- private String toString(TemporaryBuffer b) throws IOException {
- return RawParseUtils.decode(b.toByteArray());
- }
-
- private String[] cgitIgnored() throws Exception {
- FS fs = db.getFS();
- ProcessBuilder builder = fs.runInShell("git", new String[] { "ls-files",
- "--ignored", "--exclude-standard", "-o" });
- builder.directory(db.getWorkTree());
- builder.environment().put("HOME", fs.userHome().getAbsolutePath());
- ExecutionResult result = fs.execute(builder,
- new ByteArrayInputStream(new byte[0]));
- String errorOut = toString(result.getStderr());
- assertEquals("External git failed", "exit 0\n",
- "exit " + result.getRc() + '\n' + errorOut);
- try (BufferedReader r = new BufferedReader(new InputStreamReader(
- new BufferedInputStream(result.getStdout().openInputStream()),
- Constants.CHARSET))) {
- return r.lines().toArray(String[]::new);
- }
- }
-
- private LinkedHashSet<String> jgitIgnored() throws IOException {
- // Do a tree walk that does descend into ignored directories and return
- // a list of all ignored files
- LinkedHashSet<String> result = new LinkedHashSet<>();
- try (TreeWalk walk = new TreeWalk(db)) {
- walk.addTree(new FileTreeIterator(db));
- walk.setRecursive(true);
- while (walk.next()) {
- if (walk.getTree(WorkingTreeIterator.class).isEntryIgnored()) {
- result.add(walk.getPathString());
- }
- }
- }
- return result;
- }
-
- private void assertNoIgnoredVisited(Set<String> ignored) throws Exception {
- // Do a recursive tree walk with a NotIgnoredFilter and verify that none
- // of the files visited is in the ignored set
- try (TreeWalk walk = new TreeWalk(db)) {
- walk.addTree(new FileTreeIterator(db));
- walk.setFilter(new NotIgnoredFilter(0));
- walk.setRecursive(true);
- while (walk.next()) {
- String path = walk.getPathString();
- assertFalse("File " + path + " is ignored, should not appear",
- ignored.contains(path));
- }
- }
- }
-
- private void assertSameAsCGit(String... notIgnored) throws Exception {
- LinkedHashSet<String> ignored = jgitIgnored();
- String[] cgit = cgitIgnored();
- assertArrayEquals(cgit, ignored.toArray());
- for (String notExcluded : notIgnored) {
- assertFalse("File " + notExcluded + " should not be ignored",
- ignored.contains(notExcluded));
- }
- assertNoIgnoredVisited(ignored);
- }
-
- @Test
- public void testSimpleIgnored() throws Exception {
- createFiles("a.txt", "a.tmp", "src/sub/a.txt", "src/a.tmp",
- "src/a.txt/b.tmp", "ignored/a.tmp", "ignored/not_ignored/a.tmp",
- "ignored/other/a.tmp");
- writeTrashFile(".gitignore",
- "*.txt\n" + "/ignored/*\n" + "!/ignored/not_ignored");
- assertSameAsCGit("ignored/not_ignored/a.tmp");
- }
-
- @Test
- public void testDirOnlyMatch() throws Exception {
- createFiles("a.txt", "src/foo/a.txt", "src/a.txt", "foo/a.txt");
- writeTrashFile(".gitignore", "foo/");
- assertSameAsCGit();
- }
-
- @Test
- public void testDirOnlyMatchDeep() throws Exception {
- createFiles("a.txt", "src/foo/a.txt", "src/a.txt", "foo/a.txt");
- writeTrashFile(".gitignore", "**/foo/");
- assertSameAsCGit();
- }
-
- @Test
- public void testStarMatchOnSlashNot() throws Exception {
- createFiles("sub/a.txt", "foo/sext", "foo/s.txt");
- writeTrashFile(".gitignore", "s*xt");
- assertSameAsCGit("sub/a.txt");
- }
-
- @Test
- public void testPrefixMatch() throws Exception {
- createFiles("src/new/foo.txt");
- writeTrashFile(".gitignore", "src/new");
- assertSameAsCGit();
- }
-
- @Test
- public void testDirectoryMatchSubRecursive() throws Exception {
- createFiles("src/new/foo.txt", "foo/src/new/foo.txt", "sub/src/new");
- writeTrashFile(".gitignore", "**/src/new/");
- assertSameAsCGit();
- }
-
- @Test
- public void testDirectoryMatchSubRecursiveBacktrack() throws Exception {
- createFiles("src/new/foo.txt", "src/src/new/foo.txt");
- writeTrashFile(".gitignore", "**/src/new/");
- assertSameAsCGit();
- }
-
- @Test
- public void testDirectoryMatchSubRecursiveBacktrack2() throws Exception {
- createFiles("src/new/foo.txt", "src/src/new/foo.txt");
- writeTrashFile(".gitignore", "**/**/src/new/");
- assertSameAsCGit();
- }
-
- @Test
- public void testDirectoryMatchSubRecursiveBacktrack3() throws Exception {
- createFiles("x/a/a/b/foo.txt");
- writeTrashFile(".gitignore", "**/*/a/b/");
- assertSameAsCGit();
- }
-
- @Test
- public void testDirectoryMatchSubRecursiveBacktrack4() throws Exception {
- createFiles("x/a/a/b/foo.txt", "x/y/z/b/a/b/foo.txt",
- "x/y/a/a/a/a/b/foo.txt", "x/y/a/a/a/a/b/a/b/foo.txt");
- writeTrashFile(".gitignore", "**/*/a/b bar\n");
- assertSameAsCGit();
- }
-
- @Test
- public void testDirectoryMatchSubRecursiveBacktrack5() throws Exception {
- createFiles("x/a/a/b/foo.txt", "x/y/a/b/a/b/foo.txt");
- writeTrashFile(".gitignore", "**/*/**/a/b bar\n");
- assertSameAsCGit();
- }
-
- @Test
- public void testUnescapedBracketsInGroup() throws Exception {
- createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]");
- writeTrashFile(".gitignore", "[[]]\n");
- assertSameAsCGit();
- }
-
- @Test
- public void testEscapedFirstBracketInGroup() throws Exception {
- createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]");
- writeTrashFile(".gitignore", "[\\[]]\n");
- assertSameAsCGit();
- }
-
- @Test
- public void testEscapedSecondBracketInGroup() throws Exception {
- createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]");
- writeTrashFile(".gitignore", "[[\\]]\n");
- assertSameAsCGit();
- }
-
- @Test
- public void testEscapedBothBracketsInGroup() throws Exception {
- createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]");
- writeTrashFile(".gitignore", "[\\[\\]]\n");
- assertSameAsCGit();
- }
- }
|