Skip to content

Commit 2dcb928

Browse files
committed
injected GitVersionContext
1 parent 57ecfd1 commit 2dcb928

24 files changed

+152
-144
lines changed

src/GitVersionCore.Tests/GitToolsTestingExtensions.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Co
2323
configuration.Reset();
2424
}
2525

26-
var options = Options.Create(new Arguments { OverrideConfig = configuration });
26+
repository ??= fixture.Repository;
27+
28+
var options = Options.Create(new Arguments { OverrideConfig = configuration, TargetPath = repository.GetRepositoryDirectory() });
2729

2830
var sp = ConfigureServices(services =>
2931
{
@@ -32,13 +34,9 @@ public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Co
3234

3335
var variableProvider = sp.GetService<IVariableProvider>();
3436
var nextVersionCalculator = sp.GetService<INextVersionCalculator>();
35-
var gitVersionContextFactory = sp.GetService<IGitVersionContextFactory>();
36-
37-
repository ??= fixture.Repository;
38-
var targetBranch = repository.GetTargetBranch(branch);
37+
var contextOptions = sp.GetService<IOptions<GitVersionContext>>();
3938

40-
gitVersionContextFactory.Init(repository, targetBranch, commitId, onlyTrackedBranches);
41-
var context = gitVersionContextFactory.Context;
39+
var context = contextOptions.Value;
4240

4341
try
4442
{

src/GitVersionCore.Tests/GitVersionContextBuilder.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public GitVersionContextBuilder WithConfig(Config config)
3030
return this;
3131
}
3232

33-
public GitVersionContextBuilder OverrideServices(Action<IServiceCollection> overrideServices = null)
33+
public GitVersionContextBuilder OverrideServices(Action<IServiceCollection> overrides = null)
3434
{
35-
this.overrideServices = overrideServices;
35+
this.overrideServices = overrides;
3636
return this;
3737
}
3838

@@ -79,18 +79,15 @@ public GitVersionContext Build()
7979

8080
config.Reset();
8181

82-
var options = Options.Create(new Arguments { OverrideConfig = config });
82+
var options = Options.Create(new Arguments { OverrideConfig = config, TargetPath = repo.GetRepositoryDirectory() });
8383

8484
ServicesProvider = ConfigureServices(services =>
8585
{
8686
services.AddSingleton(options);
8787
overrideServices?.Invoke(services);
8888
});
89-
90-
var gitVersionContextFactory = ServicesProvider.GetService<IGitVersionContextFactory>();
91-
92-
gitVersionContextFactory.Init(repo, repo.Head);
93-
return gitVersionContextFactory.Context;
89+
var context = ServicesProvider.GetService<IOptions<GitVersionContext>>().Value;
90+
return context;
9491
}
9592

9693
private static IRepository CreateRepository()

src/GitVersionCore.Tests/GitVersionContextTests.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public void CanInheritVersioningMode(VersioningMode mode)
3636
};
3737

3838
var gitVersionContextFactory = GetGitVersionContextFactory(config);
39-
gitVersionContextFactory.Init(mockRepository, mockBranch);
40-
var context = gitVersionContextFactory.Context;
39+
var context = gitVersionContextFactory.Init(mockRepository, mockBranch);
4140

4241
context.Configuration.VersioningMode.ShouldBe(mode);
4342
}
@@ -64,8 +63,7 @@ public void CanInheritIncrement(IncrementStrategy increment, IncrementStrategy?
6463
fixture.MakeACommit();
6564

6665
var gitVersionContextFactory = GetGitVersionContextFactory(config);
67-
gitVersionContextFactory.Init(fixture.Repository, fixture.Repository.Branches[dummyBranchName]);
68-
var context = gitVersionContextFactory.Context;
66+
var context = gitVersionContextFactory.Init(fixture.Repository, fixture.Repository.Branches[dummyBranchName]);
6967

7068
context.Configuration.Increment.ShouldBe(alternateExpected ?? increment);
7169
}
@@ -99,8 +97,7 @@ public void UsesBranchSpecificConfigOverTopLevelDefaults()
9997
};
10098

