aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test
diff options
context:
space:
mode:
authorShawn O. Pearce <spearce@spearce.org>2010-07-21 10:51:15 +0200
committerShawn O. Pearce <spearce@spearce.org>2010-07-21 10:34:08 -0700
commit09910ffa32e472481a3ba237cf99d803576c9172 (patch)
treee94c076de081f7a5e345acef8b5d653245f21ca8 /org.eclipse.jgit.test
parent0ec0e21fdf6a60692cf796ea789b689eb1f0b0b2 (diff)
downloadjgit-09910ffa32e472481a3ba237cf99d803576c9172.tar.gz
jgit-09910ffa32e472481a3ba237cf99d803576c9172.zip
Move ignore node handling into WorkingTreeIterator
The working tree iterator has perfect knowledge of the path structure as well as immediate information about whether or not an ignore file even exists at this level. We can exploit that to simplify the logic and running time for testing ignored file status by pushing all of the checks down into the iterator itself. Change-Id: I22ff534853e8c5672cc5c2d9444aeb14e294070e Signed-off-by: Shawn O. Pearce <spearce@spearce.org> CC: Charley Wang <chwang@redhat.com> CC: Chris Aniszczyk <caniszczyk@gmail.com> CC: Stefan Lay <stefan.lay@sap.com> CC: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r--org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/.gitignore1
-rw-r--r--org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/.gitignore1
-rw-r--r--org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/.gitignore0
-rw-r--r--org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b1/.gitignore1
-rw-r--r--org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b1/test.stp0
-rw-r--r--org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b2/c/.gitignore1
-rw-r--r--org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b2/c/test.stp0
-rw-r--r--org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/notignored0
-rw-r--r--org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/test.stp0
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreCacheTest.java403
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreNodeTest.java184
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java3
12 files changed, 185 insertions, 409 deletions
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/.gitignore b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/.gitignore
deleted file mode 100644
index b3f6bc97fb..0000000000
--- a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-!/notignored
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/.gitignore b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/.gitignore
deleted file mode 100644
index 09b8574b00..0000000000
--- a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-notarealfile
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/.gitignore b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/.gitignore
deleted file mode 100644
index e69de29bb2..0000000000
--- a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/.gitignore
+++ /dev/null
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b1/.gitignore b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b1/.gitignore
deleted file mode 100644
index 82b0f5d464..0000000000
--- a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b1/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-/c
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b1/test.stp b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b1/test.stp
deleted file mode 100644
index e69de29bb2..0000000000
--- a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b1/test.stp
+++ /dev/null
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b2/c/.gitignore b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b2/c/.gitignore
deleted file mode 100644
index 3c6cf10b1d..0000000000
--- a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b2/c/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-/notarealfile2
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b2/c/test.stp b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b2/c/test.stp
deleted file mode 100644
index e69de29bb2..0000000000
--- a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/new/a/b2/c/test.stp
+++ /dev/null
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/notignored b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/notignored
deleted file mode 100644
index e69de29bb2..0000000000
--- a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/notignored
+++ /dev/null
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/test.stp b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/test.stp
deleted file mode 100644
index e69de29bb2..0000000000
--- a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/excludeTest/test.stp
+++ /dev/null
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreCacheTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreCacheTest.java
deleted file mode 100644
index a9c1c31e4d..0000000000
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreCacheTest.java
+++ /dev/null
@@ -1,403 +0,0 @@
-/*
- * Copyright (C) 2010, Red Hat Inc.
- * 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 java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.util.ArrayList;
-
-import org.eclipse.jgit.lib.RepositoryTestCase;
-import org.eclipse.jgit.util.JGitTestUtil;
-
-/**
- * Tests for the ignore cache
- */
-public class IgnoreCacheTest extends RepositoryTestCase {
-
- private File ignoreTestDir = JGitTestUtil.getTestResourceFile("excludeTest");
- private SimpleIgnoreCache cache;
- private final ArrayList<File> toDelete = new ArrayList<File>();
-
- //TODO: Do not use OS dependent strings to encode file paths
-
- public void tearDown() throws Exception {
- super.tearDown();
- deleteIgnoreFiles();
- cache.clear();
- toDelete.clear();
- }
-
- public void setUp() throws Exception {
- super.setUp();
- ignoreTestDir = JGitTestUtil.getTestResourceFile("excludeTest");
- assertTrue("Test resource directory is not a directory",ignoreTestDir.isDirectory());
-
- db = createWorkRepository();
- recursiveCopy(ignoreTestDir, db.getDirectory().getParentFile());
- cache = new SimpleIgnoreCache(db);
- initCache();
- }
-
- protected void recursiveCopy(File src, File parent) throws IOException {
- for (File file : src.listFiles()) {
- String rel = file.getName();
- File dst = new File(parent.toURI().resolve(rel));
- copyFileOrDirectory(file, dst);
- if (file.isDirectory())
- recursiveCopy(file, dst);
- }
- }
-
- protected static void copyFileOrDirectory(File src, File dst) throws IOException {
- if (src.isDirectory())
- dst.mkdir();
- else
- copyFile(src, dst);
- }
-
- public void testInitialization() {
- File test = new File(db.getDirectory().getParentFile() + "/new/a/b1/test.stp");
- assertTrue("Missing file " + test.getAbsolutePath(), test.exists());
-
- /*
- * Every folder along the path has a .gitignore file. Therefore every
- * folder should have been added and initialized
- */
- boolean result = isIgnored(getRelativePath(test));
- assertFalse("Unexpected match for " + test.toString(), result);
-
- /*
- * Check that every .gitignore along the path has been initialized
- */
- File folder = test.getParentFile();
- IgnoreNode rules = null;
- String fp = folder.getAbsolutePath();
- while (!folder.equals(db.getDirectory().getParentFile()) && fp.length() > 0) {
- rules = cache.getRules(getRelativePath(folder));
- assertNotNull("Ignore file not initialized for " + fp, rules);
- if (getRelativePath(folder).endsWith("new/a"))
- //The /new/a directory has an empty ignore file
- assertEquals("Ignore file not initialized for " + fp, 0, rules.getRules().size());
- else
- assertEquals("Ignore file not initialized for " + fp, 1, rules.getRules().size());
-
- folder = folder.getParentFile();
- fp = folder.getAbsolutePath();
- }
- if (rules != null)
- assertEquals(1, rules.getRules().size());
- else
- fail("Base directory not initialized");
-
- }
-
- public void testRules() {
- ignoreTestDir = JGitTestUtil.getTestResourceFile("excludeTest");
- assertTrue("Test resource directory is not a directory", ignoreTestDir.isDirectory());
- createExcludeFile();
- initCache();
-
- File test = new File(db.getDirectory().getParentFile(), "test.stp");
- String path = test.getAbsolutePath();
- assertTrue("Could not find test file " + path, test.exists());
-
- IgnoreNode baseRules = cache.getRules("");
- assertNotNull("Could not find base rules", baseRules);
-
- /*
- * .git/info/excludes:
- * /test.stp
- * /notignored
- *
- * new/.gitignore:
- * notarealfile
- *
- * new/a/.gitignore:
- * <empty>
- *
- * new/a/b2/.gitignore:
- * <does not exist>
- *
- * new/a/b1/.gitignore:
- * /c
- *
- * new/a/b1/c/.gitignore:
- * !/shouldbeignored.txt
- *
- * .gitignore:
- * !/notignored
- * /commentNotIgnored.tx#t
- * /commentIgnored.txt#comment
- * /commentIgnored.txt #comment
- */
- boolean result = isIgnored(getRelativePath(test));
- assertEquals(3, baseRules.getRules().size());
- assertTrue(db.getDirectory().getParentFile().toURI().equals(baseRules.getBaseDir().toURI()));
- //Test basic exclude file
- assertTrue("Did not match file " + test.toString(), result);
- //Test exclude file priority
- assertNotIgnored("notignored");
- //Test that /src/test.stp is not matched by /test.stp in exclude file (Do not reinitialize)
- assertNotIgnored("/src/test.stp");
- //Test file that is not mentioned -- should just return unmatched
- assertNotIgnored("not/mentioned/file.txt");
-
- //Test adding nonexistent node
- test = new File(db.getDirectory().getParentFile(), "new/a/b2/d/test.stp");
- assertNotIgnored("new/a/b2/d/test.stp");
- assertNotIgnored("new/a/b2/d/");
- assertNotIgnored("new/a/b2/d");
-
- //Test folder
- test = new File(db.getDirectory().getParentFile(), "new/a/b1/c");
- assertIgnored("new/a/b1/c");
- assertIgnored("new/a/b1/c/anything.c");
- assertIgnored("new/a/b1/c/and.o");
- assertIgnored("new/a/b1/c/everything.d");
- assertIgnored("new/a/b1/c/everything.d");
- //Special case -- the normally higher priority negation in c/.gitignore is cancelled by the folder being ignored
- assertIgnored("new/a/b1/c/shouldbeignored.txt");
-
- //Test name-only (use non-existent folders)
- assertNotIgnored("notarealfile");
- assertNotIgnored("/notarealfile");
- assertIgnored("new/notarealfile");
- assertIgnored("new/notarealfile/fake");
- assertIgnored("new/a/notarealfile");
- assertIgnored("new/a/b1/notarealfile");
-
- //Test clearing node -- create empty .gitignore
- createIgnoreFile(db.getDirectory().getParentFile() + "/new/a/b2/.gitignore", new String[0]);
- test = new File(db.getDirectory().getParentFile(), "new/a/b2/c");
- initCache();
- baseRules = cache.getRules("new/a/b2");
- assertNotNull(baseRules);
- baseRules.clear();
- assertEquals(baseRules.getRules().size(), 0);
- try {
- assertFalse("Node not properly cleared", baseRules.isIgnored(getRelativePath(test)));
- } catch (IOException e) {
- e.printStackTrace();
- fail("IO exception when testing base rules");
- }
-
- //Test clearing entire cache, and isEmpty
- assertNotNull(cache.getRules(""));
- assertFalse(cache.isEmpty());
- cache.clear();
- assertNull(cache.getRules(""));
- assertTrue(cache.isEmpty());
- assertNotIgnored("/anything");
- assertNotIgnored("/new/anything");
- assertNotIgnored("/src/anything");
- }
-
- public void testPriorities() {
- ignoreTestDir = JGitTestUtil.getTestResourceFile("excludeTest");
- assertTrue("Test resource directory is not a directory",ignoreTestDir.isDirectory());
- createExcludeFile();
- initCache();
-
- File test = new File(db.getDirectory().getParentFile(), "/src/test.stp");
- assertTrue("Resource file " + test.getName() + " is missing", test.exists());
-
- //Test basic exclude file
- IgnoreNode node = cache.getRules("src");
- assertNotNull("Excludes file was not initialized", node);
-
- /*
- * src/.gitignore:
- * /*.st?
- * !/test.stp
- * !/a.c
- * /a.c
- *
- * ./.gitignore:
- * !/notignored
- *
- * .git/info/exclude:
- * /test.stp
- * /notignored
- */
- assertIgnored("src/a.c");
- assertIgnored("test.stp");
- assertIgnored("src/blank.stp");
- assertNotIgnored("notignored");
- assertNotIgnored("src/test.stp");
-
- assertEquals(4, node.getRules().size());
-
- /*
- * new/.gitignore:
- * notarealfile
- *
- * new/a/.gitignore:
- * <empty>
- *
- * new/a/b2/.gitignore:
- * <does not exist>
- *
- * new/a/b2/c/.gitignore:
- * /notarealfile2
- */
- assertIgnored("new/a/b2/c/notarealfile2");
- assertIgnored("new/notarealfile");
- assertIgnored("new/a/notarealfile");
- assertNotIgnored("new/a/b2/c/test.stp");
- assertNotIgnored("new/a/b2/c");
- assertNotIgnored("new/a/b2/nonexistent");
- }
-
- /**
- * Check if a file is not matched as ignored
- * @param relativePath
- * Path to file, relative to db.getDirectory. Use "/" as a separator,
- * this method will replace all instances of "/" with File.separator
- */
- private void assertNotIgnored(String relativePath) {
- File test = new File(db.getDirectory().getParentFile(), relativePath);
- assertFalse("Should not match " + test.toString(), isIgnored(getRelativePath(test)));
- }
-
- /**
- * Check if a file is matched as ignored
- * @param relativePath
- * Path to file, relative to db.getDirectory. Use "/" as a separator,
- * this method will replace all instances of "/" with File.separator.
- */
- private void assertIgnored(String relativePath) {
- File test = new File(db.getDirectory().getParentFile(), relativePath);
- assertTrue("Failed to match " + test.toString(), isIgnored(getRelativePath(test)));
- }
-
- /**
- * Attempt to write an ignore file at the given location
- * @param path
- * Will create file at this path
- * @param contents
- * Each entry in contents will be entered on its own line
- */
- private void createIgnoreFile(String path, String[] contents) {
- File ignoreFile = new File(path);
- ignoreFile.delete();
- ignoreFile.deleteOnExit(); //Hope to catch in the event of crash
- toDelete.add(ignoreFile); //For teardown purposes
-
- //Jump through some hoops to create the exclude file
- try {
- if (!ignoreFile.createNewFile())
- fail("Could not create ignore file" + ignoreFile.getAbsolutePath());
-
- BufferedWriter bw = new BufferedWriter(new FileWriter (ignoreFile));
- for (String s : contents)
- bw.write(s + System.getProperty("line.separator"));
- bw.flush();
- bw.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- fail("Could not create exclude file");
- }
- }
-
- private void createExcludeFile() {
- String[] content = new String[2];
- content[0] = "/test.stp";
- content[1] = "/notignored";
-
- //We can do this because we explicitly delete parent directories later in deleteIgnoreFiles.
- File parent= new File(db.getDirectory().getParentFile(), ".git/info");
- if (!parent.exists())
- parent.mkdirs();
-
- createIgnoreFile(db.getDirectory().getParentFile() + "/.git/info/exclude", content);
- }
-
- private void deleteIgnoreFiles() {
- for (File f : toDelete)
- f.delete();
-
- //Systematically delete exclude parent dirs
- File f = new File(ignoreTestDir.getAbsoluteFile(), ".git/info");
- f.delete();
- f = new File(ignoreTestDir.getAbsoluteFile(), ".git");
- f.delete();
- }
-
- /**
- * @param path
- * Filepath relative to the git directory
- * @return
- * Results of cache.isIgnored(path) -- true if ignored, false if
- * a negation is encountered or if no rules apply
- */
- private boolean isIgnored(String path) {
- try {
- return cache.isIgnored(path);
- } catch (IOException e) {
- fail("IOException when attempting to check ignored status");
- }
- return false;
- }
-
- private String getRelativePath(File file) {
- String retVal = db.getDirectory().getParentFile().toURI().relativize(file.toURI()).getPath();
- if (retVal.length() == file.getAbsolutePath().length())
- fail("Not a child of the git directory");
- if (retVal.endsWith("/"))
- retVal = retVal.substring(0, retVal.length() - 1);
-
- return retVal;
- }
-
- private void initCache() {
- try {
- cache.initialize();
- } catch (IOException e) {
- e.printStackTrace();
- fail("Could not initialize cache");
- }
- }
-
-}
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreNodeTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreNodeTest.java
new file mode 100644
index 0000000000..ea7820428f
--- /dev/null
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreNodeTest.java
@@ -0,0 +1,184 @@
+/*
+ * Copyright (C) 2010, Red Hat Inc.
+ * 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 java.io.File;
+import java.io.IOException;
+
+import org.eclipse.jgit.errors.CorruptObjectException;
+import org.eclipse.jgit.lib.FileMode;
+import org.eclipse.jgit.lib.RepositoryTestCase;
+import org.eclipse.jgit.treewalk.FileTreeIterator;
+import org.eclipse.jgit.treewalk.TreeWalk;
+import org.eclipse.jgit.treewalk.WorkingTreeIterator;
+
+/**
+ * Tests ignore node behavior on the local filesystem.
+ */
+public class IgnoreNodeTest extends RepositoryTestCase {
+ private static final FileMode D = FileMode.TREE;
+
+ private static final FileMode F = FileMode.REGULAR_FILE;
+
+ private static final boolean ignored = true;
+
+ private static final boolean tracked = false;
+
+ private TreeWalk walk;
+
+ public void testRules() throws IOException {
+ writeIgnoreFile(".git/info/exclude", "*~", "/out");
+
+ writeIgnoreFile(".gitignore", "*.o", "/config");
+ writeTrashFile("config/secret", "");
+ writeTrashFile("mylib.c", "");
+ writeTrashFile("mylib.c~", "");
+ writeTrashFile("mylib.o", "");
+
+ writeTrashFile("out/object/foo.exe", "");
+ writeIgnoreFile("src/config/.gitignore", "lex.out");
+ writeTrashFile("src/config/lex.out", "");
+ writeTrashFile("src/config/config.c", "");
+ writeTrashFile("src/config/config.c~", "");
+ writeTrashFile("src/config/old/lex.out", "");
+
+ beginWalk();
+ assertEntry(F, tracked, ".gitignore");
+ assertEntry(D, ignored, "config");
+ assertEntry(F, ignored, "config/secret");
+ assertEntry(F, tracked, "mylib.c");
+ assertEntry(F, ignored, "mylib.c~");
+ assertEntry(F, ignored, "mylib.o");
+
+ assertEntry(D, ignored, "out");
+ assertEntry(D, ignored, "out/object");
+ assertEntry(F, ignored, "out/object/foo.exe");
+
+ assertEntry(D, tracked, "src");
+ assertEntry(D, tracked, "src/config");
+ assertEntry(F, tracked, "src/config/.gitignore");
+ assertEntry(F, tracked, "src/config/config.c");
+ assertEntry(F, ignored, "src/config/config.c~");
+ assertEntry(F, ignored, "src/config/lex.out");
+ assertEntry(D, tracked, "src/config/old");
+ assertEntry(F, ignored, "src/config/old/lex.out");
+ }
+
+ public void testNegation() throws IOException {
+ writeIgnoreFile(".gitignore", "*.o");
+ writeIgnoreFile("src/a/b/.gitignore", "!keep.o");
+ writeTrashFile("src/a/b/keep.o", "");
+ writeTrashFile("src/a/b/nothere.o", "");
+
+ beginWalk();
+ assertEntry(F, tracked, ".gitignore");
+ assertEntry(D, tracked, "src");
+ assertEntry(D, tracked, "src/a");
+ assertEntry(D, tracked, "src/a/b");
+ assertEntry(F, tracked, "src/a/b/.gitignore");
+ assertEntry(F, tracked, "src/a/b/keep.o");
+ assertEntry(F, ignored, "src/a/b/nothere.o");
+ }
+
+ public void testSlashOnlyMatchesDirectory() throws IOException {
+ writeIgnoreFile(".gitignore", "out/");
+ writeTrashFile("out", "");
+
+ beginWalk();
+ assertEntry(F, tracked, ".gitignore");
+ assertEntry(F, tracked, "out");
+
+ new File(trash, "out").delete();
+ writeTrashFile("out/foo", "");
+
+ beginWalk();
+ assertEntry(F, tracked, ".gitignore");
+ assertEntry(D, ignored, "out");
+ assertEntry(F, ignored, "out/foo");
+ }
+
+ public void testWithSlashDoesNotMatchInSubDirectory() throws IOException {
+ writeIgnoreFile(".gitignore", "a/b");
+ writeTrashFile("a/a", "");
+ writeTrashFile("a/b", "");
+ writeTrashFile("src/a/a", "");
+ writeTrashFile("src/a/b", "");
+
+ beginWalk();
+ assertEntry(F, tracked, ".gitignore");
+ assertEntry(D, tracked, "a");
+ assertEntry(F, tracked, "a/a");
+ assertEntry(F, ignored, "a/b");
+ assertEntry(D, tracked, "src");
+ assertEntry(D, tracked, "src/a");
+ assertEntry(F, tracked, "src/a/a");
+ assertEntry(F, tracked, "src/a/b");
+ }
+
+ private void beginWalk() throws CorruptObjectException {
+ walk = new TreeWalk(db);
+ walk.reset();
+ walk.addTree(new FileTreeIterator(db));
+ }
+
+ private void assertEntry(FileMode type, boolean entryIgnored,
+ String pathName) throws IOException {
+ assertTrue("walk has entry", walk.next());
+ assertEquals(pathName, walk.getPathString());
+ assertEquals(type, walk.getFileMode(0));
+
+ WorkingTreeIterator itr = walk.getTree(0, WorkingTreeIterator.class);
+ assertNotNull("has tree", itr);
+ assertEquals("is ignored", entryIgnored, itr.isEntryIgnored());
+ if (D.equals(type))
+ walk.enterSubtree();
+ }
+
+ private void writeIgnoreFile(String name, String... rules)
+ throws IOException {
+ StringBuilder data = new StringBuilder();
+ for (String line : rules)
+ data.append(line + "\n");
+ writeTrashFile(name, data.toString());
+ }
+}
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java
index 5b4be4379e..02bfdc1df5 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ReadTreeTest.java
@@ -58,7 +58,6 @@ import org.eclipse.jgit.errors.CheckoutConflictException;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.TreeWalk;
-import org.eclipse.jgit.util.FS;
public abstract class ReadTreeTest extends RepositoryTestCase {
protected Tree theHead;
@@ -613,7 +612,7 @@ public abstract class ReadTreeTest extends RepositoryTestCase {
TreeWalk walk = new TreeWalk(db);
walk.reset();
walk.setRecursive(true);
- walk.addTree(new FileTreeIterator(db.getWorkDir(), FS.DETECTED));
+ walk.addTree(new FileTreeIterator(db));
String expectedValue;
String path;
int nrFiles = 0;