From 1a65fb9123588081f71969b1066dd6be53ecf81e Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 15 Jun 2018 18:38:34 +0100 Subject: [PATCH] When starting an anonymous transaction then transaction starts on the same commit --- src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java | 2 +- .../net/kemitix/gitdb/impl/GitDBTransactionImpl.java | 2 +- src/test/java/net/kemitix/gitdb/test/GitDBTest.java | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java index 88093a3..2095f5b 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java @@ -229,6 +229,6 @@ class GitDBRepo { } Ref createBranch(final Ref branchRef, final String name) throws IOException { - return repository.getRefDatabase().newUpdate(name, false).getRef(); + return writeHead(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 index 0e1630c..d468567 100644 --- a/src/main/java/net/kemitix/gitdb/impl/GitDBTransactionImpl.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBTransactionImpl.java @@ -79,6 +79,6 @@ class GitDBTransactionImpl implements GitDBTransaction { @Override public String getCommitId() { - return null; + 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 6cfd077..79f7a76 100644 --- a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java @@ -353,6 +353,18 @@ class GitDBTest implements WithAssertions { 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