When opening a repo in a dir that is not a bare repo then an exception is thrown

This commit is contained in:
Paul Campbell 2018-06-01 23:34:40 +01:00
parent e1e94c7770
commit 18e9d51248
4 changed files with 46 additions and 3 deletions

View file

@ -22,6 +22,7 @@
package net.kemitix.gitdb; package net.kemitix.gitdb;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; 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);
}
}
} }

View file

@ -58,6 +58,10 @@ class GitDBLocal implements GitDB {
} }
} }
GitDBLocal(final Git git) {
this.git = git;
}
private void validateDbDir(final File dbDir) throws IOException { private void validateDbDir(final File dbDir) throws IOException {
verifyIsNotAFile(dbDir); verifyIsNotAFile(dbDir);
if (dbDir.exists()) { if (dbDir.exists()) {

View file

@ -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);
}
}

View file

@ -6,14 +6,11 @@ import org.eclipse.jgit.api.errors.GitAPIException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import java.io.IOException; import java.io.IOException;
import java.nio.file.*; import java.nio.file.*;
import java.util.List; import java.util.List;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*; 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 // 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 is a file then an exception is thrown
// When opening a repo in a dir that doesn't exist then an exception is thrown // When opening a repo in a dir that doesn't exist then an exception is thrown