Skip to content

Commit 821fad1

Browse files
committed
decouple GitRepository from GitRepositoryInfo
1 parent f417ae1 commit 821fad1

File tree

12 files changed

+49
-45
lines changed

12 files changed

+49
-45
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,6 @@ private static IServiceProvider GetServiceProvider(GitVersionOptions gitVersionO
588588
if (environment != null) services.AddSingleton(environment);
589589
var options = Options.Create(gitVersionOptions);
590590
services.AddSingleton(options);
591-
services.AddSingleton(RepositoryExtensions.ToGitRepositoryInfo(options));
591+
services.AddSingleton<IGitRepositoryInfo>(new GitRepositoryInfo(options));
592592
});
593593
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using GitVersion.Configuration;
22
using GitVersion.Core.Tests.Helpers;
33
using GitVersion.Extensions;
4+
using GitVersion.Logging;
45
using GitVersion.OutputVariables;
56
using GitVersion.VersionCalculation;
67
using LibGit2Sharp;
@@ -138,6 +139,13 @@ public static void InitializeRepo(this RemoteRepositoryFixture fixture)
138139
gitPreparer.Prepare();
139140
}
140141

142+
internal static IGitRepository ToGitRepository(this IRepository repository)
143+
{
144+
var gitRepository = new GitRepository(new NullLog());
145+
gitRepository.DiscoverRepository(repository.Info.Path);
146+
return gitRepository;
147+
}
148+
141149
private static IServiceProvider ConfigureServices(Action<IServiceCollection>? servicesOverrides = null)
142150
{
143151
var services = new ServiceCollection()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void CanTakeVersionFromHotfixesBranch()
5555
});
5656
// Merge hotfix branch to support
5757
Commands.Checkout(fixture.Repository, MainBranch);
58-
Commands.Checkout(fixture.Repository, fixture.Repository.CreateBranch("support-1.1", (Commit)fixture.Repository.Tags.Single(t => t.FriendlyName == "1.1.0").Target));
58+
Commands.Checkout(fixture.Repository, fixture.Repository.CreateBranch("support-1.1", (LibGit2Sharp.Commit)fixture.Repository.Tags.Single(t => t.FriendlyName == "1.1.0").Target));
5959
fixture.AssertFullSemver("1.1.0");
6060

6161
// create hotfix branch
@@ -80,7 +80,7 @@ public void PatchOlderReleaseExample()
8080
// Merge hotfix branch to support
8181
Commands.Checkout(fixture.Repository, MainBranch);
8282
var tag = fixture.Repository.Tags.Single(t => t.FriendlyName == "1.1.0");
83-
var supportBranch = fixture.Repository.CreateBranch("support-1.1", (Commit)tag.Target);
83+
var supportBranch = fixture.Repository.CreateBranch("support-1.1", (LibGit2Sharp.Commit)tag.Target);
8484
Commands.Checkout(fixture.Repository, supportBranch);
8585
fixture.AssertFullSemver("1.1.0");
8686

src/GitVersion.Core/Git/IGitRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ public interface IGitRepository
1414

1515
ICommit? FindMergeBase(ICommit commit, ICommit otherCommit);
1616
int GetNumberOfUncommittedChanges();
17+
void DiscoverRepository(string? gitDirectory);
1718
}

src/GitVersion.Core/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ GitVersion.IGitPreparer.Prepare() -> void
457457
GitVersion.IGitRepository
458458
GitVersion.IGitRepository.Branches.get -> GitVersion.IBranchCollection!
459459
GitVersion.IGitRepository.Commits.get -> GitVersion.ICommitCollection!
460+
GitVersion.IGitRepository.DiscoverRepository(string? gitDirectory) -> void
460461
GitVersion.IGitRepository.FindMergeBase(GitVersion.ICommit! commit, GitVersion.ICommit! otherCommit) -> GitVersion.ICommit?
461462
GitVersion.IGitRepository.GetNumberOfUncommittedChanges() -> int
462463
GitVersion.IGitRepository.Head.get -> GitVersion.IBranch!

