Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/generated/Versions.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
*/
@NullMarked
public final class Versions {

private Versions() {
// Prevent instantiation
}

/**
* User agent value
*/
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/land/oras/LayoutRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import land.oras.exception.OrasException;
import land.oras.utils.SupportedAlgorithm;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/**
* A referer of a container on a {@link OCILayout}.
Expand All @@ -43,7 +44,7 @@ public final class LayoutRef extends Ref<LayoutRef> {
* Private constructor
* @param tag The tag.
*/
private LayoutRef(Path folder, String tag) {
private LayoutRef(Path folder, @Nullable String tag) {
super(tag);
this.folder = folder;
}
Expand Down Expand Up @@ -85,6 +86,25 @@ public static LayoutRef parse(String name) {
return new LayoutRef(path, tag);
}

/**
* Return a new layout reference for a path and digest or tag
* @param layout The OCI layout
* @param digest The digest
* @return The layout ref
*/
public static LayoutRef of(OCILayout layout, String digest) {
return new LayoutRef(layout.getPath(), digest);
}

/**
* Return a new layout reference for a path and digest or tag
* @param layout The OCI layout
* @return The layout ref
*/
public static LayoutRef of(OCILayout layout) {
return new LayoutRef(layout.getPath(), null);
}

/**
* Return a new layout reference for a path
* @param path The path
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/land/oras/Ref.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract sealed class Ref<T extends Ref<T>> permits ContainerRef, LayoutR
* Constructor
* @param tag The tag
*/
protected Ref(String tag) {
protected Ref(@Nullable String tag) {
this.tag = tag;
}

Expand Down
9 changes: 5 additions & 4 deletions src/test/java/land/oras/OCILayoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,8 @@ void shouldPushBlob() throws IOException {
byte[] content = "hi".getBytes(StandardCharsets.UTF_8);
String digest = SupportedAlgorithm.SHA256.digest(content);

LayoutRef layoutRef = LayoutRef.parse("%s@%s".formatted(path.toString(), digest));
OCILayout ociLayout = OCILayout.Builder.builder().defaults(path).build();
LayoutRef layoutRef = LayoutRef.of(ociLayout, digest);

// Push more blobs
ociLayout.pushBlob(layoutRef, "hi".getBytes(StandardCharsets.UTF_8));
Expand All @@ -621,11 +621,12 @@ void cannotPushBlobWithoutTagOrDigest() throws IOException {

Path invalidBlobPushDir = layoutPath.resolve("shouldPushArtifact");

LayoutRef noTagLayout = LayoutRef.parse("%s".formatted(invalidBlobPushDir.toString()));
LayoutRef noDigestLayout = LayoutRef.parse("%s:latest".formatted(invalidBlobPushDir.toString()));
OCILayout ociLayout =
OCILayout.Builder.builder().defaults(invalidBlobPushDir).build();

LayoutRef noTagLayout = LayoutRef.of(ociLayout);
LayoutRef noDigestLayout = LayoutRef.of(ociLayout, "latest");

// Push more blobs
assertThrows(OrasException.class, () -> {
ociLayout.pushBlob(noTagLayout, "hi".getBytes(StandardCharsets.UTF_8));
Expand Down Expand Up @@ -665,7 +666,7 @@ void testShouldCopyArtifactFromRegistryIntoOciLayout() throws IOException {
.build();

OCILayout ociLayout = OCILayout.builder().defaults(layoutPath).build();
LayoutRef layoutRef = LayoutRef.parse("%s".formatted(ociLayout.getPath()));
LayoutRef layoutRef = LayoutRef.of(ociLayout);

ContainerRef containerRef =
ContainerRef.parse("%s/library/artifact-oci-layout".formatted(this.registry.getRegistry()));
Expand Down