From 98b81a44dcd02349db150c1f9952047a0a95c98e Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 26 Jun 2018 23:06:03 +0100 Subject: [PATCH] NamedRevBlob returns Result values --- .../net/kemitix/gitdb/impl/GitDBRepo.java | 20 ++++++++++--------- .../net/kemitix/gitdb/impl/NamedRevBlob.java | 13 +++++++++--- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java index 7af698b..ffc12c3 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java @@ -21,7 +21,6 @@ package net.kemitix.gitdb.impl; -import lombok.val; import net.kemitix.mon.maybe.Maybe; import net.kemitix.mon.result.Result; import org.eclipse.jgit.lib.ObjectId; @@ -30,6 +29,7 @@ import org.eclipse.jgit.lib.Repository; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.stream.Stream; /** * Wrapper for interacting with the GitDB Repository. @@ -85,15 +85,17 @@ class GitDBRepo { final Ref branchRef, final String key ) { + final GitTreeReader treeFilter = new GitTreeReader(repository).treeFilter(key); + return streamTree(branchRef, treeFilter).flatMap(s -> + Result.invert(s.findFirst() + .map(NamedRevBlob::blobAsString) + .map(Maybe::just) + .orElseGet(Maybe::nothing))); + } + + private Result> streamTree(final Ref branchRef, final GitTreeReader treeFilter) { try { - val blob = new GitTreeReader(repository) - .treeFilter(key) - .stream(branchRef) - .findFirst(); - if (blob.isPresent()) { - return Result.ok(Maybe.just(blob.get().blobAsString())); - } - return Result.ok(Maybe.nothing()); + return Result.ok(treeFilter.stream(branchRef)); } catch (IOException e) { return Result.error(e); } diff --git a/src/main/java/net/kemitix/gitdb/impl/NamedRevBlob.java b/src/main/java/net/kemitix/gitdb/impl/NamedRevBlob.java index 09a39ec..d8376b6 100644 --- a/src/main/java/net/kemitix/gitdb/impl/NamedRevBlob.java +++ b/src/main/java/net/kemitix/gitdb/impl/NamedRevBlob.java @@ -24,7 +24,9 @@ package net.kemitix.gitdb.impl; import lombok.AccessLevel; import lombok.Getter; import lombok.RequiredArgsConstructor; +import net.kemitix.mon.result.Result; import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectLoader; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevBlob; @@ -48,10 +50,15 @@ class NamedRevBlob { * Converts the blob to a String. * * @return a string - * @throws IOException of there was an error reading the blob */ - String blobAsString() throws IOException { - return new String(repository.open(revBlob.getId(), Constants.OBJ_BLOB).getBytes()); + Result blobAsString() { + try { + return Result.ok(repository.open(revBlob.getId(), Constants.OBJ_BLOB)) + .map(ObjectLoader::getBytes) + .map(String::new); + } catch (IOException e) { + return Result.error(e); + } } }