@@ -541,6 +541,79 @@ public void CalculateVersionFromWorktreeHead()
541541 version . Sha . ShouldBe ( commits . First ( ) . Sha ) ;
542542 }
543543
544+ [ Test ]
545+ [ Category ( NoMono ) ]
546+ [ Description ( NoMonoDescription ) ]
547+ public void CalculateVersionVariables_TwoBranchHasSameCommitHeadDetachedAndNotTagged_ThrowException ( )
548+ {
549+ // Setup
550+ using var fixture = new RemoteRepositoryFixture ( ) ;
551+ var repoDir = new DirectoryInfo ( fixture . RepositoryPath ) ;
552+ var Init = fixture . LocalRepositoryFixture . Repository . MakeACommit ( "Init commit" ) ;
553+ var branchV1 = fixture . LocalRepositoryFixture . Repository . CreateBranch ( "feature/1.0" ) ;
554+ fixture . LocalRepositoryFixture . Checkout ( "feature/1.0" ) ;
555+ var commit = fixture . LocalRepositoryFixture . Repository . MakeACommit ( "feat: a new commit" ) ;
556+ var branchV2 = fixture . LocalRepositoryFixture . Repository . CreateBranch ( "support/1.0" ) ;
557+ fixture . LocalRepositoryFixture . Checkout ( commit . Sha ) ;
558+
559+ using var worktreeFixture = new LocalRepositoryFixture ( new Repository ( fixture . LocalRepositoryFixture . RepositoryPath ) ) ;
560+ var gitVersionOptions = new GitVersionOptions { WorkingDirectory = worktreeFixture . RepositoryPath } ;
561+
562+ var environment = new TestEnvironment ( ) ;
563+ environment . SetEnvironmentVariable ( AzurePipelines . EnvironmentVariableName , "true" ) ;
564+
565+ this . sp = GetServiceProvider ( gitVersionOptions , environment : environment ) ;
566+
567+ var lazyContext = this . sp . GetService < Lazy < GitVersionContext > > ( ) ;
568+ var context = lazyContext . Value ;
569+
570+ var preparer = this . sp . GetService < IGitPreparer > ( ) ;
571+ var sut = sp . GetService < IGitVersionCalculateTool > ( ) ;
572+
573+ // Execute & Verify
574+ var exception = Assert . Throws < WarningException > ( ( ) => sut . CalculateVersionVariables ( ) ) ;
575+ exception . Message . ShouldBe ( "Failed to try and guess branch to use. Move one of the branches along a commit to remove warning" ) ;
576+ }
577+
578+ [ Test ]
579+ [ Category ( NoMono ) ]
580+ [ Description ( NoMonoDescription ) ]
581+ public void CalculateVersionVariables_TwoBranchHasSameCommitHeadDetachedAndTagged_ReturnSemver ( )
582+ {
583+ // Setup
584+ using var fixture = new RemoteRepositoryFixture ( ) ;
585+ var repoDir = new DirectoryInfo ( fixture . RepositoryPath ) ;
586+ var Init = fixture . LocalRepositoryFixture . Repository . MakeACommit ( "Init commit" ) ;
587+ var branchV1 = fixture . LocalRepositoryFixture . Repository . CreateBranch ( "feature/1.0" ) ;
588+ fixture . LocalRepositoryFixture . Checkout ( "feature/1.0" ) ;
589+ var commit = fixture . LocalRepositoryFixture . Repository . MakeACommit ( "feat: a new commit" ) ;
590+ var branchV2 = fixture . LocalRepositoryFixture . Repository . CreateBranch ( "support/1.0" ) ;
591+ fixture . LocalRepositoryFixture . ApplyTag ( "1.0.1" ) ;
592+ fixture . LocalRepositoryFixture . Checkout ( commit . Sha ) ;
593+
594+ using var worktreeFixture = new LocalRepositoryFixture ( new Repository ( fixture . LocalRepositoryFixture . RepositoryPath ) ) ;
595+ var gitVersionOptions = new GitVersionOptions { WorkingDirectory = worktreeFixture . RepositoryPath } ;
596+
597+ var environment = new TestEnvironment ( ) ;
598+ environment . SetEnvironmentVariable ( AzurePipelines . EnvironmentVariableName , "true" ) ;
599+
600+ this . sp = GetServiceProvider ( gitVersionOptions , environment : environment ) ;
601+
602+ var lazyContext = this . sp . GetService < Lazy < GitVersionContext > > ( ) ;
603+ var context = lazyContext . Value ;
604+
605+ var preparer = this . sp . GetService < IGitPreparer > ( ) ;
606+ var sut = sp . GetService < IGitVersionCalculateTool > ( ) ;
607+
608+ // Execute
609+ var version = sut . CalculateVersionVariables ( ) ;
610+
611+ // Verify
612+ version . SemVer . ShouldBe ( "1.0.1" ) ;
613+ var commits = worktreeFixture . Repository . Head . Commits ;
614+ version . Sha . ShouldBe ( commits . First ( ) . Sha ) ;
615+ }
616+
544617 private IGitVersionCalculateTool GetGitVersionCalculator ( GitVersionOptions gitVersionOptions , ILog logger = null , IGitRepository repository = null , IFileSystem fs = null )
545618 {
546619 this . sp = GetServiceProvider ( gitVersionOptions , logger , repository , fs ) ;
@@ -555,6 +628,13 @@ private IGitVersionCalculateTool GetGitVersionCalculator(GitVersionOptions gitVe
555628 private static IServiceProvider GetServiceProvider ( GitVersionOptions gitVersionOptions , ILog log = null , IGitRepository repository = null , IFileSystem fileSystem = null , IEnvironment environment = null ) =>
556629 ConfigureServices ( services =>
557630 {
631+ services . AddSingleton < IGitVersionContextFactory , GitVersionContextFactory > ( ) ;
632+ services . AddSingleton ( sp =>
633+ {
634+ var options = sp . GetService < IOptions < GitVersionOptions > > ( ) ;
635+ var contextFactory = sp . GetService < IGitVersionContextFactory > ( ) ;
636+ return new Lazy < GitVersionContext ? > ( ( ) => contextFactory ? . Create ( options ? . Value ) ) ;
637+ } ) ;
558638 if ( log != null ) services . AddSingleton ( log ) ;
559639 if ( fileSystem != null ) services . AddSingleton ( fileSystem ) ;
560640 if ( repository != null ) services . AddSingleton ( repository ) ;
0 commit comments