Extract HeadWriter

This commit is contained in:
Paul Campbell 2018-06-15 19:26:02 +01:00
parent 9b8c4765ae
commit 8f044e6b77
2 changed files with 66 additions and 18 deletions

View file

@ -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());
}
}

View file

@ -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);
}
}