From 18e9d512486fdec70e2e4c24ac63e8249af27831 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 1 Jun 2018 23:34:40 +0100 Subject: [PATCH] When opening a repo in a dir that is not a bare repo then an exception is thrown --- src/main/java/net/kemitix/gitdb/GitDB.java | 9 ++++++++ .../java/net/kemitix/gitdb/GitDBLocal.java | 4 ++++ .../gitdb/GitDbRepoNotFoundException.java | 23 +++++++++++++++++++ .../java/net/kemitix/gitdb/GitDBTest.java | 13 ++++++++--- 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/kemitix/gitdb/GitDbRepoNotFoundException.java diff --git a/src/main/java/net/kemitix/gitdb/GitDB.java b/src/main/java/net/kemitix/gitdb/GitDB.java index 36952b8..d82a86b 100644 --- a/src/main/java/net/kemitix/gitdb/GitDB.java +++ b/src/main/java/net/kemitix/gitdb/GitDB.java @@ -22,6 +22,7 @@ package net.kemitix.gitdb; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.errors.RepositoryNotFoundException; import java.io.IOException; import java.nio.file.Path; @@ -47,4 +48,12 @@ public interface GitDB { ); } + static GitDBLocal openLocal(final Path dbDir) throws IOException { + try { + final Git git = Git.open(dbDir.toFile()); + return new GitDBLocal(git); + } catch (RepositoryNotFoundException e) { + throw new GitDbRepoNotFoundException(dbDir, e); + } + } } diff --git a/src/main/java/net/kemitix/gitdb/GitDBLocal.java b/src/main/java/net/kemitix/gitdb/GitDBLocal.java index 6586a47..32228e0 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBLocal.java +++ b/src/main/java/net/kemitix/gitdb/GitDBLocal.java @@ -58,6 +58,10 @@ class GitDBLocal implements GitDB { } } + GitDBLocal(final Git git) { + this.git = git; + } + private void validateDbDir(final File dbDir) throws IOException { verifyIsNotAFile(dbDir); if (dbDir.exists()) { diff --git a/src/main/java/net/kemitix/gitdb/GitDbRepoNotFoundException.java b/src/main/java/net/kemitix/gitdb/GitDbRepoNotFoundException.java new file mode 100644 index 0000000..d91faed --- /dev/null +++ b/src/main/java/net/kemitix/gitdb/GitDbRepoNotFoundException.java @@ -0,0 +1,23 @@ +package net.kemitix.gitdb; + +import org.eclipse.jgit.errors.RepositoryNotFoundException; + +import java.nio.file.Path; + +/** + * Runtime exception thrown when attempting to open to location that is not a GitDB repo. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +public class GitDbRepoNotFoundException extends RuntimeException { + + /** + * Constructor. + * + * @param path the path that is not a valid GitDB repo + * @param cause the original exception + */ + GitDbRepoNotFoundException(final Path path, final RepositoryNotFoundException cause) { + super(cause); + } +} diff --git a/src/test/java/net/kemitix/gitdb/GitDBTest.java b/src/test/java/net/kemitix/gitdb/GitDBTest.java index a98a35d..e007d8a 100644 --- a/src/test/java/net/kemitix/gitdb/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/GitDBTest.java @@ -6,14 +6,11 @@ import org.eclipse.jgit.api.errors.GitAPIException; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; -import org.mockito.junit.jupiter.MockitoSettings; -import org.mockito.quality.Strictness; import java.io.IOException; import java.nio.file.*; import java.util.List; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.*; @@ -120,6 +117,16 @@ class GitDBTest implements WithAssertions { } // When opening a repo in a dir that is not a bare repo then an exception is thrown + @Test + void openRepo_NotBareRepo_thenThrowException() throws IOException { + //given + final Path dir = dirExists(); + //then + assertThatExceptionOfType(GitDbRepoNotFoundException.class) + .isThrownBy(() -> GitDB.openLocal(dir)) + .withMessageContaining(dir.toString()); + } + // When opening a repo in a dir that is a file then an exception is thrown // When opening a repo in a dir that doesn't exist then an exception is thrown