Skip to content

Commit dad5d2c

Browse files
authored
Merge pull request #4259 from arturcic/feature/git-repository
Moved direct usages of IGitRepository to IRepositoryStore
2 parents 2cb399b + de1be55 commit dad5d2c

19 files changed

+135
-220
lines changed

src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ namespace GitVersion.Common;
55

66
public interface IRepositoryStore
77
{
8+
int UncommittedChangesCount { get; }
9+
IBranch Head { get; }
10+
IBranchCollection Branches { get; }
11+
ITagCollection Tags { get; }
12+
813
/// <summary>
914
/// Find the merge base of the two branches, i.e. the best common ancestor of the two branches' tips.
1015
/// </summary>
@@ -13,12 +18,14 @@ public interface IRepositoryStore
1318
ICommit? FindMergeBase(ICommit commit, ICommit mainlineTip);
1419

1520
ICommit? GetCurrentCommit(IBranch currentBranch, string? commitId, IIgnoreConfiguration ignore);
21+
ICommit? GetForwardMerge(ICommit? commitToFindCommonBase, ICommit? findMergeBase);
1622

1723
IReadOnlyList<ICommit> GetCommitLog(ICommit? baseVersionSource, ICommit currentCommit, IIgnoreConfiguration ignore);
24+
IReadOnlyList<ICommit> GetCommitsReacheableFromHead(ICommit? headCommit, IIgnoreConfiguration ignore);
25+
IReadOnlyList<ICommit> GetCommitsReacheableFrom(IGitObject commit, IBranch branch);
1826

1927
IBranch GetTargetBranch(string? targetBranchName);
2028
IBranch? FindBranch(ReferenceName branchName);
21-
IBranch? FindBranch(string branchName);
2229

2330
IEnumerable<IBranch> ExcludingBranches(IEnumerable<IBranch> branchesToExclude);
2431
IEnumerable<IBranch> GetBranchesContainingCommit(ICommit commit, IEnumerable<IBranch>? branches = null, bool onlyTrackedBranches = false);
@@ -31,13 +38,9 @@ public interface IRepositoryStore
3138

3239
IEnumerable<BranchCommit> FindCommitBranchesBranchedFrom(IBranch branch, IGitVersionConfiguration configuration, params IBranch[] excludedBranches);
3340

34-
IEnumerable<BranchCommit> FindCommitBranchesBranchedFrom(IBranch branch, IGitVersionConfiguration configuration, IEnumerable<IBranch> excludedBranches);
35-
3641
IEnumerable<IBranch> GetSourceBranches(IBranch branch, IGitVersionConfiguration configuration, params IBranch[] excludedBranches);
3742

3843
IEnumerable<IBranch> GetSourceBranches(IBranch branch, IGitVersionConfiguration configuration, IEnumerable<IBranch> excludedBranches);
3944

4045
bool IsCommitOnBranch(ICommit? baseVersionSource, IBranch branch, ICommit firstMatchingCommit);
41-
42-
int GetNumberOfUncommittedChanges();
4346
}

src/GitVersion.Core/Core/BranchRepository.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
using GitVersion.Common;
12
using GitVersion.Configuration;
23
using GitVersion.Extensions;
34
using GitVersion.Git;
45

56
namespace GitVersion.Core;
67

