Remove transactions - bare git repos don't support merging

May reconsider this at some point using a clone of the bare repo.
This commit is contained in:
Paul Campbell 2018-06-16 12:34:29 +01:00
parent 3b89df6589
commit 88452071f8
7 changed files with 11 additions and 384 deletions

View file

@ -71,34 +71,4 @@ public interface GitDBBranch {
*/ */
Optional<Version> getFormatVersion() throws IOException; Optional<Version> getFormatVersion() throws IOException;
/**
* Begins a new anonymous transaction.
*
* @return a new Transaction
* @throws IOException error writing transaction branch
*/
GitDBTransaction transaction() throws IOException;
/**
* Begins a new named transaction.
*
* @param transactionName the transaction name
* @return a new Transaction
* @throws IOException error writing transaction branch
*/
GitDBTransaction transaction(String transactionName) throws IOException;
/**
* Gets the name of the branch.
*
* @return the branch name
*/
String getName();
/**
* Gets the commit id for the head of the branch.
*
* @return an Object Id.
*/
String getCommitId();
} }

View file

@ -1,44 +0,0 @@
/*
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;
/**
* Represents a transaction (a git branch).
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public interface GitDBTransaction extends GitDBBranch {
/**
* The name of the transaction.
*
* @return the transaction name
*/
String getName();
/**
* Close the transaction, merging results into the base branch.
*
* @return the merged branch
*/
GitDBBranch close();
}

View file

@ -46,8 +46,8 @@ class CommitWriter {
/** /**
* Write a commit into the repository. * Write a commit into the repository.
* *
* @param treeId the tree to commit * @param treeId the tree to commit
* @param parentId the id of the parent commit * @param parentId the id of the parent commit
* @param message the message * @param message the message
* @param userName the user name * @param userName the user name
* @param userEmailAddress the user email address * @param userEmailAddress the user email address
@ -76,8 +76,8 @@ class CommitWriter {
* *
* <p>N.B. While this adds the commit to a branch, it doesn't update the branch itself.</p> * <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 treeId the tree to commit
* @param branchRef the branch to add the commit to * @param branchRef the branch to add the commit to
* @param message the message * @param message the message
* @param userName the user name * @param userName the user name
* @param userEmailAddress the user email address * @param userEmailAddress the user email address

View file

@ -23,17 +23,14 @@ package net.kemitix.gitdb.impl;
import com.github.zafarkhaja.semver.Version; import com.github.zafarkhaja.semver.Version;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.kemitix.gitdb.GitDBBranch; import net.kemitix.gitdb.GitDBBranch;
import net.kemitix.gitdb.GitDBTransaction;
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 org.eclipse.jgit.lib.Repository;
import java.io.*; import java.io.IOException;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
import java.util.function.Function; import java.util.function.Function;
/** /**
@ -49,7 +46,6 @@ class GitDBBranchImpl implements GitDBBranch {
private final GitDBRepo gitDBRepo; private final GitDBRepo gitDBRepo;
private final String userName; private final String userName;
private final String userEmailAddress; private final String userEmailAddress;
@Getter
private final String name; private final String name;
private static GitDBBranch select( private static GitDBBranch select(
@ -111,21 +107,4 @@ class GitDBBranchImpl implements GitDBBranch {
.map(Version::valueOf); .map(Version::valueOf);
} }
@Override
public GitDBTransaction transaction(final String transactionName) throws IOException {
final Ref ref = gitDBRepo.createBranch(branchRef, UUID.randomUUID().toString());
final GitDBBranch branch = new GitDBBranchImpl(ref, gitDBRepo, userName, userEmailAddress, transactionName);
return new GitDBTransactionImpl(this, branch);
}
@Override
public GitDBTransaction transaction() throws IOException {
return transaction(UUID.randomUUID().toString());
}
@Override
public String getCommitId() {
return branchRef.getObjectId().name();
}
} }

View file

@ -22,11 +22,13 @@
package net.kemitix.gitdb.impl; package net.kemitix.gitdb.impl;
import lombok.val; import lombok.val;
import org.eclipse.jgit.lib.*; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.Optional;
/** /**
* Wrapper for interacting with the GitDB Repository. * Wrapper for interacting with the GitDB Repository.
@ -45,7 +47,7 @@ class GitDBRepo {
/** /**
* Creates a new instance of this class. * Creates a new instance of this class.
* *
* @param repository the Git Repository * @param repository the Git Repository
*/ */
GitDBRepo(final Repository repository) { GitDBRepo(final Repository repository) {
this.repository = repository; this.repository = repository;
@ -186,15 +188,4 @@ class GitDBRepo {
return keyRemover.remove(branchRef, key); return keyRemover.remove(branchRef, key);
} }
/**
* Create a new branch.
*
* @param branchRef the branch source
* @param name the name of the new branch
* @return the Ref of the new branch
* @throws IOException error writing the branch
*/
Ref createBranch(final Ref branchRef, final String name) throws IOException {
return headWriter.write(branchRef.getName(), branchRef.getObjectId());
}
} }

