Refactoring - push jgit from GitDB class
This commit is contained in:
parent
36d968335e
commit
2246513bee
3 changed files with 73 additions and 56 deletions
|
@ -21,9 +21,6 @@
|
||||||
|
|
||||||
package net.kemitix.gitdb;
|
package net.kemitix.gitdb;
|
||||||
|
|
||||||
import org.eclipse.jgit.api.Git;
|
|
||||||
import org.eclipse.jgit.lib.Repository;
|
|
||||||
|
|
||||||
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;
|
||||||
|
@ -44,10 +41,12 @@ public interface GitDB {
|
||||||
* @return a GitDB instance for the created local gitdb
|
* @return a GitDB instance for the created local gitdb
|
||||||
* @throws IOException if there {@code dbDir} is a file or a non-empty directory
|
* @throws IOException if there {@code dbDir} is a file or a non-empty directory
|
||||||
*/
|
*/
|
||||||
static GitDB initLocal(final Path dbDir, final String userName, final String userEmailAddress) throws IOException {
|
static GitDB initLocal(
|
||||||
return new GitDBLocal(
|
final Path dbDir,
|
||||||
dbDir.toFile(), userName, userEmailAddress
|
final String userName,
|
||||||
);
|
final String userEmailAddress
|
||||||
|
) throws IOException {
|
||||||
|
return GitDBLocal.init(dbDir, userName, userEmailAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -58,16 +57,10 @@ public interface GitDB {
|
||||||
* @param userEmailAddress the user email address
|
* @param userEmailAddress the user email address
|
||||||
* @return a GitDB instance for the local gitdb
|
* @return a GitDB instance for the local gitdb
|
||||||
*/
|
*/
|
||||||
static GitDBLocal openLocal(final Path dbDir, final String userName, final String userEmailAddress) {
|
static GitDB openLocal(final Path dbDir, final String userName, final String userEmailAddress) {
|
||||||
try {
|
return GitDBLocal.open(dbDir, userName, userEmailAddress);
|
||||||
return Optional.of(Git.open(dbDir.toFile()))
|
|
||||||
.map(Git::getRepository)
|
|
||||||
.filter(Repository::isBare)
|
|
||||||
.map(repository -> new GitDBLocal(repository, userName, userEmailAddress))
|
|
||||||
.orElseThrow(() -> new InvalidRepositoryException("Not a bare repo", dbDir));
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new InvalidRepositoryException("Error opening repository", dbDir, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -21,6 +21,8 @@
|
||||||
|
|
||||||
package net.kemitix.gitdb;
|
package net.kemitix.gitdb;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.eclipse.jgit.api.Git;
|
||||||
import org.eclipse.jgit.lib.*;
|
import org.eclipse.jgit.lib.*;
|
||||||
import org.eclipse.jgit.util.FS;
|
import org.eclipse.jgit.util.FS;
|
||||||
|
|
||||||
|
@ -38,40 +40,62 @@ import java.util.Optional;
|
||||||
*
|
*
|
||||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
*/
|
*/
|
||||||
|
@RequiredArgsConstructor
|
||||||
class GitDBLocal implements GitDB {
|
class GitDBLocal implements GitDB {
|
||||||
|
|
||||||
|
private static final String INIT_MESSAGE = "Initialise GitDB v1";
|
||||||
|
private static final String INIT_USER = "GitDB";
|
||||||
|
private static final String INIT_EMAIL = "pcampbell@kemitix.net";
|
||||||
|
private static final String MASTER = "master";
|
||||||
|
private static final String IS_GIT_DB = "isGitDB";
|
||||||
|
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 REFS_HEADS_FORMAT = "refs/heads/%s";
|
||||||
|
|
||||||
private final Repository repository;
|
private final Repository repository;
|
||||||
private final String userName;
|
private final String userName;
|
||||||
private final String userEmailAddress;
|
private final String userEmailAddress;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new GitDB instance using the Git repo.
|
||||||
|
*
|
||||||
|
* @param dbDir the path to instantiate the git repo in
|
||||||
|
* @param userName the user name
|
||||||
|
* @param userEmailAddress the user email address
|
||||||
|
* @return a GitDB instance for the created local gitdb
|
||||||
|
*/
|
||||||
|
static GitDB open(
|
||||||
|
final Path dbDir,
|
||||||
|
final String userName,
|
||||||
|
final String userEmailAddress
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
return Optional.of(Git.open(dbDir.toFile()))
|
||||||
|
.map(Git::getRepository)
|
||||||
|
.filter(Repository::isBare)
|
||||||
|
.map(repository -> new GitDBLocal(repository, userName, userEmailAddress))
|
||||||
|
.orElseThrow(() -> new InvalidRepositoryException(NOT_A_BARE_REPO, dbDir));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new InvalidRepositoryException(ERROR_OPENING_REPOSITORY, dbDir, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new GitDB instance, while initialising a new git repo.
|
* Create a new GitDB instance, while initialising a new git repo.
|
||||||
*
|
*
|
||||||
* @param dbDir the path to instantiate the git repo in
|
* @param dbDir the path to instantiate the git repo in
|
||||||
* @param userName the user name
|
* @param userName the user name
|
||||||
* @param userEmailAddress the user email address
|
* @param userEmailAddress the user email address
|
||||||
|
* @return a GitDB instance for the created local gitdb
|
||||||
* @throws IOException if there {@code dbDir} is a file or a non-empty directory
|
* @throws IOException if there {@code dbDir} is a file or a non-empty directory
|
||||||
*/
|
*/
|
||||||
GitDBLocal(
|
static GitDB init(
|
||||||
final File dbDir,
|
final Path dbDir,
|
||||||
final String userName,
|
final String userName,
|
||||||
final String userEmailAddress
|
final String userEmailAddress
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
this(GitDBLocal.initRepo(validDbDir(dbDir)), userName, userEmailAddress);
|
initRepo(validDbDir(dbDir.toFile()));
|
||||||
}
|
return open(dbDir, userName, userEmailAddress);
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new GitDB instance using the Git repo.
|
|
||||||
*
|
|
||||||
* @param repository the Git repository
|
|
||||||
* @param userName the user name
|
|
||||||
* @param userEmailAddress the user email address
|
|
||||||
*/
|
|
||||||
GitDBLocal(final Repository repository, final String userName, final String userEmailAddress) {
|
|
||||||
this.repository = repository;
|
|
||||||
this.userName = userName;
|
|
||||||
this.userEmailAddress = userEmailAddress;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static File validDbDir(final File dbDir) throws IOException {
|
private static File validDbDir(final File dbDir) throws IOException {
|
||||||
|
@ -94,31 +118,19 @@ class GitDBLocal implements GitDB {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private static void initRepo(final File dbDir) throws IOException {
|
||||||
public Optional<GitDBBranch> branch(final String name) throws IOException {
|
|
||||||
return Optional.ofNullable(repository.findRef(name))
|
|
||||||
.map(ref -> GitDBBranch.withRef(ref, GitDBRepo.in(repository), userName, userEmailAddress));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Repository initRepo(final File dbDir) throws IOException {
|
|
||||||
dbDir.mkdirs();
|
dbDir.mkdirs();
|
||||||
final Repository repository = RepositoryCache.FileKey.exact(dbDir, FS.DETECTED).open(false);
|
final Repository repository = RepositoryCache.FileKey.exact(dbDir, FS.DETECTED).open(false);
|
||||||
repository.create(true);
|
repository.create(true);
|
||||||
createInitialBranchOnMaster(repository);
|
createInitialBranchOnMaster(repository);
|
||||||
return repository;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void createInitialBranchOnMaster(final Repository repository) throws IOException {
|
private static void createInitialBranchOnMaster(final Repository repository) throws IOException {
|
||||||
final GitDBRepo repo = GitDBRepo.in(repository);
|
final GitDBRepo repo = GitDBRepo.in(repository);
|
||||||
final ObjectId objectId = repo.insertBlob(new byte[0]);
|
final ObjectId objectId = repo.insertBlob(new byte[0]);
|
||||||
final ObjectId treeId = repo.insertNewTree("isGitDB", objectId);
|
final ObjectId treeId = repo.insertNewTree(IS_GIT_DB, objectId);
|
||||||
final ObjectId commitId = repo.insertCommit(
|
final ObjectId commitId = repo.insertCommit(treeId, INIT_MESSAGE, INIT_USER, INIT_EMAIL, ObjectId.zeroId());
|
||||||
treeId,
|
createBranch(repository, commitId, MASTER);
|
||||||
"Initialise GitDB v1",
|
|
||||||
"GitDB",
|
|
||||||
"pcampbell@kemitix.net",
|
|
||||||
ObjectId.zeroId());
|
|
||||||
createBranch(repository, commitId, "master");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void createBranch(
|
private static void createBranch(
|
||||||
|
@ -126,13 +138,25 @@ class GitDBLocal implements GitDB {
|
||||||
final ObjectId commitId,
|
final ObjectId commitId,
|
||||||
final String branchName
|
final String branchName
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
final Path branchRefPath = repository
|
final Path branchRefPath = branchRefPath(repository, branchName);
|
||||||
.getDirectory()
|
|
||||||
.toPath()
|
|
||||||
.resolve("refs/heads/" + branchName)
|
|
||||||
.toAbsolutePath();
|
|
||||||
final byte[] commitIdBytes = commitId.name().getBytes(StandardCharsets.UTF_8);
|
final byte[] commitIdBytes = commitId.name().getBytes(StandardCharsets.UTF_8);
|
||||||
Files.write(branchRefPath, commitIdBytes);
|
Files.write(branchRefPath, commitIdBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Path branchRefPath(
|
||||||
|
final Repository repository,
|
||||||
|
final String branchName
|
||||||
|
) {
|
||||||
|
return repository.getDirectory()
|
||||||
|
.toPath()
|
||||||
|
.resolve(String.format(REFS_HEADS_FORMAT, branchName))
|
||||||
|
.toAbsolutePath();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<GitDBBranch> branch(final String name) throws IOException {
|
||||||
|
return Optional.ofNullable(repository.findRef(name))
|
||||||
|
.map(ref -> GitDBBranch.withRef(ref, GitDBRepo.in(repository), userName, userEmailAddress));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -153,7 +153,7 @@ class GitDBTest implements WithAssertions {
|
||||||
//given
|
//given
|
||||||
final Path dir = gitDBRepoPath();
|
final Path dir = gitDBRepoPath();
|
||||||
//when
|
//when
|
||||||
final GitDBLocal gitDB = GitDB.openLocal(dir, userName, userEmailAddress);
|
final GitDB gitDB = GitDB.openLocal(dir, userName, userEmailAddress);
|
||||||
//then
|
//then
|
||||||
assertThat(gitDB).isNotNull();
|
assertThat(gitDB).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue