Skip to content

Commit

Permalink
Using effective branch configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
HHobeck committed Feb 23, 2023
1 parent dc14bcf commit e7f8903
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ ignore:
sha: []
mode: ContinuousDelivery
label: '{BranchName}'
increment: Inherit
increment: Patch
prevent-increment-of-merged-branch-version: false
track-merge-target: false
track-merge-message: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ public void DoesNotTakeVersionFromNameOfRemoteReleaseBranchInCustomRemote()

fixture.LocalRepositoryFixture.Checkout("upstream/release/2.0.0");

fixture.LocalRepositoryFixture.AssertFullSemver("0.0.0-beta.1+6");
fixture.LocalRepositoryFixture.AssertFullSemver("0.0.0-beta.1+6"); // This test fails with 2.0.0-beta.1+1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ public void CanTakeVersionFromNameOfConfiguredReleaseBranch(string branchName, s
fixture.Repository.MakeACommit();
fixture.Repository.CreateBranch(branchName);

var configurationBuilder = GitFlowConfigurationBuilder.New
.WithBranch("support", builder => builder.WithIsReleaseBranch(true));
ConfigurationHelper configurationHelper = new(configurationBuilder.Build());
var configuration = GitFlowConfigurationBuilder.New
.WithBranch("support", builder => builder.WithIsReleaseBranch(true))
.Build();
ConfigurationHelper configurationHelper = new(configuration);

var gitRepository = fixture.Repository.ToGitRepository();
var strategy = GetVersionStrategy(fixture.RepositoryPath, gitRepository, branchName, configurationHelper.Dictionary);

var configuration = GitFlowConfigurationBuilder.New.Build();
var branchConfiguration = configuration.GetBranchConfiguration(branchName);
var effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration);

Expand Down
10 changes: 0 additions & 10 deletions src/GitVersion.Core/Configuration/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ public static BranchConfiguration GetBranchConfiguration(this GitVersionConfigur
Label = "{BranchName}",
Increment = IncrementStrategy.Inherit
};

if (branchConfiguration.Increment == IncrementStrategy.Inherit)
return branchConfiguration;

var fallbackBranchConfiguration = GetFallbackBranchConfiguration(configuration);
branchConfiguration.Increment ??= fallbackBranchConfiguration.Increment;
if (branchConfiguration.Increment != IncrementStrategy.Inherit)
{
branchConfiguration = branchConfiguration.Inherit(fallbackBranchConfiguration);
}
return branchConfiguration;
}

