diff --git a/src/main/java/net/kemitix/gitdb/GitDB.java b/src/main/java/net/kemitix/gitdb/GitDB.java index 83f6fc6..4511917 100644 --- a/src/main/java/net/kemitix/gitdb/GitDB.java +++ b/src/main/java/net/kemitix/gitdb/GitDB.java @@ -21,9 +21,6 @@ package net.kemitix.gitdb; -import org.eclipse.jgit.api.Git; -import org.eclipse.jgit.lib.Repository; - import java.io.IOException; import java.nio.file.Path; import java.util.Optional; @@ -44,10 +41,12 @@ public interface GitDB { * @return a GitDB instance for the created local gitdb * @throws IOException if there {@code dbDir} is a file or a non-empty directory */ - static GitDB initLocal(final Path dbDir, final String userName, final String userEmailAddress) throws IOException { - return new GitDBLocal( - dbDir.toFile(), userName, userEmailAddress - ); + static GitDB initLocal( + final Path dbDir, + final String userName, + final String userEmailAddress + ) throws IOException { + return GitDBLocal.init(dbDir, userName, userEmailAddress); } /** @@ -58,16 +57,10 @@ public interface GitDB { * @param userEmailAddress the user email address * @return a GitDB instance for the local gitdb */ - static GitDBLocal openLocal(final Path dbDir, final String userName, final String userEmailAddress) { - try { - return Optional.of(Git.open(dbDir.toFile())) - .map(Git::getRepository) - .filter(Repository::isBare) - .map(repository -> new GitDBLocal(repository, userName, userEmailAddress)) - .orElseThrow(() -> new InvalidRepositoryException("Not a bare repo", dbDir)); - } catch (IOException e) { - throw new InvalidRepositoryException("Error opening repository", dbDir, e); - } + static GitDB openLocal(final Path dbDir, final String userName, final String userEmailAddress) { + return GitDBLocal.open(dbDir, userName, userEmailAddress); + + } /** diff --git a/src/main/java/net/kemitix/gitdb/GitDBLocal.java b/src/main/java/net/kemitix/gitdb/GitDBLocal.java index ae12c83..601940e 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBLocal.java +++ b/src/main/java/net/kemitix/gitdb/GitDBLocal.java @@ -21,6 +21,8 @@ package net.kemitix.gitdb; +import lombok.RequiredArgsConstructor; +import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.*; import org.eclipse.jgit.util.FS; @@ -38,40 +40,62 @@ import java.util.Optional; * * @author Paul Campbell (pcampbell@kemitix.net) */ - +@RequiredArgsConstructor class GitDBLocal implements GitDB { + private static final String INIT_MESSAGE = "Initialise GitDB v1"; + private static final String INIT_USER = "GitDB"; + private static final String INIT_EMAIL = "pcampbell@kemitix.net"; + private static final String MASTER = "master"; + private static final String IS_GIT_DB = "isGitDB"; + private static final String NOT_A_BARE_REPO = "Not a bare repo"; + private static final String ERROR_OPENING_REPOSITORY = "Error opening repository"; + private static final String REFS_HEADS_FORMAT = "refs/heads/%s"; + private final Repository repository; private final String userName; private final String userEmailAddress; + /** + * Create a new GitDB instance using the Git repo. + * + * @param dbDir the path to instantiate the git repo in + * @param userName the user name + * @param userEmailAddress the user email address + * @return a GitDB instance for the created local gitdb + */ + static GitDB open( + final Path dbDir, + final String userName, + final String userEmailAddress + ) { + try { + return Optional.of(Git.open(dbDir.toFile())) + .map(Git::getRepository) + .filter(Repository::isBare) + .map(repository -> new GitDBLocal(repository, userName, userEmailAddress)) + .orElseThrow(() -> new InvalidRepositoryException(NOT_A_BARE_REPO, dbDir)); + } catch (IOException e) { + throw new InvalidRepositoryException(ERROR_OPENING_REPOSITORY, dbDir, e); + } + } + /** * Create a new GitDB instance, while initialising a new git repo. * * @param dbDir the path to instantiate the git repo in * @param userName the user name * @param userEmailAddress the user email address + * @return a GitDB instance for the created local gitdb * @throws IOException if there {@code dbDir} is a file or a non-empty directory */ - GitDBLocal( - final File dbDir, + static GitDB init( + final Path dbDir, final String userName, final String userEmailAddress ) throws IOException { - this(GitDBLocal.initRepo(validDbDir(dbDir)), userName, userEmailAddress); - } - - /** - * Create a new GitDB instance using the Git repo. - * - * @param repository the Git repository - * @param userName the user name - * @param userEmailAddress the user email address - */ - GitDBLocal(final Repository repository, final String userName, final String userEmailAddress) { - this.repository = repository; - this.userName = userName; - this.userEmailAddress = userEmailAddress; + initRepo(validDbDir(dbDir.toFile())); + return open(dbDir, userName, userEmailAddress); } private static File validDbDir(final File dbDir) throws IOException { @@ -94,31 +118,19 @@ class GitDBLocal implements GitDB { } } - @Override - public Optional branch(final String name) throws IOException { - return Optional.ofNullable(repository.findRef(name)) - .map(ref -> GitDBBranch.withRef(ref, GitDBRepo.in(repository), userName, userEmailAddress)); - } - - private static Repository initRepo(final File dbDir) throws IOException { + private static void initRepo(final File dbDir) throws IOException { dbDir.mkdirs(); final Repository repository = RepositoryCache.FileKey.exact(dbDir, FS.DETECTED).open(false); repository.create(true); createInitialBranchOnMaster(repository); - return repository; } private static void createInitialBranchOnMaster(final Repository repository) throws IOException { final GitDBRepo repo = GitDBRepo.in(repository); final ObjectId objectId = repo.insertBlob(new byte[0]); - final ObjectId treeId = repo.insertNewTree("isGitDB", objectId); - final ObjectId commitId = repo.insertCommit( - treeId, - "Initialise GitDB v1", - "GitDB", - "pcampbell@kemitix.net", - ObjectId.zeroId()); - createBranch(repository, commitId, "master"); + final ObjectId treeId = repo.insertNewTree(IS_GIT_DB, objectId); + final ObjectId commitId = repo.insertCommit(treeId, INIT_MESSAGE, INIT_USER, INIT_EMAIL, ObjectId.zeroId()); + createBranch(repository, commitId, MASTER); } private static void createBranch( @@ -126,13 +138,25 @@ class GitDBLocal implements GitDB { final ObjectId commitId, final String branchName ) throws IOException { - final Path branchRefPath = repository - .getDirectory() - .toPath() - .resolve("refs/heads/" + branchName) - .toAbsolutePath(); + final Path branchRefPath = branchRefPath(repository, branchName); final byte[] commitIdBytes = commitId.name().getBytes(StandardCharsets.UTF_8); Files.write(branchRefPath, commitIdBytes); } + private static Path branchRefPath( + final Repository repository, + final String branchName + ) { + return repository.getDirectory() + .toPath() + .resolve(String.format(REFS_HEADS_FORMAT, branchName)) + .toAbsolutePath(); + } + + @Override + public Optional branch(final String name) throws IOException { + return Optional.ofNullable(repository.findRef(name)) + .map(ref -> GitDBBranch.withRef(ref, GitDBRepo.in(repository), userName, userEmailAddress)); + } + } diff --git a/src/test/java/net/kemitix/gitdb/GitDBTest.java b/src/test/java/net/kemitix/gitdb/GitDBTest.java index ba569d4..05c05c6 100644 --- a/src/test/java/net/kemitix/gitdb/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/GitDBTest.java @@ -153,7 +153,7 @@ class GitDBTest implements WithAssertions { //given final Path dir = gitDBRepoPath(); //when - final GitDBLocal gitDB = GitDB.openLocal(dir, userName, userEmailAddress); + final GitDB gitDB = GitDB.openLocal(dir, userName, userEmailAddress); //then assertThat(gitDB).isNotNull(); }