Skip to content

Commit 3c7793b

Browse files
authored
Compare version segments of any width so build-id updates stop vanishing (#5547)
1 parent 63ac781 commit 3c7793b

3 files changed

Lines changed: 129 additions & 25 deletions

File tree

‎src/UniGetUI.Core.Tools.Tests/ToolsTests.cs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,65 @@ public void TestGetVersionStringAsFloat_WithNonNumericVersion_ReturnsNull()
449449
Assert.Equal(CoreTools.Version.Null, v);
450450
}
451451

452+
[Theory]
453+
[InlineData("37149234619", "38756550835")]
454+
[InlineData("1.2147483648", "1.2147483649")]
455+
[InlineData("20260101.120000000001", "20260101.120000000002")]
456+
[InlineData("99999999999999999999", "100000000000000000000")]
457+
[InlineData("1.20260101120000000000001", "1.20260101120000000000002")]
458+
public void TestGetVersionStringAsFloat_WithSegmentsBeyondInt32_OrdersCorrectly(
459+
string older,
460+
string newer
461+
)
462+
{
463+
Assert.True(
464+
CoreTools.VersionStringToStruct(older) < CoreTools.VersionStringToStruct(newer)
465+
);
466+
}
467+
468+
[Theory]
469+
[InlineData("1.0.0+20260101123456", 1, 0, 0)]
470+
[InlineData("1.0 (20260101123456)", 1, 0, 0)]
471+
[InlineData("2.0.0+42", 2, 0, 42)]
472+
[InlineData("1.0.0+2147483647", 1, 0, 2147483647)]
473+
[InlineData("1.0.0+2147483648", 1, 0, 0)]
474+
public void TestGetVersionStringAsFloat_WithSplicedDigitRuns_KeepsInt32Limit(
475+
string version,
476+
int major,
477+
int minor,
478+
int patch
479+
)
480+
{
481+
Assert.Equal(
482+
new CoreTools.Version(major, minor, patch),
483+
CoreTools.VersionStringToStruct(version)
484+
);
485+
}
486+
487+
[Fact]
488+
public void TestGetVersionStringAsFloat_WithExtraSegmentBeyondInt32_HashesConsistently()
489+
{
490+
CoreTools.Version a = CoreTools.VersionStringToStruct("1.2.3.4.37149234619");
491+
CoreTools.Version b = CoreTools.VersionStringToStruct("1.2.3.4.37149234619.0");
492+
493+
Assert.Equal(a, b);
494+
Assert.Equal(a.GetHashCode(), b.GetHashCode());
495+
Assert.NotEqual(a, CoreTools.VersionStringToStruct("1.2.3.4.38756550835"));
496+
}
497+
498+
[Theory]
499+
[InlineData("20250301123456", "2.0.0")]
500+
[InlineData("37149234619", "1.0.0")]
501+
public void TestGetVersionStringAsFloat_WithLongNumericScheme_RanksAboveDottedVersions(
502+
string numericScheme,
503+
string dotted
504+
)
505+
{
506+
Assert.True(
507+
CoreTools.VersionStringToStruct(numericScheme) > CoreTools.VersionStringToStruct(dotted)
508+
);
509+
}
510+
452511
// An underscore introducing a pre-release tag rather than a numeric revision must keep
453512
// reporting as unknown, as it did before '_' became a segment separator. Parsing these
454513
// would rank the pre-release above the final release and hide the 2.0.0_rc1 -> 2.0.0

