@@ -152,6 +152,28 @@ internal SemanticVersion.Position? VersionHeightPosition
152152 }
153153 }
154154
155+ /// <summary>
156+ /// Gets the position in a computed version that the first 16 bits of a git commit ID should appear, if any.
157+ /// </summary>
158+ internal SemanticVersion . Position ? GitCommitIdPosition
159+ {
160+ get
161+ {
162+ // We can only store the git commit ID info after there was a place to put the version height.
163+ // We don't want to store the commit ID (which is effectively a random integer) in the revision slot
164+ // if the version height does not appear, or only appears later (in the -prerelease tag) since that
165+ // would mess up version ordering.
166+ if ( this . VersionHeightPosition == SemanticVersion . Position . Build )
167+ {
168+ return SemanticVersion . Position . Revision ;
169+ }
170+ else
171+ {
172+ return null ;
173+ }
174+ }
175+ }
176+
155177 /// <summary>
156178 /// Gets a value indicating whether this instance is the default "0.0" instance.
157179 /// </summary>
@@ -286,40 +308,44 @@ internal static bool WillVersionChangeResetVersionHeight(SemanticVersion first,
286308 return false ;
287309 }
288310
289- internal static int ReadVersionPosition ( Version version , SemanticVersion . Position position )
311+ internal static int ReadVersionPosition ( Version version , Position position )
290312 {
291313 Requires . NotNull ( version , nameof ( version ) ) ;
292314
293- switch ( position )
315+ return position switch
294316 {
295- case SemanticVersion . Position . Major :
296- return version . Major ;
297- case SemanticVersion . Position . Minor :
298- return version . Minor ;
299- case SemanticVersion . Position . Build :
300- return version . Build ;
301- case SemanticVersion . Position . Revision :
302- return version . Revision ;
303- default :
304- throw new ArgumentOutOfRangeException ( nameof ( position ) , position , "Must be one of the 4 integer parts." ) ;
305- }
317+ Position . Major => version . Major ,
318+ Position . Minor => version . Minor ,
319+ Position . Build => version . Build ,
320+ Position . Revision => version . Revision ,
321+ _ => throw new ArgumentOutOfRangeException ( nameof ( position ) , position , "Must be one of the 4 integer parts." ) ,
322+ } ;
306323 }
307324
308- internal int ReadVersionPosition ( SemanticVersion . Position position )
325+ internal int ReadVersionPosition ( Position position ) => ReadVersionPosition ( this . Version , position ) ;
326+
327+ /// <summary>
328+ /// Checks whether a given version may have been produced by this semantic version.
329+ /// </summary>
330+ /// <param name="version">The version to test.</param>
331+ /// <returns><see langword="true"/> if the <paramref name="version"/> is a match; <see langword="false"/> otherwise.</returns>
332+ internal bool IsMatchingVersion ( Version version )
309333 {
310- switch ( position )
334+ Position lastPositionToConsider = Position . Revision ;
335+ if ( this . VersionHeightPosition <= lastPositionToConsider )
311336 {
312- case SemanticVersion . Position . Major :
313- return this . Version . Major ;
314- case SemanticVersion . Position . Minor :
315- return this . Version . Minor ;
316- case SemanticVersion . Position . Build :
317- return this . Version . Build ;
318- case SemanticVersion . Position . Revision :
319- return this . Version . Revision ;
320- default :
321- throw new ArgumentOutOfRangeException ( nameof ( position ) , position , "Must be one of the 4 integer parts." ) ;
337+ lastPositionToConsider = this . VersionHeightPosition . Value - 1 ;
322338 }
339+
340+ for ( Position i = Position . Major ; i <= lastPositionToConsider ; i ++ )
341+ {
342+ if ( this . ReadVersionPosition ( i ) != ReadVersionPosition ( version , i ) )
343+ {
344+ return false ;
345+ }
346+ }
347+
348+ return true ;
323349 }
324350
325351 /// <summary>
0 commit comments