Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ namespace GitVersion.Common;

public interface IRepositoryStore
{
int UncommittedChangesCount { get; }
IBranch Head { get; }
IBranchCollection Branches { get; }
ITagCollection Tags { get; }

/// <summary>
/// Find the merge base of the two branches, i.e. the best common ancestor of the two branches' tips.
/// </summary>
Expand All @@ -13,12 +18,14 @@ public interface IRepositoryStore
ICommit? FindMergeBase(ICommit commit, ICommit mainlineTip);

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

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

IBranch GetTargetBranch(string? targetBranchName);
IBranch? FindBranch(ReferenceName branchName);
IBranch? FindBranch(string branchName);

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

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

IEnumerable<BranchCommit> FindCommitBranchesBranchedFrom(IBranch branch, IGitVersionConfiguration configuration, IEnumerable<IBranch> excludedBranches);

IEnumerable<IBranch> GetSourceBranches(IBranch branch, IGitVersionConfiguration configuration, params IBranch[] excludedBranches);

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

bool IsCommitOnBranch(ICommit? baseVersionSource, IBranch branch, ICommit firstMatchingCommit);

int GetNumberOfUncommittedChanges();
}
7 changes: 4 additions & 3 deletions src/GitVersion.Core/Core/BranchRepository.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using GitVersion.Common;
using GitVersion.Configuration;
using GitVersion.Extensions;
using GitVersion.Git;

namespace GitVersion.Core;

internal sealed class BranchRepository(IGitRepository gitRepository) : IBranchRepository
internal sealed class BranchRepository(IRepositoryStore repositoryStore) : IBranchRepository
{
private readonly IGitRepository gitRepository = gitRepository.NotNull();
private readonly IRepositoryStore repositoryStore = repositoryStore.NotNull();

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

foreach (var branch in this.gitRepository.Branches)
foreach (var branch in this.repositoryStore.Branches)
{
if (!excludeBranches.Contains(branch))
{
Expand Down
17 changes: 5 additions & 12 deletions src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using GitVersion.Common;
using GitVersion.Extensions;
using GitVersion.Git;
using GitVersion.Logging;

namespace GitVersion;

internal class BranchesContainingCommitFinder(IGitRepository repository, ILog log)
internal class BranchesContainingCommitFinder(IRepositoryStore repositoryStore, ILog log)
{
private readonly ILog log = log.NotNull();
private readonly IGitRepository repository = repository.NotNull();
private readonly IRepositoryStore repositoryStore = repositoryStore.NotNull();

public IEnumerable<IBranch> GetBranchesContainingCommit(ICommit commit, IEnumerable<IBranch>? branches = null, bool onlyTrackedBranches = false)
{
commit.NotNull();
branches ??= [.. this.repository.Branches];
branches ??= [.. this.repositoryStore.Branches];

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

var commits = GetCommitsReacheableFrom(commit, branch);
var commits = this.repositoryStore.GetCommitsReacheableFrom(commit, branch);

if (!commits.Any())
{
Expand All @@ -59,14 +60,6 @@ private IEnumerable<IBranch> InnerGetBranchesContainingCommit(IGitObject commit,
}
}

private IEnumerable<ICommit> GetCommitsReacheableFrom(IGitObject commit, IBranch branch)
{
var filter = new CommitFilter { IncludeReachableFrom = branch };
var commitCollection = this.repository.Commits.QueryBy(filter);

return commitCollection.Where(c => c.Sha == commit.Sha);
}

private static bool IncludeTrackedBranches(IBranch branch, bool includeOnlyTracked)
=> (includeOnlyTracked && branch.IsTracking) || !includeOnlyTracked;

Expand Down
8 changes: 2 additions & 6 deletions src/GitVersion.Core/Core/GitVersionContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions)
?? throw new InvalidOperationException("Need a branch to operate on");
var currentCommit = this.repositoryStore.GetCurrentCommit(
currentBranch, gitVersionOptions.RepositoryInfo.CommitId, configuration.Ignore
);

if (currentCommit is null)
throw new GitVersionException("No commits found on the current branch.");

) ?? throw new GitVersionException("No commits found on the current branch.");
if (currentBranch.IsDetachedHead)
{
var branchForCommit = this.repositoryStore.GetBranchesContainingCommit(
Expand All @@ -45,7 +41,7 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions)
format: configuration.SemanticVersionFormat,
ignore: configuration.Ignore
).Contains(currentCommit);
var numberOfUncommittedChanges = this.repositoryStore.GetNumberOfUncommittedChanges();
var numberOfUncommittedChanges = this.repositoryStore.UncommittedChangesCount;

return new(currentBranch, currentCommit, configuration, isCurrentCommitTagged, numberOfUncommittedChanges);
}
Expand Down
97 changes: 0 additions & 97 deletions src/GitVersion.Core/Core/MainlineBranchFinder.cs

This file was deleted.

17 changes: 2 additions & 15 deletions src/GitVersion.Core/Core/MergeBaseFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

namespace GitVersion;

internal class MergeBaseFinder(IRepositoryStore repositoryStore, IGitRepository gitRepository, ILog log)
internal class MergeBaseFinder(IRepositoryStore repositoryStore, ILog log)
{
private readonly ILog log = log.NotNull();
private readonly IGitRepository repository = gitRepository.NotNull();
private readonly IRepositoryStore repositoryStore = repositoryStore.NotNull();
private readonly Dictionary<Tuple<IBranch, IBranch>, ICommit> mergeBaseCache = [];

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

if (forwardMerge == null)
continue;
Expand Down Expand Up @@ -102,16 +101,4 @@ internal class MergeBaseFinder(IRepositoryStore repositoryStore, IGitRepository

return findMergeBase;
}

private ICommit? GetForwardMerge(ICommit? commitToFindCommonBase, ICommit? findMergeBase)
{
var filter = new CommitFilter
{
IncludeReachableFrom = commitToFindCommonBase,
ExcludeReachableFrom = findMergeBase
};
var commitCollection = this.repository.Commits.QueryBy(filter);

return commitCollection.FirstOrDefault(c => c.Parents.Contains(findMergeBase));
}
}
5 changes: 3 additions & 2 deletions src/GitVersion.Core/Core/MergeCommitFinder.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using GitVersion.Common;
using GitVersion.Configuration;
using GitVersion.Extensions;
using GitVersion.Git;
using GitVersion.Logging;

namespace GitVersion;

internal class MergeCommitFinder(RepositoryStore repositoryStore, IGitVersionConfiguration configuration, IEnumerable<IBranch> excludedBranches, ILog log)
internal class MergeCommitFinder(IRepositoryStore repositoryStore, IGitVersionConfiguration configuration, IEnumerable<IBranch> excludedBranches, ILog log)
{
private readonly ILog log = log.NotNull();
private readonly IEnumerable<IBranch> branches = repositoryStore.ExcludingBranches(excludedBranches.NotNull());
private readonly RepositoryStore repositoryStore = repositoryStore.NotNull();
private readonly IRepositoryStore repositoryStore = repositoryStore.NotNull();
private readonly IGitVersionConfiguration configuration = configuration.NotNull();
private readonly Dictionary<IBranch, List<BranchCommit>> mergeBaseCommitsCache = [];

Expand Down
Loading