Skip to content

Commit

Permalink
Make code clean up and integrate code review suggestions.
Browse files Browse the repository at this point in the history
  • Loading branch information
HHobeck committed Jan 28, 2024
1 parent c5512f2 commit 0bd90f5
Show file tree
Hide file tree
Showing 8 changed files with 4 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace GitVersion.Core.Tests.VersionCalculation.Strategies;

[TestFixture]
public class ConfiguredNextVersionBaseVersionStrategyTests : TestBase
public class ConfiguredNextVersionVersionStrategyTests : TestBase
{
[Test]
public void ReturnsNullWhenNoNextVersionIsInConfig()
Expand Down
6 changes: 0 additions & 6 deletions src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@ public interface IRepositoryStore

ICommit? FindMergeBase(ICommit commit, ICommit mainlineTip);
ICommit? GetCurrentCommit(IBranch currentBranch, string? commitId);
IEnumerable<ICommit> GetMainlineCommitLog(ICommit? baseVersionSource, ICommit? mainlineTip);
IEnumerable<ICommit> GetMergeBaseCommits(ICommit? mergeCommit, ICommit? mergedHead, ICommit? findMergeBase);
IEnumerable<ICommit> GetCommitLog(ICommit? baseVersionSource, ICommit? currentCommit);

IBranch GetTargetBranch(string? targetBranchName);
IBranch? FindBranch(ReferenceName branchName);
IBranch? FindBranch(string branchName);
IBranch? FindMainBranch(IGitVersionConfiguration configuration);
IEnumerable<IBranch> FindMainlineBranches(IGitVersionConfiguration configuration);
IEnumerable<IBranch> GetReleaseBranches(IEnumerable<KeyValuePair<string, IBranchConfiguration>> releaseBranchConfig);
IEnumerable<IBranch> ExcludingBranches(IEnumerable<IBranch> branchesToExclude);
IEnumerable<IBranch> GetBranchesContainingCommit(ICommit commit, IEnumerable<IBranch>? branches = null, bool onlyTrackedBranches = false);

IDictionary<string, List<IBranch>> GetMainlineBranches(ICommit commit, IGitVersionConfiguration configuration);

/// <summary>
/// Find the commit where the given branch was branched from another branch.
/// If there are multiple such commits and branches, tries to guess based on commit histories.
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.Core/Core/BranchRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal sealed class BranchRepository(Lazy<GitVersionContext> versionContext, I

private readonly IGitRepository gitRepository = gitRepository.NotNull();

public IEnumerable<IBranch> GetMainlineBranches(params IBranch[] excludeBranches)
public IEnumerable<IBranch> GetMainBranches(params IBranch[] excludeBranches)
=> GetBranches([.. excludeBranches], configuration => configuration.IsMainBranch == true);

public IEnumerable<IBranch> GetReleaseBranches(params IBranch[] excludeBranches)
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.Core/Core/IBranchRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace GitVersion.Core;

internal interface IBranchRepository
{
IEnumerable<IBranch> GetMainlineBranches(params IBranch[] excludeBranches);
IEnumerable<IBranch> GetMainBranches(params IBranch[] excludeBranches);

IEnumerable<IBranch> GetReleaseBranches(params IBranch[] excludeBranches);
}
58 changes: 0 additions & 58 deletions src/GitVersion.Core/Core/RepositoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,6 @@ public RepositoryStore(ILog log, IGitRepository repository)
return currentBranch.Tip;
}

public IEnumerable<ICommit> GetMainlineCommitLog(ICommit? baseVersionSource, ICommit? mainlineTip)
{
if (mainlineTip is null)
{
return [];
}

var filter = new CommitFilter { IncludeReachableFrom = mainlineTip, ExcludeReachableFrom = baseVersionSource, SortBy = CommitSortStrategies.Reverse, FirstParentOnly = true };

return this.repository.Commits.QueryBy(filter);
}

public IEnumerable<ICommit> GetMergeBaseCommits(ICommit? mergeCommit, ICommit? mergedHead, ICommit? findMergeBase)
{
var filter = new CommitFilter { IncludeReachableFrom = mergedHead, ExcludeReachableFrom = findMergeBase };
var commitCollection = this.repository.Commits.QueryBy(filter);

var commits = mergeCommit != null
? new[] { mergeCommit }.Union(commitCollection)
: commitCollection;
return commits;
}

public IBranch GetTargetBranch(string? targetBranchName)
{
// By default, we assume HEAD is pointing to the desired branch
Expand Down Expand Up @@ -108,35 +85,6 @@ public IBranch GetTargetBranch(string? targetBranchName)

public IBranch? FindBranch(string branchName) => this.repository.Branches.FirstOrDefault(x => x.Name.EquivalentTo(branchName));

public IBranch? FindMainBranch(IGitVersionConfiguration configuration)
{
var branches = configuration.Branches;
var mainBranchRegex = branches[ConfigurationConstants.MainBranchKey].RegularExpression
?? branches[ConfigurationConstants.MasterBranchKey].RegularExpression;

if (mainBranchRegex == null)
{
return FindBranch(ConfigurationConstants.MainBranchKey) ?? FindBranch(ConfigurationConstants.MasterBranchKey);
}

return this.repository.Branches.FirstOrDefault(b =>
Regex.IsMatch(b.Name.WithoutOrigin, mainBranchRegex, RegexOptions.IgnoreCase));
}

public IEnumerable<IBranch> FindMainlineBranches(IGitVersionConfiguration configuration)
{
configuration.NotNull();

foreach (var branch in this.repository.Branches)
{
var branchConfiguration = configuration.GetBranchConfiguration(branch.Name);
if (branchConfiguration.IsMainBranch == true)
{
yield return branch;
}
}
}

public IEnumerable<IBranch> GetReleaseBranches(IEnumerable<KeyValuePair<string, IBranchConfiguration>> releaseBranchConfig)
=> this.repository.Branches.Where(b => IsReleaseBranch(b, releaseBranchConfig));

Expand All @@ -153,12 +101,6 @@ public IEnumerable<IBranch> GetBranchesContainingCommit(ICommit commit, IEnumera
return branchesContainingCommitFinder.GetBranchesContainingCommit(commit, branches, onlyTrackedBranches);
}

public IDictionary<string, List<IBranch>> GetMainlineBranches(ICommit commit, IGitVersionConfiguration configuration)
{
var mainlineBranchFinder = new MainlineBranchFinder(this, this.repository, configuration, this.log);
return mainlineBranchFinder.FindMainlineBranches(commit);
}

public IEnumerable<IBranch> GetSourceBranches(IBranch branch, IGitVersionConfiguration configuration,
params IBranch[] excludedBranches)
=> GetSourceBranches(branch, configuration, (IEnumerable<IBranch>)excludedBranches);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ IEnumerable<SemanticVersionWithTag> GetElements()
using (this.log.IndentLog($"Getting tagged semantic versions of mainline branches. " +
$"TagPrefix: {tagPrefix} and Format: {format}"))
{
foreach (var mainlinemBranch in branchRepository.GetMainlineBranches(excludeBranches))
foreach (var mainlinemBranch in branchRepository.GetMainBranches(excludeBranches))
{
foreach (var semanticVersion in GetTaggedSemanticVersionsOfBranch(mainlinemBranch, tagPrefix, format).SelectMany(_ => _))
{
Expand Down
5 changes: 0 additions & 5 deletions src/GitVersion.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,12 @@ GitVersion.Common.IRepositoryStore.FindBranch(string! branchName) -> GitVersion.
GitVersion.Common.IRepositoryStore.FindCommitBranchWasBranchedFrom(GitVersion.IBranch? branch, GitVersion.Configuration.IGitVersionConfiguration! configuration, params GitVersion.IBranch![]! excludedBranches) -> GitVersion.BranchCommit
GitVersion.Common.IRepositoryStore.FindCommitBranchesWasBranchedFrom(GitVersion.IBranch! branch, GitVersion.Configuration.IGitVersionConfiguration! configuration, System.Collections.Generic.IEnumerable<GitVersion.IBranch!>! excludedBranches) -> System.Collections.Generic.IEnumerable<GitVersion.BranchCommit>!
GitVersion.Common.IRepositoryStore.FindCommitBranchesWasBranchedFrom(GitVersion.IBranch! branch, GitVersion.Configuration.IGitVersionConfiguration! configuration, params GitVersion.IBranch![]! excludedBranches) -> System.Collections.Generic.IEnumerable<GitVersion.BranchCommit>!
GitVersion.Common.IRepositoryStore.FindMainBranch(GitVersion.Configuration.IGitVersionConfiguration! configuration) -> GitVersion.IBranch?
GitVersion.Common.IRepositoryStore.FindMainlineBranches(GitVersion.Configuration.IGitVersionConfiguration! configuration) -> System.Collections.Generic.IEnumerable<GitVersion.IBranch!>!
GitVersion.Common.IRepositoryStore.FindMergeBase(GitVersion.IBranch? branch, GitVersion.IBranch? otherBranch) -> GitVersion.ICommit?
GitVersion.Common.IRepositoryStore.FindMergeBase(GitVersion.ICommit! commit, GitVersion.ICommit! mainlineTip) -> GitVersion.ICommit?
GitVersion.Common.IRepositoryStore.GetBranchesContainingCommit(GitVersion.ICommit! commit, System.Collections.Generic.IEnumerable<GitVersion.IBranch!>? branches = null, bool onlyTrackedBranches = false) -> System.Collections.Generic.IEnumerable<GitVersion.IBranch!>!
GitVersion.Common.IRepositoryStore.GetCommitLog(GitVersion.ICommit? baseVersionSource, GitVersion.ICommit? currentCommit) -> System.Collections.Generic.IEnumerable<GitVersion.ICommit!>!
GitVersion.Common.IRepositoryStore.GetCurrentCommit(GitVersion.IBranch! currentBranch, string? commitId) -> GitVersion.ICommit?
GitVersion.Common.IRepositoryStore.GetCurrentCommitTaggedVersion(GitVersion.ICommit? commit, string? tagPrefix, GitVersion.SemanticVersionFormat format, bool handleDetachedBranch) -> GitVersion.SemanticVersion?
GitVersion.Common.IRepositoryStore.GetMainlineBranches(GitVersion.ICommit! commit, GitVersion.Configuration.IGitVersionConfiguration! configuration) -> System.Collections.Generic.IDictionary<string!, System.Collections.Generic.List<GitVersion.IBranch!>!>!
GitVersion.Common.IRepositoryStore.GetMainlineCommitLog(GitVersion.ICommit? baseVersionSource, GitVersion.ICommit? mainlineTip) -> System.Collections.Generic.IEnumerable<GitVersion.ICommit!>!
GitVersion.Common.IRepositoryStore.GetMergeBaseCommits(GitVersion.ICommit? mergeCommit, GitVersion.ICommit? mergedHead, GitVersion.ICommit? findMergeBase) -> System.Collections.Generic.IEnumerable<GitVersion.ICommit!>!
GitVersion.Common.IRepositoryStore.GetNumberOfUncommittedChanges() -> int
GitVersion.Common.IRepositoryStore.GetReleaseBranches(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, GitVersion.Configuration.IBranchConfiguration!>>! releaseBranchConfig) -> System.Collections.Generic.IEnumerable<GitVersion.IBranch!>!
GitVersion.Common.IRepositoryStore.GetSourceBranches(GitVersion.IBranch! branch, GitVersion.Configuration.IGitVersionConfiguration! configuration, System.Collections.Generic.IEnumerable<GitVersion.IBranch!>! excludedBranches) -> System.Collections.Generic.IEnumerable<GitVersion.IBranch!>!
Expand Down
42 changes: 0 additions & 42 deletions src/GitVersion.Core/VersionCalculation/VariableProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,48 +79,6 @@ public GitVersionVariables GetVariablesFor(
);
}

private static SemanticVersion PromoteNumberOfCommitsToTagNumber(SemanticVersion semanticVersion, string preReleaseTagName)
{
var preReleaseTagNumber = semanticVersion.PreReleaseTag.Number;
long buildMetaDataCommitsSinceVersionSource;
var buildMetaDataCommitsSinceTag = semanticVersion.BuildMetaData.CommitsSinceTag;

// For continuous deployment the commits since tag gets promoted to the pre-release number
if (!semanticVersion.BuildMetaData.CommitsSinceTag.HasValue)
{
preReleaseTagNumber = null;
buildMetaDataCommitsSinceVersionSource = 0;
}
else
{
// Number of commits since last tag should be added to PreRelease number if given. Remember to deduct automatic version bump.
if (preReleaseTagNumber.HasValue)
{
preReleaseTagNumber += semanticVersion.BuildMetaData.CommitsSinceTag - 1;
}
else
{
preReleaseTagNumber = semanticVersion.BuildMetaData.CommitsSinceTag;
}
buildMetaDataCommitsSinceVersionSource = semanticVersion.BuildMetaData.CommitsSinceTag.Value;
buildMetaDataCommitsSinceTag = null; // why is this set to null ?
}

return new(semanticVersion)
{
PreReleaseTag = new(semanticVersion.PreReleaseTag)
{
Name = preReleaseTagName,
Number = preReleaseTagNumber
},
BuildMetaData = new(semanticVersion.BuildMetaData)
{
CommitsSinceVersionSource = buildMetaDataCommitsSinceVersionSource,
CommitsSinceTag = buildMetaDataCommitsSinceTag
}
};
}

private string? CheckAndFormatString<T>(string? formatString, T source, string? defaultValue, string formatVarName)
{
string? formattedString;
Expand Down

0 comments on commit 0bd90f5

Please sign in to comment.