@@ -104,7 +104,9 @@ public IBranch GetTargetBranch(string? targetBranchName)
104
104
return desiredBranch ;
105
105
}
106
106
107
- public IBranch ? FindBranch ( string ? branchName ) => this . repository . Branches . FirstOrDefault ( x => x . Name . EquivalentTo ( branchName ) ) ;
107
+ public IBranch ? FindBranch ( ReferenceName branchName ) => this . repository . Branches . FirstOrDefault ( x => x . Name . Equals ( branchName ) ) ;
108
+
109
+ public IBranch ? FindBranch ( string branchName ) => this . repository . Branches . FirstOrDefault ( x => x . Name . EquivalentTo ( branchName ) ) ;
108
110
109
111
public IBranch ? FindMainBranch ( IGitVersionConfiguration configuration )
110
112
{
@@ -143,7 +145,7 @@ private static bool IsReleaseBranch(INamedReference branch, IEnumerable<KeyValue
143
145
144
146
public IEnumerable < IBranch > ExcludingBranches ( IEnumerable < IBranch > branchesToExclude ) => this . repository . Branches . ExcludeBranches ( branchesToExclude ) ;
145
147
146
- public IEnumerable < IBranch > GetBranchesContainingCommit ( ICommit ? commit , IEnumerable < IBranch > ? branches = null , bool onlyTrackedBranches = false )
148
+ public IEnumerable < IBranch > GetBranchesContainingCommit ( ICommit commit , IEnumerable < IBranch > ? branches = null , bool onlyTrackedBranches = false )
147
149
{
148
150
var branchesContainingCommitFinder = new BranchesContainingCommitFinder ( this . repository , this . log ) ;
149
151
return branchesContainingCommitFinder . GetBranchesContainingCommit ( commit , branches , onlyTrackedBranches ) ;
@@ -268,7 +270,7 @@ public IEnumerable<SemanticVersion> GetVersionTagsOnBranch(IBranch branch, strin
268
270
var semanticVersions = GetTaggedSemanticVersions ( tagPrefix , format ) ;
269
271
var tagsBySha = semanticVersions . Where ( t => t . Tag . TargetSha != null ) . ToLookup ( t => t . Tag . TargetSha , t => t ) ;
270
272
271
- var versionTags = ( branch . Commits ? . SelectMany ( c => tagsBySha [ c . Sha ] . Select ( t => t ) )
273
+ var versionTags = ( branch . Commits . SelectMany ( c => tagsBySha [ c . Sha ] . Select ( t => t ) )
272
274
?? Enumerable . Empty < SemanticVersionWithTag > ( ) ) . ToList ( ) ;
273
275
274
276
this . taggedSemanticVersionsOnBranchCache . Add ( branch , versionTags ) ;
@@ -316,7 +318,7 @@ public IReadOnlyList<SemanticVersionWithTag> GetTaggedSemanticVersionsOnBranch(
316
318
var semanticVersions = GetTaggedSemanticVersions ( tagPrefix , format ) ;
317
319
var tagsBySha = semanticVersions . Where ( t => t . Tag . TargetSha != null ) . ToLookup ( t => t . Tag . TargetSha , t => t ) ;
318
320
319
- var versionTags = ( branch . Commits ? . SelectMany ( c => tagsBySha [ c . Sha ] . Select ( t => t ) )
321
+ var versionTags = ( branch . Commits . SelectMany ( c => tagsBySha [ c . Sha ] . Select ( t => t ) )
320
322
?? Enumerable . Empty < SemanticVersionWithTag > ( ) ) . ToList ( ) ;
321
323
322
324
this . taggedSemanticVersionsOnBranchCache . Add ( branch , versionTags ) ;
@@ -355,4 +357,93 @@ private IEnumerable<SemanticVersion> GetCurrentCommitSemanticVersions(ICommit? c
355
357
? new [ ] { version }
356
358
: Array . Empty < SemanticVersion > ( ) ;
357
359
}
360
+
361
+ public IEnumerable < SemanticVersionWithTag > GetSemanticVersions ( IGitVersionConfiguration configuration ,
362
+ IBranch currentBranch , ICommit currentCommit , bool trackMergeTarget , bool tracksReleaseBranches )
363
+ {
364
+ var olderThan = currentCommit ? . When ;
365
+
366
+ IEnumerable < SemanticVersionWithTag > GetSemanticVersions ( )
367
+ {
368
+ var semanticVersions = GetTaggedSemanticVersions (
369
+ configuration . TagPrefix , configuration . SemanticVersionFormat
370
+ ) . ToList ( ) ;
371
+ var semanticVersionsByCommitLazy = new Lazy < ILookup < string , SemanticVersionWithTag > > (
372
+ ( ) => semanticVersions . ToLookup ( element => element . Tag . Commit . Id . Sha )
373
+ ) ;
374
+
375
+ foreach ( var semanticVersion in GetSemanticVersionsCommon ( currentBranch , semanticVersionsByCommitLazy ) )
376
+ {
377
+ if ( semanticVersion . Tag . Commit . When <= olderThan ) yield return semanticVersion ;
378
+ }
379
+
380
+ if ( trackMergeTarget )
381
+ {
382
+ foreach ( var semanticVersion in GetSemanticVersionsTrackMergeTarget ( currentBranch , semanticVersionsByCommitLazy ) )
383
+ {
384
+ if ( semanticVersion . Tag . Commit . When <= olderThan ) yield return semanticVersion ;
385
+ }
386
+ }
387
+
388
+ if ( tracksReleaseBranches )
389
+ {
390
+ foreach ( var semanticVersion in GetSemanticVersionsTracksReleaseBranches ( configuration , semanticVersionsByCommitLazy ) )
391
+ {
392
+ yield return semanticVersion ;
393
+ }
394
+ }
395
+ }
396
+
397
+ var alreadyReturnedValues = new HashSet < SemanticVersionWithTag > ( ) ;
398
+
399
+ foreach ( var semanticVersion in GetSemanticVersions ( ) )
400
+ {
401
+ if ( alreadyReturnedValues . Add ( semanticVersion ) )
402
+ yield return semanticVersion ;
403
+ }
404
+ }
405
+
406
+ private static IEnumerable < SemanticVersionWithTag > GetSemanticVersionsCommon (
407
+ IBranch currentBranch , Lazy < ILookup < string , SemanticVersionWithTag > > semanticVersionsByCommitLazy )
408
+ {
409
+ foreach ( var commit in currentBranch . Commits )
410
+ {
411
+ foreach ( var semanticVersion in semanticVersionsByCommitLazy . Value [ commit . Id . Sha ] )
412
+ {
413
+ yield return semanticVersion ;
414
+ }
415
+ }
416
+ }
417
+
418
+ private static IEnumerable < SemanticVersionWithTag > GetSemanticVersionsTrackMergeTarget (
419
+ IBranch currentBranch , Lazy < ILookup < string , SemanticVersionWithTag > > semanticVersionsByCommitLazy )
420
+ {
421
+ var commitsOnCurrentBranch = currentBranch . Commits . ToArray ( ) ;
422
+ if ( ! commitsOnCurrentBranch . Any ( ) ) yield break ;
423
+
424
+ var shaHashSet = new HashSet < string > ( commitsOnCurrentBranch . Select ( element => element . Id . Sha ) ) ;
425
+ foreach ( var semanticVersion in semanticVersionsByCommitLazy . Value . SelectMany ( element => element ) )
426
+ {
427
+ var parentCommits = semanticVersion . Tag . Commit . Parents ?? Array . Empty < ICommit > ( ) ;
428
+ if ( parentCommits . Any ( element => shaHashSet . Contains ( element . Id . Sha ) ) )
429
+ {
430
+ yield return semanticVersion ;
431
+ }
432
+ }
433
+ }
434
+
435
+ private IEnumerable < SemanticVersionWithTag > GetSemanticVersionsTracksReleaseBranches (
436
+ IGitVersionConfiguration configuration , Lazy < ILookup < string , SemanticVersionWithTag > > semanticVersionsByCommitLazy )
437
+ {
438
+ foreach ( var mainBranch in FindMainlineBranches ( configuration ) )
439
+ {
440
+ foreach ( var commit in mainBranch . Commits ? . ToArray ( ) ?? Array . Empty < ICommit > ( ) )
441
+ {
442
+ foreach ( var semanticVersion in semanticVersionsByCommitLazy . Value [ commit . Id . Sha ] )
443
+ {
444
+ yield return semanticVersion ;
445
+ }
446
+ }
447
+ }
448
+ }
358
449
}
0 commit comments