From c8413932634cda7e601c9071be2c269a2d325808 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 13 May 2018 11:09:20 +0100 Subject: [PATCH] Rewrite repo creation --- src/main/java/net/kemitix/gitdb/GitDB.java | 2 - .../java/net/kemitix/gitdb/GitDBLocal.java | 43 +++++++++++++------ .../java/net/kemitix/gitdb/GitDBTest.java | 25 +++++------ 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/src/main/java/net/kemitix/gitdb/GitDB.java b/src/main/java/net/kemitix/gitdb/GitDB.java index c63decd..810038e 100644 --- a/src/main/java/net/kemitix/gitdb/GitDB.java +++ b/src/main/java/net/kemitix/gitdb/GitDB.java @@ -11,8 +11,6 @@ public interface GitDB { void close(); - Path getGitDir(); - String get(Branch branch, Key key); T get(Branch branch, Key key, Class type); diff --git a/src/main/java/net/kemitix/gitdb/GitDBLocal.java b/src/main/java/net/kemitix/gitdb/GitDBLocal.java index da4ff8f..f66f290 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBLocal.java +++ b/src/main/java/net/kemitix/gitdb/GitDBLocal.java @@ -1,31 +1,48 @@ package net.kemitix.gitdb; -import lombok.Getter; +import lombok.SneakyThrows; import org.eclipse.jgit.api.Git; -import org.eclipse.jgit.api.errors.GitAPIException; -import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.*; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; +import java.io.IOException; import java.nio.file.Path; +import java.util.Optional; import java.util.stream.Stream; class GitDBLocal implements GitDB { - @Getter private final Repository repository; - @Getter - private final Path gitDir; + @SneakyThrows + GitDBLocal(final Path gitDir) { + this.repository = openRepo(gitDir) + .orElseGet(() -> initRepo(gitDir)); + } - GitDBLocal(final Path gitDir) throws GitAPIException { - this.gitDir = gitDir; - this.repository = Git - .init() - .setBare(true) + @SneakyThrows + private Repository initRepo(Path gitDir) { + return Git.init() .setGitDir(gitDir.toFile()) + .setBare(true) .call() .getRepository(); } + private Optional openRepo(final Path gitDir) throws IOException { + final Repository build = new FileRepositoryBuilder() + .setBare() + .setMustExist(false) + .setGitDir(gitDir.toFile()) + .setup() + .build(); + if (build.getObjectDatabase().exists()) { + return Optional.of(build); + } else { + return Optional.empty(); + } + } + @Override public void close() { repository.close(); @@ -33,7 +50,7 @@ class GitDBLocal implements GitDB { @Override public String get(Branch branch, Key key) { - return null; + return get(branch, key, String.class); } @Override @@ -43,7 +60,7 @@ class GitDBLocal implements GitDB { @Override public Stream getFiles(Branch branch, Key key) { - return null; + return getFiles(branch, key, String.class); } @Override diff --git a/src/test/java/net/kemitix/gitdb/GitDBTest.java b/src/test/java/net/kemitix/gitdb/GitDBTest.java index 5b388ec..c82c7d6 100644 --- a/src/test/java/net/kemitix/gitdb/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/GitDBTest.java @@ -1,15 +1,11 @@ package net.kemitix.gitdb; -import org.eclipse.jgit.api.errors.GitAPIException; -import org.eclipse.jgit.lib.Repository; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Comparator; import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; @@ -23,19 +19,24 @@ class GitDBTest { private final Key key = Key.name(UUID.randomUUID().toString()); private final Author author = Author.name("junit", "gitdb@kemitix.net"); - GitDBTest() throws IOException, GitAPIException { + GitDBTest() throws IOException { } @Test - void shouldInitialiseGitDB() { + void shouldInitialiseGitDB() throws IOException { //then assertThat(gitDB).isNotNull(); - assertThat(gitDB.getGitDir()).isDirectory() - .isEqualTo(dbDir); - final Repository repository = gitDB.getRepository(); - assertThat(repository.isBare()).isTrue(); - assertThat(repository.getObjectDatabase().exists()).isTrue(); - assertThat(repository.getRefDatabase()).isNotNull(); + assertThat(Files.isDirectory(dbDir)).isTrue(); + assertThat(Files.newDirectoryStream(dbDir).iterator()) + .contains( + dbDir.resolve("branches"), + dbDir.resolve("HEAD"), + dbDir.resolve("config"), + dbDir.resolve("refs"), + dbDir.resolve("logs"), + dbDir.resolve("hooks"), + dbDir.resolve("objects") + ); } @Test