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(); void close();
Path getGitDir();
String get(Branch branch, Key key); String get(Branch branch, Key key);
<T> T get(Branch branch, Key key, Class<T> type); <T> T get(Branch branch, Key key, Class<T> type);

View file

@ -1,31 +1,48 @@
package net.kemitix.gitdb; package net.kemitix.gitdb;
import lombok.Getter; import lombok.SneakyThrows;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.*;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Optional;
import java.util.stream.Stream; import java.util.stream.Stream;
class GitDBLocal implements GitDB { class GitDBLocal implements GitDB {
@Getter
private final Repository repository; private final Repository repository;
@Getter @SneakyThrows
private final Path gitDir; GitDBLocal(final Path gitDir) {
this.repository = openRepo(gitDir)
.orElseGet(() -> initRepo(gitDir));
}
GitDBLocal(final Path gitDir) throws GitAPIException { @SneakyThrows
this.gitDir = gitDir; private Repository initRepo(Path gitDir) {
this.repository = Git return Git.init()
.init()
.setBare(true)
.setGitDir(gitDir.toFile()) .setGitDir(gitDir.toFile())
.setBare(true)
.call() .call()
.getRepository(); .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 @Override
public void close() { public void close() {
repository.close(); repository.close();
@ -33,7 +50,7 @@ class GitDBLocal implements GitDB {
@Override @Override
public String get(Branch branch, Key key) { public String get(Branch branch, Key key) {
return null; return get(branch, key, String.class);
} }
@Override @Override
@ -43,7 +60,7 @@ class GitDBLocal implements GitDB {
@Override @Override
public Stream<String> getFiles(Branch branch, Key key) { public Stream<String> getFiles(Branch branch, Key key) {
return null; return getFiles(branch, key, String.class);
} }
@Override @Override

View file

@ -1,15 +1,11 @@
package net.kemitix.gitdb; 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.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Comparator;
import java.util.UUID; import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat; 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 Key key = Key.name(UUID.randomUUID().toString());
private final Author author = Author.name("junit", "gitdb@kemitix.net"); private final Author author = Author.name("junit", "gitdb@kemitix.net");
GitDBTest() throws IOException, GitAPIException { GitDBTest() throws IOException {
} }
@Test @Test
void shouldInitialiseGitDB() { void shouldInitialiseGitDB() throws IOException {
//then //then
assertThat(gitDB).isNotNull(); assertThat(gitDB).isNotNull();
assertThat(gitDB.getGitDir()).isDirectory() assertThat(Files.isDirectory(dbDir)).isTrue();
.isEqualTo(dbDir); assertThat(Files.newDirectoryStream(dbDir).iterator())
final Repository repository = gitDB.getRepository(); .contains(
assertThat(repository.isBare()).isTrue(); dbDir.resolve("branches"),
assertThat(repository.getObjectDatabase().exists()).isTrue(); dbDir.resolve("HEAD"),
assertThat(repository.getRefDatabase()).isNotNull(); dbDir.resolve("config"),
dbDir.resolve("refs"),
dbDir.resolve("logs"),
dbDir.resolve("hooks"),
dbDir.resolve("objects")
);
} }
@Test @Test