@@ -6,7 +6,7 @@ namespace GitVersion
6
6
public class SemanticVersion : IFormattable , IComparable < SemanticVersion >
7
7
{
8
8
private static SemanticVersion Empty = new SemanticVersion ( ) ;
9
-
9
+
10
10
private static readonly Regex ParseSemVer = new Regex (
11
11
@"^(?<SemVer>(?<Major>\d+)?(\.(?<Minor>\d+))?(\.(?<Patch>\d+))?)(\.(?<FourthPart>\d+))?(-(?<Tag>[^\+]*))?(\+(?<BuildMetaData>.*))?$" ,
12
12
RegexOptions . Compiled ) ;
@@ -147,10 +147,10 @@ public static SemanticVersion Parse(string version, string tagPrefixRegex)
147
147
public static bool TryParse ( string version , string tagPrefixRegex , out SemanticVersion semanticVersion )
148
148
{
149
149
var match = Regex . Match ( version , $ "^({ tagPrefixRegex } )?(?<version>.*)$") ;
150
+ semanticVersion = null ;
150
151
151
152
if ( ! match . Success )
152
153
{
153
- semanticVersion = null ;
154
154
return false ;
155
155
}
156
156
@@ -159,7 +159,6 @@ public static bool TryParse(string version, string tagPrefixRegex, out SemanticV
159
159
160
160
if ( ! parsed . Success )
161
161
{
162
- semanticVersion = null ;
163
162
return false ;
164
163
}
165
164
@@ -170,34 +169,37 @@ public static bool TryParse(string version, string tagPrefixRegex, out SemanticV
170
169
semanticVersionBuildMetaData . CommitsSinceTag = int . Parse ( fourthPart . Value ) ;
171
170
}
172
171
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 ) )
174
175
{
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 ;
183
177
}
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
178
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
+ }
194
183
195
- throw exception ;
184
+ if ( parsed . Groups [ "Patch" ] . Success && ! int . TryParse ( parsed . Groups [ "Minor" ] . Value , out patch ) )
185
+ {
186
+ return false ;
196
187
}
197
188
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
+
198
198
return true ;
199
199
}
200
200
201
+
202
+
201
203
public int CompareTo ( SemanticVersion value )
202
204
{
203
205
return CompareTo ( value , true ) ;
0 commit comments