Refactoring - change API for creating GitDBBranch
This commit is contained in:
parent
8aabdf8be3
commit
f2ee721ec4
2 changed files with 41 additions and 28 deletions
|
@ -23,13 +23,14 @@ package net.kemitix.gitdb;
|
||||||
|
|
||||||
import lombok.AccessLevel;
|
import lombok.AccessLevel;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.eclipse.jgit.lib.AnyObjectId;
|
|
||||||
import org.eclipse.jgit.lib.ObjectId;
|
import org.eclipse.jgit.lib.ObjectId;
|
||||||
import org.eclipse.jgit.lib.Ref;
|
import org.eclipse.jgit.lib.Ref;
|
||||||
|
import org.eclipse.jgit.lib.Repository;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* API for interacting with a branch in a GirDB.
|
* API for interacting with a branch in a GirDB.
|
||||||
|
@ -45,22 +46,29 @@ public class GitDBBranch {
|
||||||
private final String userName;
|
private final String userName;
|
||||||
private final String userEmailAddress;
|
private final String userEmailAddress;
|
||||||
|
|
||||||
/**
|
private static GitDBBranch select(
|
||||||
* Create a new instance of GitDBBranch for the Ref.
|
final Ref branchRef,
|
||||||
*
|
|
||||||
* @param ref the Ref
|
|
||||||
* @param gitDBRepo the GitDBRepo
|
|
||||||
* @param userName the user name
|
|
||||||
* @param userEmailAddress the user email address
|
|
||||||
* @return a GitDBBranch
|
|
||||||
*/
|
|
||||||
public static GitDBBranch withRef(
|
|
||||||
final Ref ref,
|
|
||||||
final GitDBRepo gitDBRepo,
|
final GitDBRepo gitDBRepo,
|
||||||
final String userName,
|
final String userName,
|
||||||
final String userEmailAddress
|
final String userEmailAddress
|
||||||
) {
|
) {
|
||||||
return new GitDBBranch(ref, gitDBRepo, userName, userEmailAddress);
|
return new GitDBBranch(branchRef, gitDBRepo, userName, userEmailAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialise the creation of new GitDBBranch instances.
|
||||||
|
*
|
||||||
|
* @param repository the Git Repository
|
||||||
|
* @param userName the user name to record against changes
|
||||||
|
* @param userEmailAddress the user's email address to record against changes
|
||||||
|
* @return a Function for creating a GitDBBranch when supplied with a Ref for a branch
|
||||||
|
*/
|
||||||
|
static Function<Ref, GitDBBranch> init(
|
||||||
|
final Repository repository,
|
||||||
|
final String userName,
|
||||||
|
final String userEmailAddress
|
||||||
|
) {
|
||||||
|
return ref -> select(ref, GitDBRepo.in(repository), userName, userEmailAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -109,19 +117,13 @@ public class GitDBBranch {
|
||||||
return gitDBRepo.insertTree(branchRef, key, valueId);
|
return gitDBRepo.insertTree(branchRef, key, valueId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ObjectId insertCommit(
|
private ObjectId insertCommit(final ObjectId treeId, final String message) throws IOException {
|
||||||
final ObjectId treeId,
|
final ObjectId headCommitId = branchRef.getObjectId();
|
||||||
final String message
|
return gitDBRepo.insertCommit(treeId, message, userName, userEmailAddress, headCommitId);
|
||||||
) throws IOException {
|
|
||||||
return gitDBRepo.insertCommit(treeId, message, userName, userEmailAddress, head());
|
|
||||||
}
|
|
||||||
|
|
||||||
private AnyObjectId head() {
|
|
||||||
return branchRef.getObjectId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private GitDBBranch updateBranch(final ObjectId commitId) throws IOException {
|
private GitDBBranch updateBranch(final ObjectId commitId) throws IOException {
|
||||||
final Ref updatedRef = gitDBRepo.writeHead(branchRef.getName(), commitId);
|
final Ref updatedRef = gitDBRepo.writeHead(branchRef.getName(), commitId);
|
||||||
return GitDBBranch.withRef(updatedRef, gitDBRepo, userName, userEmailAddress);
|
return select(updatedRef, gitDBRepo, userName, userEmailAddress);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,21 +21,20 @@
|
||||||
|
|
||||||
package net.kemitix.gitdb;
|
package net.kemitix.gitdb;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.eclipse.jgit.api.Git;
|
import org.eclipse.jgit.api.Git;
|
||||||
import org.eclipse.jgit.lib.*;
|
import org.eclipse.jgit.lib.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of GitDB for working with a local Repo.
|
* Implementation of GitDB for working with a local Repo.
|
||||||
*
|
*
|
||||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
*/
|
*/
|
||||||
@RequiredArgsConstructor
|
final class LocalGitDB implements GitDB {
|
||||||
class LocalGitDB implements GitDB {
|
|
||||||
|
|
||||||
private static final String NOT_A_BARE_REPO = "Not a bare repo";
|
private static final String NOT_A_BARE_REPO = "Not a bare repo";
|
||||||
private static final String ERROR_OPENING_REPOSITORY = "Error opening repository";
|
private static final String ERROR_OPENING_REPOSITORY = "Error opening repository";
|
||||||
|
@ -44,6 +43,19 @@ class LocalGitDB implements GitDB {
|
||||||
private final String userName;
|
private final String userName;
|
||||||
private final String userEmailAddress;
|
private final String userEmailAddress;
|
||||||
|
|
||||||
|
private final Function<Ref, GitDBBranch> branchInit;
|
||||||
|
|
||||||
|
private LocalGitDB(
|
||||||
|
final Repository repository,
|
||||||
|
final String userName,
|
||||||
|
final String userEmailAddress
|
||||||
|
) {
|
||||||
|
this.repository = repository;
|
||||||
|
this.userName = userName;
|
||||||
|
this.userEmailAddress = userEmailAddress;
|
||||||
|
branchInit = GitDBBranch.init(this.repository, this.userName, this.userEmailAddress);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new GitDB instance using the Git repo.
|
* Create a new GitDB instance using the Git repo.
|
||||||
*
|
*
|
||||||
|
@ -88,8 +100,7 @@ class LocalGitDB implements GitDB {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<GitDBBranch> branch(final String name) throws IOException {
|
public Optional<GitDBBranch> branch(final String name) throws IOException {
|
||||||
return Optional.ofNullable(repository.findRef(name))
|
return Optional.ofNullable(repository.findRef(name)).map(branchInit);
|
||||||
.map(ref -> GitDBBranch.withRef(ref, GitDBRepo.in(repository), userName, userEmailAddress));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue