When init in {non-}empty dir then an {exception thrown,repo created}

* When initialising a repo in a non-empty dir then an exception is thrown
* When initialising a repo in a empty dir then a bare repo is created
This commit is contained in:
Paul Campbell 2018-06-01 23:16:45 +01:00
parent fbab2f27cc
commit e1e94c7770
3 changed files with 68 additions and 14 deletions

View file

@ -23,7 +23,7 @@ package net.kemitix.gitdb;
import org.eclipse.jgit.api.Git;
import java.nio.file.NotDirectoryException;
import java.io.IOException;
import java.nio.file.Path;
/**
@ -38,10 +38,13 @@ public interface GitDB {
*
* @param dbDir the path to initialise the local repo in
* @return a GitDB instance for the created local gitdb
* @throws NotDirectoryException if {@code dbDir} it is not a directory
* @throws IOException if there {@code dbDir} is a file or a non-empty directory
*/
static GitDB initLocal(final Path dbDir) throws NotDirectoryException {
return new GitDBLocal(Git.init(), dbDir.toFile());
static GitDB initLocal(final Path dbDir) throws IOException {
return new GitDBLocal(
Git.init(),
dbDir.toFile()
);
}
}

View file

@ -24,9 +24,11 @@ package net.kemitix.gitdb;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.InitCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files;
import java.nio.file.NotDirectoryException;
/**
@ -44,19 +46,41 @@ class GitDBLocal implements GitDB {
*
* @param initCommand a JGit InitCommand
* @param dbDir the path to instantiate the git repo in
* @throws NotDirectoryException if {@code dbDir} it is not a directory
* @throws IOException if there {@code dbDir} is a file or a non-empty directory
*/
@SuppressWarnings("avoidhidingcauseexception")
GitDBLocal(final InitCommand initCommand, final File dbDir) throws NotDirectoryException {
GitDBLocal(final InitCommand initCommand, final File dbDir) throws IOException {
validateDbDir(dbDir);
try {
this.git = initCommand.setGitDir(dbDir).setBare(true).call();
} catch (JGitInternalException e) {
throw new NotDirectoryException(dbDir.toString());
this.git = initRepo(initCommand, dbDir);
} catch (GitAPIException e) {
throw new UnexpectedGitDbException("Unhandled Git API Exception", e);
}
}
private void validateDbDir(final File dbDir) throws IOException {
verifyIsNotAFile(dbDir);
if (dbDir.exists()) {
verifyIsEmpty(dbDir);
}
}
private static void verifyIsEmpty(final File dbDir) throws IOException {
if (Files.newDirectoryStream(dbDir.toPath()).iterator().hasNext()) {
throw new DirectoryNotEmptyException(dbDir.toString());
}
}
private static void verifyIsNotAFile(final File dbDir) throws NotDirectoryException {
if (dbDir.isFile()) {
throw new NotDirectoryException(dbDir.toString());
}
}
private static Git initRepo(final InitCommand initCommand, final File dbDir) throws GitAPIException {
return initCommand.setGitDir(dbDir).setBare(true).call();
}
// @Override
// @SneakyThrows
// public <T> T get(Branch branch, Key key, Class<T> type) {

View file

@ -87,10 +87,37 @@ class GitDBTest implements WithAssertions {
return Files.createTempFile("gitdb", "file");
}
// When initialising a repo in a dir is exists then an exception is thrown
//@Test
//void initRepo_whenDirExists_thenThrowException() {
//}
// When initialising a repo in a non-empty dir then an exception is thrown
@Test
void initRepo_whenNotEmptyDir_thenThrowException() throws IOException {
//given
final Path dir = dirExists();
filesExistIn(dir);
//then
assertThatExceptionOfType(DirectoryNotEmptyException.class)
.isThrownBy(() -> GitDB.initLocal(dir))
.withMessageContaining(dir.toString());
}
private void filesExistIn(final Path dir) throws IOException {
Files.createTempFile(dir, "gitdb", "file");
}
private Path dirExists() throws IOException {
return Files.createTempDirectory("gitdb");
}
// When initialising a repo in a empty dir then a bare repo is created
@Test
void initRepo_whenEmptyDir_thenCreateBareRepo() throws IOException {
//given
final Path dir = dirExists();
//when
final GitDB gitDB = GitDB.initLocal(dir);
//then
assertThat(gitDB).isNotNull();
assertThatIsBareRepo(dir);
}
// When opening a repo in a dir that is not a bare repo then an exception is thrown
// When opening a repo in a dir that is a file then an exception is thrown