Extract CommitWriter

This commit is contained in:
Paul Campbell 2018-06-14 22:52:00 +01:00
parent d392295401
commit 9d567ae8d0
3 changed files with 123 additions and 13 deletions

View file

@ -0,0 +1,96 @@
/*
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;
import org.eclipse.jgit.lib.*;
import java.io.IOException;
/**
* Commits Key/Value updates into the Git Repository.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
class CommitWriter {
private final ObjectInserter objectInserter;
/**
* Create new instance of this class.
*
* @param repository the repository to write commits to
*/
CommitWriter(final Repository repository) {
objectInserter = repository.getObjectDatabase().newInserter();
}
/**
* Write a commit into the repository.
*
* @param treeId the tree to commit
* @param parentId the id of the parent commit
* @param message the message
* @param userName the user name
* @param userEmailAddress the user email address
* @return the id of the commit
* @throws IOException if there is an error writing the value
*/
ObjectId write(
final ObjectId treeId,
final ObjectId parentId,
final String message,
final String userName,
final String userEmailAddress
) throws IOException {
final CommitBuilder commitBuilder = new CommitBuilder();
commitBuilder.setTreeId(treeId);
commitBuilder.setMessage(message);
final PersonIdent ident = new PersonIdent(userName, userEmailAddress);
commitBuilder.setAuthor(ident);
commitBuilder.setCommitter(ident);
commitBuilder.setParentId(parentId);
return objectInserter.insert(commitBuilder);
}
/**
* Write a commit into the repository.
*
* <p>N.B. While this adds the commit to a branch, it doesn't update the branch itself.</p>
*
* @param treeId the tree to commit
* @param branchRef the branch to add the commit to
* @param message the message
* @param userName the user name
* @param userEmailAddress the user email address
* @return the id of the commit
* @throws IOException if there is an error writing the value
*/
ObjectId write(
final ObjectId treeId,
final Ref branchRef,
final String message,
final String userName,
final String userEmailAddress
) throws IOException {
return write(treeId, branchRef.getObjectId(), message, userName, userEmailAddress);
}
}

View file

@ -40,6 +40,7 @@ class GitDBRepo {
private final Repository repository;
private final ValueWriter valueWriter;
private final KeyWriter keyWriter;
private final CommitWriter commitWriter;
/**
* Creates a new instance of this class.
@ -50,6 +51,7 @@ class GitDBRepo {
this.repository = repository;
valueWriter = new ValueWriter(repository);
keyWriter = new KeyWriter(repository);
commitWriter = new CommitWriter(repository);
}
/**
@ -99,10 +101,10 @@ class GitDBRepo {
* Insert a commit into the store, returning its unique id.
*
* @param treeId id of the tree
* @param branchRef the branch to commit to
* @param message the message
* @param userName the user name
* @param userEmailAddress the user email address
* @param parent the commit to link to as parent
* @return the id of the commit
* @throws IOException the commit could not be stored
*/
@ -111,16 +113,9 @@ class GitDBRepo {
final String message,
final String userName,
final String userEmailAddress,
final AnyObjectId parent
final Ref branchRef
) throws IOException {
final CommitBuilder commitBuilder = new CommitBuilder();
commitBuilder.setTreeId(treeId);
commitBuilder.setMessage(message);
final PersonIdent ident = new PersonIdent(userName, userEmailAddress);
commitBuilder.setAuthor(ident);
commitBuilder.setCommitter(ident);
commitBuilder.setParentId(parent);
return repository.getObjectDatabase().newInserter().insert(commitBuilder);
return commitWriter.write(treeId, branchRef, message, userName, userEmailAddress);
}
private Ref writeHead(
@ -182,7 +177,7 @@ class GitDBRepo {
* @param tree the tree to commit onto the branch
* @param message the commit message
* @param userName the user name
* @param userEmailAddress the use email address
* @param userEmailAddress the user email address
* @return the Ref of the updated branch
* @throws IOException if there was an error writing the branch
*/
@ -193,7 +188,26 @@ class GitDBRepo {
final String userName,
final String userEmailAddress
) throws IOException {
final ObjectId commitId = insertCommit(tree, message, userName, userEmailAddress, branchRef.getObjectId());
final ObjectId commitId = insertCommit(tree, message, userName, userEmailAddress, branchRef);
return writeHead(branchRef.getName(), commitId);
}
/**
* Writes the initial commit into a repo (no parent).
*
* @param treeId the to commit
* @param initMessage the commit message
* @param initUser the user name
* @param initEmail the user email address
* @return the id of the commit
* @throws IOException if there was an error writing the commit
*/
ObjectId initialCommit(
final ObjectId treeId,
final String initMessage,
final String initUser,
final String initEmail
) throws IOException {
return commitWriter.write(treeId, ObjectId.zeroId(), initMessage, initUser, initEmail);
}
}

View file

@ -67,7 +67,7 @@ class InitGitDBRepo {
final GitDBRepo repo = new GitDBRepo(repository);
final ObjectId objectId = repo.insertBlob(new byte[0]);
final ObjectId treeId = repo.insertNewTree(IS_GIT_DB, objectId);
final ObjectId commitId = repo.insertCommit(treeId, INIT_MESSAGE, INIT_USER, INIT_EMAIL, ObjectId.zeroId());
final ObjectId commitId = repo.initialCommit(treeId, INIT_MESSAGE, INIT_USER, INIT_EMAIL);
createBranch(repository, commitId, MASTER);
}