]> source.dussan.org Git - jgit.git/commitdiff
Ensure a directory exists before trying to create/merge a file into it. 96/7096/5
authorJevgeni Zelenkov <jevgeni.zelenkov@gmail.com>
Mon, 6 Aug 2012 06:59:28 +0000 (08:59 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Mon, 6 Aug 2012 06:59:28 +0000 (08:59 +0200)
Since git doesn't keep track of empty directories, they should be
created first. Test case included demonstrates that using
StashApplyCommand(). Bugfix is applied to the DirCacheCheckout class,
because StashApplyCommand() uses it internally to apply a stash.

Change-Id: Iac259229ef919f9e92e7e51a671d877172bb88a8
Signed-off-by: Jevgeni Zelenkov <jevgeni.zelenkov@gmail.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/JGitTestUtil.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/StashApplyCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryTestCase.java
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java

index d3da09ffbd6f22863457571c981eb538cefe69e7..b3376a54be313302ded59711463cd9998a35a099 100644 (file)
@@ -141,6 +141,14 @@ public abstract class JGitTestUtil {
                return path;
        }
 
+       public static File writeTrashFile(final FileRepository db,
+                       final String subdir,
+                       final String name, final String data) throws IOException {
+               File path = new File(db.getWorkTree() + "/" + subdir, name);
+               write(path, data);
+               return path;
+       }
+
        /**
         * Write a string as a UTF-8 file.
         *
index bc11b7a703e89b6b6fee406e83ffe9961c0bf3ba..117ef88dc974a8e29e80140f0b4c1b34bc083bb9 100644 (file)
@@ -421,6 +421,38 @@ public class StashApplyCommandTest extends RepositoryTestCase {
                assertTrue(status.getModified().contains(PATH));
        }
 
+       @Test
+       public void stashChangeInANewSubdirectory() throws Exception {
+               String subdir = "subdir";
+               String fname = "file2.txt";
+               String path = subdir + System.getProperty("file.separator") + fname;
+               String otherBranch = "otherbranch";
+
+               writeTrashFile(subdir, fname, "content2");
+
+               git.add().addFilepattern(path).call();
+               RevCommit stashed = git.stashCreate().call();
+               assertNotNull(stashed);
+               assertTrue(git.status().call().isClean());
+
+               git.branchCreate().setName(otherBranch).call();
+               git.checkout().setName(otherBranch).call();
+
+               ObjectId unstashed = git.stashApply().call();
+               assertEquals(stashed, unstashed);
+
+               Status status = git.status().call();
+               assertTrue(status.getChanged().isEmpty());
+               assertTrue(status.getConflicting().isEmpty());
+               assertTrue(status.getMissing().isEmpty());
+               assertTrue(status.getRemoved().isEmpty());
+               assertTrue(status.getModified().isEmpty());
+               assertTrue(status.getUntracked().isEmpty());
+
+               assertEquals(1, status.getAdded().size());
+               assertTrue(status.getAdded().contains(path));
+       }
+
        @Test
        public void unstashNonStashCommit() throws Exception {
                try {
index c06322e8e49efd9cb2fe35921e8224fffc588440..457fd1a7ef72c560e4595e6f8e5e20e57893fbb4 100644 (file)
@@ -102,6 +102,12 @@ public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
                return JGitTestUtil.writeTrashFile(db, name, data);
        }
 
+       protected File writeTrashFile(final String subdir, final String name,
+                       final String data)
+                       throws IOException {
+               return JGitTestUtil.writeTrashFile(db, subdir, name, data);
+       }
+
        protected void deleteTrashFile(final String name) throws IOException {
                JGitTestUtil.deleteTrashFile(db, name);
        }
index f46869e421b3ebf686ddf1eaf6613925ae6fd0d0..6e417b33b3b4fcebfbce7ce12f86e8304ab7c58c 100644 (file)
@@ -962,6 +962,7 @@ public class DirCacheCheckout {
                        DirCacheEntry entry, ObjectReader or) throws IOException {
                ObjectLoader ol = or.open(entry.getObjectId());
                File parentDir = f.getParentFile();
+               parentDir.mkdirs();
                File tmpFile = File.createTempFile("._" + f.getName(), null, parentDir);
                WorkingTreeOptions opt = repo.getConfig().get(WorkingTreeOptions.KEY);
                FileOutputStream rawChannel = new FileOutputStream(tmpFile);