diff options
author | Thomas Wolf <twolf@apache.org> | 2022-07-30 00:27:49 +0200 |
---|---|---|
committer | Thomas Wolf <twolf@apache.org> | 2022-08-03 07:42:27 +0200 |
commit | 8184683f7e8d0e62bb70ff27af92dcc2d7332939 (patch) | |
tree | ea2bbcb361f919369ce49efa22680e06f299c418 /org.eclipse.jgit.test | |
parent | 59e8bec6e7705a89b5d0b9c6ac004b323ffa16b0 (diff) | |
download | jgit-8184683f7e8d0e62bb70ff27af92dcc2d7332939.tar.gz jgit-8184683f7e8d0e62bb70ff27af92dcc2d7332939.zip |
CleanCommand: fix prefix matching
String.startsWith() is not a valid test for file path prefixes:
directory "a" is _not_ a prefix of a file "ab", only of "a/b".
Add a proper Paths.isEqualOrPrefix() method and use it in CleanCommand.
Bug: 580478
Change-Id: I6863e6ba94a8ffba6561835cc57044a0945d2770
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/api/CleanCommandTest.java | 21 | ||||
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/util/PathsTest.java | 19 |
2 files changed, 40 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CleanCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CleanCommandTest.java index f8078890b7..204c89db9b 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CleanCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CleanCommandTest.java @@ -301,4 +301,25 @@ public class CleanCommandTest extends RepositoryTestCase { writeTrashFile("this_is/not_ok/more/subdirs/file.txt", "2"); git.clean().setCleanDirectories(true).setIgnore(false).call(); } + + @Test + public void testPrefix() throws Exception { + File a = writeTrashFile("a.txt", "a"); + File b = writeTrashFile("a/a.txt", "sub a"); + File dir = b.getParentFile(); + git.clean().call(); + assertFalse(a.exists()); + assertTrue(dir.exists()); + assertTrue(b.exists()); + } + + @Test + public void testPrefixWithDir() throws Exception { + File a = writeTrashFile("a.txt", "a"); + File b = writeTrashFile("a/a.txt", "sub a"); + File dir = b.getParentFile(); + git.clean().setCleanDirectories(true).call(); + assertFalse(a.exists()); + assertFalse(dir.exists()); + } } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/PathsTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/PathsTest.java index c6976882e4..2fae9099fb 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/PathsTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/PathsTest.java @@ -13,7 +13,9 @@ package org.eclipse.jgit.util; import static org.eclipse.jgit.util.Paths.compare; import static org.eclipse.jgit.util.Paths.compareSameName; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.FileMode; @@ -32,6 +34,23 @@ public class PathsTest { } @Test + public void testPrefix() { + assertTrue(Paths.isEqualOrPrefix("a", "a")); + assertTrue(Paths.isEqualOrPrefix("a", "a/b")); + assertTrue(Paths.isEqualOrPrefix("a", "a/a.txt")); + assertFalse(Paths.isEqualOrPrefix("a", "ab")); + assertFalse(Paths.isEqualOrPrefix("a", "a.txt")); + assertFalse(Paths.isEqualOrPrefix("a", "b/a.txt")); + assertFalse(Paths.isEqualOrPrefix("a", "b/a")); + assertFalse(Paths.isEqualOrPrefix("a", "ab/a.txt")); + assertFalse(Paths.isEqualOrPrefix("", "a")); + assertTrue(Paths.isEqualOrPrefix("", "")); + assertTrue(Paths.isEqualOrPrefix("a/b", "a/b")); + assertTrue(Paths.isEqualOrPrefix("a/b", "a/b/c")); + assertFalse(Paths.isEqualOrPrefix("a/b", "a/bc")); + } + + @Test public void testPathCompare() { byte[] a = Constants.encode("afoo/bar.c"); byte[] b = Constants.encode("bfoo/bar.c"); |