Skip to content

Commit 65b7b68

Browse files
committed
use TryParse
1 parent e8c0bf8 commit 65b7b68

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace GitVersion
66
public class SemanticVersion : IFormattable, IComparable<SemanticVersion>
77
{
88
private static SemanticVersion Empty = new SemanticVersion();
9-
9+
1010
private static readonly Regex ParseSemVer = new Regex(
1111
@"^(?<SemVer>(?<Major>\d+)?(\.(?<Minor>\d+))?(\.(?<Patch>\d+))?)(\.(?<FourthPart>\d+))?(-(?<Tag>[^\+]*))?(\+(?<BuildMetaData>.*))?$",
1212
RegexOptions.Compiled);
@@ -147,10 +147,10 @@ public static SemanticVersion Parse(string version, string tagPrefixRegex)
147147
public static bool TryParse(string version, string tagPrefixRegex, out SemanticVersion semanticVersion)
148148
{
149149
var match = Regex.Match(version, $"^({tagPrefixRegex})?(?<version>.*)$");
150+
semanticVersion = null;
150151

151152
if (!match.Success)
152153
{
153-
semanticVersion = null;
154154
return false;
155155
}
156156

@@ -159,7 +159,6 @@ public static bool TryParse(string version, string tagPrefixRegex, out SemanticV
159159

160160
if (!parsed.Success)
161161
{
162-
semanticVersion = null;
163162
return false;
164163
}
165164

@@ -170,34 +169,37 @@ public static bool TryParse(string version, string tagPrefixRegex, out SemanticV
170169
semanticVersionBuildMetaData.CommitsSinceTag = int.Parse(fourthPart.Value);
171170
}
172171

173-
try
172+
int major = 0, minor = 0, patch = 0;
173+
174+
if (!parsed.Groups["Major"].Success || !int.TryParse(parsed.Groups["Minor"].Value, out major))
174175
{
175-
semanticVersion = new SemanticVersion
176-
{
177-
Major = int.Parse(parsed.Groups["Major"].Value),
178-
Minor = parsed.Groups["Minor"].Success ? int.Parse(parsed.Groups["Minor"].Value) : 0,
179-
Patch = parsed.Groups["Patch"].Success ? int.Parse(parsed.Groups["Patch"].Value) : 0,
180-
PreReleaseTag = SemanticVersionPreReleaseTag.Parse(parsed.Groups["Tag"].Value),
181-
BuildMetaData = semanticVersionBuildMetaData
182-
};
176+
return false;
183177
}
184-
catch (Exception exception)
185-
{
186-
if (exception is FormatException || exception is OverflowException)
187-
{
188-
Console.Error.WriteLine("Failed to Parse Tag:");
189-
Console.Error.WriteLine(exception.Message);
190178

191-
semanticVersion = null;
192-
return false;
193-
}
179+
if (parsed.Groups["Minor"].Success && !int.TryParse(parsed.Groups["Minor"].Value, out minor))
180+
{
181+
return false;
182+
}
194183

195-
throw exception;
184+
if (parsed.Groups["Patch"].Success && !int.TryParse(parsed.Groups["Minor"].Value, out patch))
185+
{
186+
return false;
196187
}
197188

189+
semanticVersion = new SemanticVersion
190+
{
191+
Major = major,
192+
Minor = minor,
193+
Patch = patch,
194+
PreReleaseTag = SemanticVersionPreReleaseTag.Parse(parsed.Groups["Tag"].Value),
195+
BuildMetaData = semanticVersionBuildMetaData
196+
};
197+
198198
return true;
199199
}
200200

201+
202+
201203
public int CompareTo(SemanticVersion value)
202204
{
203205
return CompareTo(value, true);

0 commit comments

Comments
 (0)