View file

@ -1,87 +0,0 @@
/*
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 com.github.zafarkhaja.semver.Version;
import lombok.RequiredArgsConstructor;
import net.kemitix.gitdb.GitDBBranch;
import net.kemitix.gitdb.GitDBTransaction;
import java.io.IOException;
import java.util.Optional;
/**
* An anonymous transaction.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
@RequiredArgsConstructor
class GitDBTransactionImpl implements GitDBTransaction {
private final GitDBBranch base;
private final GitDBBranch branch;
@Override
public Optional<String> get(final String key) throws IOException {
return branch.get(key);
}
@Override
public GitDBBranch put(final String key, final String value) throws IOException {
return branch.put(key, value);
}
@Override
public GitDBBranch remove(final String key) throws IOException {
return branch.remove(key);
}
@Override
public Optional<Version> getFormatVersion() throws IOException {
return branch.getFormatVersion();
}
@Override
public GitDBTransaction transaction() throws IOException {
return branch.transaction();
}
@Override
public GitDBTransaction transaction(final String transactionName) throws IOException {
return branch.transaction(transactionName);
}
@Override
public String getName() {
return branch.getName();
}
@Override
public GitDBBranch close() {
return base;
}
@Override
public String getCommitId() {
return branch.getCommitId();
}
}

View file

@ -3,12 +3,12 @@ package net.kemitix.gitdb.test;
import com.github.zafarkhaja.semver.Version; import com.github.zafarkhaja.semver.Version;
import net.kemitix.gitdb.GitDB; import net.kemitix.gitdb.GitDB;
import net.kemitix.gitdb.GitDBBranch; import net.kemitix.gitdb.GitDBBranch;
import net.kemitix.gitdb.GitDBTransaction;
import net.kemitix.gitdb.InvalidRepositoryException; import net.kemitix.gitdb.InvalidRepositoryException;
import org.assertj.core.api.WithAssertions; import org.assertj.core.api.WithAssertions;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
@ -328,186 +328,4 @@ class GitDBTest implements WithAssertions {
assertThat(updatedBranch.get(key)).isEmpty(); assertThat(updatedBranch.get(key)).isEmpty();
} }
// When starting a named transaction then GitDbTransaction is returned
@Test
void startTransaction_thenReturnGitDBTransaction() throws IOException {
//given
final String key = stringSupplier.get();
final String value = stringSupplier.get();
final GitDBBranch gitDBBranch = gitDBBranchWithKeyValue(key, value);
final String name = stringSupplier.get();
//when
final GitDBTransaction transaction = gitDBBranch.transaction(name);
//then
assertThat(transaction).isNotNull();
assertThat(transaction.getName()).isEqualTo(name);
}
// When starting an anonymous transaction then a GitDbTransaction is returned
@Test
void startAnonymousTransaction_thenReturnGitDBTransaction() throws IOException {
//given
final GitDBBranch gitDBBranch = gitDBBranch();
//when
final GitDBTransaction transaction = gitDBBranch.transaction();
//then
assertThat(transaction).isNotNull();
assertThat(transaction.getName()).isNotNull();
}
// When starting an anonymous transaction then original branch is unchanged
@Test
void startAnonymousTransaction_thenOriginalBranchUnchanged() throws IOException {
//given
final GitDB gitDB = gitDB(dirDoesNotExist());
final GitDBBranch gitDBBranch = gitDB.branch("master").get();
final String commitId = gitDBBranch.getCommitId();
//when
final GitDBTransaction transaction = gitDBBranch.transaction();
//then
assertThat(gitDBBranch.getCommitId()).isEqualTo(commitId);
assertThat(gitDB.branch("master").map(GitDBBranch::getCommitId)).contains(commitId);
}
// When starting an anonymous transaction then transaction starts on the same commit
@Test
void startAnonymousTransaction_thenTransactionCommitMatchesOriginal() throws IOException {
//given
final GitDBBranch gitDBBranch = gitDBBranch();
final String commitId = gitDBBranch.getCommitId();
//when
final GitDBTransaction transaction = gitDBBranch.transaction();
//then
assertThat(transaction.getCommitId()).isEqualTo(commitId);
}
// Given a GitDbTransaction handle (i.e. a new branch)
// When putting a new key/value pair then the original GitDbBranch can't find it
@Test
void putValue_whenTransaction_thenOriginalBranchNotFind() throws IOException {
//given
final GitDBBranch originalBranch = gitDBBranch();
final GitDBTransaction transaction = originalBranch.transaction();
final String key = stringSupplier.get();
final String value = stringSupplier.get();
//when
final GitDBBranch updatedBranch = transaction.put(key, value);
//then
assertThat(originalBranch.get(key)).isEmpty();
}
// When putting an existing key/value pair then the original GitDbBranch finds the original value
@Test
void putValue_whenTransaction_thenUpdatedBranchFinds() throws IOException {
//given
final GitDBBranch originalBranch = gitDBBranch();
final GitDBTransaction transaction = originalBranch.transaction();
final String key = stringSupplier.get();
final String value = stringSupplier.get();
//when
final GitDBBranch updatedBranch = transaction.put(key, value);
//then
assertThat(updatedBranch.get(key)).contains(value);
}
// When removing a key in a transaction then the original GitDbBRanch still finds it
@Test
void removeKey_whenTransaction_thenOriginalBranchStillFinds() throws IOException {
//given
final String key = stringSupplier.get();
final String value = stringSupplier.get();
final GitDBBranch originalBranch = gitDBBranchWithKeyValue(key, value);
final GitDBTransaction transaction = originalBranch.transaction();
//when
final GitDBBranch removed = transaction.remove(key);
//then
assertThat(originalBranch.get(key)).contains(value);
}
// When removing a key in a transaction then the transaction branch no longer finds it
@Test
void removeKey_whenTransaction_thenTransactionNotFind() throws IOException {
//given
final String key = stringSupplier.get();
final String value = stringSupplier.get();
final GitDBBranch originalBranch = gitDBBranchWithKeyValue(key, value);
final GitDBTransaction transaction = originalBranch.transaction();
//when
final GitDBBranch removed = transaction.remove(key);
//then
assertThat(removed.get(key)).isEmpty();
}
// When get format version in transaction then the version is the same
@Test
void getFormatVersion_whenTransaction_thenFormatIsSame() throws IOException {
//given
final GitDBBranch gitDBBranch = gitDBBranch();
final GitDBTransaction transaction = gitDBBranch.transaction();
//when
final Optional<Version> formatVersion = transaction.getFormatVersion();
//then
assertThat(formatVersion).contains(GitDB.VERSION);
}
// When get key/value in a transaction then the value is returned
@Test
void getKey_whenTransaction_thenReturnValueInOptional() throws IOException {
//given
final String key = stringSupplier.get();
final String value = stringSupplier.get();
final GitDBBranch gitDBBranch = gitDBBranchWithKeyValue(key, value);
final GitDBTransaction transaction = gitDBBranch.transaction();
//when
final Optional<String> result = transaction.get(key);
//then
assertThat(result).contains(value);
}
// When start an anonymous transaction in a transaction then a new transaction is created
@Test
void startAnonymousTransaction_whenTransaction_thenReturnAnotherTransaction() throws IOException {
//given
final GitDBBranch initialBranch = gitDBBranch();
final GitDBTransaction firstTransaction = initialBranch.transaction();
//when
final GitDBTransaction secondTransaction = firstTransaction.transaction();
//then
assertThat(secondTransaction).isNotNull();
assertThat(secondTransaction).isNotSameAs(firstTransaction);
}
// When start a named transaction in a transaction then a new transaction is created
@Test
void startNamedTransaction_whenTransaction_thenReturnAnotherTransaction() throws IOException {
//given
final GitDBBranch initialBranch = gitDBBranch();
final GitDBTransaction firstTransaction = initialBranch.transaction();
final String name = stringSupplier.get();
//when
final GitDBTransaction secondTransaction = firstTransaction.transaction(name);
//then
assertThat(secondTransaction).isNotNull();
assertThat(secondTransaction).isNotSameAs(firstTransaction);
assertThat(secondTransaction.getName()).isEqualTo(name);
}
// When closing the transaction with no additional commits then the base GirDBBranch is returned
@Test
void closeTransaction_whenNoCommits_thenReturnBaseBranch() throws IOException {
//given
final GitDBBranch baseBranch = gitDBBranch();
final GitDBTransaction transaction = baseBranch.transaction();
//when
final GitDBBranch mergedBranch = transaction.close();
//then
assertThat(mergedBranch).isSameAs(baseBranch);
}
// Given a GitDbTransaction handle with a added, updated and removed keys
// When closing the transaction an GitDbBranch is returned
// When closing the transaction the added key/value is found
// When closing the transaction the updated value is found
// When closing the transaction the removed key is not found
} }