From 71bb51c729f94dc926c3b12a878932e653e06466 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 15 Jun 2018 18:34:10 +0100 Subject: [PATCH] When starting an anonymous transaction then original branch is unchanged --- src/main/java/net/kemitix/gitdb/GitDBBranch.java | 7 +++++++ .../net/kemitix/gitdb/impl/GitDBBranchImpl.java | 5 +++++ .../kemitix/gitdb/impl/GitDBTransactionImpl.java | 5 +++++ .../java/net/kemitix/gitdb/test/GitDBTest.java | 14 ++++++++++++++ 4 files changed, 31 insertions(+) diff --git a/src/main/java/net/kemitix/gitdb/GitDBBranch.java b/src/main/java/net/kemitix/gitdb/GitDBBranch.java index 99d0618..f6bd3ba 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBBranch.java +++ b/src/main/java/net/kemitix/gitdb/GitDBBranch.java @@ -92,4 +92,11 @@ public interface GitDBBranch { * @return the branch name */ String name(); + + /** + * 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/impl/GitDBBranchImpl.java b/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java index ca2e451..1adecf9 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java @@ -126,6 +126,11 @@ class GitDBBranchImpl implements GitDBBranch { return name; } + @Override + public String getCommitId() { + return branchRef.getObjectId().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/GitDBTransactionImpl.java b/src/main/java/net/kemitix/gitdb/impl/GitDBTransactionImpl.java index de6ecd2..0e1630c 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitDBTransactionImpl.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBTransactionImpl.java @@ -76,4 +76,9 @@ class GitDBTransactionImpl implements GitDBTransaction { public String name() { return branch.name(); } + + @Override + public String getCommitId() { + return null; + } } diff --git a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java index 4eeccb6..6cfd077 100644 --- a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java @@ -339,6 +339,20 @@ class GitDBTest implements WithAssertions { assertThat(transaction.name()).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); + } + // 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