]> source.dussan.org Git - jgit.git/commitdiff
Cloning should fail when destination directory exists and is not empty 18/3518/8
authorAdrian Goerler <adrian.goerler@sap.com>
Wed, 6 Jul 2011 21:10:44 +0000 (23:10 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Wed, 6 Jul 2011 21:10:53 +0000 (23:10 +0200)
When trying to clone into a folder that already contains a cloned
repository native git will fail with a message "fatal: destination path
'folder' already exists and is not an empty directory.". Now JGit will
also fail in this situation throwing a JGitInternalException.

The test case was provided by Tomasz Zarna.

Bug: 347852
Change-Id: If9e9919a5f92d13cf038dc470c21ee5967322dac
Also-by: Tomasz Zarna <Tomasz.Zarna@pl.ibm.com>
Signed-off-by: Adrian Goerler <adrian.goerler@sap.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java
org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties
org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java

index ca5260766932c2adc32bfcb9f70035c824e6b9b2..e43ce3431f8c0e24ddf5996612aa61802fd3316e 100644 (file)
@@ -44,6 +44,8 @@ package org.eclipse.jgit.api;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.IOException;
@@ -51,6 +53,7 @@ import java.util.Collections;
 import java.util.List;
 
 import org.eclipse.jgit.api.ListBranchCommand.ListMode;
+import org.eclipse.jgit.api.errors.JGitInternalException;
 import org.eclipse.jgit.junit.TestRepository;
 import org.eclipse.jgit.lib.ConfigConstants;
 import org.eclipse.jgit.lib.Constants;
@@ -209,6 +212,30 @@ public class CloneCommandTest extends RepositoryTestCase {
                return sb.toString();
        }
 
+       @Test
+       public void testCloneRepositoryWhenDestinationDirectoryExistsAndIsNotEmpty()
+                       throws IOException {
+               String dirName = "testCloneTargetDirectoryNotEmpty";
+               File directory = createTempDirectory(dirName);
+               CloneCommand command = Git.cloneRepository();
+               command.setDirectory(directory);
+               command.setURI("file://" + git.getRepository().getWorkTree().getPath());
+               Git git2 = command.call();
+               assertNotNull(git2);
+               // clone again
+               command = Git.cloneRepository();
+               command.setDirectory(directory);
+               command.setURI("file://" + git.getRepository().getWorkTree().getPath());
+               try {
+                       git2 = command.call();
+                       // we shouldn't get here
+                       fail("destination directory already exists and is not an empty folder, cloning should fail");
+               } catch (JGitInternalException e) {
+                       assertTrue(e.getMessage().contains("not an empty directory"));
+                       assertTrue(e.getMessage().contains(dirName));
+               }
+       }
+
        public static File createTempDirectory(String name) throws IOException {
                final File temp;
                temp = File.createTempFile(name, Long.toString(System.nanoTime()));
index 7f47295c3f307543e3b75d0f5e7b8da25e9c1f23..797aad4efb037eeb740c55847c9025758ceb57f7 100644 (file)
@@ -72,6 +72,20 @@ public class InitCommandTest extends RepositoryTestCase {
                assertNotNull(repository);
        }
 
+       @Test
+       public void testInitNonEmptyRepository() throws IOException {
+               File directory = createTempDirectory("testInitRepository2");
+               File someFile = new File(directory, "someFile");
+               someFile.createNewFile();
+               assertTrue(someFile.exists());
+               assertTrue(directory.listFiles().length > 0);
+               InitCommand command = new InitCommand();
+               command.setDirectory(directory);
+               Repository repository = command.call().getRepository();
+               addRepoToClose(repository);
+               assertNotNull(repository);
+       }
+
        @Test
        public void testInitBareRepository() throws IOException {
                File directory = createTempDirectory("testInitBareRepository");
index 570895e8109a1431345c544ab0580dbeba33c306..8c87d2a6b0c9b0b47823c13230bcb1fc4a68b815 100644 (file)
@@ -86,6 +86,7 @@ checkoutConflictWithFile=Checkout conflict with file: {0}
 checkoutConflictWithFiles=Checkout conflict with files: {0}
 checkoutUnexpectedResult=Checkout returned unexpected result {0}
 classCastNotA=Not a {0}
+cloneNonEmptyDirectory=Destination path "{0}" already exists and is not an empty directory
 collisionOn=Collision on {0}
 commandWasCalledInTheWrongState=Command {0} was called in the wrong state
 commitAlreadyExists=exists {0}
index 7054cf800f2fbb2873d0dce1344cbe5a41f123f0..af20477a65bcfb4b0f6745e590e34c2f50d92702 100644 (file)
@@ -146,6 +146,7 @@ public class JGitText extends TranslationBundle {
        /***/ public String checkoutConflictWithFile;
        /***/ public String checkoutConflictWithFiles;
        /***/ public String classCastNotA;
+       /***/ public String cloneNonEmptyDirectory;
        /***/ public String collisionOn;
        /***/ public String commandWasCalledInTheWrongState;
        /***/ public String commitAlreadyExists;
index aa8cf6e30a8d9e2c3c348edc772cc480237062d2..cfbcbf1b6d2bc61c03a5dfadd245e2ef927ee358 100644 (file)
@@ -45,11 +45,13 @@ package org.eclipse.jgit.api;
 import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;
+import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.concurrent.Callable;
 
+import org.eclipse.jgit.JGitText;
 import org.eclipse.jgit.api.errors.InvalidRemoteException;
 import org.eclipse.jgit.api.errors.JGitInternalException;
 import org.eclipse.jgit.dircache.DirCache;
@@ -131,6 +133,9 @@ public class CloneCommand implements Callable<Git> {
                command.setBare(bare);
                if (directory == null)
                        directory = new File(u.getHumanishName(), Constants.DOT_GIT);
+               if (directory.exists() && directory.listFiles().length != 0)
+                       throw new JGitInternalException(MessageFormat.format(
+                                       JGitText.get().cloneNonEmptyDirectory, directory.getName()));
                command.setDirectory(directory);
                return command.call().getRepository();
        }