Expand Down
43 changes: 23 additions & 20 deletions src/GitVersion.Core/Configuration/EffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ namespace GitVersion.Configuration;
/// </summary>
public class EffectiveConfiguration
{
public EffectiveConfiguration(GitVersionConfiguration configuration, BranchConfiguration currentBranchConfig)
public EffectiveConfiguration(GitVersionConfiguration configuration, BranchConfiguration branchConfiguration)
{
configuration.NotNull();
currentBranchConfig.NotNull();
branchConfiguration.NotNull();

var name = currentBranchConfig.Name;
var fallbackBranchConfiguration = configuration.GetFallbackBranchConfiguration();
branchConfiguration = branchConfiguration.Inherit(fallbackBranchConfiguration);

if (!currentBranchConfig.VersioningMode.HasValue)
var name = branchConfiguration.Name;

if (!branchConfiguration.VersioningMode.HasValue)
throw new Exception($"Configuration value for 'Versioning mode' for branch {name} has no value. (this should not happen, please report an issue)");

if (!currentBranchConfig.Increment.HasValue)
if (!branchConfiguration.Increment.HasValue)
throw new Exception($"Configuration value for 'Increment' for branch {name} has no value. (this should not happen, please report an issue)");

if (!configuration.AssemblyVersioningScheme.HasValue)
Expand All @@ -28,7 +31,7 @@ public EffectiveConfiguration(GitVersionConfiguration configuration, BranchConfi
if (!configuration.AssemblyFileVersioningScheme.HasValue)
throw new Exception("Configuration value for 'AssemblyFileVersioningScheme' has no value. (this should not happen, please report an issue)");

if (!currentBranchConfig.CommitMessageIncrementing.HasValue)
if (!branchConfiguration.CommitMessageIncrementing.HasValue)
throw new Exception("Configuration value for 'CommitMessageIncrementing' has no value. (this should not happen, please report an issue)");

if (!configuration.LabelPreReleaseWeight.HasValue)
Expand All @@ -39,29 +42,29 @@ public EffectiveConfiguration(GitVersionConfiguration configuration, BranchConfi
AssemblyInformationalFormat = configuration.AssemblyInformationalFormat;
AssemblyVersioningFormat = configuration.AssemblyVersioningFormat;
AssemblyFileVersioningFormat = configuration.AssemblyFileVersioningFormat;
VersioningMode = currentBranchConfig.VersioningMode.Value;
VersioningMode = branchConfiguration.VersioningMode.Value;
LabelPrefix = configuration.LabelPrefix;
Label = currentBranchConfig.Label ?? string.Empty;
Label = branchConfiguration.Label ?? string.Empty;
NextVersion = configuration.NextVersion;
Increment = currentBranchConfig.Increment.Value;
BranchPrefixToTrim = currentBranchConfig.Regex;
PreventIncrementOfMergedBranchVersion = currentBranchConfig.PreventIncrementOfMergedBranchVersion ?? false;
LabelNumberPattern = currentBranchConfig.LabelNumberPattern;
TrackMergeTarget = currentBranchConfig.TrackMergeTarget ?? false;
TrackMergeMessage = currentBranchConfig.TrackMergeMessage ?? true;
Increment = branchConfiguration.Increment.Value;
BranchPrefixToTrim = branchConfiguration.Regex;
PreventIncrementOfMergedBranchVersion = branchConfiguration.PreventIncrementOfMergedBranchVersion ?? false;
LabelNumberPattern = branchConfiguration.LabelNumberPattern;
TrackMergeTarget = branchConfiguration.TrackMergeTarget ?? false;
TrackMergeMessage = branchConfiguration.TrackMergeMessage ?? true;
MajorVersionBumpMessage = configuration.MajorVersionBumpMessage;
MinorVersionBumpMessage = configuration.MinorVersionBumpMessage;
PatchVersionBumpMessage = configuration.PatchVersionBumpMessage;
NoBumpMessage = configuration.NoBumpMessage;
CommitMessageIncrementing = currentBranchConfig.CommitMessageIncrementing.Value;
CommitMessageIncrementing = branchConfiguration.CommitMessageIncrementing.Value;
VersionFilters = configuration.Ignore.ToFilters();
TracksReleaseBranches = currentBranchConfig.TracksReleaseBranches ?? false;
IsReleaseBranch = currentBranchConfig.IsReleaseBranch ?? false;
IsMainline = currentBranchConfig.IsMainline ?? false;
TracksReleaseBranches = branchConfiguration.TracksReleaseBranches ?? false;
IsReleaseBranch = branchConfiguration.IsReleaseBranch ?? false;
IsMainline = branchConfiguration.IsMainline ?? false;
CommitDateFormat = configuration.CommitDateFormat;
UpdateBuildNumber = configuration.UpdateBuildNumber;
SemanticVersionFormat = configuration.SemanticVersionFormat;
PreReleaseWeight = currentBranchConfig.PreReleaseWeight ?? 0;
PreReleaseWeight = branchConfiguration.PreReleaseWeight ?? 0;
LabelPreReleaseWeight = configuration.LabelPreReleaseWeight.Value;
}

Expand Down Expand Up @@ -174,7 +177,7 @@ protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningSche

public bool UpdateBuildNumber { get; }

public SemanticVersionFormat SemanticVersionFormat { get; set; } = SemanticVersionFormat.Strict;
public SemanticVersionFormat SemanticVersionFormat { get; set; }

public int PreReleaseWeight { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private GitFlowConfigurationBuilder()
VersioningMode = VersioningMode.ContinuousDelivery,
Regex = string.Empty,
Label = "{BranchName}",
Increment = IncrementStrategy.Inherit,
Increment = IncrementStrategy.Patch,
CommitMessageIncrementing = CommitMessageIncrementMode.Enabled,
PreventIncrementOfMergedBranchVersion = false,
TrackMergeTarget = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private GitHubFlowConfigurationBuilder()
VersioningMode = VersioningMode.ContinuousDelivery,
Regex = string.Empty,
Label = "{BranchName}",
Increment = IncrementStrategy.Inherit,
Increment = IncrementStrategy.Patch,
CommitMessageIncrementing = CommitMessageIncrementMode.Enabled,
PreventIncrementOfMergedBranchVersion = false,
TrackMergeTarget = false,
Expand Down
6 changes: 6 additions & 0 deletions src/GitVersion.Core/GitVersionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ public EffectiveConfiguration GetEffectiveConfiguration(IBranch branch)
BranchConfiguration branchConfiguration = Configuration.GetBranchConfiguration(branch);
return new EffectiveConfiguration(Configuration, branchConfiguration);
}

public EffectiveConfiguration GetEffectiveConfiguration(string branchName)
{
BranchConfiguration branchConfiguration = Configuration.GetBranchConfiguration(branchName);
return new EffectiveConfiguration(Configuration, branchConfiguration);
}
}
3 changes: 2 additions & 1 deletion src/GitVersion.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ GitVersion.Configuration.EffectiveConfiguration.AssemblyVersioningScheme.get ->
GitVersion.Configuration.EffectiveConfiguration.BranchPrefixToTrim.get -> string?
GitVersion.Configuration.EffectiveConfiguration.CommitDateFormat.get -> string?
GitVersion.Configuration.EffectiveConfiguration.CommitMessageIncrementing.get -> GitVersion.VersionCalculation.CommitMessageIncrementMode
GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(GitVersion.Configuration.GitVersionConfiguration! configuration, GitVersion.Configuration.BranchConfiguration! currentBranchConfig) -> void
GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(GitVersion.Configuration.GitVersionConfiguration! configuration, GitVersion.Configuration.BranchConfiguration! branchConfiguration) -> void
GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(GitVersion.Extensions.AssemblyVersioningScheme assemblyVersioningScheme, GitVersion.Extensions.AssemblyFileVersioningScheme assemblyFileVersioningScheme, string? assemblyInformationalFormat, string? assemblyVersioningFormat, string? assemblyFileVersioningFormat, GitVersion.VersionCalculation.VersioningMode versioningMode, string? labelPrefix, string! label, string? nextVersion, GitVersion.IncrementStrategy increment, string? branchPrefixToTrim, bool preventIncrementOfMergedBranchVersion, string? labelNumberPattern, bool trackMergeTarget, string? majorVersionBumpMessage, string? minorVersionBumpMessage, string? patchVersionBumpMessage, string? noBumpMessage, GitVersion.VersionCalculation.CommitMessageIncrementMode commitMessageIncrementing, System.Collections.Generic.IEnumerable<GitVersion.VersionCalculation.IVersionFilter!>! versionFilters, bool tracksReleaseBranches, bool isReleaseBranch, bool isMainline, string? commitDateFormat, bool updateBuildNumber, GitVersion.SemanticVersionFormat semanticVersionFormat, int preReleaseWeight, int labelPreReleaseWeight) -> void
GitVersion.Configuration.EffectiveConfiguration.Increment.get -> GitVersion.IncrementStrategy
GitVersion.Configuration.EffectiveConfiguration.IsMainline.get -> bool
Expand Down Expand Up @@ -356,6 +356,7 @@ GitVersion.GitVersionContext.CurrentBranch.get -> GitVersion.IBranch!
GitVersion.GitVersionContext.CurrentCommit.get -> GitVersion.ICommit?
GitVersion.GitVersionContext.CurrentCommitTaggedVersion.get -> GitVersion.SemanticVersion?
GitVersion.GitVersionContext.GetEffectiveConfiguration(GitVersion.IBranch! branch) -> GitVersion.Configuration.EffectiveConfiguration!
GitVersion.GitVersionContext.GetEffectiveConfiguration(string! branchName) -> GitVersion.Configuration.EffectiveConfiguration!
GitVersion.GitVersionContext.GitVersionContext(GitVersion.IBranch! currentBranch, GitVersion.ICommit? currentCommit, GitVersion.Configuration.GitVersionConfiguration! configuration, GitVersion.SemanticVersion? currentCommitTaggedVersion, int numberOfUncommittedChanges) -> void
GitVersion.GitVersionContext.IsCurrentCommitTagged.get -> bool
GitVersion.GitVersionContext.NumberOfUncommittedChanges.get -> int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ public VersionInBranchNameVersionStrategy(IRepositoryStore repositoryStore, Lazy

public override IEnumerable<BaseVersion> GetBaseVersions(EffectiveBranchConfiguration configuration)
{
string nameWithoutOrigin = NameWithoutOrigin(configuration.Branch);
var contextConfiguration = Context.Configuration;
if (contextConfiguration.IsReleaseBranch(nameWithoutOrigin))
if (!configuration.Value.IsReleaseBranch) yield break;

var versionInBranch = GetVersionInBranch(
configuration.Branch.Name.Friendly, configuration.Value.LabelPrefix, configuration.Value.SemanticVersionFormat
);
if (versionInBranch != null)
{
var versionInBranch = GetVersionInBranch(configuration.Branch.Name.Friendly, contextConfiguration.LabelPrefix, contextConfiguration.SemanticVersionFormat);
if (versionInBranch != null)
{
var commitBranchWasBranchedFrom = this.repositoryStore.FindCommitBranchWasBranchedFrom(configuration.Branch, contextConfiguration);
var branchNameOverride = Context.CurrentBranch.Name.Friendly.RegexReplace("[-/]" + versionInBranch.Item1, string.Empty);
yield return new BaseVersion("Version in branch name", false, versionInBranch.Item2, commitBranchWasBranchedFrom.Commit, branchNameOverride);
}
var commitBranchWasBranchedFrom = this.repositoryStore.FindCommitBranchWasBranchedFrom(
configuration.Branch, Context.Configuration
);
var branchNameOverride = Context.CurrentBranch.Name.Friendly.RegexReplace("[-/]" + versionInBranch.Item1, string.Empty);
yield return new BaseVersion("Version in branch name", false, versionInBranch.Item2, commitBranchWasBranchedFrom.Commit, branchNameOverride);
}
}

Expand All @@ -45,8 +46,4 @@ public override IEnumerable<BaseVersion> GetBaseVersions(EffectiveBranchConfigur

return null;
}

private static string NameWithoutOrigin(IBranch branch) => branch.IsRemote && branch.Name.Friendly.StartsWith("origin/")
? branch.Name.Friendly.Substring("origin/".Length)
: branch.Name.Friendly;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ private IEnumerable<EffectiveBranchConfiguration> GetEffectiveConfigurationsRecu
{
// Because the actual branch is marked with the inherit increment strategy we need to either skip the iteration or go further
// while inheriting from the fallback branch configuration. This behavior is configurable via the increment settings of the configuration.
var fallbackBranchConfiguration = configuration.GetFallbackBranchConfiguration();
var skipTraversingOfOrphanedBranches = fallbackBranchConfiguration.Increment == null;
var skipTraversingOfOrphanedBranches = configuration.Increment == null;
this.log.Info(
$"An orphaned branch '{branch}' has been detected and will be skipped={skipTraversingOfOrphanedBranches}."
);
Expand All @@ -67,8 +66,6 @@ in GetEffectiveConfigurationsRecursive(sourceBranch, configuration, branchConfig
}
else
{
var fallbackBranchConfiguration = configuration.GetFallbackBranchConfiguration();
branchConfiguration = branchConfiguration.Inherit(fallbackBranchConfiguration);
yield return new(branch, new EffectiveConfiguration(configuration, branchConfiguration));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,12 @@ private IBranch FindMainlineBranch(List<IBranch> possibleMainlineBranches, IComm
return (null, null);
}

private SemanticVersion IncrementForEachCommit(IEnumerable<ICommit> directCommits, SemanticVersion mainlineVersion, INamedReference mainline)
private SemanticVersion IncrementForEachCommit(IEnumerable<ICommit> directCommits, SemanticVersion mainlineVersion, IBranch mainline)
{
foreach (var directCommit in directCommits)
{
var directCommitIncrement = this.incrementStrategyFinder.GetIncrementForCommits(context.Configuration, new[] { directCommit })
?? FindDefaultIncrementForBranch(context, mainline.Name.Friendly);
?? FindDefaultIncrementForBranch(context, mainline);
mainlineVersion = mainlineVersion.IncrementVersion(directCommitIncrement);
this.log.Info($"Direct commit on main {directCommit} incremented base versions {directCommitIncrement}, now {mainlineVersion}");
}
Expand Down Expand Up @@ -310,20 +310,11 @@ private VersionField TryFindIncrementFromMergeMessage(ICommit? mergeCommit)
return FindDefaultIncrementForBranch(context);
}

private static VersionField FindDefaultIncrementForBranch(GitVersionContext context, string? branchName = null)
{
var configuration = context.Configuration.GetBranchConfiguration(branchName ?? context.CurrentBranch.Name.WithoutRemote);
if (configuration.Increment != null && configuration.Increment != IncrementStrategy.Inherit)
{
return configuration.Increment.Value.ToVersionField();
}
private static VersionField FindDefaultIncrementForBranch(GitVersionContext context)
=> FindDefaultIncrementForBranch(context, context.CurrentBranch);

// TODO: Hardcoded fallback values are not so good. It might be better to get this information either from the fallback or the unknown
// branch configuration settings I have introduced. We should think about it: This is a cooking machine... the ingredients are coming from the user. ;)

// Fallback to patch
return VersionField.Patch;
}
private static VersionField FindDefaultIncrementForBranch(GitVersionContext context, IBranch branch)
=> context.GetEffectiveConfiguration(branch).Increment.ToVersionField();

private static ICommit GetMergedHead(ICommit mergeCommit)
{
Expand Down

0 comments on commit e7f8903

Please sign in to comment.