When opening a repo in a dir that is not a bare repo then an exception is thrown
This commit is contained in:
parent
d39af20fdf
commit
418bfeaee9
3 changed files with 49 additions and 1 deletions
|
@ -53,14 +53,21 @@ public interface GitDB {
|
||||||
*
|
*
|
||||||
* @param dbDir the path to open as a local repo
|
* @param dbDir the path to open as a local repo
|
||||||
* @return a GitDB instance for the local gitdb
|
* @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 {
|
static GitDBLocal openLocal(final Path dbDir) throws IOException {
|
||||||
try {
|
try {
|
||||||
final Git git = Git.open(dbDir.toFile());
|
final Git git = Git.open(dbDir.toFile());
|
||||||
|
verifyIsBareRepo(dbDir, git);
|
||||||
return new GitDBLocal(git);
|
return new GitDBLocal(git);
|
||||||
} catch (RepositoryNotFoundException e) {
|
} catch (RepositoryNotFoundException e) {
|
||||||
throw new GitDBRepoNotFoundException(dbDir, 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package net.kemitix.gitdb;
|
package net.kemitix.gitdb;
|
||||||
|
|
||||||
import org.assertj.core.api.WithAssertions;
|
import org.assertj.core.api.WithAssertions;
|
||||||
|
import org.eclipse.jgit.api.Git;
|
||||||
import org.eclipse.jgit.api.InitCommand;
|
import org.eclipse.jgit.api.InitCommand;
|
||||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
@ -149,6 +150,25 @@ class GitDBTest implements WithAssertions {
|
||||||
.withMessageContaining(dir.toString());
|
.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
|
// When opening a repo in a dir that is a bare repo then GitDb is returned
|
||||||
@Test
|
@Test
|
||||||
void openRepo_whenGitDB_thenReturnGitDB() throws IOException {
|
void openRepo_whenGitDB_thenReturnGitDB() throws IOException {
|
||||||
|
|
Loading…
Reference in a new issue