From ae852be86f86df982ea35c1655dbc4306d0e9466 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 30 Jun 2018 20:47:00 +0100 Subject: [PATCH] CommitWriter and GitDBRepo return Result values --- .../net/kemitix/gitdb/impl/CommitWriter.java | 15 +++++-------- .../net/kemitix/gitdb/impl/GitDBRepo.java | 22 ++++++------------- .../net/kemitix/gitdb/impl/InitGitDBRepo.java | 2 +- 3 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/main/java/net/kemitix/gitdb/impl/CommitWriter.java b/src/main/java/net/kemitix/gitdb/impl/CommitWriter.java index 4f9cf11..9b2dd07 100644 --- a/src/main/java/net/kemitix/gitdb/impl/CommitWriter.java +++ b/src/main/java/net/kemitix/gitdb/impl/CommitWriter.java @@ -21,10 +21,9 @@ package net.kemitix.gitdb.impl; +import net.kemitix.mon.result.Result; import org.eclipse.jgit.lib.*; -import java.io.IOException; - /** * Commits Key/Value updates into the Git Repository. * @@ -52,15 +51,14 @@ class CommitWriter { * @param userName the user name * @param userEmailAddress the user email address * @return the id of the commit - * @throws IOException if there is an error writing the value */ - ObjectId write( + Result write( final ObjectId treeId, final ObjectId parentId, final String message, final String userName, final String userEmailAddress - ) throws IOException { + ) { final CommitBuilder commitBuilder = new CommitBuilder(); commitBuilder.setTreeId(treeId); commitBuilder.setMessage(message); @@ -68,7 +66,7 @@ class CommitWriter { commitBuilder.setAuthor(ident); commitBuilder.setCommitter(ident); commitBuilder.setParentId(parentId); - return objectInserter.insert(commitBuilder); + return Result.of(() -> objectInserter.insert(commitBuilder)); } /** @@ -82,15 +80,14 @@ class CommitWriter { * @param userName the user name * @param userEmailAddress the user email address * @return the id of the commit - * @throws IOException if there is an error writing the value */ - ObjectId write( + Result write( final ObjectId treeId, final Ref branchRef, final String message, final String userName, final String userEmailAddress - ) throws IOException { + ) { return write(treeId, branchRef.getObjectId(), message, userName, userEmailAddress); } } diff --git a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java index 3a9e1ab..8bdedce 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java @@ -27,7 +27,6 @@ import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; -import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.stream.Stream; @@ -120,7 +119,6 @@ class GitDBRepo { * @param userName the user name * @param userEmailAddress the user email address * @return the Ref of the updated branch - * @throws IOException if there was an error writing the branch */ Result writeCommit( final Ref branchRef, @@ -129,12 +127,9 @@ class GitDBRepo { final String userName, final String userEmailAddress ) { - try { - final ObjectId commitId = insertCommit(tree, message, userName, userEmailAddress, branchRef); - return Result.ok(headWriter.write(branchRef.getName(), commitId)); - } catch (IOException e) { - return Result.error(e); - } + return insertCommit(tree, message, userName, userEmailAddress, branchRef) + .flatMap(cid -> Result.of(() -> + headWriter.write(branchRef.getName(), cid))); } /** @@ -146,15 +141,14 @@ class GitDBRepo { * @param userName the user name * @param userEmailAddress the user email address * @return the id of the commit - * @throws IOException the commit could not be stored */ - ObjectId insertCommit( + Result insertCommit( final ObjectId treeId, final String message, final String userName, final String userEmailAddress, final Ref branchRef - ) throws IOException { + ) { return commitWriter.write(treeId, branchRef, message, userName, userEmailAddress); } @@ -166,14 +160,13 @@ class GitDBRepo { * @param initUser the user name * @param initEmail the user email address * @return the id of the commit - * @throws IOException if there was an error writing the commit */ - ObjectId initialCommit( + Result initialCommit( final ObjectId treeId, final String initMessage, final String initUser, final String initEmail - ) throws IOException { + ) { return commitWriter.write(treeId, ObjectId.zeroId(), initMessage, initUser, initEmail); } @@ -186,7 +179,6 @@ class GitDBRepo { * @param key the key to place the value under * @return an Optional containing the id of the updated tree containing the update, if the key was found, or an * empty Optional if there key was not found, the there was no changes made - * @throws IOException if there was an error writing the value */ Result> removeKey(final Ref branchRef, final String key) { return keyRemover.remove(branchRef, key); diff --git a/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java index 41004bd..ae39f7b 100644 --- a/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java @@ -84,7 +84,7 @@ class InitGitDBRepo { return new ValueWriter(repository) .write(new FormatVersion().toBytes()) .flatMap(oid -> repo.insertNewTree(GIT_DB_VERSION, oid)) - .flatMap(tid -> Result.of(() -> repo.initialCommit(tid, INIT_MESSAGE, INIT_USER, INIT_EMAIL))) + .flatMap(tid -> repo.initialCommit(tid, INIT_MESSAGE, INIT_USER, INIT_EMAIL)) .flatMap(cid -> Result.of(() -> { createBranch(repository, cid, MASTER); return null;