Skip to content

Commit

Permalink
Resolve hashes against DETACHED
Browse files Browse the repository at this point in the history
  • Loading branch information
adutra committed Aug 1, 2023
1 parent f535bd6 commit b22da40
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ additionalRefName, defaultBranch(), invalidHash))
"ASSIGN BRANCH %s TO %s AT %s IN nessie",
additionalRefName, defaultBranch(), unknownHash))
.isInstanceOf(NessieNotFoundException.class)
.hasMessage(
String.format(
"Could not find commit '%s' in reference '%s'.", unknownHash, defaultBranch()));
.hasMessage(String.format("Commit '%s' not found", unknownHash));
assertThatThrownBy(
() ->
sql(
Expand Down Expand Up @@ -329,9 +327,7 @@ additionalRefName, defaultBranch(), invalidHash))
"ASSIGN TAG %s TO %s AT %s IN nessie",
additionalRefName, defaultBranch(), unknownHash))
.isInstanceOf(NessieNotFoundException.class)
.hasMessage(
String.format(
"Could not find commit '%s' in reference '%s'.", unknownHash, defaultBranch()));
.hasMessage(String.format("Commit '%s' not found", unknownHash));
assertThatThrownBy(
() ->
sql(
Expand Down Expand Up @@ -458,8 +454,7 @@ void useShowReferencesAtWithFailureConditions()

assertThatThrownBy(() -> sql("USE REFERENCE %s AT %s IN nessie ", refName, randomHash))
.isInstanceOf(NessieNotFoundException.class)
.hasMessage(
String.format("Could not find commit '%s' in reference '%s'.", randomHash, refName));
.hasMessage(String.format("Commit '%s' not found", randomHash));

assertThatThrownBy(() -> sql("USE REFERENCE %s AT `%s` IN nessie ", refName, invalidTimestamp))
.isInstanceOf(NessieNotFoundException.class)
Expand All @@ -469,8 +464,7 @@ void useShowReferencesAtWithFailureConditions()

assertThatThrownBy(() -> sql("USE REFERENCE %s AT %s IN nessie ", refName, invalidHash))
.isInstanceOf(NessieNotFoundException.class)
.hasMessageStartingWith(
String.format("Could not find commit '%s' in reference '%s'", invalidHash, refName));
.hasMessage(String.format("Commit '%s' not found", invalidHash));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
import org.projectnessie.error.NessieNamespaceNotFoundException;
import org.projectnessie.error.NessieNotFoundException;
import org.projectnessie.error.NessieReferenceConflictException;
import org.projectnessie.error.NessieReferenceNotFoundException;
import org.projectnessie.error.ReferenceConflicts;
import org.projectnessie.model.Branch;
import org.projectnessie.model.CommitMeta;
Expand Down Expand Up @@ -374,36 +373,10 @@ public void references() throws Exception {
api().deleteTag().tag(tag).delete();
}

// We cannot delete a branch if the expected hash is not reachable from its HEAD. Here,
// the expected hash is not reachable anymore, because the branch was reassigned to main
// previously.
// In such cases we expect a not-found error.
AbstractThrowableAssert<?, ? extends Throwable> deleteConflict =
isV2()
? soft.assertThatThrownBy(() -> api().deleteBranch().branch(branch).getAndDelete())
: soft.assertThatThrownBy(() -> api().deleteBranch().branch(branch).delete());
deleteConflict
.isInstanceOf(NessieReferenceNotFoundException.class)
.hasMessageContaining("Could not find commit");

// Move the HEAD of the branch from main to a new commit.
Branch branchWithNewCommit =
prepCommit(
branchAssigned,
"commit",
Put.of(ContentKey.of("key"), Namespace.of("key")),
dummyPut("key", "foo"))
.commit();

// We cannot delete a branch if the expected hash is reachable from HEAD, but is not HEAD. Here,
// the expected hash is not the HEAD anymore, because the branch was updated to
// branchWithNewCommit above.
// In such cases we expect a conflict.
deleteConflict =
isV2()
? soft.assertThatThrownBy(
() -> api().deleteBranch().branch(branchAssigned).getAndDelete())
: soft.assertThatThrownBy(() -> api().deleteBranch().branch(branchAssigned).delete());
deleteConflict
.isInstanceOf(NessieReferenceConflictException.class)
.asInstanceOf(type(NessieReferenceConflictException.class))
Expand All @@ -412,12 +385,11 @@ public void references() throws Exception {
.extracting(Conflict::conflictType)
.containsExactly(ConflictType.UNEXPECTED_HASH);

// Delete branch with new commit as expected HEAD: OK
if (isV2()) {
Branch deleted = api().deleteBranch().branch(branchWithNewCommit).getAndDelete();
soft.assertThat(deleted).isEqualTo(branchWithNewCommit);
Branch deleted = api().deleteBranch().branch(branchAssigned).getAndDelete();
soft.assertThat(deleted).isEqualTo(branchAssigned);
} else {
api().deleteBranch().branch(branchWithNewCommit).delete();
api().deleteBranch().branch(branchAssigned).delete();
}

soft.assertThat(api().getAllReferences().get().getReferences())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -153,8 +152,12 @@ public ResolvedHash resolveHashOnRef(
List<RelativeCommitSpec> relativeParts =
parsed.map(ParsedHash::getRelativeParts).orElse(Collections.emptyList());
Optional<Hash> resolved = Optional.of(startOrHead);
if (!Objects.equals(startOrHead, currentHead) || !relativeParts.isEmpty()) {
resolved = Optional.ofNullable(store.hashOnReference(ref, resolved, relativeParts));
if (!relativeParts.isEmpty()) {
// Resolve the hash against DETACHED because we are only interested in
// resolving the hash, not checking if it is on the branch. This will
// be done later on.
resolved =
Optional.ofNullable(store.hashOnReference(DetachedRef.INSTANCE, resolved, relativeParts));
}
return ResolvedHash.of(ref, resolved, Optional.ofNullable(currentHead));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,21 @@ public void testUnknownHashesOnValidNamedRefs() throws BaseNessieClientServerExc
createCommits(branch, 1, commits, currentHash);
assertThatThrownBy(() -> commitLog(branch.getName(), MINIMAL, null, invalidHash, null))
.isInstanceOf(NessieNotFoundException.class)
.hasMessageContaining(
String.format(
"Could not find commit '%s' in reference '%s'.", invalidHash, branch.getName()));
.hasMessageContaining(String.format("Commit '%s' not found", invalidHash));

assertThatThrownBy(() -> entries(branch.getName(), invalidHash))
.isInstanceOf(NessieNotFoundException.class)
.hasMessageContaining(
String.format(
"Could not find commit '%s' in reference '%s'.", invalidHash, branch.getName()));
.hasMessageContaining(String.format("Commit '%s' not found", invalidHash));

assertThatThrownBy(() -> contents(branch.getName(), invalidHash, ContentKey.of("table0")))
.isInstanceOf(NessieNotFoundException.class)
.hasMessageContaining(
String.format(
"Could not find commit '%s' in reference '%s'.", invalidHash, branch.getName()));
.hasMessageContaining(String.format("Commit '%s' not found", invalidHash));

assertThatThrownBy(
() ->
contentApi()
.getContent(ContentKey.of("table0"), branch.getName(), invalidHash, false))
.isInstanceOf(NessieNotFoundException.class)
.hasMessageContaining(
String.format(
"Could not find commit '%s' in reference '%s'.", invalidHash, branch.getName()));
.hasMessageContaining(String.format("Commit '%s' not found", invalidHash));
}
}

0 comments on commit b22da40

Please sign in to comment.