From 7f8dde5bd3fcb57f07a5ab7f4198b650ad4ff865 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 20 Jul 2018 07:47:18 +0100 Subject: [PATCH] HeadWriter: refactored --- .../net/kemitix/gitdb/impl/HeadWriter.java | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/kemitix/gitdb/impl/HeadWriter.java b/src/main/java/net/kemitix/gitdb/impl/HeadWriter.java index f7f668d..f1f8fb8 100644 --- a/src/main/java/net/kemitix/gitdb/impl/HeadWriter.java +++ b/src/main/java/net/kemitix/gitdb/impl/HeadWriter.java @@ -50,23 +50,38 @@ class HeadWriter { * @return the Ref of the new branch */ Result write(final String branchName, final ObjectId commitId) { - final Path branchRefPath = branchRefPath(branchName).toAbsolutePath(); - final byte[] commitIdBytes = commitId.name().getBytes(StandardCharsets.UTF_8); - return Result.of(() -> { - Files.write(branchRefPath, commitIdBytes); - return repository.findRef(branchName); - }); + return writeRef(branchName, commitId, repository) + .flatMap(x -> findRef(branchName, repository)); } - private Path branchRefPath(String branchName) { - return gitDirPath().resolve(branchName); + private static Result writeRef( + final String branchName, + final ObjectId commitId, + final Repository repository + ) { + return Result.of(() -> + Files.write( + branchRefPath(branchName, repository).toAbsolutePath(), + commitIdBytes(commitId))); } - private Path gitDirPath() { - return gitDir().toPath(); + private static Result findRef(final String branchName, final Repository repository) { + return Result.of(() -> repository.findRef(branchName)); } - private File gitDir() { + private static Path branchRefPath(final String branchName, final Repository repository) { + return gitDirPath(repository).resolve(branchName); + } + + private static byte[] commitIdBytes(final ObjectId commitId) { + return commitId.name().getBytes(StandardCharsets.UTF_8); + } + + private static Path gitDirPath(final Repository repository) { + return gitDir(repository).toPath(); + } + + private static File gitDir(final Repository repository) { return repository.getDirectory(); } }