Skip to content

Commit e32433c

Browse files
committed
Deprecate FetchTag(tag, remote) in favor of a ctor injected remote.
1 parent 7edcfbe commit e32433c

7 files changed

+70
-21
lines changed

src/Verlite.Core/GitRepoInspector.cs

+37-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Diagnostics.CodeAnalysis;
45
using System.Linq;
56
using System.Text.RegularExpressions;
67
using System.Threading.Tasks;
@@ -21,23 +22,43 @@ public class GitMissingOrNotGitRepoException : RepoInspectionException
2122
/// <seealso cref="IRepoInspector"/>
2223
public sealed class GitRepoInspector : IRepoInspector
2324
{
25+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
26+
// TODO: Remove all of these enumerated permutations of the old optional args in 2.0.0
27+
[ExcludeFromCodeCoverage]
28+
[Obsolete("Use FromPath(path, remote, log, commandRunner)", error: false)]
29+
public static Task<GitRepoInspector> FromPath(string path, ILogger? log, ICommandRunner? commandRunner)
30+
=> FromPath(path, "origin", log, commandRunner ?? new SystemCommandRunner());
31+
[ExcludeFromCodeCoverage]
32+
[Obsolete("Use FromPath(path, remote, log, commandRunner)", error: false)]
33+
public static Task<GitRepoInspector> FromPath(string path, ILogger? log)
34+
=> FromPath(path, "origin", log, new SystemCommandRunner());
35+
[ExcludeFromCodeCoverage]
36+
[Obsolete("Use FromPath(path, remote, log, commandRunner)", error: false)]
37+
public static Task<GitRepoInspector> FromPath(string path, ICommandRunner? commandRunner)
38+
=> FromPath(path, "origin", null, commandRunner?? new SystemCommandRunner());
39+
[ExcludeFromCodeCoverage]
40+
[Obsolete("Use FromPath(path, remote, log, commandRunner)", error: false)]
41+
public static Task<GitRepoInspector> FromPath(string path)
42+
=> FromPath(path, "origin", null, new SystemCommandRunner());
43+
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
44+
2445
/// <summary>
2546
/// Creates an inspector from the specified path.
2647
/// </summary>
2748
/// <param name="path">The path of the Git repository.</param>
49+
/// <param name="remote">The remote endpoint.</param>
2850
/// <param name="log">A logger for diagnostics.</param>
29-
/// <param name="commandRunner">A command runner to use. Defaults to <see cref="SystemCommandRunner"/> if null is given.</param>
51+
/// <param name="commandRunner">A command runner to use.</param>
3052
/// <exception cref="GitMissingOrNotGitRepoException">Thrown if the path is not a Git repository.</exception>
3153
/// <returns>A task containing the Git repo inspector.</returns>
32-
public static async Task<GitRepoInspector> FromPath(string path, ILogger? log = null, ICommandRunner? commandRunner = null)
54+
public static async Task<GitRepoInspector> FromPath(string path, string remote, ILogger? log, ICommandRunner commandRunner)
3355
{
34-
commandRunner ??= new SystemCommandRunner();
35-
3656
try
3757
{
3858
var (root, _) = await commandRunner.Run(path, "git", new string[] { "rev-parse", "--show-toplevel" });
3959
var ret = new GitRepoInspector(
4060
root,
61+
remote,
4162
log,
4263
commandRunner);
4364
await ret.CacheParents();
@@ -61,10 +82,12 @@ public static async Task<GitRepoInspector> FromPath(string path, ILogger? log =
6182
public string Root { get; }
6283
private Dictionary<Commit, Commit> CachedParents { get; } = new();
6384
private (int depth, bool shallow, Commit deepest)? FetchDepth { get; set; }
85+
public string Remote { get; }
6486

65-
private GitRepoInspector(string root, ILogger? log, ICommandRunner commandRunner)
87+
private GitRepoInspector(string root, string remote, ILogger? log, ICommandRunner commandRunner)
6688
{
6789
Root = root;
90+
Remote = remote;
6891
Log = log;
6992
CommandRunner = commandRunner;
7093
}
@@ -170,11 +193,9 @@ private async Task Deepen()
170193
try
171194
{
172195
if (DeepenFromCommitSupported)
173-
_ = await Git("fetch", "origin", FetchDepth.Value.deepest.Id, $"--depth={deltaDepth}");
196+
_ = await Git("fetch", Remote, FetchDepth.Value.deepest.Id, $"--depth={deltaDepth}");
174197
else
175-
_ = await Git("fetch", $"--depth={newDepth}");
176-
177-
await CacheParents();
198+
_ = await Git("fetch", Remote, $"--depth={newDepth}");
178199
}
179200
catch (CommandException ex)
180201
when (
@@ -275,7 +296,7 @@ public async Task<TagContainer> GetTags(QueryTarget queryTarget)
275296
try
276297
{
277298
Log?.Verbatim($"GetTags(): Reading remote tags.");
278-
var (response, _) = await Git("ls-remote", "--tags");
299+
var (response, _) = await Git("ls-remote", "--tags", Remote, "*");
279300

280301
foreach (Tag tag in MatchTags(response))
281302
{
@@ -318,5 +339,11 @@ public async Task FetchTag(Tag tag, string remote)
318339
else
319340
await Git("fetch", remote, $"refs/tags/{tag.Name}:refs/tags/{tag.Name}");
320341
}
342+
343+
/// <inheritdoc/>
344+
public Task FetchTag(Tag tag)
345+
{
346+
return FetchTag(tag, Remote);
347+
}
321348
}
322349
}

src/Verlite.Core/IRepoInspector.cs

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ public interface IRepoInspector
9494
/// </summary>
9595
/// <param name="tag">The tag to fetch.</param>
9696
/// <param name="remote">Which remote to fetch the tag from.</param>
97+
[Obsolete("Use FetchTag(tag)", error: false)]
9798
Task FetchTag(Tag tag, string remote);
99+
/// <summary>
100+
/// Fetches the specified tag from the remote.
101+
/// </summary>
102+
/// <param name="tag">The tag to fetch.</param>
103+
Task FetchTag(Tag tag);
98104
}
99105
}

src/Verlite.Core/PublicAPI.Shipped.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ static Verlite.Command.ParseCommandLine(string! cmdLine) -> System.Collections.G
1414
static Verlite.Command.Run(string! directory, string! command, string![]! args, System.Collections.Generic.IDictionary<string!, string!>? envVars = null) -> System.Threading.Tasks.Task<(string! stdout, string! stderr)>!
1515
static Verlite.Commit.operator !=(Verlite.Commit left, Verlite.Commit right) -> bool
1616
static Verlite.Commit.operator ==(Verlite.Commit left, Verlite.Commit right) -> bool
17-
static Verlite.GitRepoInspector.FromPath(string! path, Verlite.ILogger? log = null, Verlite.ICommandRunner? commandRunner = null) -> System.Threading.Tasks.Task<Verlite.GitRepoInspector!>!
17+
static Verlite.GitRepoInspector.FromPath(string! path) -> System.Threading.Tasks.Task<Verlite.GitRepoInspector!>!
18+
static Verlite.GitRepoInspector.FromPath(string! path, string! remote, Verlite.ILogger? log, Verlite.ICommandRunner! commandRunner) -> System.Threading.Tasks.Task<Verlite.GitRepoInspector!>!
19+
static Verlite.GitRepoInspector.FromPath(string! path, Verlite.ICommandRunner? commandRunner) -> System.Threading.Tasks.Task<Verlite.GitRepoInspector!>!
20+
static Verlite.GitRepoInspector.FromPath(string! path, Verlite.ILogger? log) -> System.Threading.Tasks.Task<Verlite.GitRepoInspector!>!
21+
static Verlite.GitRepoInspector.FromPath(string! path, Verlite.ILogger? log, Verlite.ICommandRunner? commandRunner) -> System.Threading.Tasks.Task<Verlite.GitRepoInspector!>!
1822
static Verlite.HeightCalculator.FromRepository(Verlite.IRepoInspector! repo, string! tagPrefix, bool queryRemoteTags, Verlite.ILogger? log = null, Verlite.ITagFilter? tagFilter = null) -> System.Threading.Tasks.Task<(int height, Verlite.TaggedVersion?)>!
1923
static Verlite.SemVer.ComparePrerelease(string! left, string! right) -> int
2024
static Verlite.SemVer.IsValidIdentifierCharacter(char input) -> bool
@@ -57,6 +61,7 @@ Verlite.GitMissingOrNotGitRepoException.GitMissingOrNotGitRepoException() -> voi
5761
Verlite.GitRepoInspector
5862
Verlite.GitRepoInspector.CanDeepen.get -> bool
5963
Verlite.GitRepoInspector.CanDeepen.set -> void
64+
Verlite.GitRepoInspector.FetchTag(Verlite.Tag tag) -> System.Threading.Tasks.Task!
6065
Verlite.GitRepoInspector.FetchTag(Verlite.Tag tag, string! remote) -> System.Threading.Tasks.Task!
6166
Verlite.GitRepoInspector.GetHead() -> System.Threading.Tasks.Task<Verlite.Commit?>!
6267
Verlite.GitRepoInspector.GetParent(Verlite.Commit commit) -> System.Threading.Tasks.Task<Verlite.Commit?>!
@@ -71,6 +76,7 @@ Verlite.ILogger.Normal(string! message) -> void
7176
Verlite.ILogger.Verbatim(string! message) -> void
7277
Verlite.ILogger.Verbose(string! message) -> void
7378
Verlite.IRepoInspector
79+
Verlite.IRepoInspector.FetchTag(Verlite.Tag tag) -> System.Threading.Tasks.Task!
7480
Verlite.IRepoInspector.FetchTag(Verlite.Tag tag, string! remote) -> System.Threading.Tasks.Task!
7581
Verlite.IRepoInspector.GetHead() -> System.Threading.Tasks.Task<Verlite.Commit?>!
7682
Verlite.IRepoInspector.GetParent(Verlite.Commit commit) -> System.Threading.Tasks.Task<Verlite.Commit?>!

src/Verlite.Core/mark-shipped.sh

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ cd "$(dirname "$0")"
55

66
shipped="$(cat PublicAPI.Shipped.txt PublicAPI.Unshipped.txt \
77
| sort --ignore-case \
8+
| awk '{$1=$1};1' \
89
| uniq)"
910

1011
echo "$shipped" > PublicAPI.Shipped.txt

tests/UnitTests/GitRepoInspectorTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public async Task FetchTagFetchesRemoteTagOnly()
244244
firstTags.Should().ContainSingle();
245245
var firstTag = firstTags[0];
246246

247-
await repo.FetchTag(firstTag, "origin");
247+
await repo.FetchTag(firstTag);
248248

249249
var localTags = await repo.GetTags(QueryTarget.Local);
250250
localTags.Should().Contain(new Tag[]
@@ -347,7 +347,7 @@ public async Task FetchingTagInShallowCloneDoesNotDeepenBeyondTag()
347347
.Where(tag => tag.Name == "tag-two")
348348
.First();
349349

350-
await repo.FetchTag(desiredTag, "origin");
350+
await repo.FetchTag(desiredTag);
351351

352352
var desiredParent = await repo.GetParent(desiredTag.PointsTo);
353353
desiredParent.Should().Be(deeperTag.PointsTo);
@@ -384,7 +384,7 @@ public async Task FetchingTagInDeepCloneDoesNotMakeShallow()
384384
.Where(tag => tag.Name == "tag-two")
385385
.First();
386386

387-
await repo.FetchTag(desiredTag, "origin");
387+
await repo.FetchTag(desiredTag);
388388
repo = null; // repo modified invalidating internal cache, so remake inspector
389389
repo = await clone.MakeInspector();
390390

@@ -422,7 +422,7 @@ public async Task ShallowGitFetchFromCommitCanFallBack()
422422
var filteredHistory = mockCommandRunner.CommandHistory
423423
.Where(cmd => cmd.Contains("git fetch", StringComparison.Ordinal))
424424
.Select(cmd =>
425-
cmd.Contains("origin", StringComparison.Ordinal) ?
425+
!cmd.Contains("origin --depth", StringComparison.Ordinal) ?
426426
"normal fetch" :
427427
"legacy fetch");
428428

tests/UnitTests/Mocks/MockCommandRunnerWithOldRemoteGitVersion.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading.Tasks;
@@ -30,12 +31,15 @@ public MockCommandRunnerWithOldRemoteGitVersion(ICommandRunner baseRunner)
3031
else
3132
commandHistory.Add($"{command} {string.Join(' ', args)}");
3233

33-
return (command, firstArg, args) switch
34+
if (firstArg == "fetch")
3435
{
35-
("git", "fetch", _) when args.Contains("origin") =>
36-
throw new CommandException(128, "", "error: Server does not allow request for unadvertised object a1b2c3"),
37-
_ => BaseRunner.Run(directory, command, args, envVars),
38-
};
36+
if (args.Length is not 3 and not 4)
37+
throw new NotSupportedException();
38+
if (args.Length == 4 && !args[2].StartsWith("--depth", StringComparison.Ordinal))
39+
throw new CommandException(128, "", "error: Server does not allow request for unadvertised object a1b2c3");
40+
}
41+
42+
return BaseRunner.Run(directory, command, args, envVars);
3943
}
4044
}
4145
}

tests/UnitTests/Mocks/MockRepoInspector.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ public MockRepoInspector(IReadOnlyList<MockRepoCommit> commits)
5959
Commits.Reverse(); // reverse it to make First the HEAD.
6060
}
6161

62-
async Task IRepoInspector.FetchTag(Tag tag, string remote)
62+
Task IRepoInspector.FetchTag(Tag tag, string remote)
63+
{
64+
return (this as IRepoInspector).FetchTag(tag);
65+
}
66+
67+
async Task IRepoInspector.FetchTag(Tag tag)
6368
{
6469
if (LocalTags.Contains(tag))
6570
return;

0 commit comments

Comments
 (0)