From e1e94c7770c6614bc237cd531fa6dc7cd9bee637 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 1 Jun 2018 23:16:45 +0100 Subject: [PATCH] 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 --- src/main/java/net/kemitix/gitdb/GitDB.java | 11 +++--- .../java/net/kemitix/gitdb/GitDBLocal.java | 36 +++++++++++++++---- .../java/net/kemitix/gitdb/GitDBTest.java | 35 +++++++++++++++--- 3 files changed, 68 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/kemitix/gitdb/GitDB.java b/src/main/java/net/kemitix/gitdb/GitDB.java index 25d5302..36952b8 100644 --- a/src/main/java/net/kemitix/gitdb/GitDB.java +++ b/src/main/java/net/kemitix/gitdb/GitDB.java @@ -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() + ); } } diff --git a/src/main/java/net/kemitix/gitdb/GitDBLocal.java b/src/main/java/net/kemitix/gitdb/GitDBLocal.java index 290bccd..6586a47 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBLocal.java +++ b/src/main/java/net/kemitix/gitdb/GitDBLocal.java @@ -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 get(Branch branch, Key key, Class type) { diff --git a/src/test/java/net/kemitix/gitdb/GitDBTest.java b/src/test/java/net/kemitix/gitdb/GitDBTest.java index 325c812..a98a35d 100644 --- a/src/test/java/net/kemitix/gitdb/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/GitDBTest.java @@ -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