From cb553e3583aec02336edd1d4115ac8a14537a475 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Thu, 10 Sep 2020 23:39:07 +0200 Subject: [PATCH] IndexDiffFilter: handle path prefixes correctly When comparing git directory paths to check whether one is a prefix of another, one must add a slash to avoid false prefix matches when one directory name is a prefix of another. The path "audio" is not a prefix of the path "audio-new", but would be a prefix of a path "audio/new". Bug: 566799 Change-Id: I6f671ca043c7c2c6044eb05a71dc8cca8d0ee040 Signed-off-by: Thomas Wolf --- .../eclipse/jgit/api/StatusCommandTest.java | 21 +++++++++++++++++++ .../jgit/treewalk/filter/IndexDiffFilter.java | 11 +++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java index 7e0de82d82..5311edb0eb 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java @@ -160,4 +160,25 @@ public class StatusCommandTest extends RepositoryTestCase { assertTrue("Expected no differences", status.isClean()); } } + + @Test + public void testFolderPrefix() throws Exception { + // "audio" is a prefix of "audio-new" and "audio.new". + try (Git git = new Git(db)) { + // Order here is the git order, but that doesn't really matter. + // They are processed by StatusCommand in this order even if written + // in a different order. Bug 566799 would, when having processed + // audio/foo, remove previously recorded untracked folders that have + // "audio" as a prefix: audio-new and audio.new. + writeTrashFile("audi", "foo", "foo"); + writeTrashFile("audio-new", "foo", "foo"); + writeTrashFile("audio.new", "foo", "foo"); + writeTrashFile("audio", "foo", "foo"); + writeTrashFile("audio_new", "foo", "foo"); + Status stat = git.status().call(); + assertEquals(Sets.of("audi", "audio-new", "audio.new", "audio", + "audio_new"), stat.getUntrackedFolders()); + } + } + } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/filter/IndexDiffFilter.java b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/filter/IndexDiffFilter.java index 19cda42485..4731f345bc 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/filter/IndexDiffFilter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/filter/IndexDiffFilter.java @@ -217,14 +217,15 @@ public class IndexDiffFilter extends TreeFilter { */ private void copyUntrackedFolders(String currentPath) { String pathToBeSaved = null; - while (!untrackedParentFolders.isEmpty() - && !currentPath.startsWith(untrackedParentFolders.getFirst() - + "/")) //$NON-NLS-1$ + while (!untrackedParentFolders.isEmpty() && !currentPath + .startsWith(untrackedParentFolders.getFirst() + '/')) { pathToBeSaved = untrackedParentFolders.removeFirst(); + } if (pathToBeSaved != null) { - while (!untrackedFolders.isEmpty() - && untrackedFolders.getLast().startsWith(pathToBeSaved)) + while (!untrackedFolders.isEmpty() && untrackedFolders.getLast() + .startsWith(pathToBeSaved + '/')) { untrackedFolders.removeLast(); + } untrackedFolders.addLast(pathToBeSaved); } } -- 2.39.5