‎src/UniGetUI.Core.Tools/Tools.cs‎

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Globalization;
77
using System.Net;
88
using System.Net.NetworkInformation;
9+
using System.Numerics;
910
using System.Runtime.InteropServices;
1011
using System.Security.Cryptography;
1112
using System.Security.Principal;
@@ -534,10 +535,10 @@ public struct Version : IComparable, IComparable<Version>, IEquatable<Version>
534535
{
535536
public static readonly Version Null = new(-1, -1, -1, -1);
536537

537-
public readonly int Major;
538-
public readonly int Minor;
539-
public readonly int Patch;
540-
public readonly int Remainder;
538+
public readonly BigInteger Major;
539+
public readonly BigInteger Minor;
540+
public readonly BigInteger Patch;
541+
public readonly BigInteger Remainder;
541542

542543
/// <summary>
543544
/// Segments past the fourth, with trailing zeroes trimmed so that equal versions
@@ -546,12 +547,23 @@ public struct Version : IComparable, IComparable<Version>, IEquatable<Version>
546547
/// (e.g. a build revision on a four-part version: 1.2.3.4_1 vs 1.2.3.4_2) order
547548
/// correctly instead of collapsing into Remainder.
548549
/// </summary>
549-
private readonly int[]? _extraSegments;
550+
private readonly BigInteger[]? _extraSegments;
550551

551-
public Version(int major, int minor = 0, int patch = 0, int remainder = 0)
552+
public Version(
553+
BigInteger major,
554+
BigInteger minor = default,
555+
BigInteger patch = default,
556+
BigInteger remainder = default
557+
)
552558
: this(major, minor, patch, remainder, null) { }
553559

554-
private Version(int major, int minor, int patch, int remainder, int[]? extraSegments)
560+
private Version(
561+
BigInteger major,
562+
BigInteger minor,
563+
BigInteger patch,
564+
BigInteger remainder,
565+
BigInteger[]? extraSegments
566+
)
555567
{
556568
Major = major;
557569
Minor = minor;
@@ -564,16 +576,16 @@ private Version(int major, int minor, int patch, int remainder, int[]? extraSegm
564576
/// Builds a Version from an arbitrary number of segments. Segments past the fourth are
565577
/// retained for comparison rather than being folded into Remainder.
566578
/// </summary>
567-
internal static Version FromSegments(IReadOnlyList<int> segments)
579+
internal static Version FromSegments(IReadOnlyList<BigInteger> segments)
568580
{
569581
int count = segments.Count;
570582
while (count > 4 && segments[count - 1] == 0)
571583
count--;
572584

573-
int[]? extra = null;
585+
BigInteger[]? extra = null;
574586
if (count > 4)
575587
{
576-
extra = new int[count - 4];
588+
extra = new BigInteger[count - 4];
577589
for (int i = 4; i < count; i++)
578590
extra[i - 4] = segments[i];
579591
}
@@ -586,12 +598,14 @@ internal static Version FromSegments(IReadOnlyList<int> segments)
586598
extra
587599
);
588600

589-
static int Segment(IReadOnlyList<int> values, int index) =>
590-
index < values.Count ? values[index] : 0;
601+
static BigInteger Segment(IReadOnlyList<BigInteger> values, int index) =>
602+
index < values.Count ? values[index] : BigInteger.Zero;
591603
}
592604

593-
private int ExtraSegment(int index) =>
594-
_extraSegments is not null && index < _extraSegments.Length ? _extraSegments[index] : 0;
605+
private BigInteger ExtraSegment(int index) =>
606+
_extraSegments is not null && index < _extraSegments.Length
607+
? _extraSegments[index]
608+
: BigInteger.Zero;
595609

596610
public int CompareTo(object? other_) => other_ is Version other ? CompareTo(other) : 0;
597611

@@ -669,7 +683,7 @@ public override int GetHashCode()
669683
hash.Add(Minor);
670684
hash.Add(Patch);
671685
hash.Add(Remainder);
672-
foreach (int segment in _extraSegments ?? [])
686+
foreach (BigInteger segment in _extraSegments ?? [])
673687
hash.Add(segment);
674688
return hash.ToHashCode();
675689
}
@@ -732,25 +746,34 @@ public static Version VersionStringToStruct(string version)
732746
// Collect every segment rather than capping at four. Capping used to append the
733747
// surplus digits to the fourth segment, which inflated it: 1.2.3.4_1 became
734748
// 1.2.3.41 and so compared as greater than the newer 1.2.3.5, hiding the update.
735-
List<string> segmentDigits = [""];
749+
List<BigInteger> numbers = [];
750+
StringBuilder digits = new();
751+
bool gapInSegment = false;
752+
bool spliced = false;
736753
bool first = true;
737754

738755
foreach (char c in version)
739756
{
740757
if (char.IsDigit(c))
741-
segmentDigits[^1] += c;
758+
{
759+
spliced |= gapInSegment;
760+
digits.Append(c);
761+
}
742762
else if (!first && separators.Contains(c))
743-
segmentDigits.Add("");
763+
{
764+
numbers.Add(ParseVersionSegment(digits.ToString(), spliced));
765+
digits.Clear();
766+
gapInSegment = false;
767+
spliced = false;
768+
}
769+
else if (digits.Length > 0)
770+
{
771+
gapInSegment = true;
772+
}
744773
first = false;
745774
}
746775

747-
int[] numbers = new int[segmentDigits.Count];
748-
for (int i = 0; i < numbers.Length; i++)
749-
{
750-
if (int.TryParse(segmentDigits[i], out int val))
751-
numbers[i] = val;
752-
}
753-
776+
numbers.Add(ParseVersionSegment(digits.ToString(), spliced));
754777
return Version.FromSegments(numbers);
755778
}
756779
catch
@@ -760,6 +783,24 @@ public static Version VersionStringToStruct(string version)
760783
}
761784
}
762785

786+
private static BigInteger ParseVersionSegment(string digits, bool spliced)
787+
{
788+
if (spliced)
789+
return int.TryParse(digits, out int small) ? small : BigInteger.Zero;
790+
791+
if (long.TryParse(digits, out long value))
792+
return value;
793+
794+
return BigInteger.TryParse(
795+
digits,
796+
NumberStyles.None,
797+
CultureInfo.InvariantCulture,
798+
out BigInteger large
799+
)
800+
? large
801+
: BigInteger.Zero;
802+
}
803+
763804
public static System.Version NormalizeVersionForComparison(System.Version version)
764805
{
765806
return version.Revision >= 0

‎src/UniGetUI.PackageEngine.Tests/PackageTests.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public void Dispose()
6464
// onto the final release must remain visible rather than being read as 2.0.0.1 >= 2.0.0.
6565
[InlineData("2.0.0_rc1", "2.0.0", false)]
6666
[InlineData("2.0.0_beta2", "2.0.0", false)]
67+
[InlineData("37149234619", "38756550835", false)]
68+
[InlineData("38756550835", "37149234619", true)]
69+
[InlineData("99999999999999999999", "100000000000000000000", false)]
70+
[InlineData("1.0.0+20260101123456", "1.0.1", false)]
6771
public async Task NewerVersionIsInstalled_ReturnsExpectedResult(string installedVersions, string newVersion, bool expected)
6872
{
6973
var manager = new PackageManagerBuilder().Build();

0 commit comments

Comments
 (0)