7-
internal sealed class BranchRepository(IGitRepository gitRepository) : IBranchRepository
8+
internal sealed class BranchRepository(IRepositoryStore repositoryStore) : IBranchRepository
89
{
9-
private readonly IGitRepository gitRepository = gitRepository.NotNull();
10+
private readonly IRepositoryStore repositoryStore = repositoryStore.NotNull();
1011

1112
public IEnumerable<IBranch> GetMainBranches(IGitVersionConfiguration configuration, params IBranch[] excludeBranches)
1213
=> GetBranches(configuration, [.. excludeBranches], branchConfiguration => branchConfiguration.IsMainBranch == true);
@@ -19,7 +20,7 @@ private IEnumerable<IBranch> GetBranches(
1920
{
2021
predicate.NotNull();
2122

22-
foreach (var branch in this.gitRepository.Branches)
23+
foreach (var branch in this.repositoryStore.Branches)
2324
{
2425
if (!excludeBranches.Contains(branch))
2526
{

src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
using GitVersion.Common;
12
using GitVersion.Extensions;
23
using GitVersion.Git;
34
using GitVersion.Logging;
45

56
namespace GitVersion;
67

7-
internal class BranchesContainingCommitFinder(IGitRepository repository, ILog log)
8+
internal class BranchesContainingCommitFinder(IRepositoryStore repositoryStore, ILog log)
89
{
910
private readonly ILog log = log.NotNull();
10-
private readonly IGitRepository repository = repository.NotNull();
11+
private readonly IRepositoryStore repositoryStore = repositoryStore.NotNull();
1112

1213
public IEnumerable<IBranch> GetBranchesContainingCommit(ICommit commit, IEnumerable<IBranch>? branches = null, bool onlyTrackedBranches = false)
1314
{
1415
commit.NotNull();
15-
branches ??= [.. this.repository.Branches];
16+
branches ??= [.. this.repositoryStore.Branches];
1617

1718
// TODO Should we cache this?
1819
// Yielding part is split from the main part of the method to avoid having the exception check performed lazily.
@@ -45,7 +46,7 @@ private IEnumerable<IBranch> InnerGetBranchesContainingCommit(IGitObject commit,
4546
{
4647
log.Info($"Searching for commits reachable from '{branch}'.");
4748

48-
var commits = GetCommitsReacheableFrom(commit, branch);
49+
var commits = this.repositoryStore.GetCommitsReacheableFrom(commit, branch);
4950

5051
if (!commits.Any())
5152
{
@@ -59,14 +60,6 @@ private IEnumerable<IBranch> InnerGetBranchesContainingCommit(IGitObject commit,
5960
}
6061
}
6162

62-
private IEnumerable<ICommit> GetCommitsReacheableFrom(IGitObject commit, IBranch branch)
63-
{
64-
var filter = new CommitFilter { IncludeReachableFrom = branch };
65-
var commitCollection = this.repository.Commits.QueryBy(filter);
66-
67-
return commitCollection.Where(c => c.Sha == commit.Sha);
68-
}
69-
7063
private static bool IncludeTrackedBranches(IBranch branch, bool includeOnlyTracked)
7164
=> (includeOnlyTracked && branch.IsTracking) || !includeOnlyTracked;
7265

src/GitVersion.Core/Core/GitVersionContextFactory.cs

+2-6
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions)
2727
?? throw new InvalidOperationException("Need a branch to operate on");
2828
var currentCommit = this.repositoryStore.GetCurrentCommit(
2929
currentBranch, gitVersionOptions.RepositoryInfo.CommitId, configuration.Ignore
30-
);
31-
32-
if (currentCommit is null)
33-
throw new GitVersionException("No commits found on the current branch.");
34-
30+
) ?? throw new GitVersionException("No commits found on the current branch.");
3531
if (currentBranch.IsDetachedHead)
3632
{
3733
var branchForCommit = this.repositoryStore.GetBranchesContainingCommit(
@@ -45,7 +41,7 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions)
4541
format: configuration.SemanticVersionFormat,
4642
ignore: configuration.Ignore
4743
).Contains(currentCommit);
48-
var numberOfUncommittedChanges = this.repositoryStore.GetNumberOfUncommittedChanges();
44+
var numberOfUncommittedChanges = this.repositoryStore.UncommittedChangesCount;
4945

5046
return new(currentBranch, currentCommit, configuration, isCurrentCommitTagged, numberOfUncommittedChanges);
5147
}

src/GitVersion.Core/Core/MainlineBranchFinder.cs

-97
This file was deleted.

src/GitVersion.Core/Core/MergeBaseFinder.cs

+2-15
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
namespace GitVersion;
77

8-
internal class MergeBaseFinder(IRepositoryStore repositoryStore, IGitRepository gitRepository, ILog log)
8+
internal class MergeBaseFinder(IRepositoryStore repositoryStore, ILog log)
99
{
1010
private readonly ILog log = log.NotNull();
11-
private readonly IGitRepository repository = gitRepository.NotNull();
1211
private readonly IRepositoryStore repositoryStore = repositoryStore.NotNull();
1312
private readonly Dictionary<Tuple<IBranch, IBranch>, ICommit> mergeBaseCache = [];
1413

@@ -71,7 +70,7 @@ internal class MergeBaseFinder(IRepositoryStore repositoryStore, IGitRepository
7170
do
7271
{
7372
// Now make sure that the merge base is not a forward merge
74-
forwardMerge = GetForwardMerge(commitToFindCommonBase, findMergeBase);
73+
forwardMerge = this.repositoryStore.GetForwardMerge(commitToFindCommonBase, findMergeBase);
7574

7675
if (forwardMerge == null)
7776
continue;
@@ -102,16 +101,4 @@ internal class MergeBaseFinder(IRepositoryStore repositoryStore, IGitRepository
102101

103102
return findMergeBase;
104103
}
105-
106-
private ICommit? GetForwardMerge(ICommit? commitToFindCommonBase, ICommit? findMergeBase)
107-
{
108-
var filter = new CommitFilter
109-
{
110-
IncludeReachableFrom = commitToFindCommonBase,
111-
ExcludeReachableFrom = findMergeBase
112-
};
113-
var commitCollection = this.repository.Commits.QueryBy(filter);
114-
115-
return commitCollection.FirstOrDefault(c => c.Parents.Contains(findMergeBase));
116-
}
117104
}

src/GitVersion.Core/Core/MergeCommitFinder.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
using GitVersion.Common;
12
using GitVersion.Configuration;
23
using GitVersion.Extensions;
34
using GitVersion.Git;
45
using GitVersion.Logging;
56

67
namespace GitVersion;
78

8-
internal class MergeCommitFinder(RepositoryStore repositoryStore, IGitVersionConfiguration configuration, IEnumerable<IBranch> excludedBranches, ILog log)
9+
internal class MergeCommitFinder(IRepositoryStore repositoryStore, IGitVersionConfiguration configuration, IEnumerable<IBranch> excludedBranches, ILog log)
910
{
1011
private readonly ILog log = log.NotNull();
1112
private readonly IEnumerable<IBranch> branches = repositoryStore.ExcludingBranches(excludedBranches.NotNull());
12-
private readonly RepositoryStore repositoryStore = repositoryStore.NotNull();
13+
private readonly IRepositoryStore repositoryStore = repositoryStore.NotNull();
1314
private readonly IGitVersionConfiguration configuration = configuration.NotNull();
1415
private readonly Dictionary<IBranch, List<BranchCommit>> mergeBaseCommitsCache = [];
1516

0 commit comments

Comments
 (0)