From 8f044e6b77fc2ae4d0d468febe9eb1e30daf5770 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 15 Jun 2018 19:26:02 +0100 Subject: [PATCH] Extract HeadWriter --- .../net/kemitix/gitdb/impl/GitDBRepo.java | 22 ++----- .../net/kemitix/gitdb/impl/HeadWriter.java | 62 +++++++++++++++++++ 2 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 src/main/java/net/kemitix/gitdb/impl/HeadWriter.java diff --git a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java index 1eb03c1..c7e1102 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java @@ -26,8 +26,6 @@ import org.eclipse.jgit.lib.*; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.*; /** @@ -42,6 +40,7 @@ class GitDBRepo { private final KeyWriter keyWriter; private final CommitWriter commitWriter; private final KeyRemover keyRemover; + private final HeadWriter headWriter; /** * Creates a new instance of this class. @@ -54,6 +53,7 @@ class GitDBRepo { keyWriter = new KeyWriter(repository); commitWriter = new CommitWriter(repository); keyRemover = new KeyRemover(repository); + headWriter = new HeadWriter(repository); } /** @@ -120,20 +120,6 @@ class GitDBRepo { return commitWriter.write(treeId, branchRef, message, userName, userEmailAddress); } - private Ref writeHead( - final String branchName, - final ObjectId commitId - ) throws IOException { - final Path branchRefPath = repository - .getDirectory() - .toPath() - .resolve(branchName) - .toAbsolutePath(); - final byte[] commitIdBytes = commitId.name().getBytes(StandardCharsets.UTF_8); - Files.write(branchRefPath, commitIdBytes); - return repository.findRef(branchName); - } - /** * Reads a value from the branch with the given key. * @@ -191,7 +177,7 @@ class GitDBRepo { final String userEmailAddress ) throws IOException { final ObjectId commitId = insertCommit(tree, message, userName, userEmailAddress, branchRef); - return writeHead(branchRef.getName(), commitId); + return headWriter.write(branchRef.getName(), commitId); } /** @@ -237,6 +223,6 @@ class GitDBRepo { * @throws IOException error writing the branch */ Ref createBranch(final Ref branchRef, final String name) throws IOException { - return writeHead(branchRef.getName(), branchRef.getObjectId()); + return headWriter.write(branchRef.getName(), branchRef.getObjectId()); } } diff --git a/src/main/java/net/kemitix/gitdb/impl/HeadWriter.java b/src/main/java/net/kemitix/gitdb/impl/HeadWriter.java new file mode 100644 index 0000000..39f2107 --- /dev/null +++ b/src/main/java/net/kemitix/gitdb/impl/HeadWriter.java @@ -0,0 +1,62 @@ +/* + The MIT License (MIT) + + Copyright (c) 2018 Paul Campbell + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software + and associated documentation files (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies + or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.gitdb.impl; + +import lombok.RequiredArgsConstructor; +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.nio.file.Files; +import java.nio.file.Path; + +/** + * Writes the head. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +@RequiredArgsConstructor +class HeadWriter { + + private final Repository repository; + + /** + * Writes the head for the named branch for the given commit. + * + * @param branchName the branch name + * @param commitId the commit to point the branch at + * @return the Ref of the new branch + * @throws IOException error writing branch head + */ + Ref write(final String branchName, final ObjectId commitId) throws IOException { + final Path branchRefPath = repository + .getDirectory() + .toPath() + .resolve(branchName) + .toAbsolutePath(); + final byte[] commitIdBytes = commitId.name().getBytes(StandardCharsets.UTF_8); + Files.write(branchRefPath, commitIdBytes); + return repository.findRef(branchName); + } +}