10199
var gitVersionContextFactory = GetGitVersionContextFactory(config);
102-
gitVersionContextFactory.Init(mockRepository, develop);
103-
var context = gitVersionContextFactory.Context;
100+
var context = gitVersionContextFactory.Init(mockRepository, develop);
104101

105102
context.Configuration.Tag.ShouldBe("alpha");
106103
}
@@ -141,13 +138,11 @@ public void UsesFirstBranchConfigWhenMultipleMatch()
141138
};
142139

143140
var gitVersionContextFactory = GetGitVersionContextFactory(config);
144-
gitVersionContextFactory.Init(mockRepository, releaseLatestBranch);
145-
var latestContext = gitVersionContextFactory.Context;
141+
var latestContext = gitVersionContextFactory.Init(mockRepository, releaseLatestBranch);
146142

147143
latestContext.Configuration.Increment.ShouldBe(IncrementStrategy.None);
148144

149-
gitVersionContextFactory.Init(mockRepository, releaseVersionBranch);
150-
var versionContext = gitVersionContextFactory.Context;
145+
var versionContext = gitVersionContextFactory.Init(mockRepository, releaseVersionBranch);
151146
versionContext.Configuration.Increment.ShouldBe(IncrementStrategy.Patch);
152147
}
153148

@@ -172,8 +167,7 @@ public void CanFindParentBranchForInheritingIncrementStrategy()
172167
repo.Repository.MakeACommit();
173168

174169
var gitVersionContextFactory = GetGitVersionContextFactory(config);
175-
gitVersionContextFactory.Init(repo.Repository, repo.Repository.Head);
176-
var context = gitVersionContextFactory.Context;
170+
var context = gitVersionContextFactory.Init(repo.Repository, repo.Repository.Head);
177171

178172
context.Configuration.Increment.ShouldBe(IncrementStrategy.Major);
179173
}

src/GitVersionCore.Tests/VersionCalculation/NextVersionCalculatorTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void ShouldIncrementVersionBasedOnConfig()
2323

2424
var contextBuilder = new GitVersionContextBuilder();
2525