src/GitVersion.LibGit2Sharp/Git/GitRepository.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,28 @@ namespace GitVersion;
99
internal sealed class GitRepository : IMutatingGitRepository
1010
{
1111
private readonly ILog log;
12-
private readonly Lazy<IRepository> repositoryLazy;
12+
private Lazy<IRepository> repositoryLazy;
1313

14-
public GitRepository(ILog log, IGitRepositoryInfo repositoryInfo)
15-
: this(log, () => repositoryInfo.GitRootPath)
16-
{
17-
}
18-
19-
internal GitRepository(string gitRootDirectory)
20-
: this(new NullLog(), () => gitRootDirectory)
21-
{
22-
}
14+
public GitRepository(ILog log) => this.log = log;
2315

24-
internal GitRepository(IRepository repository)
16+
public void DiscoverRepository(string? gitDirectory)
2517
{
26-
repository = repository.NotNull();
27-
this.log = new NullLog();
28-
this.repositoryLazy = new Lazy<IRepository>(() => repository);
18+
if (gitDirectory != null && !gitDirectory.EndsWith(".git"))
19+
{
20+
gitDirectory = Repository.Discover(gitDirectory);
21+
}
22+
this.repositoryLazy = new Lazy<IRepository>(() => new Repository(gitDirectory));
2923
}
3024

31-
private GitRepository(ILog log, Func<string?> getGitRootDirectory)
25+
private IRepository RepositoryInstance
3226
{
33-
this.log = log.NotNull();
34-
this.repositoryLazy = new Lazy<IRepository>(() => new Repository(getGitRootDirectory()));
27+
get
28+
{
29+
var lazy = this.repositoryLazy ?? throw new NullReferenceException("Repository not initialized. Call DiscoverRepository() first.");
30+
return lazy.Value;
31+
}
3532
}
3633

37-
private IRepository RepositoryInstance => this.repositoryLazy.Value;
3834
public string Path => RepositoryInstance.Info.Path;
3935
public string WorkingDirectory => RepositoryInstance.Info.WorkingDirectory;
4036
public bool IsHeadDetached => RepositoryInstance.Info.IsHeadDetached;

src/GitVersion.LibGit2Sharp/Git/GitRepositoryInfo.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using GitVersion.Extensions;
22
using GitVersion.Helpers;
3+
using LibGit2Sharp;
34
using Microsoft.Extensions.Options;
45

56
namespace GitVersion;
@@ -90,7 +91,7 @@ private string GetProjectRootDirectory()
9091
if (gitDirectory.IsNullOrEmpty())
9192
throw new DirectoryNotFoundException("Cannot find the .git directory");
9293

93-
return new GitRepository(gitDirectory).WorkingDirectory;
94+
return new Repository(gitDirectory).Info.WorkingDirectory;
9495
}
9596

9697
private string? GetGitRootPath()
@@ -105,8 +106,8 @@ private static bool GitRepoHasMatchingRemote(string possiblePath, string targetU
105106
{
106107
try
107108
{
108-
var gitRepository = new GitRepository(possiblePath);
109-
return gitRepository.Remotes.Any(r => r.Url == targetUrl);
109+
var gitRepository = new Repository(possiblePath);
110+
return gitRepository.Network.Remotes.Any(r => r.Url == targetUrl);
110111
}
111112
catch (Exception)
112113
{

src/GitVersion.LibGit2Sharp/GitVersion.LibGit2Sharp.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
<ItemGroup>
88
<ProjectReference Include="..\GitVersion.Core\GitVersion.Core.csproj"/>
99
</ItemGroup>
10+
11+
<ItemGroup>
12+
<InternalsVisibleTo Include="GitVersion.Core.Tests" />
13+
<InternalsVisibleTo Include="GitVersion.MsBuild.Tests" />
14+
</ItemGroup>
1015
</Project>
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitVersion.Logging;
12
using Microsoft.Extensions.DependencyInjection;
23

34
namespace GitVersion;
@@ -6,8 +7,16 @@ public class GitVersionLibGit2SharpModule : IGitVersionModule
67
{
78
public void RegisterTypes(IServiceCollection services)
89
{
9-
services.AddSingleton<IGitRepository, GitRepository>();
10-
services.AddSingleton<IMutatingGitRepository, GitRepository>();
10+
services.AddSingleton<IGitRepository>(sp =>
11+
{
12+
var repositoryInfo = sp.GetRequiredService<IGitRepositoryInfo>();
13+
var log = sp.GetRequiredService<ILog>();
14+
IGitRepository gitRepository = new GitRepository(log);
15+
gitRepository.DiscoverRepository(repositoryInfo.GitRootPath);
16+
return gitRepository;
17+
});
18+
19+
services.AddSingleton<IMutatingGitRepository>(sp => (IMutatingGitRepository)sp.GetRequiredService<IGitRepository>());
1120
services.AddSingleton<IGitRepositoryInfo, GitRepositoryInfo>();
1221
}
1322
}

src/GitVersion.LibGit2Sharp/PublicAPI.Unshipped.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,3 @@ GitVersion.GitVersionLibGit2SharpModule
33
GitVersion.GitVersionLibGit2SharpModule.GitVersionLibGit2SharpModule() -> void
44
GitVersion.GitVersionLibGit2SharpModule.RegisterTypes(Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> void
55
GitVersion.RepositoryExtensions
6-
static GitVersion.RepositoryExtensions.RunSafe(System.Action! operation) -> void
7-
static GitVersion.RepositoryExtensions.RunSafe<T>(System.Func<T>! operation) -> T
8-
static GitVersion.RepositoryExtensions.ToGitRepository(this LibGit2Sharp.IRepository! repository) -> GitVersion.IGitRepository!
9-
static GitVersion.RepositoryExtensions.ToGitRepositoryInfo(Microsoft.Extensions.Options.IOptions<GitVersion.GitVersionOptions!>! options) -> GitVersion.IGitRepositoryInfo!

0 commit comments

Comments
 (0)