When starting an anonymous transaction then original branch is unchanged

This commit is contained in:
Paul Campbell 2018-06-15 18:34:10 +01:00
parent 53e1fd394b
commit 71bb51c729
4 changed files with 31 additions and 0 deletions

View file

@ -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();
}

View file

@ -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);
}

View file

@ -76,4 +76,9 @@ class GitDBTransactionImpl implements GitDBTransaction {
public String name() {
return branch.name();
}
@Override
public String getCommitId() {
return null;
}
}

View file

@ -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