diff options
author | Adrian Goerler <adrian.goerler@sap.com> | 2011-07-06 23:10:44 +0200 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2011-07-06 23:10:53 +0200 |
commit | 172a9f05216394f243805844b446cc357dd6b943 (patch) | |
tree | 9976ca1edd329cbb9903c8bbbad02e6d8d149e7a | |
parent | 7e1b2466e3928f3b2863912ef83b316ed39082db (diff) | |
download | jgit-172a9f05216394f243805844b446cc357dd6b943.tar.gz jgit-172a9f05216394f243805844b446cc357dd6b943.zip |
Cloning should fail when destination directory exists and is not empty
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>
5 files changed, 48 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java index ca52607669..e43ce3431f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java @@ -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())); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java index 7f47295c3f..797aad4efb 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java @@ -73,6 +73,20 @@ public class InitCommandTest extends RepositoryTestCase { } @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"); InitCommand command = new InitCommand(); diff --git a/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties b/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties index 570895e810..8c87d2a6b0 100644 --- a/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties +++ b/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties @@ -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} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java b/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java index 7054cf800f..af20477a65 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java @@ -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; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java index aa8cf6e30a..cfbcbf1b6d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java @@ -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(); } |