Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed Jun 27, 2023
1 parent e4af1ea commit 08c870f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
29 changes: 29 additions & 0 deletions CenterCLR.RelaxVersioner.Core/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,35 @@ public override string ToString() =>
$"StartDepth={this.StartDepth}, {this.Commit}";
}

private static async Task<Version> LookupVersionLabelAsync(
int depth, Commit commit, Dictionary<Commit, Version> reached, CancellationToken ct)
{
while (true)
{
// Rejoined parent branch.
if (reached.TryGetValue(commit, out Version version))
{
return Utilities.IncrementLastVersionComponent(version, depth);
}

// If found be applied tags at this commit:
if (commit.Tags.Count >= 1)
{
var filteredTags = commit.Tags.
Select(tag => Version.TryParse(tag.Name, out var version) ? (Version?)version : null).
Where(version => version.HasValue).
Select(version => Utilities.IncrementLastVersionComponent(version!.Value, depth)).
ToArray();

// Found first valid tag.
if (filteredTags.Length >= 1)
{
return filteredTags[0];
}
}
}
}

public static async Task<Version> LookupVersionLabelAsync(
Branch targetBranch, CancellationToken ct)
{
Expand Down
53 changes: 52 additions & 1 deletion CenterCLR.RelaxVersioner.Core/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

namespace RelaxVersioner;

public struct Version
public struct Version :
IEquatable<Version>, IComparable<Version>
{
private static readonly char[] separators = new[] { '.', ',', '/', '-', '_' };

Expand Down Expand Up @@ -58,6 +59,56 @@ public Version(int major, int minor, int build, int revision)
this.Revision = revision;
}

public override int GetHashCode() =>
this.Major.GetHashCode() ^
this.Minor.GetHashCode() ^
this.Build.GetHashCode() ^
this.Revision.GetHashCode();

public bool Equals(Version rhs) =>
this.Major.Equals(rhs.Major) &&
this.Minor.Equals(rhs.Minor) &&
this.Build.Equals(rhs.Build) &&
this.Revision.Equals(rhs.Revision);

public override bool Equals(object? obj) =>
obj is Version rhs && this.Equals(rhs);

bool IEquatable<Version>.Equals(Version other) =>
this.Equals(other);

public int CompareTo(Version rhs)
{
var major = this.Major.CompareTo(rhs.Major);
if (major != 0)
{
return major;
}

static int Compare(int? lhs, int? rhs)
{
var l = lhs is { } lv ? lv : 0;
var r = rhs is { } rv ? rv : 0;
return l.CompareTo(r);
}

var minor = Compare(this.Minor, rhs.Minor);
if (minor != 0)
{
return minor;
}
var build = Compare(this.Build, rhs.Build);
if (build != 0)
{
return build;
}
var revision = Compare(this.Revision, rhs.Revision);
return revision;
}

int IComparable<Version>.CompareTo(Version other) =>
this.CompareTo(other);

private IEnumerable<int> GetComponents()
{
yield return this.Major;
Expand Down

0 comments on commit 08c870f

Please sign in to comment.