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;
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;
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()));
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");
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}
/***/ public String checkoutConflictWithFile;
/***/ public String checkoutConflictWithFiles;
/***/ public String classCastNotA;
+ /***/ public String cloneNonEmptyDirectory;
/***/ public String collisionOn;
/***/ public String commandWasCalledInTheWrongState;
/***/ public String commitAlreadyExists;
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;
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();
}