summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Wolf <thomas.wolf@paranor.ch>2020-09-10 23:39:07 +0200
committerMatthias Sohn <matthias.sohn@sap.com>2020-09-21 16:52:43 -0400
commitcb553e3583aec02336edd1d4115ac8a14537a475 (patch)
tree3af80560beead7d71913f73d62d0b9a2e42c2836
parent566e49d7d39b12c785be24b8b61b4960a4b7ea17 (diff)
downloadjgit-cb553e3583aec02336edd1d4115ac8a14537a475.tar.gz
jgit-cb553e3583aec02336edd1d4115ac8a14537a475.zip
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 <thomas.wolf@paranor.ch>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StatusCommandTest.java21
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/treewalk/filter/IndexDiffFilter.java11
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);
}
}