Skip to content

Commit 62128fe

Browse files
committed
#4125 - fix version and help run options to not depend on .git directory
1 parent 9365660 commit 62128fe

File tree

7 files changed

+47
-21
lines changed

7 files changed

+47
-21
lines changed

src/GitVersion.App/GitVersionExecutor.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,29 @@ namespace GitVersion;
99
internal class GitVersionExecutor(
1010
ILog log,
1111
IConsole console,
12+
IVersionWriter versionWriter,
13+
IHelpWriter helpWriter,
1214
IConfigurationFileLocator configurationFileLocator,
1315
IConfigurationProvider configurationProvider,
1416
IConfigurationSerializer configurationSerializer,
1517
IGitVersionCalculateTool gitVersionCalculateTool,
1618
IGitVersionOutputTool gitVersionOutputTool,
17-
IVersionWriter versionWriter,
18-
IHelpWriter helpWriter,
19+
IGitRepository gitRepository,
1920
IGitRepositoryInfo repositoryInfo)
2021
: IGitVersionExecutor
2122
{
2223
private readonly ILog log = log.NotNull();
2324
private readonly IConsole console = console.NotNull();
25+
private readonly IVersionWriter versionWriter = versionWriter.NotNull();
26+
private readonly IHelpWriter helpWriter = helpWriter.NotNull();
27+
2428
private readonly IConfigurationFileLocator configurationFileLocator = configurationFileLocator.NotNull();
2529
private readonly IConfigurationProvider configurationProvider = configurationProvider.NotNull();
2630
private readonly IConfigurationSerializer configurationSerializer = configurationSerializer.NotNull();
2731

2832
private readonly IGitVersionCalculateTool gitVersionCalculateTool = gitVersionCalculateTool.NotNull();
2933
private readonly IGitVersionOutputTool gitVersionOutputTool = gitVersionOutputTool.NotNull();
30-
private readonly IVersionWriter versionWriter = versionWriter.NotNull();
31-
private readonly IHelpWriter helpWriter = helpWriter.NotNull();
34+
private readonly IGitRepository gitRepository = gitRepository.NotNull();
3235
private readonly IGitRepositoryInfo repositoryInfo = repositoryInfo.NotNull();
3336

3437
public int Execute(GitVersionOptions gitVersionOptions)
@@ -49,6 +52,7 @@ public int Execute(GitVersionOptions gitVersionOptions)
4952

5053
private int RunGitVersionTool(GitVersionOptions gitVersionOptions)
5154
{
55+
this.gitRepository.DiscoverRepository(gitVersionOptions.WorkingDirectory);
5256
var mutexName = this.repositoryInfo.DotGitDirectory?.Replace(Path.DirectorySeparatorChar.ToString(), "") ?? string.Empty;
5357
using var mutex = new Mutex(true, $@"Global\gitversion{mutexName}", out var acquired);
5458

src/GitVersion.Core.Tests/Core/DynamicRepositoryTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public void FindsVersionInDynamicRepo(string name, string url, string targetBran
7474

7575
var sp = ConfigureServices(services => services.AddSingleton(options));
7676

77+
sp.DiscoverRepository();
78+
7779
var gitPreparer = sp.GetRequiredService<IGitPreparer>();
7880
gitPreparer.Prepare();
7981

src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public void CacheKeySameAfterReNormalizing()
3434

3535
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
3636

37+
sp.DiscoverRepository();
38+
3739
var preparer = this.sp.GetRequiredService<IGitPreparer>();
3840

3941
preparer.Prepare();
@@ -78,6 +80,8 @@ public void CacheKeyForWorktree()
7880

7981
this.sp = GetServiceProvider(gitVersionOptions);
8082

83+
sp.DiscoverRepository();
84+
8185
var preparer = this.sp.GetRequiredService<IGitPreparer>();
8286
preparer.Prepare();
8387
var cacheKeyFactory = this.sp.GetRequiredService<IGitVersionCacheKeyFactory>();
@@ -516,6 +520,9 @@ public void CalculateVersionVariables_TwoBranchHasSameCommitHeadDetachedAndNotTa
516520
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
517521

518522
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
523+
524+
sp.DiscoverRepository();
525+
519526
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
520527

521528
// Execute & Verify
@@ -543,6 +550,9 @@ public void CalculateVersionVariables_TwoBranchHasSameCommitHeadDetachedAndTagge
543550
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
544551

545552
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
553+
554+
sp.DiscoverRepository();
555+
546556
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
547557

548558
// Execute
@@ -568,6 +578,9 @@ public void CalculateVersionVariables_ShallowFetch_ThrowException()
568578
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
569579

570580
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
581+
582+
sp.DiscoverRepository();
583+
571584
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
572585

573586
// Execute & Verify
@@ -583,6 +596,8 @@ private IGitVersionCalculateTool GetGitVersionCalculator(GitVersionOptions gitVe
583596
this.log = this.sp.GetRequiredService<ILog>();
584597
this.gitVersionCacheProvider = (GitVersionCacheProvider)this.sp.GetRequiredService<IGitVersionCacheProvider>();
585598

599+
sp.DiscoverRepository();
600+
586601
return this.sp.GetRequiredService<IGitVersionCalculateTool>();
587602
}
588603

src/GitVersion.Core.Tests/Extensions/GitToolsTestingExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public static IBranch CreateMockBranch(string name, params ICommit[] commits)
4646
return branch;
4747
}
4848

49+
public static void DiscoverRepository(this IServiceProvider sp)
50+
{
51+
var gitRepository = sp.GetRequiredService<IGitRepository>();
52+
var gitRepositoryInfo = sp.GetRequiredService<IGitRepositoryInfo>();
53+
gitRepository.DiscoverRepository(gitRepositoryInfo.GitRootPath);
54+
}
55+
4956
public static IBranch FindBranch(this IGitRepository repository, string branchName)
5057
=> repository.Branches.FirstOrDefault(branch => branch.Name.WithoutOrigin == branchName)
5158
?? throw new GitVersionException($"Branch {branchName} not found");
@@ -84,6 +91,9 @@ public static GitVersionVariables GetVersion(this RepositoryFixtureBase fixture,
8491
services.AddSingleton(options);
8592
services.AddSingleton(configurationProviderMock);
8693
});
94+
95+
sp.DiscoverRepository();
96+
8797
var variableProvider = sp.GetRequiredService<IVariableProvider>();
8898
var nextVersionCalculator = sp.GetRequiredService<INextVersionCalculator>();
8999
var contextOptions = sp.GetRequiredService<Lazy<GitVersionContext>>();

src/GitVersion.Core.Tests/IntegrationTests/RemoteRepositoryScenarios.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public void GivenARemoteGitRepositoryWithCommitsAndBranchesThenClonedLocalShould
6363
services.AddSingleton<IEnvironment>(environment);
6464
});
6565

