From 297a952f431a10155d4ca42ce0ee26855ca78234 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 19 Jul 2018 17:26:45 +0100 Subject: [PATCH] InitGitDBRepo: refactored --- .../net/kemitix/gitdb/impl/InitGitDBRepo.java | 98 +++++++++++-------- .../kemitix/gitdb/impl/InitGitDBRepoTest.java | 18 ++++ 2 files changed, 73 insertions(+), 43 deletions(-) create mode 100644 src/test/java/net/kemitix/gitdb/impl/InitGitDBRepoTest.java diff --git a/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java index c38b1a4..ac71878 100644 --- a/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java @@ -23,6 +23,7 @@ package net.kemitix.gitdb.impl; import net.kemitix.gitdb.FormatVersion; import net.kemitix.mon.result.Result; +import net.kemitix.mon.result.WithResultContinuation; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryCache; @@ -31,13 +32,16 @@ import org.eclipse.jgit.util.FS; import java.io.File; import java.nio.charset.StandardCharsets; import java.nio.file.*; +import java.util.concurrent.Callable; + +import static net.kemitix.conditional.Condition.where; /** * Initialise a new GitDB Repo. * * @author Paul Campbell (pcampbell@kemitix.net) */ -class InitGitDBRepo { +final class InitGitDBRepo { private static final String INIT_MESSAGE = "Initialise GitDB v1"; private static final String INIT_USER = "GitDB"; @@ -46,6 +50,10 @@ class InitGitDBRepo { private static final String GIT_DB_VERSION = "GitDB.Version"; private static final String REFS_HEADS_FORMAT = "refs/heads/%s"; + private InitGitDBRepo() { + throw new UnsupportedOperationException(); + } + /** * Initialise a new GitDB repo. * @@ -53,69 +61,73 @@ class InitGitDBRepo { * @return a Result containing the created Repository */ static Result create(final Path dbDir) { - final InitGitDBRepo initRepo = new InitGitDBRepo(); - return initRepo.validDbDir(dbDir.toFile()) + return validDbDir(dbDir.toFile()) .peek(File::mkdirs) - .map(dir -> RepositoryCache.FileKey.exact(dir, FS.DETECTED)) - .andThen(fileKey -> () -> fileKey.open(false)) - .thenWith(repository -> () -> repository.create(true)) - .thenWith(repository -> () -> initRepo.createInitialBranchOnMaster(repository)); + .map(InitGitDBRepo::exactDirectory) + .andThen(InitGitDBRepo::openRepository) + .thenWith(InitGitDBRepo::createRepoDirectory) + .thenWith(InitGitDBRepo::createInitialMasterBranch); } - private Result validDbDir(final File dbDir) { + private static Result validDbDir(final File dbDir) { return Result.ok(dbDir) - .flatMap(this::verifyIsNotAFile) - .flatMap(this::isEmptyIfExists); + .flatMap(InitGitDBRepo::isNotAFile) + .flatMap(InitGitDBRepo::ifExistsThenIsEmpty); } - private Result createInitialBranchOnMaster(final Repository repository) { - final GitDBRepo repo = new GitDBRepo(repository); - return new ValueWriter(repository) - .write(new FormatVersion().toBytes()) - .flatMap(oid -> repo.insertNewTree(GIT_DB_VERSION, oid)) - .flatMap(tid -> repo.initialCommit(tid, INIT_MESSAGE, INIT_USER, INIT_EMAIL)) - .flatMap(cid -> Result.of(() -> { - createBranch(repository, cid, MASTER); - return null; - })); + private static Result isNotAFile(final File dbDir) { + return Result.ok(dbDir) + .thenWith(dir -> () -> where(dir.isFile()).thenThrow(new NotDirectoryException(dbDir.toString()))); } - private Result verifyIsNotAFile(final File dbDir) { - if (dbDir.isFile()) { - return Result.error(new NotDirectoryException(dbDir.toString())); - } - return Result.ok(dbDir); - } - - private Result isEmptyIfExists(final File dbDir) { - if (dbDir.exists()) { - return Result.of(() -> { + private static Result ifExistsThenIsEmpty(final File dbDir) { + return Result.ok(dbDir) + .thenWith(dir -> () -> { + if (dir.exists()) { try (DirectoryStream directoryStream = Files.newDirectoryStream(dbDir.toPath())) { - if (directoryStream.iterator().hasNext()) { - throw new DirectoryNotEmptyException(dbDir.toString()); - } + where(directoryStream.iterator().hasNext()) + .thenThrow(new DirectoryNotEmptyException(dbDir.toString())); } - return dbDir; } - ); - } - return Result.ok(dbDir); + }); } - private Result createBranch( + private static RepositoryCache.FileKey exactDirectory(final File dir) { + return RepositoryCache.FileKey.exact(dir, FS.DETECTED); + } + + private static Callable openRepository(final RepositoryCache.FileKey fileKey) { + return () -> fileKey.open(false); + } + + private static WithResultContinuation createRepoDirectory(final Repository repository) { + return () -> repository.create(true); + } + + private static WithResultContinuation createInitialMasterBranch( + final Repository repository + ) { + return () -> { + final GitDBRepo repo = new GitDBRepo(repository); + new ValueWriter(repository) + .write(new FormatVersion().toBytes()) + .flatMap(oid -> repo.insertNewTree(GIT_DB_VERSION, oid)) + .flatMap(tid -> repo.initialCommit(tid, INIT_MESSAGE, INIT_USER, INIT_EMAIL)) + .flatMap(cid -> createBranch(repository, cid, MASTER)); + }; + } + + private static Result createBranch( final Repository repository, final ObjectId commitId, final String branchName ) { final Path branchRefPath = branchRefPath(repository, branchName); final byte[] commitIdBytes = commitId.name().getBytes(StandardCharsets.UTF_8); - return Result.of(() -> { - Files.write(branchRefPath, commitIdBytes); - return null; - }); + return Result.of(() -> Files.write(branchRefPath, commitIdBytes)); } - private Path branchRefPath( + private static Path branchRefPath( final Repository repository, final String branchName ) { diff --git a/src/test/java/net/kemitix/gitdb/impl/InitGitDBRepoTest.java b/src/test/java/net/kemitix/gitdb/impl/InitGitDBRepoTest.java new file mode 100644 index 0000000..8bbec03 --- /dev/null +++ b/src/test/java/net/kemitix/gitdb/impl/InitGitDBRepoTest.java @@ -0,0 +1,18 @@ +package net.kemitix.gitdb.impl; + +import org.assertj.core.api.WithAssertions; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Constructor; + +class InitGitDBRepoTest implements WithAssertions { + + @Test + void utilityClassCannotBeInstantiated() throws NoSuchMethodException { + final Constructor constructor = InitGitDBRepo.class.getDeclaredConstructor(); + constructor.setAccessible(true); + assertThatCode(constructor::newInstance) + .hasCauseInstanceOf(UnsupportedOperationException.class); + } + +} \ No newline at end of file