Skip to content

Commit db41c7e

Browse files
committed
Change Int32 to Int64
Change the following properties from `Int32` to to `Int64` to avoid `System.OverflowException` on `int.Parse()` of very large numbers: - `SemanticVersion.Major` - `SemanticVersion.Minor` - `SemanticVersion.Patch` - `SemanticVersionBuildMetaData.CommitsSinceTag` - `SemanticVersionBuildMetaData.CommitsSinceVersionSource` - `SemanticVersionBuildMetaData.UncommittedChanges` - `SemanticVersionPreReleaseTag.Number`
1 parent 3297736 commit db41c7e

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

src/GitVersion.Core.Tests/VersionCalculation/SemanticVersionTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public class SemanticVersionTests : TestBase
3535
[TestCase("version-1.2.3", 1, 2, 3, null, null, null, null, null, null, "1.2.3", "version-")]
3636
[TestCase("1", 1, 0, 0, null, null, null, null, null, null, "1.0.0", null)]
3737
[TestCase("1.1", 1, 1, 0, null, null, null, null, null, null, "1.1.0", null)]
38+
[TestCase("1.0.0-develop-20201007113711", 1, 0, 0, "develop-20201007113711", null, null, null, null, null, "1.0.0-develop-20201007113711", null)]
39+
[TestCase("20201007113711.658165168461351.64136516984163213-develop-20201007113711.98848747823+65416321321", 20201007113711, 658165168461351, 64136516984163213, "develop-20201007113711", 98848747823, 65416321321, null, null, null, "20201007113711.658165168461351.64136516984163213-develop-20201007113711.98848747823+65416321321", null)]
3840
public void ValidateVersionParsing(
39-
string versionString, int major, int minor, int patch, string tag, int? tagNumber, int? numberOfBuilds,
41+
string versionString, long major, long minor, long patch, string tag, long? tagNumber, long? numberOfBuilds,
4042
string branchName, string sha, string otherMetaData, string fullFormattedVersionString, string tagPrefixRegex)
4143
{
4244
fullFormattedVersionString ??= versionString;

src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private void UpdatePreReleaseTag(SemanticVersion semanticVersion, string? branch
117117
{
118118
var tagToUse = context.Configuration?.GetBranchSpecificTag(this.log, context.CurrentBranch?.Name.Friendly, branchNameOverride);
119119

120-
int? number = null;
120+
long? number = null;
121121

122122
var lastTag = this.repositoryStore
123123
.GetVersionTagsOnBranch(context.CurrentBranch!, context.Configuration?.GitTagPrefix)

src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersion.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ public class SemanticVersion : IFormattable, IComparable<SemanticVersion>, IEqua
1313
@"^(?<SemVer>(?<Major>\d+)(\.(?<Minor>\d+))?(\.(?<Patch>\d+))?)(\.(?<FourthPart>\d+))?(-(?<Tag>[^\+]*))?(\+(?<BuildMetaData>.*))?$",
1414
RegexOptions.Compiled);
1515

16-
public int Major;
17-
public int Minor;
18-
public int Patch;
16+
public long Major;
17+
public long Minor;
18+
public long Patch;
1919
public SemanticVersionPreReleaseTag? PreReleaseTag;
2020
public SemanticVersionBuildMetaData? BuildMetaData;
2121

22-
public SemanticVersion(int major = 0, int minor = 0, int patch = 0)
22+
public SemanticVersion(long major = 0, long minor = 0, long patch = 0)
2323
{
2424
this.Major = major;
2525
this.Minor = minor;
@@ -74,9 +74,9 @@ public override int GetHashCode()
7474
{
7575
unchecked
7676
{
77-
var hashCode = this.Major;
78-
hashCode = (hashCode * 397) ^ this.Minor;
79-
hashCode = (hashCode * 397) ^ this.Patch;
77+
var hashCode = this.Major.GetHashCode();
78+
hashCode = (hashCode * 397) ^ this.Minor.GetHashCode();
79+
hashCode = (hashCode * 397) ^ this.Patch.GetHashCode();
8080
hashCode = (hashCode * 397) ^ (this.PreReleaseTag != null ? this.PreReleaseTag.GetHashCode() : 0);
8181
hashCode = (hashCode * 397) ^ (this.BuildMetaData != null ? this.BuildMetaData.GetHashCode() : 0);
8282
return hashCode;
@@ -168,9 +168,9 @@ public static bool TryParse(string version, string? tagPrefixRegex, [NotNullWhen
168168

169169
semanticVersion = new SemanticVersion
170170
{
171-
Major = int.Parse(parsed.Groups["Major"].Value),
172-
Minor = parsed.Groups["Minor"].Success ? int.Parse(parsed.Groups["Minor"].Value) : 0,
173-
Patch = parsed.Groups["Patch"].Success ? int.Parse(parsed.Groups["Patch"].Value) : 0,
171+
Major = long.Parse(parsed.Groups["Major"].Value),
172+
Minor = parsed.Groups["Minor"].Success ? long.Parse(parsed.Groups["Minor"].Value) : 0,
173+
Patch = parsed.Groups["Patch"].Success ? long.Parse(parsed.Groups["Patch"].Value) : 0,
174174
PreReleaseTag = SemanticVersionPreReleaseTag.Parse(parsed.Groups["Tag"].Value),
175175
BuildMetaData = semanticVersionBuildMetaData
176176
};

src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionBuildMetaData.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ public class SemanticVersionBuildMetaData : IFormattable, IEquatable<SemanticVer
1414
private static readonly LambdaEqualityHelper<SemanticVersionBuildMetaData> EqualityHelper =
1515
new(x => x.CommitsSinceTag, x => x.Branch, x => x.Sha);
1616

17-
public int? CommitsSinceTag;
17+
public long? CommitsSinceTag;
1818
public string? Branch;
1919
public string? Sha;
2020
public string? ShortSha;
2121
public string? OtherMetaData;
2222
public DateTimeOffset? CommitDate;
2323
public string? VersionSourceSha;
24-
public int? CommitsSinceVersionSource;
25-
public int UncommittedChanges;
24+
public long? CommitsSinceVersionSource;
25+
public long UncommittedChanges;
2626

2727
public SemanticVersionBuildMetaData()
2828
{
@@ -124,7 +124,7 @@ public static SemanticVersionBuildMetaData Parse(string? buildMetaData)
124124

125125
if (parsed.Groups["BuildNumber"].Success)
126126
{
127-
semanticVersionBuildMetaData.CommitsSinceTag = int.Parse(parsed.Groups["BuildNumber"].Value);
127+
semanticVersionBuildMetaData.CommitsSinceTag = long.Parse(parsed.Groups["BuildNumber"].Value);
128128
semanticVersionBuildMetaData.CommitsSinceVersionSource = semanticVersionBuildMetaData.CommitsSinceTag ?? 0;
129129
}
130130

src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionPreReleaseTag.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public SemanticVersionPreReleaseTag()
1515
{
1616
}
1717

18-
public SemanticVersionPreReleaseTag(string? name, int? number)
18+
public SemanticVersionPreReleaseTag(string? name, long? number)
1919
{
2020
Name = name;
2121
Number = number;
@@ -29,7 +29,7 @@ public SemanticVersionPreReleaseTag(SemanticVersionPreReleaseTag? preReleaseTag)
2929
}
3030

3131
public string? Name { get; set; }
32-
public int? Number { get; set; }
32+
public long? Number { get; set; }
3333
public bool? PromotedFromCommits { get; set; }
3434

3535
public override bool Equals(object? obj) => Equals(obj as SemanticVersionPreReleaseTag);
@@ -76,7 +76,7 @@ public static SemanticVersionPreReleaseTag Parse(string? preReleaseTag)
7676
}
7777

7878
var value = match.Groups["name"].Value;
79-
var number = match.Groups["number"].Success ? int.Parse(match.Groups["number"].Value) : (int?)null;
79+
var number = match.Groups["number"].Success ? long.Parse(match.Groups["number"].Value) : (long?)null;
8080
if (value.EndsWith("-"))
8181
return new SemanticVersionPreReleaseTag(preReleaseTag, null);
8282

0 commit comments

Comments
 (0)