diff options
author | Thomas Wolf <twolf@apache.org> | 2023-06-15 22:08:06 +0200 |
---|---|---|
committer | Thomas Wolf <twolf@apache.org> | 2023-06-19 08:19:29 +0200 |
commit | faefa90f990858db7bec199501cb37f2631c43d0 (patch) | |
tree | 0a0500f2e2cd11b8582ee01a4582f5dcaa756bc2 /org.eclipse.jgit.test | |
parent | 7b955048eb86e1c12114554beacb27b329252b15 (diff) | |
download | jgit-faefa90f990858db7bec199501cb37f2631c43d0.tar.gz jgit-faefa90f990858db7bec199501cb37f2631c43d0.zip |
Default for global (user) git ignore file
C git has a default for git config core.excludesfile: "Its default
value is $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either
not set or empty, $HOME/.config/git/ignore is used instead." [1]
Implement this in the WorkingTreeIterator$RootIgnoreNode.
To make this testable, mock the "user.home" directory for all JGit
tests, otherwise tests might pick up a real user's git ignore file.
Also ensure that JGit code always reads "user.home" via the
SystemReader.
Add tests for both locations.
[1] https://git-scm.com/docs/gitignore#_description
Bug: 436127
Change-Id: Ie510259320286c3c13a6464a37da1bd9ca1e373a
Signed-off-by: Thomas Wolf <twolf@apache.org>
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreNodeTest.java | 108 | ||||
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/CommitTemplateConfigTest.java | 10 |
2 files changed, 115 insertions, 3 deletions
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 index 05a953e081..ab08c99796 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreNodeTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreNodeTest.java @@ -20,14 +20,18 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import org.eclipse.jgit.junit.MockSystemReader; import org.eclipse.jgit.junit.RepositoryTestCase; import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.treewalk.FileTreeIterator; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.WorkingTreeIterator; +import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.FileUtils; import org.eclipse.jgit.util.SystemReader; import org.junit.After; @@ -698,6 +702,110 @@ public class IgnoreNodeTest extends RepositoryTestCase { } @Test + public void testUserGitIgnoreFound() throws IOException { + File homeDir = FS.DETECTED.userHome(); + Path userIgnore = homeDir.toPath().resolve(".config").resolve("git") + .resolve("ignore"); + Files.createDirectories(userIgnore.getParent()); + Files.writeString(userIgnore, "x"); + try { + writeTrashFile(".foo", ""); + writeTrashFile("a/x/file", ""); + writeTrashFile("b/x", ""); + writeTrashFile("x/file", ""); + + beginWalk(); + assertEntry(F, tracked, ".foo"); + assertEntry(D, tracked, "a"); + assertEntry(D, ignored, "a/x"); + assertEntry(F, ignored, "a/x/file"); + assertEntry(D, tracked, "b"); + assertEntry(F, ignored, "b/x"); + assertEntry(D, ignored, "x"); + assertEntry(F, ignored, "x/file"); + endWalk(); + } finally { + Files.deleteIfExists(userIgnore); + } + } + + @Test + public void testXdgIgnoreFound() throws IOException { + File tmp = getTemporaryDirectory(); + Path xdg = tmp.toPath().resolve("xdg"); + Path userIgnore = xdg.resolve("git").resolve("ignore"); + Files.createDirectories(userIgnore.getParent()); + Files.writeString(userIgnore, "x"); + SystemReader system = SystemReader.getInstance(); + assertTrue(system instanceof MockSystemReader); + ((MockSystemReader) system).setProperty("XDG_CONFIG_HOME", + xdg.toAbsolutePath().toString()); + // Also create the one in the home directory -- it should not be active + File homeDir = FS.DETECTED.userHome(); + Path userIgnore2 = homeDir.toPath().resolve(".config").resolve("git") + .resolve("ignore"); + Files.createDirectories(userIgnore2.getParent()); + Files.writeString(userIgnore2, "a"); + try { + writeTrashFile(".foo", ""); + writeTrashFile("a/x/file", ""); + writeTrashFile("b/x", ""); + writeTrashFile("x/file", ""); + + beginWalk(); + assertEntry(F, tracked, ".foo"); + assertEntry(D, tracked, "a"); + assertEntry(D, ignored, "a/x"); + assertEntry(F, ignored, "a/x/file"); + assertEntry(D, tracked, "b"); + assertEntry(F, ignored, "b/x"); + assertEntry(D, ignored, "x"); + assertEntry(F, ignored, "x/file"); + endWalk(); + } finally { + ((MockSystemReader) system).setProperty("XDG_CONFIG_HOME", null); + Files.deleteIfExists(userIgnore2); + } + } + + @Test + public void testXdgWrong() throws IOException { + File tmp = getTemporaryDirectory(); + Path xdg = tmp.toPath().resolve("xdg"); + SystemReader system = SystemReader.getInstance(); + assertTrue(system instanceof MockSystemReader); + // Valid value, but the directory doesn't exist + ((MockSystemReader) system).setProperty("XDG_CONFIG_HOME", + xdg.toAbsolutePath().toString()); + // Also create the one in the home directory -- it should not be active + File homeDir = FS.DETECTED.userHome(); + Path userIgnore2 = homeDir.toPath().resolve(".config").resolve("git") + .resolve("ignore"); + Files.createDirectories(userIgnore2.getParent()); + Files.writeString(userIgnore2, "x"); + try { + writeTrashFile(".foo", ""); + writeTrashFile("a/x/file", ""); + writeTrashFile("b/x", ""); + writeTrashFile("x/file", ""); + + beginWalk(); + assertEntry(F, tracked, ".foo"); + assertEntry(D, tracked, "a"); + assertEntry(D, tracked, "a/x"); + assertEntry(F, tracked, "a/x/file"); + assertEntry(D, tracked, "b"); + assertEntry(F, tracked, "b/x"); + assertEntry(D, tracked, "x"); + assertEntry(F, tracked, "x/file"); + endWalk(); + } finally { + ((MockSystemReader) system).setProperty("XDG_CONFIG_HOME", null); + Files.deleteIfExists(userIgnore2); + } + } + + @Test public void testToString() throws Exception { assertEquals(Arrays.asList("").toString(), new IgnoreNode().toString()); assertEquals(Arrays.asList("hello").toString(), diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/CommitTemplateConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/CommitTemplateConfigTest.java index 42bafb60ca..3ccd0ef021 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/CommitTemplateConfigTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/CommitTemplateConfigTest.java @@ -17,7 +17,9 @@ import java.io.IOException; import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.junit.JGitTestUtil; +import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase; import org.eclipse.jgit.storage.file.FileRepositoryBuilder; +import org.eclipse.jgit.util.FS; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -27,7 +29,7 @@ import org.junit.rules.TemporaryFolder; * test using bazel which doesn't allow tests to create files in the home * directory */ -public class CommitTemplateConfigTest { +public class CommitTemplateConfigTest extends LocalDiskRepositoryTestCase { @Rule public TemporaryFolder tmp = new TemporaryFolder(); @@ -42,9 +44,11 @@ public class CommitTemplateConfigTest { String templateContent = "content of the template"; JGitTestUtil.write(tempFile, templateContent); // proper evaluation of the ~/ directory - String homeDir = System.getProperty("user.home"); + File homeDir = FS.DETECTED.userHome(); File tempFileInHomeDirectory = File.createTempFile("fileInHomeFolder", - ".tmp", new File(homeDir)); + ".tmp", homeDir); + // The home directory should be a mocked temporary directory, but + // still... tempFileInHomeDirectory.deleteOnExit(); JGitTestUtil.write(tempFileInHomeDirectory, templateContent); String expectedTemplatePath = "~/" + tempFileInHomeDirectory.getName(); |