Rewrite repo creation

This commit is contained in:
Paul Campbell 2018-05-13 11:09:20 +01:00
parent 04d7acc06c
commit c841393263
3 changed files with 43 additions and 27 deletions

View file

@ -11,8 +11,6 @@ public interface GitDB {
void close();
Path getGitDir();
String get(Branch branch, Key key);
<T> T get(Branch branch, Key key, Class<T> type);

View file

@ -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<Repository> 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<String> getFiles(Branch branch, Key key) {
return null;
return getFiles(branch, key, String.class);
}
@Override

View file

@ -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