Skip to content

Commit 6e4d1b6

Browse files
committed
use TryParse
1 parent e8c0bf8 commit 6e4d1b6

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

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

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

151151
if (!match.Success)
152152
{
153-
semanticVersion = null;
154153
return false;
155154
}
156155

@@ -159,45 +158,47 @@ public static bool TryParse(string version, string tagPrefixRegex, out SemanticV
159158

160159
if (!parsed.Success)
161160
{
162-
semanticVersion = null;
163161
return false;
164162
}
165163

166164
var semanticVersionBuildMetaData = SemanticVersionBuildMetaData.Parse(parsed.Groups["BuildMetaData"].Value);
167165
var fourthPart = parsed.Groups["FourthPart"];
168166
if (fourthPart.Success && semanticVersionBuildMetaData.CommitsSinceTag == null)
169167
{
170-
semanticVersionBuildMetaData.CommitsSinceTag = int.Parse(fourthPart.Value);
168+
semanticVersionBuildMetaData.CommitsSinceTag = Int32.Parse(fourthPart.Value);
171169
}
172170

173-
try
171+
int major = 0, minor = 0, patch = 0;
172+
173+
if (!parsed.Groups["Major"].Success || !Int32.TryParse(parsed.Groups["Major"].Value, out major))
174174
{
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-
};
175+
return false;
183176
}
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);
190-
191-
semanticVersion = null;
192-
return false;
193-
}
194177

195-
throw exception;
178+
if (parsed.Groups["Minor"].Success && !Int32.TryParse(parsed.Groups["Minor"].Value, out minor))
179+
{
180+
return false;
181+
}
182+
183+
if (parsed.Groups["Patch"].Success && !Int32.TryParse(parsed.Groups["Patch"].Value, out patch))
184+
{
185+
return false;
196186
}
197187

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

200+
201+
201202
public int CompareTo(SemanticVersion value)
202203
{
203204
return CompareTo(value, true);

0 commit comments

Comments
 (0)