Skip to content

Commit

Permalink
refactor!: modernize copy
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <[email protected]>
  • Loading branch information
shizhMSFT committed Dec 29, 2023
1 parent d0f45f5 commit 0e2f5ac
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 86 deletions.
2 changes: 1 addition & 1 deletion src/OrasProject.Oras/Content/MemoryGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Task<IEnumerable<Descriptor>> GetPredecessorsAsync(Descriptor node, Cance
var key = node.BasicDescriptor;
if (_predecessors.TryGetValue(key, out var predecessors))
{
return Task.FromResult<IEnumerable<Descriptor>> (predecessors.Values);
return Task.FromResult<IEnumerable<Descriptor>>(predecessors.Values);
}
return Task.FromResult<IEnumerable<Descriptor>>(Array.Empty<Descriptor>());
}
Expand Down
80 changes: 0 additions & 80 deletions src/OrasProject.Oras/Copy.cs

This file was deleted.

73 changes: 73 additions & 0 deletions src/OrasProject.Oras/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright The ORAS Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using OrasProject.Oras.Oci;
using System;
using System.Threading;
using System.Threading.Tasks;
using static OrasProject.Oras.Content.Extensions;

namespace OrasProject.Oras;

public static class Extensions
{

/// <summary>
/// Copy copies a rooted directed acyclic graph (DAG) with the tagged root node
/// in the source Target to the destination Target.
/// The destination reference will be the same as the source reference if the
/// destination reference is left blank.
/// Returns the descriptor of the root node on successful copy.
/// </summary>
/// <param name="src"></param>
/// <param name="srcRef"></param>
/// <param name="dst"></param>
/// <param name="dstRef"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static async Task<Descriptor> CopyAsync(this ITarget src, string srcRef, ITarget dst, string dstRef, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(dstRef))
{
dstRef = srcRef;
}
var root = await src.ResolveAsync(srcRef, cancellationToken).ConfigureAwait(false);
await src.CopyGraphAsync(dst, root, cancellationToken).ConfigureAwait(false);
await dst.TagAsync(root, dstRef, cancellationToken).ConfigureAwait(false);
return root;
}

public static async Task CopyGraphAsync(this ITarget src, ITarget dst, Descriptor node, CancellationToken cancellationToken)
{
// check if node exists in target
if (await dst.ExistsAsync(node, cancellationToken).ConfigureAwait(false))
{
return;

Check warning on line 56 in src/OrasProject.Oras/Extensions.cs

View check run for this annotation

Codecov / codecov/patch

src/OrasProject.Oras/Extensions.cs#L55-L56

Added lines #L55 - L56 were not covered by tests
}

// retrieve successors
var successors = await src.GetSuccessorsAsync(node, cancellationToken).ConfigureAwait(false);
// obtain data stream
var dataStream = await src.FetchAsync(node, cancellationToken).ConfigureAwait(false);
// check if the node has successors
if (successors != null)
{
foreach (var childNode in successors)
{
await src.CopyGraphAsync(dst, childNode, cancellationToken).ConfigureAwait(false);
}
}
await dst.PushAsync(node, dataStream, cancellationToken).ConfigureAwait(false);
}
}
4 changes: 2 additions & 2 deletions src/OrasProject.Oras/ITarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// limitations under the License.

using OrasProject.Oras.Content;

namespace OrasProject.Oras;
Expand Down
4 changes: 2 additions & 2 deletions tests/OrasProject.Oras.Tests/CopyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task CanCopyBetweenMemoryTargetsWithTaggedNode()
var reference = "foobar";
await sourceTarget.TagAsync(root, reference, cancellationToken);
var destinationTarget = new MemoryTarget();
var gotDesc = await Copy.CopyAsync(sourceTarget, reference, destinationTarget, "", cancellationToken);
var gotDesc = await sourceTarget.CopyAsync(reference, destinationTarget, "", cancellationToken);
Assert.Equal(gotDesc, root);
Assert.Equal(await destinationTarget.ResolveAsync(reference, cancellationToken), root);

Expand Down Expand Up @@ -130,7 +130,7 @@ public async Task CanCopyBetweenMemoryTargets()
}
var root = descs[3];
var destinationTarget = new MemoryTarget();
await Copy.CopyGraphAsync(sourceTarget, destinationTarget, root, cancellationToken);
await sourceTarget.CopyGraphAsync(destinationTarget, root, cancellationToken);
for (var i = 0; i < descs.Count; i++)
{
Assert.True(await destinationTarget.ExistsAsync(descs[i], cancellationToken));
Expand Down
2 changes: 1 addition & 1 deletion tests/OrasProject.Oras.Tests/RemoteTest/RepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@ public async Task CopyFromRepositoryToMemory()

var dst = new MemoryTarget();
var tagName = "latest";
var desc = await Copy.CopyAsync(src, tagName, dst, tagName, CancellationToken.None);
var desc = await src.CopyAsync(tagName, dst, tagName, CancellationToken.None);
}

[Fact]
Expand Down

0 comments on commit 0e2f5ac

Please sign in to comment.