]> source.dussan.org Git - jgit.git/commitdiff
IndexDiffFilter: handle path prefixes correctly 70/169170/2
authorThomas Wolf <thomas.wolf@paranor.ch>
Thu, 10 Sep 2020 21:39:07 +0000 (23:39 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Mon, 21 Sep 2020 20:52:43 +0000 (16:52 -0400)
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 <thomas.wolf@paranor.ch>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/treewalk/filter/IndexDiffFilter.java

index 7e0de82d82f29583a6cff103207e4bffeb514603..5311edb0eb03fb7758f29b25b5827b92958b2a3f 100644 (file)
@@ -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());
+               }
+       }
+
 }
index 19cda42485a82680ba8bfb9e312d589e2c813266..4731f345bcd7796665c7859a95070dc26a5ef467 100644 (file)
@@ -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);
                }
        }