use of org.projectnessie.versioned.ReferenceInfo in project nessie by projectnessie.
the class AbstractGetNamedReferences method verifyReferences.
private void verifyReferences(GetNamedRefsParams params, ExpectedNamedReference... references) throws ReferenceNotFoundException {
List<ReferenceInfo<ByteString>> expectedRefs = Arrays.stream(references).map(expectedRef -> expectedRef.expected(params)).filter(Objects::nonNull).collect(Collectors.toList());
try (Stream<ReferenceInfo<ByteString>> refs = databaseAdapter.namedRefs(params)) {
assertThat(refs).describedAs("GetNamedRefsParams=%s - references=%s", params, references).containsExactlyInAnyOrderElementsOf(expectedRefs);
}
for (ReferenceInfo<ByteString> expected : expectedRefs) {
assertThat(databaseAdapter.namedRef(expected.getNamedRef().getName(), params)).isEqualTo(expected);
}
List<NamedRef> failureRefs = Arrays.stream(references).map(expectedRef -> {
ReferenceInfo<ByteString> expected = expectedRef.expected(params);
if (expected == null) {
return expectedRef.ref;
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());
for (NamedRef ref : failureRefs) {
assertThatThrownBy(() -> databaseAdapter.namedRef(ref.getName(), params));
}
}
use of org.projectnessie.versioned.ReferenceInfo in project nessie by projectnessie.
the class TxDatabaseAdapter method namedRef.
@Override
public ReferenceInfo<ByteString> namedRef(String ref, GetNamedRefsParams params) throws ReferenceNotFoundException {
Preconditions.checkNotNull(params, "Parameter for GetNamedRefsParams must not be null");
try (ConnectionWrapper conn = borrowConnection()) {
ReferenceInfo<ByteString> refInfo = fetchNamedRef(conn, ref);
Hash defaultBranchHead = namedRefsDefaultBranchHead(conn, params);
Stream<ReferenceInfo<ByteString>> refs = Stream.of(refInfo);
return namedRefsFilterAndEnhance(conn, params, defaultBranchHead, refs).findFirst().orElseThrow(() -> referenceNotFound(ref));
}
}
use of org.projectnessie.versioned.ReferenceInfo in project nessie by projectnessie.
the class TxDatabaseAdapter method namedRefs.
public Stream<ReferenceInfo<ByteString>> namedRefs(GetNamedRefsParams params) throws ReferenceNotFoundException {
Preconditions.checkNotNull(params, "Parameter for GetNamedRefsParams must not be null.");
Preconditions.checkArgument(namedRefsAnyRetrieves(params), "Must retrieve branches or tags or both.");
ConnectionWrapper conn = borrowConnection();
boolean failed = true;
try {
Hash defaultBranchHead = namedRefsDefaultBranchHead(conn, params);
Stream<ReferenceInfo<ByteString>> refs = fetchNamedRefs(conn);
refs = namedRefsFilterAndEnhance(conn, params, defaultBranchHead, refs);
failed = false;
return refs.onClose(conn::close);
} finally {
if (failed) {
conn.close();
}
}
}
use of org.projectnessie.versioned.ReferenceInfo in project nessie by projectnessie.
the class AbstractDatabaseAdapter method namedRefsWithDefaultBranchRelatedInfo.
/**
* If necessary based on the given {@link GetNamedRefsParams}, updates the returned {@link
* ReferenceInfo}s with the common-ancestor of the named reference and the default branch and the
* number of commits behind/ahead compared to the default branch.
*
* <p>The common ancestor and/or information of commits behind/ahead is meaningless ({@code null})
* for the default branch. Both fields are also {@code null} if the named reference points to the
* {@link #noAncestorHash()} (beginning of time).
*/
protected Stream<ReferenceInfo<ByteString>> namedRefsWithDefaultBranchRelatedInfo(OP_CONTEXT ctx, GetNamedRefsParams params, Stream<ReferenceInfo<ByteString>> refs, Hash defaultBranchHead) {
if (defaultBranchHead == null) {
// No enhancement of common ancestor and/or commits behind/ahead.
return refs;
}
CommonAncestorState commonAncestorState = new CommonAncestorState(ctx, defaultBranchHead, params.getBranchRetrieveOptions().isComputeAheadBehind() || params.getTagRetrieveOptions().isComputeAheadBehind());
return refs.map(ref -> {
if (ref.getNamedRef().equals(params.getBaseReference())) {
return ref;
}
RetrieveOptions retrieveOptions = namedRefsRetrieveOptionsForReference(params, ref);
ReferenceInfo<ByteString> updated = namedRefsRequiresBaseReference(retrieveOptions) ? findCommonAncestor(ctx, ref.getHash(), commonAncestorState, (diffOnFrom, hash) -> {
ReferenceInfo<ByteString> newRef = ref;
if (retrieveOptions.isComputeCommonAncestor()) {
newRef = newRef.withCommonAncestor(hash);
}
if (retrieveOptions.isComputeAheadBehind()) {
int behind = commonAncestorState.indexOf(hash);
CommitsAheadBehind aheadBehind = CommitsAheadBehind.of(diffOnFrom, behind);
newRef = newRef.withAheadBehind(aheadBehind);
}
return newRef;
}) : null;
return updated != null ? updated : ref;
});
}
use of org.projectnessie.versioned.ReferenceInfo in project nessie by projectnessie.
the class AbstractAssign method assignReferenceToFreshMain.
/**
* Assigning a branch/tag to a fresh main without any commits didn't work in 0.9.2
*/
@Test
public void assignReferenceToFreshMain() throws ReferenceNotFoundException, ReferenceAlreadyExistsException, ReferenceConflictException {
ReferenceInfo<CommitMessage> main = store.getNamedRef("main", GetNamedRefsParams.DEFAULT);
try (Stream<Commit<CommitMessage, BaseContent>> commits = store().getCommits(main.getHash(), false)) {
assertThat(commits).isEmpty();
}
try (Stream<ReferenceInfo<CommitMessage>> refs = store().getNamedRefs(GetNamedRefsParams.DEFAULT)) {
assertThat(refs).extracting(r -> r.getNamedRef().getName()).containsExactly(main.getNamedRef().getName());
}
BranchName testBranch = BranchName.of("testBranch");
Hash testBranchHash = store.create(testBranch, Optional.empty());
store.assign(testBranch, Optional.of(testBranchHash), main.getHash());
assertThat(store.getNamedRef(testBranch.getName(), GetNamedRefsParams.DEFAULT).getHash()).isEqualTo(main.getHash());
TagName testTag = TagName.of("testTag");
Hash testTagHash = store.create(testTag, Optional.empty());
store.assign(testTag, Optional.of(testTagHash), main.getHash());
assertThat(store.getNamedRef(testTag.getName(), GetNamedRefsParams.DEFAULT).getHash()).isEqualTo(main.getHash());
}
Aggregations