HeadWriter: refactored

This commit is contained in:
Paul Campbell 2018-07-20 07:47:18 +01:00
parent 058572ab91
commit 7f8dde5bd3

View file

@ -50,23 +50,38 @@ class HeadWriter {
* @return the Ref of the new branch * @return the Ref of the new branch
*/ */
Result<Ref> write(final String branchName, final ObjectId commitId) { Result<Ref> write(final String branchName, final ObjectId commitId) {
final Path branchRefPath = branchRefPath(branchName).toAbsolutePath(); return writeRef(branchName, commitId, repository)
final byte[] commitIdBytes = commitId.name().getBytes(StandardCharsets.UTF_8); .flatMap(x -> findRef(branchName, repository));
return Result.of(() -> {
Files.write(branchRefPath, commitIdBytes);
return repository.findRef(branchName);
});
} }
private Path branchRefPath(String branchName) { private static Result<Path> writeRef(
return gitDirPath().resolve(branchName); final String branchName,
final ObjectId commitId,
final Repository repository
) {
return Result.of(() ->
Files.write(
branchRefPath(branchName, repository).toAbsolutePath(),
commitIdBytes(commitId)));
} }
private Path gitDirPath() { private static Result<Ref> findRef(final String branchName, final Repository repository) {
return gitDir().toPath(); 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(); return repository.getDirectory();
} }
} }