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.api.Git;
import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryBuilder;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
@ -44,7 +46,6 @@ public interface GitDB {
*/ */
static GitDB initLocal(final Path dbDir) throws IOException { static GitDB initLocal(final Path dbDir) throws IOException {
return new GitDBLocal( return new GitDBLocal(
Git.init(),
dbDir.toFile() dbDir.toFile()
); );
} }
@ -54,15 +55,24 @@ 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 bare repo
*/ */
static GitDBLocal openLocal(final Path dbDir) throws IOException { static GitDBLocal openLocal(final Path dbDir) {
try { if (dbDir.toFile().isFile()) {
final Git git = Git.open(dbDir.toFile()); throw new GitDBRepoNotFoundException("Not a directory", dbDir);
return new GitDBLocal(git);
} catch (RepositoryNotFoundException e) {
throw new GitDBRepoNotFoundException(dbDir, e);
} }
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; package net.kemitix.gitdb;
import org.eclipse.jgit.lib.Ref;
/** /**
* API for interacting with a branch in a GirDB. * API for interacting with a branch in a GirDB.
* *
* @author Paul Campbell (pcampbell@kemitix.net) * @author Paul Campbell (pcampbell@kemitix.net)
*/ */
public interface GitDBBranch { public interface GitDBBranch {
static GitDBBranch withRef(final Ref ref) {
return null;
}
} }

View file

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

View file

@ -21,8 +21,6 @@
package net.kemitix.gitdb; package net.kemitix.gitdb;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import java.nio.file.Path; import java.nio.file.Path;
/** /**
@ -35,10 +33,10 @@ public class GitDBRepoNotFoundException extends RuntimeException {
/** /**
* Constructor. * Constructor.
* *
* @param path the location where a GitDB repo was not found * @param message why the GitDB wasn't found
* @param cause the original exception * @param path the location where a GitDB repo was not found
*/ */
GitDBRepoNotFoundException(final Path path, final RepositoryNotFoundException cause) { GitDBRepoNotFoundException(final String message, final Path path) {
super(String.format("GitDB repo not found: %s", path.toString()), cause); 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.InitCommand;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
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 java.io.IOException; import java.io.IOException;
import java.nio.file.*; import java.nio.file.*;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
@ -39,8 +39,10 @@ class GitDBTest implements WithAssertions {
private void assertThatIsBareRepo(final Path dbDir) throws IOException { private void assertThatIsBareRepo(final Path dbDir) throws IOException {
final Git git = Git.open(dbDir.toFile()); final Git git = Git.open(dbDir.toFile());
assertThat(git.getRepository().findRef(Constants.HEAD)).isNotNull(); final Repository repository = git.getRepository();
assertThat(git.getRepository().isBare()).isTrue(); assertThat(repository.findRef(Constants.HEAD)).isNotNull();
assertThat(repository.isBare()).isTrue();
assertThat(repository.getDirectory()).isEqualTo(dbDir.toFile());
} }
private Path dirDoesNotExist() throws IOException { private Path dirDoesNotExist() throws IOException {
@ -49,19 +51,6 @@ class GitDBTest implements WithAssertions {
return directory; 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 // When initialising a repo in a dir that is a file then an exception is thrown
@Test @Test
void initRepo_whenDirIsFile_thenThrowException() throws IOException { void initRepo_whenDirIsFile_thenThrowException() throws IOException {
@ -115,7 +104,7 @@ class GitDBTest implements WithAssertions {
//given //given
final Path dir = dirExists(); final Path dir = dirExists();
//then //then
assertThatExceptionOfType(GitDBRepoNotFoundException.class) assertThatExceptionOfType(InvalidRepositoryException.class)
.isThrownBy(() -> GitDB.openLocal(dir)) .isThrownBy(() -> GitDB.openLocal(dir))
.withMessageContaining(dir.toString()); .withMessageContaining(dir.toString());
} }