26-
var context = contextBuilder
26+
contextBuilder
2727
.OverrideServices(services =>
2828
{
2929
services.AddSingleton<IBaseVersionCalculator>(new TestBaseVersionCalculator(true, new SemanticVersion(1), new MockCommit()));
@@ -35,7 +35,7 @@ public void ShouldIncrementVersionBasedOnConfig()
3535
var nextVersionCalculator = contextBuilder.ServicesProvider.GetService<INextVersionCalculator>() as NextVersionCalculator;
3636
nextVersionCalculator.ShouldNotBeNull();
3737

38-
var version = nextVersionCalculator.FindVersionInternal(context);
38+
var version = nextVersionCalculator.FindVersionInternal();
3939

4040
version.ToString().ShouldBe("1.0.1");
4141
}
@@ -47,7 +47,7 @@ public void DoesNotIncrementWhenBaseVersionSaysNotTo()
4747

4848
var contextBuilder = new GitVersionContextBuilder();
4949

50-
var context = contextBuilder
50+
contextBuilder
5151
.OverrideServices(services =>
5252
{
5353
services.AddSingleton<IBaseVersionCalculator>(new TestBaseVersionCalculator(false, new SemanticVersion(1), new MockCommit()));
@@ -60,7 +60,7 @@ public void DoesNotIncrementWhenBaseVersionSaysNotTo()
6060

6161
nextVersionCalculator.ShouldNotBeNull();
6262

63-
var version = nextVersionCalculator.FindVersionInternal(context);
63+
var version = nextVersionCalculator.FindVersionInternal();
6464

6565
version.ToString().ShouldBe("1.0.0");
6666
}
@@ -71,7 +71,7 @@ public void AppliesBranchPreReleaseTag()
7171
var semanticVersionBuildMetaData = new SemanticVersionBuildMetaData("ef7d0d7e1e700f1c7c9fa01ea6791bb778a5c37c", 2, "develop", "b1a34edbd80e141f7cc046c074f109be7d022074", "b1a34e", DateTimeOffset.Now);
7272
var contextBuilder = new GitVersionContextBuilder();
7373

74-
var context = contextBuilder
74+
contextBuilder
7575
.OverrideServices(services =>
7676
{
7777
services.AddSingleton<IBaseVersionCalculator>(new TestBaseVersionCalculator(false, new SemanticVersion(1), new MockCommit()));
@@ -83,7 +83,7 @@ public void AppliesBranchPreReleaseTag()
8383
var nextVersionCalculator = contextBuilder.ServicesProvider.GetService<INextVersionCalculator>() as NextVersionCalculator;
8484
nextVersionCalculator.ShouldNotBeNull();
8585

86-
var version = nextVersionCalculator.FindVersionInternal(context);
86+
var version = nextVersionCalculator.FindVersionInternal();
8787

8888
version.ToString("f").ShouldBe("1.0.0-alpha.1+2");
8989
}

src/GitVersionCore.Tests/VersionCalculation/Strategies/ConfigNextVersionBaseVersionStrategyTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using GitVersion.VersionCalculation;
55
using GitVersionCore.Tests.Helpers;
66
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Options;
78
using NUnit.Framework;
89
using Shouldly;
910

@@ -45,8 +46,8 @@ private static BaseVersion GetBaseVersion(Config config = null)
4546
}
4647

4748
contextBuilder.Build();
48-
var contextFactory = contextBuilder.ServicesProvider.GetService<IGitVersionContextFactory>();
49-
var strategy = new ConfigNextVersionVersionStrategy(contextFactory);
49+
var options = contextBuilder.ServicesProvider.GetService<IOptions<GitVersionContext>>();
50+
var strategy = new ConfigNextVersionVersionStrategy(options);
5051

5152
return strategy.GetVersions().SingleOrDefault();
5253
}

src/GitVersionCore.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using GitVersionCore.Tests.Mocks;
99
using LibGit2Sharp;
1010
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Options;
1112
using NUnit.Framework;
1213
using Shouldly;
1314

@@ -31,8 +32,8 @@ public void ShouldNotAllowIncrementOfVersion()
3132
});
3233
contextBuilder.Build();
3334
var log = contextBuilder.ServicesProvider.GetService<ILog>();
34-
var contextFactory = contextBuilder.ServicesProvider.GetService<IGitVersionContextFactory>();
35-
var strategy = new MergeMessageVersionStrategy(log, contextFactory);
35+
var options = contextBuilder.ServicesProvider.GetService<IOptions<GitVersionContext>>();
36+
var strategy = new MergeMessageVersionStrategy(log, options);
3637

3738
var baseVersion = strategy.GetVersions().Single();
3839

@@ -170,8 +171,8 @@ private void AssertMergeMessage(string message, string expectedVersion, IList<Co
170171
});
171172
contextBuilder.Build();
172173
var log = contextBuilder.ServicesProvider.GetService<ILog>();
173-
var contextFactory = contextBuilder.ServicesProvider.GetService<IGitVersionContextFactory>();
174-
var strategy = new MergeMessageVersionStrategy(log, contextFactory);
174+
var options = contextBuilder.ServicesProvider.GetService<IOptions<GitVersionContext>>();
175+
var strategy = new MergeMessageVersionStrategy(log, options);
175176

176177
var baseVersion = strategy.GetVersions().SingleOrDefault();
177178

