Skip to content

Commit

Permalink
Auto fetch the version tag too.
Browse files Browse the repository at this point in the history
Required some refactoring to propegate the tag back up.
  • Loading branch information
AshleighAdams committed Jan 28, 2021
1 parent cb5e9a4 commit fd7f547
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
15 changes: 13 additions & 2 deletions src/Verlite.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,21 @@ public async static Task RootCommandAsync(
};
await repo.SetPath(sourceDirectory);

var (height, lastTag) = await HeightCalculator.FromRepository(repo, opts.TagPrefix, autoFetch);
var version = VersionCalculator.CalculateVersion(lastTag, opts, height);
var (height, lastTagVer) = await HeightCalculator.FromRepository(repo, opts.TagPrefix, autoFetch);
var version = VersionCalculator.CalculateVersion(lastTagVer?.Version, opts, height);
version.BuildMetadata = opts.BuildMetadata;

if (lastTagVer is not null && autoFetch)
{
var localTag = (await repo.GetTags(QueryTarget.Local))
.Where(x => x == lastTagVer.Tag);
if (!localTag.Any())
{
Console.Error.WriteLine("Local repo missing version tag, fetching.");
await repo.FetchTag(lastTagVer.Tag, "origin");
}
}

string toShow = show switch
{
Show.All => version.ToString(),
Expand Down
8 changes: 8 additions & 0 deletions src/Verlite.Core/GitRepoInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,13 @@ public async Task<TagContainer> GetTags(QueryTarget queryTarget)

return new TagContainer(tags);
}

