Skip to content

Commit

Permalink
refactor: keep committed tracking in command packages
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <jinzha1@microsoft.com>
  • Loading branch information
qweeah committed Jul 12, 2024
1 parent 7c843ca commit 46084e7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/oras/internal/display/status/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (ph *TextPushHandler) UpdateCopyOptions(opts *oras.CopyGraphOptions, fetche
}
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
err, successors := descriptor.GetSuccessors(ctx, desc, fetcher, committed)
successors, err := descriptor.GetSuccessors(ctx, desc, fetcher, DeduplicatedFilter(committed))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/internal/display/status/tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (ph *TTYPushHandler) UpdateCopyOptions(opts *oras.CopyGraphOptions, fetcher
}
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
err, successors := descriptor.GetSuccessors(ctx, desc, fetcher, committed)
successors, err := descriptor.GetSuccessors(ctx, desc, fetcher, DeduplicatedFilter(committed))
if err != nil {
return err

Check warning on line 75 in cmd/oras/internal/display/status/tty.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/internal/display/status/tty.go#L75

Added line #L75 was not covered by tests
}
Expand Down
17 changes: 17 additions & 0 deletions cmd/oras/internal/display/status/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ limitations under the License.

package status

import (
"sync"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

// Prompts for pull events.
const (
PullPromptDownloading = "Downloading"
Expand All @@ -32,3 +38,14 @@ const (
PushPromptSkipped = "Skipped "
PushPromptExists = "Exists "
)

// DeduplicatedFilter filters out deduplicated descriptors.
func DeduplicatedFilter(committed *sync.Map) func(desc ocispec.Descriptor) bool {
return func(desc ocispec.Descriptor) bool {

name := desc.Annotations[ocispec.AnnotationTitle]
v, ok := committed.Load(desc.Digest.String())
// committed but not printed == deduplicated
return ok && v != name
}
}
8 changes: 4 additions & 4 deletions cmd/oras/root/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"oras.land/oras/cmd/oras/internal/argument"
"oras.land/oras/cmd/oras/internal/command"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/display/status"
"oras.land/oras/cmd/oras/internal/display/status/track"
oerrors "oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/option"
Expand Down Expand Up @@ -189,7 +190,7 @@ func doCopy(ctx context.Context, printer *output.Printer, src oras.ReadOnlyGraph
}
extendedCopyOptions.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
err, successors := descriptor.GetSuccessors(ctx, desc, dst, committed)
successors, err := descriptor.GetSuccessors(ctx, desc, dst, status.DeduplicatedFilter(committed))
if err != nil {
return err
}
Expand All @@ -216,13 +217,12 @@ func doCopy(ctx context.Context, printer *output.Printer, src oras.ReadOnlyGraph
}
extendedCopyOptions.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
err, successors := descriptor.GetSuccessors(ctx, desc, tracked, committed)
successors, err := descriptor.GetSuccessors(ctx, desc, tracked, status.DeduplicatedFilter(committed))
if err != nil {
return err

Check warning on line 222 in cmd/oras/root/cp.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/cp.go#L222

Added line #L222 was not covered by tests
}
for _, successor := range successors {
err = tracked.Prompt(successor, promptSkipped)
if err != nil {
if err = tracked.Prompt(successor, promptSkipped); err != nil {
return err

Check warning on line 226 in cmd/oras/root/cp.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/cp.go#L225-L226

Added lines #L225 - L226 were not covered by tests
}
}
Expand Down
15 changes: 8 additions & 7 deletions internal/descriptor/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ package descriptor

import (
"context"

"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/content"
"sync"

"oras.land/oras/internal/docker"
)
Expand Down Expand Up @@ -56,17 +56,18 @@ func GenerateContentKey(desc ocispec.Descriptor) string {
return desc.Digest.String() + desc.Annotations[ocispec.AnnotationTitle]
}

// GetSuccessors prints transfer status of successors.
func GetSuccessors(ctx context.Context, desc ocispec.Descriptor, fetcher content.Fetcher, committed *sync.Map) (err error, successors []ocispec.Descriptor) {
// GetSuccessors fetches successors and returns filterred ones.
func GetSuccessors(ctx context.Context, desc ocispec.Descriptor, fetcher content.Fetcher, filter func(ocispec.Descriptor) bool) ([]ocispec.Descriptor, error) {
allSuccessors, err := content.Successors(ctx, fetcher, desc)
if err != nil {
return err, nil
return nil, err

Check warning on line 63 in internal/descriptor/descriptor.go

View check run for this annotation

Codecov / codecov/patch

internal/descriptor/descriptor.go#L63

Added line #L63 was not covered by tests
}

var successors []ocispec.Descriptor
for _, s := range allSuccessors {
name := s.Annotations[ocispec.AnnotationTitle]
if v, ok := committed.Load(s.Digest.String()); ok && v != name {
if filter(s) {
successors = append(successors, s)
}
}
return nil, successors
return successors, nil
}

0 comments on commit 46084e7

Please sign in to comment.