src/GitVersionCore.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ private IGitVersionContextFactory GetGitVersionContextFactory(Config config = nu
9696

9797
var sp = ConfigureServices(services => { services.AddSingleton(options); });
9898
var gitRepoMetadataProvider = sp.GetService<IGitRepoMetadataProvider>();
99-
var gitVersionContextFactory = sp.GetService<IGitVersionContextFactory>();
100-
strategy = new VersionInBranchNameVersionStrategy(gitRepoMetadataProvider, gitVersionContextFactory);
99+
var contextOptions = sp.GetService<IOptions<GitVersionContext>>();
100+
strategy = new VersionInBranchNameVersionStrategy(gitRepoMetadataProvider, contextOptions);
101101
return sp.GetService<IGitVersionContextFactory>();
102102
}
103103
}

src/GitVersionCore/Common/IGitVersionContextFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace GitVersion
44
{
55
public interface IGitVersionContextFactory
66
{
7-
void Init(IRepository repository, Branch currentBranch, string commitId = null, bool onlyTrackedBranches = false);
8-
GitVersionContext Context { get; }
7+
GitVersionContext Init(IRepository repository, Branch currentBranch, string commitId = null, bool onlyTrackedBranches = false);
8+
GitVersionContext Create(Arguments arguments);
99
}
1010
}

src/GitVersionCore/Core/GitVersionCalculator.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using System;
2-
using GitVersion.Extensions;
32
using GitVersion.Logging;
43
using GitVersion.OutputVariables;
54
using GitVersion.VersionCalculation;
65
using GitVersion.VersionCalculation.Cache;
7-
using LibGit2Sharp;
86
using Microsoft.Extensions.Options;
97

108
namespace GitVersion
@@ -17,18 +15,18 @@ public class GitVersionCalculator : IGitVersionCalculator
1715
private readonly IVariableProvider variableProvider;
1816
private readonly IOptions<Arguments> options;
1917
private readonly IGitVersionCacheKeyFactory cacheKeyFactory;
20-
private readonly IGitVersionContextFactory gitVersionContextFactory;
18+
private readonly GitVersionContext context;
2119

2220
public GitVersionCalculator(ILog log, IGitVersionCache gitVersionCache, INextVersionCalculator nextVersionCalculator, IVariableProvider variableProvider,
23-
IOptions<Arguments> options, IGitVersionCacheKeyFactory cacheKeyFactory, IGitVersionContextFactory contextFactory)
21+
IOptions<Arguments> options, IGitVersionCacheKeyFactory cacheKeyFactory, IOptions<GitVersionContext> versionContext)
2422
{
2523
this.log = log ?? throw new ArgumentNullException(nameof(log));
2624
this.gitVersionCache = gitVersionCache ?? throw new ArgumentNullException(nameof(gitVersionCache));
2725
this.nextVersionCalculator = nextVersionCalculator ?? throw new ArgumentNullException(nameof(nextVersionCalculator));
2826
this.variableProvider = variableProvider ?? throw new ArgumentNullException(nameof(variableProvider));
2927
this.options = options ?? throw new ArgumentNullException(nameof(options));
3028
this.cacheKeyFactory = cacheKeyFactory ?? throw new ArgumentNullException(nameof(cacheKeyFactory));
31-
this.gitVersionContextFactory = contextFactory ?? throw new ArgumentNullException(nameof(contextFactory));
29+
context = versionContext.Value;
3230
}
3331

3432
public VersionVariables CalculateVersionVariables()
@@ -40,7 +38,7 @@ public VersionVariables CalculateVersionVariables()
4038

4139
if (versionVariables != null) return versionVariables;
4240

43-
versionVariables = ExecuteInternal(arguments);
41+
versionVariables = ExecuteInternal();
4442

4543
if (arguments.NoCache) return versionVariables;
4644
try
@@ -55,15 +53,9 @@ public VersionVariables CalculateVersionVariables()
5553
return versionVariables;
5654
}
5755

