Refactoring

This commit is contained in:
Paul Campbell 2018-06-03 22:18:03 +01:00
parent 048f610cf4
commit 5c1822546e
5 changed files with 56 additions and 51 deletions

View file

@ -23,6 +23,8 @@ package net.kemitix.gitdb;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryBuilder;
import java.io.IOException;
import java.nio.file.Path;
@ -44,7 +46,6 @@ public interface GitDB {
*/
static GitDB initLocal(final Path dbDir) throws IOException {
return new GitDBLocal(
Git.init(),
dbDir.toFile()
);
}
@ -54,15 +55,24 @@ 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 bare repo
*/
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);
static GitDBLocal openLocal(final Path dbDir) {
if (dbDir.toFile().isFile()) {
throw new GitDBRepoNotFoundException("Not a directory", dbDir);
}
if (!dbDir.toFile().exists()) {
throw new GitDBRepoNotFoundException("Directory not found", dbDir);
}
Repository repository = null;
try {
repository = Git.open(dbDir.toFile()).getRepository();
} catch (Exception e) {
e.printStackTrace();
}
if (repository != null && repository.isBare()) {
return new GitDBLocal(repository);
}
throw new InvalidRepositoryException("Not a bare repo", dbDir);
}
/**

View file

@ -21,10 +21,15 @@
package net.kemitix.gitdb;
import org.eclipse.jgit.lib.Ref;
/**
* API for interacting with a branch in a GirDB.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public interface GitDBBranch {
static GitDBBranch withRef(final Ref ref) {
return null;
}
}

View file

@ -22,15 +22,16 @@
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 org.eclipse.jgit.lib.*;
import org.eclipse.jgit.util.FS;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files;
import java.nio.file.NotDirectoryException;
import java.nio.file.Path;
import java.util.Optional;
/**
@ -41,38 +42,31 @@ import java.util.Optional;
class GitDBLocal implements GitDB {
private final Git git;
private final Repository repository;
/**
* 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
* @throws IOException if there {@code dbDir} is a file or a non-empty directory
*/
@SuppressWarnings("avoidhidingcauseexception")
GitDBLocal(final InitCommand initCommand, final File dbDir) throws IOException {
GitDBLocal(final File dbDir) throws IOException {
validateDbDir(dbDir);
try {
this.git = initRepo(initCommand, dbDir);
} catch (GitAPIException e) {
throw new UnexpectedGitDbException("Unhandled Git API Exception", e);
}
this.repository = initRepo(dbDir);
}
/**
* Create a new GitDB instance using the Git repo.
*
* @param git the Git handle for the repo
* @param repository the Git repository
*/
GitDBLocal(final Git git) {
this.git = verifyIsBareRepo(git);
GitDBLocal(final Repository repository) {
this.repository = verifyIsBareRepo(repository);
}
private static Git verifyIsBareRepo(final Git git) {
final Repository repository = git.getRepository();
private static Repository verifyIsBareRepo(final Repository repository) {
if (repository.isBare()) {
return git;
return repository;
}
throw new InvalidRepositoryException("Not a bare repo", repository.getDirectory().toPath());
}
@ -96,13 +90,22 @@ class GitDBLocal implements GitDB {
}
}
private static Git initRepo(final InitCommand initCommand, final File dbDir) throws GitAPIException {
return initCommand.setGitDir(dbDir).setBare(true).call();
private static Repository initRepo(final File dbDir) throws IOException {
dbDir.mkdirs();
final RepositoryCache.FileKey fileKey = RepositoryCache.FileKey.exact(dbDir, FS.DETECTED);
final Repository repository = fileKey.open(false);
repository.create(true);
return repository;
}
@Override
public Optional<GitDBBranch> branch(final String name) {
return Optional.empty();
try {
return Optional.ofNullable(repository.findRef(name))
.map(GitDBBranch::withRef);
} catch (IOException e) {
return Optional.empty();
}
}
// @Override

View file

@ -21,8 +21,6 @@
package net.kemitix.gitdb;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import java.nio.file.Path;
/**
@ -35,10 +33,10 @@ public class GitDBRepoNotFoundException extends RuntimeException {
/**
* Constructor.
*
* @param path the location where a GitDB repo was not found
* @param cause the original exception
* @param message why the GitDB wasn't found
* @param path the location where a GitDB repo was not found
*/
GitDBRepoNotFoundException(final Path path, final RepositoryNotFoundException cause) {
super(String.format("GitDB repo not found: %s", path.toString()), cause);
GitDBRepoNotFoundException(final String message, final Path path) {
super(String.format("GitDB repo not found: %s: %s", message, path));
}
}

View file

@ -5,13 +5,13 @@ import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.InitCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
import java.nio.file.*;
import java.util.List;
import java.util.Optional;
import static org.mockito.BDDMockito.given;
@ -39,8 +39,10 @@ class GitDBTest implements WithAssertions {
private void assertThatIsBareRepo(final Path dbDir) throws IOException {
final Git git = Git.open(dbDir.toFile());
assertThat(git.getRepository().findRef(Constants.HEAD)).isNotNull();
assertThat(git.getRepository().isBare()).isTrue();
final Repository repository = git.getRepository();
assertThat(repository.findRef(Constants.HEAD)).isNotNull();
assertThat(repository.isBare()).isTrue();
assertThat(repository.getDirectory()).isEqualTo(dbDir.toFile());
}
private Path dirDoesNotExist() throws IOException {
@ -49,19 +51,6 @@ class GitDBTest implements WithAssertions {
return directory;
}
// When initialising a repo and an unexpected error occurs then an exception is thrown
@Test
void initRepo_whenUnexpectedError_thenThrowException() throws IOException, GitAPIException {
//given
final Path dbDir = dirDoesNotExist();
final InitCommand initCommand = spy(InitCommand.class);
given(initCommand.call()).willThrow(mock(GitAPIException.class));
//then
assertThatExceptionOfType(UnexpectedGitDbException.class)
.isThrownBy(() -> new GitDBLocal(initCommand, dbDir.toFile()))
.withMessageContaining("Unhandled Git API Exception");
}
// When initialising a repo in a dir that is a file then an exception is thrown
@Test
void initRepo_whenDirIsFile_thenThrowException() throws IOException {
@ -115,7 +104,7 @@ class GitDBTest implements WithAssertions {
//given
final Path dir = dirExists();
//then
assertThatExceptionOfType(GitDBRepoNotFoundException.class)
assertThatExceptionOfType(InvalidRepositoryException.class)
.isThrownBy(() -> GitDB.openLocal(dir))
.withMessageContaining(dir.toString());
}