From 418bfeaee949d7af6b98a0d30d7666f20adfabf9 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 2 Jun 2018 11:27:54 +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 +++++++- .../gitdb/InvalidRepositoryException.java | 21 +++++++++++++++++++ .../java/net/kemitix/gitdb/GitDBTest.java | 20 ++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/kemitix/gitdb/InvalidRepositoryException.java diff --git a/src/main/java/net/kemitix/gitdb/GitDB.java b/src/main/java/net/kemitix/gitdb/GitDB.java index 2f25f0c..3d43790 100644 --- a/src/main/java/net/kemitix/gitdb/GitDB.java +++ b/src/main/java/net/kemitix/gitdb/GitDB.java @@ -53,14 +53,21 @@ public interface GitDB { * * @param dbDir the path to open as a local repo * @return a GitDB instance for the local gitdb - * @throws IOException if there {@code dbDir} is a file, the directory does not exist or is not a repo + * @throws IOException if there {@code dbDir} is a file, the directory does not exist or is not a bare repo */ static GitDBLocal openLocal(final Path dbDir) throws IOException { try { final Git git = Git.open(dbDir.toFile()); + verifyIsBareRepo(dbDir, git); return new GitDBLocal(git); } catch (RepositoryNotFoundException e) { throw new GitDBRepoNotFoundException(dbDir, e); } } + + static void verifyIsBareRepo(final Path dbDir, final Git git) { + if (!git.getRepository().isBare()) { + throw new InvalidRepositoryException("Not a bare repo", dbDir); + } + } } diff --git a/src/main/java/net/kemitix/gitdb/InvalidRepositoryException.java b/src/main/java/net/kemitix/gitdb/InvalidRepositoryException.java new file mode 100644 index 0000000..995445f --- /dev/null +++ b/src/main/java/net/kemitix/gitdb/InvalidRepositoryException.java @@ -0,0 +1,21 @@ +package net.kemitix.gitdb; + +import java.nio.file.Path; + +/** + * Runtime exception thrown when attempting to open a repository that it not a GitDB repo. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +public class InvalidRepositoryException extends RuntimeException { + + /** + * Constructor. + * + * @param message the reason the repo is invalid + * @param path the location of the repo + */ + public InvalidRepositoryException(final String message, final Path path) { + super(String.format("Invalid GitDB repo: %s [%s]", message, path)); + } +} diff --git a/src/test/java/net/kemitix/gitdb/GitDBTest.java b/src/test/java/net/kemitix/gitdb/GitDBTest.java index 4a18fe6..679074c 100644 --- a/src/test/java/net/kemitix/gitdb/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/GitDBTest.java @@ -1,6 +1,7 @@ package net.kemitix.gitdb; import org.assertj.core.api.WithAssertions; +import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.InitCommand; import org.eclipse.jgit.api.errors.GitAPIException; import org.junit.jupiter.api.Test; @@ -149,6 +150,25 @@ class GitDBTest implements WithAssertions { .withMessageContaining(dir.toString()); } + // When opening a repo in a dir that is not a bare repo then an exception is thrown + @Test + void openRepo_whenRepoNotBare_thenThrowException() throws IOException, GitAPIException { + //given + final Path dir = nonBareRepo(); + //then + assertThatExceptionOfType(InvalidRepositoryException.class) + .isThrownBy(() -> GitDB.openLocal(dir)) + .withMessageContaining("Invalid GitDB repo") + .withMessageContaining("Not a bare repo") + .withMessageContaining(dir.toString()); + } + + private Path nonBareRepo() throws IOException, GitAPIException { + final Path dbDir = dirDoesNotExist(); + Git.init().setGitDir(dbDir.toFile()).setBare(false).call(); + return dbDir; + } + // When opening a repo in a dir that is a bare repo then GitDb is returned @Test void openRepo_whenGitDB_thenReturnGitDB() throws IOException {