66+
sp.DiscoverRepository();
67+
6668
var gitPreparer = sp.GetRequiredService<IGitPreparer>();
6769

6870
gitPreparer.Prepare();

src/GitVersion.LibGit2Sharp/Git/GitRepository.extended.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ namespace GitVersion.Git;
88

99
internal partial class GitRepository(ILog log) : IMutatingGitRepository
1010
{
11+
private readonly ILog log = log.NotNull();
12+
1113
public void Clone(string? sourceUrl, string? workdirPath, AuthenticationInfo auth)
1214
{
1315
try
1416
{
1517
var path = Repository.Clone(sourceUrl, workdirPath, GetCloneOptions(auth));
16-
log.Info($"Returned path after repository clone: {path}");
18+
this.log.Info($"Returned path after repository clone: {path}");
1719
}
1820
catch (LibGit2Sharp.LockedFileException ex)
1921
{
@@ -48,7 +50,7 @@ public void Fetch(string remote, IEnumerable<string> refSpecs, AuthenticationInf
4850
Commands.Fetch((Repository)RepositoryInstance, remote, refSpecs, GetFetchOptions(auth), logMessage));
4951
public void CreateBranchForPullRequestBranch(AuthenticationInfo auth) => RepositoryExtensions.RunSafe(() =>
5052
{
51-
log.Info("Fetching remote refs to see if there is a pull request ref");
53+
this.log.Info("Fetching remote refs to see if there is a pull request ref");
5254

5355
// FIX ME: What to do when Tip is null?
5456
if (Head.Tip == null)
@@ -59,21 +61,21 @@ public void CreateBranchForPullRequestBranch(AuthenticationInfo auth) => Reposit
5961
var reference = GetPullRequestReference(auth, remote, headTipSha);
6062
var canonicalName = reference.CanonicalName;
6163
var referenceName = ReferenceName.Parse(reference.CanonicalName);
62-
log.Info($"Found remote tip '{canonicalName}' pointing at the commit '{headTipSha}'.");
64+
this.log.Info($"Found remote tip '{canonicalName}' pointing at the commit '{headTipSha}'.");
6365

6466
if (referenceName.IsTag)
6567
{
66-
log.Info($"Checking out tag '{canonicalName}'");
68+
this.log.Info($"Checking out tag '{canonicalName}'");
6769
Checkout(reference.Target.Sha);
6870
}
6971
else if (referenceName.IsPullRequest)
7072
{
7173
var fakeBranchName = canonicalName.Replace("refs/pull/", "refs/heads/pull/").Replace("refs/pull-requests/", "refs/heads/pull-requests/");
7274

73-
log.Info($"Creating fake local branch '{fakeBranchName}'.");
75+
this.log.Info($"Creating fake local branch '{fakeBranchName}'.");
7476
Refs.Add(fakeBranchName, headTipSha);
7577

76-
log.Info($"Checking local branch '{fakeBranchName}' out.");
78+
this.log.Info($"Checking local branch '{fakeBranchName}' out.");
7779
Checkout(fakeBranchName);
7880
}
7981
else
@@ -91,7 +93,7 @@ private DirectReference GetPullRequestReference(AuthenticationInfo auth, LibGit2
9193
: network.ListReferences(remote))
9294
.Select(r => r.ResolveToDirectReference()).ToList();
9395

94-
log.Info($"Remote Refs:{PathHelper.NewLine}" + string.Join(PathHelper.NewLine, remoteTips.Select(r => r.CanonicalName)));
96+
this.log.Info($"Remote Refs:{PathHelper.NewLine}" + string.Join(PathHelper.NewLine, remoteTips.Select(r => r.CanonicalName)));
9597
var refs = remoteTips.Where(r => r.TargetIdentifier == headTipSha).ToList();
9698

9799
switch (refs.Count)

src/GitVersion.LibGit2Sharp/GitVersionLibGit2SharpModule.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using GitVersion.Git;
2-
using GitVersion.Logging;
32
using Microsoft.Extensions.DependencyInjection;
43

54
namespace GitVersion;
@@ -8,15 +7,7 @@ public class GitVersionLibGit2SharpModule : IGitVersionModule
87
{
98
public void RegisterTypes(IServiceCollection services)
109
{
11-
services.AddSingleton<IGitRepository>(sp =>
12-
{
13-
var repositoryInfo = sp.GetRequiredService<IGitRepositoryInfo>();
14-
var log = sp.GetRequiredService<ILog>();
15-
IGitRepository gitRepository = new GitRepository(log);
16-
gitRepository.DiscoverRepository(repositoryInfo.GitRootPath);
17-
return gitRepository;
18-
});
19-
10+
services.AddSingleton<IGitRepository, GitRepository>();
2011
services.AddSingleton<IMutatingGitRepository>(sp => (IMutatingGitRepository)sp.GetRequiredService<IGitRepository>());
2112
services.AddSingleton<IGitRepositoryInfo, GitRepositoryInfo>();
2213
}

0 commit comments

Comments
 (0)