diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCachePathEditTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCachePathEditTest.java new file mode 100644 index 000000000..4a6a42b6d --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCachePathEditTest.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2011, Robin Rosenberg + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.dircache; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit; +import org.eclipse.jgit.lib.FileMode; +import org.eclipse.jgit.lib.ObjectId; +import org.junit.Test; + +public class DirCachePathEditTest { + + static final class AddEdit extends PathEdit { + + public AddEdit(String entryPath) { + super(entryPath); + } + + @Override + public void apply(DirCacheEntry ent) { + ent.setFileMode(FileMode.REGULAR_FILE); + ent.setLength(1); + ent.setObjectId(ObjectId.zeroId()); + } + + } + + @Test + public void testAddDeletePathAndTreeNormalNames() { + DirCache dc = DirCache.newInCore(); + DirCacheEditor editor = dc.editor(); + editor.add(new AddEdit("a")); + editor.add(new AddEdit("b/c")); + editor.add(new AddEdit("c/d")); + editor.finish(); + assertEquals(3, dc.getEntryCount()); + assertEquals("a", dc.getEntry(0).getPathString()); + assertEquals("b/c", dc.getEntry(1).getPathString()); + assertEquals("c/d", dc.getEntry(2).getPathString()); + + editor = dc.editor(); + editor.add(new DirCacheEditor.DeletePath("b/c")); + editor.finish(); + assertEquals(2, dc.getEntryCount()); + assertEquals("a", dc.getEntry(0).getPathString()); + assertEquals("c/d", dc.getEntry(1).getPathString()); + + editor = dc.editor(); + editor.add(new DirCacheEditor.DeleteTree("")); + editor.finish(); + assertEquals(0, dc.getEntryCount()); + } + + @Test + public void testAddDeleteTrickyNames() { + DirCache dc = DirCache.newInCore(); + DirCacheEditor editor = dc.editor(); + editor.add(new AddEdit("a/b")); + editor.add(new AddEdit("a.")); + editor.add(new AddEdit("ab")); + editor.finish(); + assertEquals(3, dc.getEntryCount()); + // Validate sort order + assertEquals("a.", dc.getEntry(0).getPathString()); + assertEquals("a/b", dc.getEntry(1).getPathString()); + assertEquals("ab", dc.getEntry(2).getPathString()); + + editor = dc.editor(); + // Sort order should not confuse DeleteTree + editor.add(new DirCacheEditor.DeleteTree("a")); + editor.finish(); + assertEquals(2, dc.getEntryCount()); + assertEquals("a.", dc.getEntry(0).getPathString()); + assertEquals("ab", dc.getEntry(1).getPathString()); + } +} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheEditor.java b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheEditor.java index 77e9d51e0..f6f82390c 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheEditor.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheEditor.java @@ -267,9 +267,12 @@ public class DirCacheEditor extends BaseDirCacheEditor { * path of the subtree within the repository. If the path * does not end with "/" a "/" is implicitly added to ensure * only the subtree's contents are matched by the command. + * The special case "" (not "/"!) deletes all entries. */ public DeleteTree(final String entryPath) { - super(entryPath.endsWith("/") ? entryPath : entryPath + "/"); + super( + (entryPath.endsWith("/") || entryPath.length() == 0) ? entryPath + : entryPath + "/"); } public void apply(final DirCacheEntry ent) {