public async Task FetchTag(Tag tag, string remote)
{
if (IsShallow ?? true)
await Git("fetch", "--depth", "1", remote, $"refs/tags/{tag.Name}:refs/tags/{tag.Name}");
else
await Git("fetch", remote, $"refs/tags/{tag.Name}:refs/tags/{tag.Name}");
}
}
}
35 changes: 30 additions & 5 deletions src/Verlite.Core/HeightCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace Verlite
{
public static class HeightCalculator
public static partial class HeightCalculator
{
private static IEnumerable<SemVer> SelectWhereSemver(this IEnumerable<Tag> tags, string tagPrefix)
private static IEnumerable<(SemVer version, Tag tag)> SelectWhereSemver(this IEnumerable<Tag> tags, string tagPrefix)
{
foreach (var tag in tags)
{
Expand All @@ -19,11 +19,34 @@ private static IEnumerable<SemVer> SelectWhereSemver(this IEnumerable<Tag> tags,
Console.Error.WriteLineAsync($"Warning: Failed to parse SemVer from tag {tag}, ignoring.");
continue;
}
yield return version.Value;
yield return (version.Value, tag);
}
}

public static async Task<(int height, SemVer? lastVersion)> FromRepository(IRepoInspector repo, string tagPrefix, bool queryRemoteTags)
private static T MaxBy<T, TSelector>(this IEnumerable<T> self, Func<T, TSelector> selector)
where TSelector : struct
{
T max = default!;
TSelector? select = null;
Comparer<TSelector>? comparer = Comparer<TSelector>.Default;

foreach (T item in self)
{
TSelector new_select = selector(item);
if (!select.HasValue || comparer.Compare(select.Value, new_select) < 0)
{
max = item;
select = new_select;
}
}

if (!select.HasValue)
throw new ArgumentException("no values to get the max of", nameof(self));

return max;
}

public static async Task<(int height, TaggedVersion?)> FromRepository(IRepoInspector repo, string tagPrefix, bool queryRemoteTags)
{
QueryTarget queryTags = QueryTarget.Local;
if (queryRemoteTags)
Expand Down Expand Up @@ -57,7 +80,9 @@ private static IEnumerable<SemVer> SelectWhereSemver(this IEnumerable<Tag> tags,
{
foreach (var ver in versions)
Debug.WriteLine($" found version: {ver}");
return (height, versions.Max());

var (version, tag) = versions.MaxBy(ver => ver.version);
return (height, new TaggedVersion(version, tag));
}

var parent = await repo.GetParent(current);
Expand Down
1 change: 1 addition & 0 deletions src/Verlite.Core/IRepoInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ public interface IRepoInspector
Task<Commit?> GetHead();
Task<Commit?> GetParent(Commit commit);
Task<TagContainer> GetTags(QueryTarget queryTarget);
Task FetchTag(Tag tag, string remote);
}
}
8 changes: 7 additions & 1 deletion src/Verlite.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ override Verlite.Tag.GetHashCode() -> int
override Verlite.Tag.ToString() -> string!
static Verlite.Commit.operator !=(Verlite.Commit left, Verlite.Commit right) -> bool
static Verlite.Commit.operator ==(Verlite.Commit left, Verlite.Commit right) -> bool
static Verlite.HeightCalculator.FromRepository(Verlite.IRepoInspector! repo, string! tagPrefix, bool queryRemoteTags) -> System.Threading.Tasks.Task<(int height, Verlite.SemVer? lastVersion)>!
static Verlite.HeightCalculator.FromRepository(Verlite.IRepoInspector! repo, string! tagPrefix, bool queryRemoteTags) -> System.Threading.Tasks.Task<(int height, Verlite.HeightCalculator.TaggedVersion?)>!
static Verlite.SemVer.ComparePrerelease(string! left, string! right) -> int
static Verlite.SemVer.IsValidIdentifierCharacter(char input) -> bool
static Verlite.SemVer.IsValidIdentifierString(string! input) -> bool
Expand All @@ -37,14 +37,20 @@ Verlite.Commit.Id.get -> string!
Verlite.GitRepoInspector
Verlite.GitRepoInspector.CanDeepen.get -> bool
Verlite.GitRepoInspector.CanDeepen.set -> void
Verlite.GitRepoInspector.FetchTag(Verlite.Tag tag, string! remote) -> System.Threading.Tasks.Task!
Verlite.GitRepoInspector.GetHead() -> System.Threading.Tasks.Task<Verlite.Commit?>!
Verlite.GitRepoInspector.GetParent(Verlite.Commit commit) -> System.Threading.Tasks.Task<Verlite.Commit?>!
Verlite.GitRepoInspector.GetTags(Verlite.QueryTarget queryTarget) -> System.Threading.Tasks.Task<Verlite.TagContainer!>!
Verlite.GitRepoInspector.GitRepoInspector() -> void
Verlite.GitRepoInspector.Root.get -> string?
Verlite.GitRepoInspector.SetPath(string! path) -> System.Threading.Tasks.Task!
Verlite.HeightCalculator
Verlite.HeightCalculator.TaggedVersion
Verlite.HeightCalculator.TaggedVersion.Tag.get -> Verlite.Tag
Verlite.HeightCalculator.TaggedVersion.TaggedVersion(Verlite.SemVer version, Verlite.Tag tag) -> void
Verlite.HeightCalculator.TaggedVersion.Version.get -> Verlite.SemVer
Verlite.IRepoInspector
Verlite.IRepoInspector.FetchTag(Verlite.Tag tag, string! remote) -> System.Threading.Tasks.Task!
Verlite.IRepoInspector.GetHead() -> System.Threading.Tasks.Task<Verlite.Commit?>!
Verlite.IRepoInspector.GetParent(Verlite.Commit commit) -> System.Threading.Tasks.Task<Verlite.Commit?>!
Verlite.IRepoInspector.GetTags(Verlite.QueryTarget queryTarget) -> System.Threading.Tasks.Task<Verlite.TagContainer!>!
Expand Down
16 changes: 16 additions & 0 deletions src/Verlite.Core/TaggedVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Verlite
{
public static partial class HeightCalculator
{
public class TaggedVersion
{
public SemVer Version { get; }
public Tag Tag { get; }
public TaggedVersion(SemVer version, Tag tag)
{
Version = version;
Tag = tag;
}
}
}
}

0 comments on commit fd7f547

Please sign in to comment.