diff --git a/src/main/java/net/kemitix/gitdb/GitDB.java b/src/main/java/net/kemitix/gitdb/GitDB.java index 3d43790..296e2d5 100644 --- a/src/main/java/net/kemitix/gitdb/GitDB.java +++ b/src/main/java/net/kemitix/gitdb/GitDB.java @@ -58,16 +58,9 @@ public interface GitDB { 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/GitDBLocal.java b/src/main/java/net/kemitix/gitdb/GitDBLocal.java index 32228e0..f0e1faa 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBLocal.java +++ b/src/main/java/net/kemitix/gitdb/GitDBLocal.java @@ -24,6 +24,7 @@ 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.lib.Repository; import java.io.File; import java.io.IOException; @@ -42,7 +43,7 @@ class GitDBLocal implements GitDB { private final Git git; /** - * Constructors a new instance of this class. + * Create a new GitDB instance, while initialising a new git repo. * * @param initCommand a JGit InitCommand * @param dbDir the path to instantiate the git repo in @@ -58,8 +59,21 @@ class GitDBLocal implements GitDB { } } + /** + * Create a new GitDB instance using the Git repo. + * + * @param git the Git handle for the repo + */ GitDBLocal(final Git git) { - this.git = git; + this.git = verifyIsBareRepo(git); + } + + private static Git verifyIsBareRepo(final Git git) { + final Repository repository = git.getRepository(); + if (repository.isBare()) { + return git; + } + throw new InvalidRepositoryException("Not a bare repo", repository.getDirectory().toPath()); } private void validateDbDir(final File dbDir) throws IOException { diff --git a/src/main/java/net/kemitix/gitdb/GitDBRepoNotFoundException.java b/src/main/java/net/kemitix/gitdb/GitDBRepoNotFoundException.java index 23eed66..0fafc1d 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBRepoNotFoundException.java +++ b/src/main/java/net/kemitix/gitdb/GitDBRepoNotFoundException.java @@ -1,3 +1,24 @@ +/* + The MIT License (MIT) + + Copyright (c) 2018 Paul Campbell + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software + and associated documentation files (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies + or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + package net.kemitix.gitdb; import org.eclipse.jgit.errors.RepositoryNotFoundException; @@ -14,6 +35,7 @@ public class GitDBRepoNotFoundException extends RuntimeException { /** * Constructor. * + * @param path the location where a GitDB repo was not found * @param cause the original exception */ GitDBRepoNotFoundException(final Path path, final RepositoryNotFoundException cause) { diff --git a/src/main/java/net/kemitix/gitdb/InvalidRepositoryException.java b/src/main/java/net/kemitix/gitdb/InvalidRepositoryException.java index 995445f..128392f 100644 --- a/src/main/java/net/kemitix/gitdb/InvalidRepositoryException.java +++ b/src/main/java/net/kemitix/gitdb/InvalidRepositoryException.java @@ -1,3 +1,24 @@ +/* + The MIT License (MIT) + + Copyright (c) 2018 Paul Campbell + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software + and associated documentation files (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies + or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + package net.kemitix.gitdb; import java.nio.file.Path;