From 57e39d71ad56dd7a6526dc0fbd9a80581a8f0545 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 15 Jun 2018 14:32:09 +0100 Subject: [PATCH] When starting a named transaction then GitDbTransaction is returned --- .../java/net/kemitix/gitdb/GitDBBranch.java | 22 ++++++ .../net/kemitix/gitdb/GitDBTransaction.java | 37 +++++++++ .../kemitix/gitdb/impl/GitDBBranchImpl.java | 22 +++++- .../net/kemitix/gitdb/impl/GitDBRepo.java | 4 + .../gitdb/impl/UnnamedTransaction.java | 79 +++++++++++++++++++ .../net/kemitix/gitdb/test/GitDBTest.java | 15 ++++ 6 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/kemitix/gitdb/GitDBTransaction.java create mode 100644 src/main/java/net/kemitix/gitdb/impl/UnnamedTransaction.java diff --git a/src/main/java/net/kemitix/gitdb/GitDBBranch.java b/src/main/java/net/kemitix/gitdb/GitDBBranch.java index 9e53123..99d0618 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBBranch.java +++ b/src/main/java/net/kemitix/gitdb/GitDBBranch.java @@ -70,4 +70,26 @@ public interface GitDBBranch { * @throws IOException error reading version */ Optional getFormatVersion() throws IOException; + + /** + * Begins a new anonymous transaction. + * + * @return a new Transaction + */ + GitDBTransaction transaction() throws IOException; + + /** + * Begins a new named transaction. + * + * @param name the transaction name + * @return a new Transaction + */ + GitDBTransaction transaction(String name) throws IOException; + + /** + * Gets the name of the branch. + * + * @return the branch name + */ + String name(); } diff --git a/src/main/java/net/kemitix/gitdb/GitDBTransaction.java b/src/main/java/net/kemitix/gitdb/GitDBTransaction.java new file mode 100644 index 0000000..139d0b9 --- /dev/null +++ b/src/main/java/net/kemitix/gitdb/GitDBTransaction.java @@ -0,0 +1,37 @@ +/* + 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 name(); +} diff --git a/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java b/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java index 2f88ab1..4cc702c 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java @@ -25,12 +25,14 @@ import com.github.zafarkhaja.semver.Version; import lombok.AccessLevel; 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.util.Optional; +import java.util.UUID; import java.util.function.Function; /** @@ -46,6 +48,7 @@ class GitDBBranchImpl implements GitDBBranch { private final GitDBRepo gitDBRepo; private final String userName; private final String userEmailAddress; + private final String name; private static GitDBBranch select( final Ref branchRef, @@ -53,7 +56,7 @@ class GitDBBranchImpl implements GitDBBranch { final String userName, final String userEmailAddress ) { - return new GitDBBranchImpl(branchRef, gitDBRepo, userName, userEmailAddress); + return new GitDBBranchImpl(branchRef, gitDBRepo, userName, userEmailAddress, branchRef.getName()); } /** @@ -106,6 +109,23 @@ class GitDBBranchImpl implements GitDBBranch { .map(Version::valueOf); } + @Override + public GitDBTransaction transaction(String name) throws IOException { + final Ref ref = gitDBRepo.createBranch(branchRef, UUID.randomUUID().toString()); + final GitDBBranch branch = new GitDBBranchImpl(ref, gitDBRepo, userName, userEmailAddress, name); + return new UnnamedTransaction(this, branch); + } + + @Override + public GitDBTransaction transaction() throws IOException { + return transaction(UUID.randomUUID().toString()); + } + + @Override + public String name() { + return name; + } + private String commitMessageForAdd(final String key, final String value) { return String.format("Add key [%s] = [%s]", key, value); } diff --git a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java index d90a59e..88093a3 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java @@ -227,4 +227,8 @@ class GitDBRepo { Optional removeKey(final Ref branchRef, final String key) throws IOException { return keyRemover.remove(branchRef, key); } + + Ref createBranch(final Ref branchRef, final String name) throws IOException { + return repository.getRefDatabase().newUpdate(name, false).getRef(); + } } diff --git a/src/main/java/net/kemitix/gitdb/impl/UnnamedTransaction.java b/src/main/java/net/kemitix/gitdb/impl/UnnamedTransaction.java new file mode 100644 index 0000000..c00a5c1 --- /dev/null +++ b/src/main/java/net/kemitix/gitdb/impl/UnnamedTransaction.java @@ -0,0 +1,79 @@ +/* + 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 net.kemitix.gitdb.GitDBBranch; +import net.kemitix.gitdb.GitDBTransaction; + +import java.util.Optional; + +/** + * An anonymous transaction. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +class UnnamedTransaction implements GitDBTransaction { + + private final GitDBBranch base; + private final GitDBBranch branch; + + UnnamedTransaction(final GitDBBranch base, final GitDBBranch branch) { + this.base = base; + this.branch = branch; + } + + @Override + public Optional get(String key) { + return Optional.empty(); + } + + @Override + public GitDBBranch put(String key, String value) { + return null; + } + + @Override + public GitDBBranch remove(String key) { + return null; + } + + @Override + public Optional getFormatVersion() { + return Optional.empty(); + } + + @Override + public GitDBTransaction transaction() { + return null; + } + + @Override + public GitDBTransaction transaction(String name) { + return null; + } + + @Override + public String name() { + return branch.name(); + } +} diff --git a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java index adfe4de..3b0ad66 100644 --- a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java @@ -3,6 +3,7 @@ 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; @@ -310,6 +311,20 @@ class GitDBTest implements WithAssertions { } // 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.name()).isEqualTo(name); + } + // When starting an anonymous transaction then a GitDbTransaction is returned // Given a GitDbTransaction handle (i.e. a new branch)