From 88452071f80a637d9a81096830c54503844ddeec Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 16 Jun 2018 12:34:29 +0100 Subject: [PATCH] Remove transactions - bare git repos don't support merging May reconsider this at some point using a clone of the bare repo. --- .../java/net/kemitix/gitdb/GitDBBranch.java | 30 --- .../net/kemitix/gitdb/GitDBTransaction.java | 44 ----- .../net/kemitix/gitdb/impl/CommitWriter.java | 8 +- .../kemitix/gitdb/impl/GitDBBranchImpl.java | 23 +-- .../net/kemitix/gitdb/impl/GitDBRepo.java | 19 +- .../gitdb/impl/GitDBTransactionImpl.java | 87 --------- .../net/kemitix/gitdb/test/GitDBTest.java | 184 +----------------- 7 files changed, 11 insertions(+), 384 deletions(-) delete mode 100644 src/main/java/net/kemitix/gitdb/GitDBTransaction.java delete mode 100644 src/main/java/net/kemitix/gitdb/impl/GitDBTransactionImpl.java diff --git a/src/main/java/net/kemitix/gitdb/GitDBBranch.java b/src/main/java/net/kemitix/gitdb/GitDBBranch.java index ded2f61..e1fa7cf 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBBranch.java +++ b/src/main/java/net/kemitix/gitdb/GitDBBranch.java @@ -71,34 +71,4 @@ public interface GitDBBranch { */ Optional 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(); } diff --git a/src/main/java/net/kemitix/gitdb/GitDBTransaction.java b/src/main/java/net/kemitix/gitdb/GitDBTransaction.java deleted file mode 100644 index 5999c11..0000000 --- a/src/main/java/net/kemitix/gitdb/GitDBTransaction.java +++ /dev/null @@ -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(); -} diff --git a/src/main/java/net/kemitix/gitdb/impl/CommitWriter.java b/src/main/java/net/kemitix/gitdb/impl/CommitWriter.java index 290ab6b..4f9cf11 100644 --- a/src/main/java/net/kemitix/gitdb/impl/CommitWriter.java +++ b/src/main/java/net/kemitix/gitdb/impl/CommitWriter.java @@ -46,8 +46,8 @@ class CommitWriter { /** * Write a commit into the repository. * - * @param treeId the tree to commit - * @param parentId the id of the parent commit + * @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 @@ -76,8 +76,8 @@ class CommitWriter { * *

N.B. While this adds the commit to a branch, it doesn't update the branch itself.

* - * @param treeId the tree to commit - * @param branchRef the branch to add the commit to + * @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 diff --git a/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java b/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java index e14485f..097c646 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java @@ -23,17 +23,14 @@ package net.kemitix.gitdb.impl; import com.github.zafarkhaja.semver.Version; import lombok.AccessLevel; -import lombok.Getter; import lombok.RequiredArgsConstructor; import net.kemitix.gitdb.GitDBBranch; -import net.kemitix.gitdb.GitDBTransaction; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; -import java.io.*; +import java.io.IOException; import java.util.Optional; -import java.util.UUID; import java.util.function.Function; /** @@ -49,7 +46,6 @@ class GitDBBranchImpl implements GitDBBranch { private final GitDBRepo gitDBRepo; private final String userName; private final String userEmailAddress; - @Getter private final String name; private static GitDBBranch select( @@ -111,21 +107,4 @@ class GitDBBranchImpl implements GitDBBranch { .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(); - } - } diff --git a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java index 388270c..1efbe7c 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java @@ -22,11 +22,13 @@ package net.kemitix.gitdb.impl; 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.nio.charset.StandardCharsets; -import java.util.*; +import java.util.Optional; /** * Wrapper for interacting with the GitDB Repository. @@ -45,7 +47,7 @@ class GitDBRepo { /** * Creates a new instance of this class. * - * @param repository the Git Repository + * @param repository the Git Repository */ GitDBRepo(final Repository repository) { this.repository = repository; @@ -186,15 +188,4 @@ class GitDBRepo { 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()); - } } diff --git a/src/main/java/net/kemitix/gitdb/impl/GitDBTransactionImpl.java b/src/main/java/net/kemitix/gitdb/impl/GitDBTransactionImpl.java deleted file mode 100644 index 95684e6..0000000 --- a/src/main/java/net/kemitix/gitdb/impl/GitDBTransactionImpl.java +++ /dev/null @@ -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 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 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(); - } -} diff --git a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java index 5de3015..01a9f3d 100644 --- a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java @@ -3,12 +3,12 @@ package net.kemitix.gitdb.test; import com.github.zafarkhaja.semver.Version; import net.kemitix.gitdb.GitDB; import net.kemitix.gitdb.GitDBBranch; -import net.kemitix.gitdb.GitDBTransaction; import net.kemitix.gitdb.InvalidRepositoryException; import org.assertj.core.api.WithAssertions; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -328,186 +328,4 @@ class GitDBTest implements WithAssertions { 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 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 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 - }