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
8 changes: 6 additions & 2 deletions src/File/FileSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,22 @@ public FileSpec(string path, Uri? uri = null, string? etag = null, string? sha =
Sha = sha;
NewSha = sha;

if (!finalPath && uri != null &&
if (!finalPath && uri != null && !uri.AbsolutePath.EndsWith('/') &&
(path.EndsWith('\\') || path.EndsWith('/')))
{
path = System.IO.Path.Combine(path, WithDefaultPath(uri!).Path);
}

// This will also normalize double slashes.
var parts = path.Split(new[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
var parts = path.Split(['\\', '/'], StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 0 && parts[0] == ".")
Path = string.Join('/', parts.Skip(1));
else
Path = string.Join('/', parts);

// Preserve trailing slash if present, for consistency with url ending in /.
if (path.EndsWith('/') || path.EndsWith("\\"))
Path += "/";
}

public string Path { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/File/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static async Task<int> Main(string[] args)
}

// Try to pair Uri+File to allow intuitive download>path mapping, such as
// https://gitub.com/org/repo/docs/file.md docs/file.md
// https://gitub.com/org/repo/docs/file.md > docs/file.md
if (Uri.TryCreate(extraArgs[i], UriKind.Absolute, out var uri))
{
var next = i + 1;
Expand Down
11 changes: 10 additions & 1 deletion src/Tests/FileSpecTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@ public void WhenPathIsDirAppendsDefaultPath(string url, string path, string expe
[Fact]
public void WhenSourceUriEndsInSlathThenTargetPathIsBaseDir()
{
var spec = new FileSpec("docs/", new Uri("https://github.com/devlooped/dotnet-file/tree/main/src/api/documentation/"));
var spec = new FileSpec("docs", new Uri("https://github.com/devlooped/dotnet-file/tree/main/src/api/documentation/"));

// NOTE: the relative `src/api/documentation` is not appended to the file path.
Assert.Equal("docs", spec.Path);
}

[Fact]
public void WhenSpecAndSourceUriEndInSlathThenPreservesBaseDirSlash()
{
var spec = new FileSpec("docs/", new Uri("https://github.com/devlooped/dotnet-file/tree/main/src/api/documentation/"));

// NOTE: the relative `src/api/documentation` is not appended to the file path.
Assert.Equal("docs/", spec.Path);
}

[Theory]
[InlineData("https://github.com/devlooped/dotnet-file/blob/main/src/Foo.cs", ".", "Foo.cs")]
[InlineData("https://github.com/devlooped/dotnet-file/blob/main/src/Foo.cs", "src/External/.", "src/External/Foo.cs")]
Expand Down