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