58-
private VersionVariables ExecuteInternal(Arguments arguments)
56+
private VersionVariables ExecuteInternal()
5957
{
60-
using var repo = new Repository(arguments.DotGitDirectory);
61-
var targetBranch = repo.GetTargetBranch(arguments.TargetBranch);
62-
gitVersionContextFactory.Init(repo, targetBranch, arguments.CommitId);
63-
var context = gitVersionContextFactory.Context;
64-
6558
var semanticVersion = nextVersionCalculator.FindVersion();
66-
6759
return variableProvider.GetVariablesFor(semanticVersion, context.Configuration, context.IsCurrentCommitTagged);
6860
}
6961
}

src/GitVersionCore/Core/GitVersionContextFactory.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class GitVersionContextFactory : IGitVersionContextFactory
1616
private readonly IGitRepoMetadataProvider gitRepoMetadataProvider;
1717
private readonly IBranchConfigurationCalculator branchConfigurationCalculator;
1818
private readonly IOptions<Arguments> options;
19-
public GitVersionContext Context { get; private set; }
2019

2120
public GitVersionContextFactory(ILog log, IConfigProvider configProvider, IGitRepoMetadataProvider gitRepoMetadataProvider, IBranchConfigurationCalculator branchConfigurationCalculator, IOptions<Arguments> options)
2221
{
@@ -27,7 +26,14 @@ public GitVersionContextFactory(ILog log, IConfigProvider configProvider, IGitRe
2726
this.options = options ?? throw new ArgumentNullException(nameof(options));
2827
}
2928

30-
public void Init(IRepository repository, Branch currentBranch, string commitId = null, bool onlyTrackedBranches = false)
29+
public GitVersionContext Create(Arguments arguments)
30+
{
31+
using var repository = new Repository(arguments.DotGitDirectory);
32+
var targetBranch = repository.GetTargetBranch(arguments.TargetBranch);
33+
return Init(repository, targetBranch, arguments.CommitId);
34+
}
35+
36+
public GitVersionContext Init(IRepository repository, Branch currentBranch, string commitId = null, bool onlyTrackedBranches = false)
3137
{
3238
if (currentBranch == null)
3339
throw new InvalidOperationException("Need a branch to operate on");
@@ -45,7 +51,7 @@ public void Init(IRepository repository, Branch currentBranch, string commitId =
4551

4652
var currentBranchConfig = branchConfigurationCalculator.GetBranchConfiguration(repository, currentBranch, currentCommit, configuration);
4753

48-
Context = new GitVersionContext(repository, currentBranch, currentCommit, currentBranchConfig, configuration);
54+
return new GitVersionContext(repository, currentBranch, currentCommit, currentBranchConfig, configuration);
4955
}
5056
}
5157
}

src/GitVersionCore/Extensions/LibGitExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ public static List<Commit> GetCommitsReacheableFromHead(this IRepository reposit
118118
SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Reverse
119119
};
120120

121-
var commitCache = repository.Commits.QueryBy(filter).ToList();
122-
return commitCache;
121+
return repository.Commits.QueryBy(filter).ToList();
123122
}
124123

125124
public static Commit GetForwardMerge(this IRepository repository, Commit commitToFindCommonBase, Commit findMergeBase)

src/GitVersionCore/GitVersionCoreModule.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using GitVersion.VersionCalculation;
88
using GitVersion.VersionCalculation.Cache;
99
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.Options;
1011

1112
namespace GitVersion
1213
{
@@ -39,6 +40,13 @@ public void RegisterTypes(IServiceCollection services)
3940

4041
services.AddSingleton(sp => sp.GetService<IConfigFileLocatorFactory>().Create());
4142

43+
services.AddSingleton(sp =>
44+
{
45+
var options = sp.GetService<IOptions<Arguments>>();
46+
var contextFactory = sp.GetService<IGitVersionContextFactory>();
47+
return Options.Create(contextFactory.Create(options.Value));
48+
});
49+
4250
services.AddModule(new BuildServerModule());
4351
services.AddModule(new GitVersionInitModule());
4452
services.AddModule(new VersionStrategyModule());

0 commit comments

Comments
 (0)