From 8cb266549924f067d24cb788479dd5f416712ae3 Mon Sep 17 00:00:00 2001 From: Hardy Hobeck Date: Thu, 23 Feb 2023 17:09:30 +0100 Subject: [PATCH 1/6] Using effective branch configuration --- global.json | 2 +- ...riteOutEffectiveConfiguration.approved.txt | 2 +- .../VersionInCurrentBranchNameScenarios.cs | 2 +- ...ionInBranchNameBaseVersionStrategyTests.cs | 8 ++-- .../Configuration/ConfigurationExtensions.cs | 15 ++----- .../Configuration/EffectiveConfiguration.cs | 43 ++++++++++--------- src/GitVersion.Core/PublicAPI.Unshipped.txt | 3 +- .../VersionInBranchNameVersionStrategy.cs | 25 +++++------ .../EffectiveBranchConfigurationFinder.cs | 5 +-- .../MainlineVersionCalculator.cs | 21 +++------ 10 files changed, 54 insertions(+), 72 deletions(-) diff --git a/global.json b/global.json index ad12120c01..aa2aca45d2 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "7.0.103" + "version": "7.0.200" } } \ No newline at end of file diff --git a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt index 31e89185aa..a864d80d99 100644 --- a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt +++ b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt @@ -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 diff --git a/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs index c3102ca145..d19aaa6cfc 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs @@ -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 } } diff --git a/src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs b/src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs index 42712fbaee..0994fba50a 100644 --- a/src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs +++ b/src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs @@ -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); diff --git a/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs b/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs index 2c3fb21fff..3ce54c904e 100644 --- a/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs +++ b/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs @@ -7,8 +7,11 @@ namespace GitVersion.Configuration; public static class ConfigurationExtensions { public static EffectiveConfiguration GetEffectiveConfiguration(this GitVersionConfiguration configuration, IBranch branch) + => GetEffectiveConfiguration(configuration, branch.NotNull().Name.WithoutRemote); + + public static EffectiveConfiguration GetEffectiveConfiguration(this GitVersionConfiguration configuration, string branchName) { - var branchConfiguration = configuration.GetBranchConfiguration(branch); + BranchConfiguration branchConfiguration = configuration.GetBranchConfiguration(branchName); return new EffectiveConfiguration(configuration, branchConfiguration); } @@ -25,16 +28,6 @@ public static BranchConfiguration GetBranchConfiguration(this GitVersionConfigur Label = ConfigurationConstants.BranchNamePlaceholder, 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; } diff --git a/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs b/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs index 3b6811d773..bcc37e3549 100644 --- a/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs +++ b/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs @@ -9,17 +9,20 @@ namespace GitVersion.Configuration; /// 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) @@ -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) @@ -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; } @@ -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; } diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index 5b4c010acc..25efaf661a 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -164,7 +164,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! 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 @@ -1063,6 +1063,7 @@ static GitVersion.Configuration.ConfigurationExtensions.GetBranchConfiguration(t static GitVersion.Configuration.ConfigurationExtensions.GetBranchConfiguration(this GitVersion.Configuration.GitVersionConfiguration! configuration, string! branchName) -> GitVersion.Configuration.BranchConfiguration! static GitVersion.Configuration.ConfigurationExtensions.GetBranchSpecificTag(this GitVersion.Configuration.EffectiveConfiguration! configuration, GitVersion.Logging.ILog! log, string? branchFriendlyName, string? branchNameOverride) -> string! static GitVersion.Configuration.ConfigurationExtensions.GetEffectiveConfiguration(this GitVersion.Configuration.GitVersionConfiguration! configuration, GitVersion.IBranch! branch) -> GitVersion.Configuration.EffectiveConfiguration! +static GitVersion.Configuration.ConfigurationExtensions.GetEffectiveConfiguration(this GitVersion.Configuration.GitVersionConfiguration! configuration, string! branchName) -> GitVersion.Configuration.EffectiveConfiguration! static GitVersion.Configuration.ConfigurationExtensions.GetFallbackBranchConfiguration(this GitVersion.Configuration.GitVersionConfiguration! configuration) -> GitVersion.Configuration.BranchConfiguration! static GitVersion.Configuration.ConfigurationExtensions.GetReleaseBranchConfiguration(this GitVersion.Configuration.GitVersionConfiguration! configuration) -> System.Collections.Generic.List>! static GitVersion.Configuration.ConfigurationExtensions.IsReleaseBranch(this GitVersion.Configuration.GitVersionConfiguration! configuration, string! branchName) -> bool diff --git a/src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/VersionInBranchNameVersionStrategy.cs b/src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/VersionInBranchNameVersionStrategy.cs index de9c7147ed..137e7accc2 100644 --- a/src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/VersionInBranchNameVersionStrategy.cs +++ b/src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/VersionInBranchNameVersionStrategy.cs @@ -18,17 +18,18 @@ public VersionInBranchNameVersionStrategy(IRepositoryStore repositoryStore, Lazy public override IEnumerable 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); } } @@ -45,8 +46,4 @@ public override IEnumerable 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; } diff --git a/src/GitVersion.Core/VersionCalculation/EffectiveBranchConfigurationFinder.cs b/src/GitVersion.Core/VersionCalculation/EffectiveBranchConfigurationFinder.cs index c528811d83..37af488151 100644 --- a/src/GitVersion.Core/VersionCalculation/EffectiveBranchConfigurationFinder.cs +++ b/src/GitVersion.Core/VersionCalculation/EffectiveBranchConfigurationFinder.cs @@ -45,8 +45,7 @@ private IEnumerable 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}." ); @@ -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)); } } diff --git a/src/GitVersion.Core/VersionCalculation/MainlineVersionCalculator.cs b/src/GitVersion.Core/VersionCalculation/MainlineVersionCalculator.cs index 82941635d3..16de07d847 100644 --- a/src/GitVersion.Core/VersionCalculation/MainlineVersionCalculator.cs +++ b/src/GitVersion.Core/VersionCalculation/MainlineVersionCalculator.cs @@ -273,12 +273,12 @@ private IBranch FindMainlineBranch(List possibleMainlineBranches, IComm return (null, null); } - private SemanticVersion IncrementForEachCommit(IEnumerable directCommits, SemanticVersion mainlineVersion, INamedReference mainline) + private SemanticVersion IncrementForEachCommit(IEnumerable 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}"); } @@ -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.Configuration.GetEffectiveConfiguration(branch).Increment.ToVersionField(); private static ICommit GetMergedHead(ICommit mergeCommit) { From d5f980fce4176f90aaeec27742358624c97864a8 Mon Sep 17 00:00:00 2001 From: Hardy Hobeck Date: Fri, 24 Feb 2023 14:50:01 +0100 Subject: [PATCH 2/6] Insert option to considered the remote name when generating a semantic version on a remote branch. --- docs/input/docs/reference/configuration.md | 9 ++++++++- ...riteOutEffectiveConfiguration.approved.txt | 1 + .../Helpers/TestEffectiveConfiguration.cs | 4 +++- .../VersionInCurrentBranchNameScenarios.cs | 20 ++++++++++++++----- .../Configuration/ConfigurationBuilderBase.cs | 10 ++++++++++ .../Configuration/EffectiveConfiguration.cs | 6 +++++- .../GitFlowConfigurationBuilder.cs | 1 + .../GitHubFlowConfigurationBuilder.cs | 1 + .../Configuration/GitVersionConfiguration.cs | 4 ---- .../Core/GitVersionContextFactory.cs | 16 ++++++++++----- src/GitVersion.Core/PublicAPI.Unshipped.txt | 7 +++++-- .../Output/AssemblyInfoFileUpdaterTests.cs | 2 +- .../Output/ProjectFileUpdaterTests.cs | 2 +- 13 files changed, 62 insertions(+), 21 deletions(-) diff --git a/docs/input/docs/reference/configuration.md b/docs/input/docs/reference/configuration.md index 73c77e610f..2bb335af3c 100644 --- a/docs/input/docs/reference/configuration.md +++ b/docs/input/docs/reference/configuration.md @@ -40,6 +40,7 @@ created. Modify this to suit your needs. The global configuration looks like this: ```yaml +remote-name-in-git: origin assembly-versioning-scheme: MajorMinorPatch assembly-file-versioning-scheme: MajorMinorPatch label-prefix: '[vV]?' @@ -160,7 +161,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 @@ -173,6 +174,12 @@ is-mainline: false The details of the available options are as follows: +### remote-name-in-git + +The user can define the name of the remote (in moste cases it's `origin`) which +will be considered when generating a semantic version on a remote branch. If the +actual remote name doesn't match this pattern an error will be thrown. + ### next-version Allows you to bump the next version explicitly. Useful for bumping `main` or a diff --git a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt index a864d80d99..2eb69f730c 100644 --- a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt +++ b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt @@ -1,3 +1,4 @@ +remote-name-in-git: origin assembly-versioning-scheme: MajorMinorPatch assembly-file-versioning-scheme: MajorMinorPatch label-prefix: '[vV]?' diff --git a/src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs b/src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs index efb2ebe568..50db90a962 100644 --- a/src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs +++ b/src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs @@ -7,6 +7,7 @@ namespace GitVersion.Core.Tests.Helpers; public class TestEffectiveConfiguration : EffectiveConfiguration { public TestEffectiveConfiguration( + string remoteNameInGit = GitVersionConfiguration.DefaultRemoteNameInGit, AssemblyVersioningScheme assemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch, AssemblyFileVersioningScheme assemblyFileVersioningScheme = AssemblyFileVersioningScheme.MajorMinorPatch, string? assemblyVersioningFormat = null, @@ -31,7 +32,8 @@ public TestEffectiveConfiguration( bool isMainline = false, string commitDateFormat = "yyyy-MM-dd", bool updateBuildNumber = false) : - base(assemblyVersioningScheme, + base(remoteNameInGit, + assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyVersioningFormat, diff --git a/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs index d19aaa6cfc..b607908983 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs @@ -38,17 +38,26 @@ public void TakesVersionFromNameOfBranchThatIsReleaseByConfig() fixture.AssertFullSemver("2.0.0+1", configuration); } - [Test] - public void TakesVersionFromNameOfRemoteReleaseBranchInOrigin() + [TestCase("origin")] + [TestCase("upstream")] + public void TakesVersionFromNameOfRemoteReleaseBranchInOrigin(string remoteNameInGit) { + var configuration = GitFlowConfigurationBuilder.New + .WithRemoteNameInGit(remoteNameInGit) + .Build(); + using var fixture = new RemoteRepositoryFixture(); + if (remoteNameInGit != "origin") + { + fixture.LocalRepositoryFixture.Repository.Network.Remotes.Rename("origin", remoteNameInGit); + } fixture.BranchTo("release/2.0.0"); fixture.MakeACommit(); Commands.Fetch((Repository)fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, Array.Empty(), new FetchOptions(), null); - fixture.LocalRepositoryFixture.Checkout("origin/release/2.0.0"); + fixture.LocalRepositoryFixture.Checkout($"{remoteNameInGit}/release/2.0.0"); - fixture.LocalRepositoryFixture.AssertFullSemver("2.0.0-beta.1+1"); + fixture.LocalRepositoryFixture.AssertFullSemver("2.0.0-beta.1+1", configuration); } [Test] @@ -61,7 +70,8 @@ public void DoesNotTakeVersionFromNameOfRemoteReleaseBranchInCustomRemote() Commands.Fetch((Repository)fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, Array.Empty(), new FetchOptions(), null); fixture.LocalRepositoryFixture.Checkout("upstream/release/2.0.0"); + Action action = () => fixture.LocalRepositoryFixture.GetVersion(); - fixture.LocalRepositoryFixture.AssertFullSemver("0.0.0-beta.1+6"); // This test fails with 2.0.0-beta.1+1 + action.ShouldThrow(); } } diff --git a/src/GitVersion.Core/Configuration/ConfigurationBuilderBase.cs b/src/GitVersion.Core/Configuration/ConfigurationBuilderBase.cs index d4a894f093..f104483295 100644 --- a/src/GitVersion.Core/Configuration/ConfigurationBuilderBase.cs +++ b/src/GitVersion.Core/Configuration/ConfigurationBuilderBase.cs @@ -6,6 +6,7 @@ namespace GitVersion.Configuration; internal abstract class ConfigurationBuilderBase where TConfigurationBuilder : ConfigurationBuilderBase { + private string? remoteNameInGit; private AssemblyVersioningScheme? assemblyVersioningScheme; private AssemblyFileVersioningScheme? assemblyFileVersioningScheme; private string? assemblyInformationalFormat; @@ -97,6 +98,13 @@ protected ConfigurationBuilderBase() } } + public virtual TConfigurationBuilder WithRemoteNameInGit(string? value) + { + this.remoteNameInGit = value; + return (TConfigurationBuilder)this; + } + + public virtual TConfigurationBuilder WithAssemblyVersioningScheme(AssemblyVersioningScheme? value) { this.assemblyVersioningScheme = value; @@ -324,6 +332,7 @@ public virtual TConfigurationBuilder WithPreReleaseWeight(int? value) public virtual TConfigurationBuilder WithConfiguration(GitVersionConfiguration value) { + WithRemoteNameInGit(value.RemoteNameInGit); WithAssemblyVersioningScheme(value.AssemblyVersioningScheme); WithAssemblyFileVersioningScheme(value.AssemblyFileVersioningScheme); WithAssemblyInformationalFormat(value.AssemblyInformationalFormat); @@ -376,6 +385,7 @@ public virtual GitVersionConfiguration Build() { GitVersionConfiguration configuration = new() { + RemoteNameInGit = this.remoteNameInGit, AssemblyVersioningScheme = this.assemblyVersioningScheme, AssemblyFileVersioningScheme = this.assemblyFileVersioningScheme, AssemblyInformationalFormat = this.assemblyInformationalFormat, diff --git a/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs b/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs index bcc37e3549..54e97dce24 100644 --- a/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs +++ b/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs @@ -37,6 +37,7 @@ public EffectiveConfiguration(GitVersionConfiguration configuration, BranchConfi if (!configuration.LabelPreReleaseWeight.HasValue) throw new Exception("Configuration value for 'LabelPreReleaseWeight' has no value. (this should not happen, please report an issue)"); + RemoteNameInGit = configuration.RemoteNameInGit; AssemblyVersioningScheme = configuration.AssemblyVersioningScheme.Value; AssemblyFileVersioningScheme = configuration.AssemblyFileVersioningScheme.Value; AssemblyInformationalFormat = configuration.AssemblyInformationalFormat; @@ -68,7 +69,8 @@ public EffectiveConfiguration(GitVersionConfiguration configuration, BranchConfi LabelPreReleaseWeight = configuration.LabelPreReleaseWeight.Value; } - protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningScheme, + protected EffectiveConfiguration(string remoteNameInGit, + AssemblyVersioningScheme assemblyVersioningScheme, AssemblyFileVersioningScheme assemblyFileVersioningScheme, string? assemblyInformationalFormat, string? assemblyVersioningFormat, @@ -97,6 +99,7 @@ protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningSche int preReleaseWeight, int labelPreReleaseWeight) { + RemoteNameInGit = remoteNameInGit; AssemblyVersioningScheme = assemblyVersioningScheme; AssemblyFileVersioningScheme = assemblyFileVersioningScheme; AssemblyInformationalFormat = assemblyInformationalFormat; @@ -127,6 +130,7 @@ protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningSche LabelPreReleaseWeight = labelPreReleaseWeight; } + public string? RemoteNameInGit { get; } public bool TracksReleaseBranches { get; } public bool IsReleaseBranch { get; } public bool IsMainline { get; } diff --git a/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs b/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs index 523bc0b819..56e4ba4735 100644 --- a/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs +++ b/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs @@ -11,6 +11,7 @@ private GitFlowConfigurationBuilder() { WithConfiguration(new() { + RemoteNameInGit = GitVersionConfiguration.DefaultRemoteNameInGit, AssemblyFileVersioningScheme = AssemblyFileVersioningScheme.MajorMinorPatch, AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch, CommitDateFormat = "yyyy-MM-dd", diff --git a/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs b/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs index 96a6a8a3f0..e060d39936 100644 --- a/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs +++ b/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs @@ -11,6 +11,7 @@ private GitHubFlowConfigurationBuilder() { WithConfiguration(new() { + RemoteNameInGit = GitVersionConfiguration.DefaultRemoteNameInGit, AssemblyFileVersioningScheme = AssemblyFileVersioningScheme.MajorMinorPatch, AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch, CommitDateFormat = "yyyy-MM-dd", diff --git a/src/GitVersion.Core/Configuration/GitVersionConfiguration.cs b/src/GitVersion.Core/Configuration/GitVersionConfiguration.cs index d4530a586b..131bb49534 100644 --- a/src/GitVersion.Core/Configuration/GitVersionConfiguration.cs +++ b/src/GitVersion.Core/Configuration/GitVersionConfiguration.cs @@ -1,7 +1,3 @@ -using System.Globalization; -using GitVersion.Attributes; -using GitVersion.Extensions; - namespace GitVersion.Configuration; public class GitVersionConfiguration : BranchConfiguration diff --git a/src/GitVersion.Core/Core/GitVersionContextFactory.cs b/src/GitVersion.Core/Core/GitVersionContextFactory.cs index b37658f95c..e50492b3a5 100644 --- a/src/GitVersion.Core/Core/GitVersionContextFactory.cs +++ b/src/GitVersion.Core/Core/GitVersionContextFactory.cs @@ -1,8 +1,3 @@ -using GitVersion.Common; -using GitVersion.Configuration; -using GitVersion.Extensions; -using Microsoft.Extensions.Options; - namespace GitVersion; public class GitVersionContextFactory : IGitVersionContextFactory @@ -31,6 +26,17 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions) currentBranch = branchForCommit ?? currentBranch; } + if (currentBranch.IsRemote) + { + var remoteNameInGit = configuration.RemoteNameInGit; + if (string.IsNullOrEmpty(remoteNameInGit) || !currentBranch.Name.Friendly.StartsWith(remoteNameInGit)) + { + throw new InvalidOperationException( + $"The remote branch name '{currentBranch.Name.Friendly}' is not valid. Please use another branch or change the configuration." + ); + } + } + var currentCommitTaggedVersion = this.repositoryStore.GetCurrentCommitTaggedVersion(currentCommit, configuration.LabelPrefix, configuration.SemanticVersionFormat, handleDetachedBranch: currentBranch.IsDetachedHead); var numberOfUncommittedChanges = this.repositoryStore.GetNumberOfUncommittedChanges(); diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index 25efaf661a..ae93da3d6e 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -165,7 +165,7 @@ 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! 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! versionFilters, bool tracksReleaseBranches, bool isReleaseBranch, bool isMainline, string? commitDateFormat, bool updateBuildNumber, GitVersion.SemanticVersionFormat semanticVersionFormat, int preReleaseWeight, int labelPreReleaseWeight) -> void +GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(string! remoteNameInGit, 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! 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 GitVersion.Configuration.EffectiveConfiguration.IsReleaseBranch.get -> bool @@ -180,6 +180,7 @@ GitVersion.Configuration.EffectiveConfiguration.NoBumpMessage.get -> string? GitVersion.Configuration.EffectiveConfiguration.PatchVersionBumpMessage.get -> string? GitVersion.Configuration.EffectiveConfiguration.PreReleaseWeight.get -> int GitVersion.Configuration.EffectiveConfiguration.PreventIncrementOfMergedBranchVersion.get -> bool +GitVersion.Configuration.EffectiveConfiguration.RemoteNameInGit.get -> string? GitVersion.Configuration.EffectiveConfiguration.SemanticVersionFormat.get -> GitVersion.SemanticVersionFormat GitVersion.Configuration.EffectiveConfiguration.SemanticVersionFormat.set -> void GitVersion.Configuration.EffectiveConfiguration.TrackMergeMessage.get -> bool @@ -206,9 +207,9 @@ GitVersion.Configuration.GitVersionConfiguration.CommitDateFormat.set -> void GitVersion.Configuration.GitVersionConfiguration.GitVersionConfiguration() -> void GitVersion.Configuration.GitVersionConfiguration.Ignore.get -> GitVersion.Configuration.IgnoreConfiguration! GitVersion.Configuration.GitVersionConfiguration.Ignore.set -> void +GitVersion.Configuration.GitVersionConfiguration.LabelPrefix.get -> string? GitVersion.Configuration.GitVersionConfiguration.LabelPreReleaseWeight.get -> int? GitVersion.Configuration.GitVersionConfiguration.LabelPreReleaseWeight.set -> void -GitVersion.Configuration.GitVersionConfiguration.LabelPrefix.get -> string? GitVersion.Configuration.GitVersionConfiguration.LabelPrefix.set -> void GitVersion.Configuration.GitVersionConfiguration.MajorVersionBumpMessage.get -> string? GitVersion.Configuration.GitVersionConfiguration.MajorVersionBumpMessage.set -> void @@ -222,6 +223,8 @@ GitVersion.Configuration.GitVersionConfiguration.NoBumpMessage.get -> string? GitVersion.Configuration.GitVersionConfiguration.NoBumpMessage.set -> void GitVersion.Configuration.GitVersionConfiguration.PatchVersionBumpMessage.get -> string? GitVersion.Configuration.GitVersionConfiguration.PatchVersionBumpMessage.set -> void +GitVersion.Configuration.GitVersionConfiguration.RemoteNameInGit.get -> string? +GitVersion.Configuration.GitVersionConfiguration.RemoteNameInGit.set -> void GitVersion.Configuration.GitVersionConfiguration.SemanticVersionFormat.get -> GitVersion.SemanticVersionFormat GitVersion.Configuration.GitVersionConfiguration.SemanticVersionFormat.set -> void GitVersion.Configuration.GitVersionConfiguration.UpdateBuildNumber.get -> bool diff --git a/src/GitVersion.Output.Tests/Output/AssemblyInfoFileUpdaterTests.cs b/src/GitVersion.Output.Tests/Output/AssemblyInfoFileUpdaterTests.cs index 2af8d14bb0..88f07fa95b 100644 --- a/src/GitVersion.Output.Tests/Output/AssemblyInfoFileUpdaterTests.cs +++ b/src/GitVersion.Output.Tests/Output/AssemblyInfoFileUpdaterTests.cs @@ -442,7 +442,7 @@ private void VerifyAssemblyInfoFile( this.fileSystem.ReadAllText(fileName).Returns(assemblyFileContent); }); - var configuration = new TestEffectiveConfiguration(versioningScheme); + var configuration = new TestEffectiveConfiguration(assemblyVersioningScheme: versioningScheme); var variables = this.variableProvider.GetVariablesFor(version, configuration, false); verify?.Invoke(this.fileSystem, variables); diff --git a/src/GitVersion.Output.Tests/Output/ProjectFileUpdaterTests.cs b/src/GitVersion.Output.Tests/Output/ProjectFileUpdaterTests.cs index 02a40684ef..f77570c43b 100644 --- a/src/GitVersion.Output.Tests/Output/ProjectFileUpdaterTests.cs +++ b/src/GitVersion.Output.Tests/Output/ProjectFileUpdaterTests.cs @@ -300,7 +300,7 @@ private void VerifyAssemblyInfoFile( this.fileSystem.ReadAllText(fileName).Returns(projectFileContent); }); - var configuration = new TestEffectiveConfiguration(versioningScheme); + var configuration = new TestEffectiveConfiguration(assemblyVersioningScheme: versioningScheme); var variables = this.variableProvider.GetVariablesFor(version, configuration, false); verify?.Invoke(this.fileSystem, variables); From 198a6577b222a6205a074ad1fc01509c9a276151 Mon Sep 17 00:00:00 2001 From: Hardy Hobeck Date: Sun, 26 Feb 2023 16:14:16 +0100 Subject: [PATCH 3/6] Revert "Insert option to considered the remote name when generating a semantic version on a remote branch." This reverts commit 3beab1ecf1feb01e6da651615c6a13c297d1ed62. --- docs/input/docs/reference/configuration.md | 9 +-------- ...riteOutEffectiveConfiguration.approved.txt | 1 - .../Helpers/TestEffectiveConfiguration.cs | 4 +--- .../VersionInCurrentBranchNameScenarios.cs | 20 +++++-------------- .../Configuration/ConfigurationBuilderBase.cs | 10 ---------- .../Configuration/EffectiveConfiguration.cs | 6 +----- .../GitFlowConfigurationBuilder.cs | 1 - .../GitHubFlowConfigurationBuilder.cs | 1 - .../Core/GitVersionContextFactory.cs | 11 ---------- src/GitVersion.Core/PublicAPI.Unshipped.txt | 7 ++----- .../Output/AssemblyInfoFileUpdaterTests.cs | 2 +- .../Output/ProjectFileUpdaterTests.cs | 2 +- 12 files changed, 12 insertions(+), 62 deletions(-) diff --git a/docs/input/docs/reference/configuration.md b/docs/input/docs/reference/configuration.md index 2bb335af3c..73c77e610f 100644 --- a/docs/input/docs/reference/configuration.md +++ b/docs/input/docs/reference/configuration.md @@ -40,7 +40,6 @@ created. Modify this to suit your needs. The global configuration looks like this: ```yaml -remote-name-in-git: origin assembly-versioning-scheme: MajorMinorPatch assembly-file-versioning-scheme: MajorMinorPatch label-prefix: '[vV]?' @@ -161,7 +160,7 @@ ignore: sha: [] mode: ContinuousDelivery label: '{BranchName}' -increment: Patch +increment: Inherit prevent-increment-of-merged-branch-version: false track-merge-target: false track-merge-message: true @@ -174,12 +173,6 @@ is-mainline: false The details of the available options are as follows: -### remote-name-in-git - -The user can define the name of the remote (in moste cases it's `origin`) which -will be considered when generating a semantic version on a remote branch. If the -actual remote name doesn't match this pattern an error will be thrown. - ### next-version Allows you to bump the next version explicitly. Useful for bumping `main` or a diff --git a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt index 2eb69f730c..a864d80d99 100644 --- a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt +++ b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt @@ -1,4 +1,3 @@ -remote-name-in-git: origin assembly-versioning-scheme: MajorMinorPatch assembly-file-versioning-scheme: MajorMinorPatch label-prefix: '[vV]?' diff --git a/src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs b/src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs index 50db90a962..efb2ebe568 100644 --- a/src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs +++ b/src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs @@ -7,7 +7,6 @@ namespace GitVersion.Core.Tests.Helpers; public class TestEffectiveConfiguration : EffectiveConfiguration { public TestEffectiveConfiguration( - string remoteNameInGit = GitVersionConfiguration.DefaultRemoteNameInGit, AssemblyVersioningScheme assemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch, AssemblyFileVersioningScheme assemblyFileVersioningScheme = AssemblyFileVersioningScheme.MajorMinorPatch, string? assemblyVersioningFormat = null, @@ -32,8 +31,7 @@ public TestEffectiveConfiguration( bool isMainline = false, string commitDateFormat = "yyyy-MM-dd", bool updateBuildNumber = false) : - base(remoteNameInGit, - assemblyVersioningScheme, + base(assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyVersioningFormat, diff --git a/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs index b607908983..d19aaa6cfc 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs @@ -38,26 +38,17 @@ public void TakesVersionFromNameOfBranchThatIsReleaseByConfig() fixture.AssertFullSemver("2.0.0+1", configuration); } - [TestCase("origin")] - [TestCase("upstream")] - public void TakesVersionFromNameOfRemoteReleaseBranchInOrigin(string remoteNameInGit) + [Test] + public void TakesVersionFromNameOfRemoteReleaseBranchInOrigin() { - var configuration = GitFlowConfigurationBuilder.New - .WithRemoteNameInGit(remoteNameInGit) - .Build(); - using var fixture = new RemoteRepositoryFixture(); - if (remoteNameInGit != "origin") - { - fixture.LocalRepositoryFixture.Repository.Network.Remotes.Rename("origin", remoteNameInGit); - } fixture.BranchTo("release/2.0.0"); fixture.MakeACommit(); Commands.Fetch((Repository)fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, Array.Empty(), new FetchOptions(), null); - fixture.LocalRepositoryFixture.Checkout($"{remoteNameInGit}/release/2.0.0"); + fixture.LocalRepositoryFixture.Checkout("origin/release/2.0.0"); - fixture.LocalRepositoryFixture.AssertFullSemver("2.0.0-beta.1+1", configuration); + fixture.LocalRepositoryFixture.AssertFullSemver("2.0.0-beta.1+1"); } [Test] @@ -70,8 +61,7 @@ public void DoesNotTakeVersionFromNameOfRemoteReleaseBranchInCustomRemote() Commands.Fetch((Repository)fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, Array.Empty(), new FetchOptions(), null); fixture.LocalRepositoryFixture.Checkout("upstream/release/2.0.0"); - Action action = () => fixture.LocalRepositoryFixture.GetVersion(); - action.ShouldThrow(); + fixture.LocalRepositoryFixture.AssertFullSemver("0.0.0-beta.1+6"); // This test fails with 2.0.0-beta.1+1 } } diff --git a/src/GitVersion.Core/Configuration/ConfigurationBuilderBase.cs b/src/GitVersion.Core/Configuration/ConfigurationBuilderBase.cs index f104483295..d4a894f093 100644 --- a/src/GitVersion.Core/Configuration/ConfigurationBuilderBase.cs +++ b/src/GitVersion.Core/Configuration/ConfigurationBuilderBase.cs @@ -6,7 +6,6 @@ namespace GitVersion.Configuration; internal abstract class ConfigurationBuilderBase where TConfigurationBuilder : ConfigurationBuilderBase { - private string? remoteNameInGit; private AssemblyVersioningScheme? assemblyVersioningScheme; private AssemblyFileVersioningScheme? assemblyFileVersioningScheme; private string? assemblyInformationalFormat; @@ -98,13 +97,6 @@ protected ConfigurationBuilderBase() } } - public virtual TConfigurationBuilder WithRemoteNameInGit(string? value) - { - this.remoteNameInGit = value; - return (TConfigurationBuilder)this; - } - - public virtual TConfigurationBuilder WithAssemblyVersioningScheme(AssemblyVersioningScheme? value) { this.assemblyVersioningScheme = value; @@ -332,7 +324,6 @@ public virtual TConfigurationBuilder WithPreReleaseWeight(int? value) public virtual TConfigurationBuilder WithConfiguration(GitVersionConfiguration value) { - WithRemoteNameInGit(value.RemoteNameInGit); WithAssemblyVersioningScheme(value.AssemblyVersioningScheme); WithAssemblyFileVersioningScheme(value.AssemblyFileVersioningScheme); WithAssemblyInformationalFormat(value.AssemblyInformationalFormat); @@ -385,7 +376,6 @@ public virtual GitVersionConfiguration Build() { GitVersionConfiguration configuration = new() { - RemoteNameInGit = this.remoteNameInGit, AssemblyVersioningScheme = this.assemblyVersioningScheme, AssemblyFileVersioningScheme = this.assemblyFileVersioningScheme, AssemblyInformationalFormat = this.assemblyInformationalFormat, diff --git a/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs b/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs index 54e97dce24..bcc37e3549 100644 --- a/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs +++ b/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs @@ -37,7 +37,6 @@ public EffectiveConfiguration(GitVersionConfiguration configuration, BranchConfi if (!configuration.LabelPreReleaseWeight.HasValue) throw new Exception("Configuration value for 'LabelPreReleaseWeight' has no value. (this should not happen, please report an issue)"); - RemoteNameInGit = configuration.RemoteNameInGit; AssemblyVersioningScheme = configuration.AssemblyVersioningScheme.Value; AssemblyFileVersioningScheme = configuration.AssemblyFileVersioningScheme.Value; AssemblyInformationalFormat = configuration.AssemblyInformationalFormat; @@ -69,8 +68,7 @@ public EffectiveConfiguration(GitVersionConfiguration configuration, BranchConfi LabelPreReleaseWeight = configuration.LabelPreReleaseWeight.Value; } - protected EffectiveConfiguration(string remoteNameInGit, - AssemblyVersioningScheme assemblyVersioningScheme, + protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningScheme, AssemblyFileVersioningScheme assemblyFileVersioningScheme, string? assemblyInformationalFormat, string? assemblyVersioningFormat, @@ -99,7 +97,6 @@ protected EffectiveConfiguration(string remoteNameInGit, int preReleaseWeight, int labelPreReleaseWeight) { - RemoteNameInGit = remoteNameInGit; AssemblyVersioningScheme = assemblyVersioningScheme; AssemblyFileVersioningScheme = assemblyFileVersioningScheme; AssemblyInformationalFormat = assemblyInformationalFormat; @@ -130,7 +127,6 @@ protected EffectiveConfiguration(string remoteNameInGit, LabelPreReleaseWeight = labelPreReleaseWeight; } - public string? RemoteNameInGit { get; } public bool TracksReleaseBranches { get; } public bool IsReleaseBranch { get; } public bool IsMainline { get; } diff --git a/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs b/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs index 56e4ba4735..523bc0b819 100644 --- a/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs +++ b/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs @@ -11,7 +11,6 @@ private GitFlowConfigurationBuilder() { WithConfiguration(new() { - RemoteNameInGit = GitVersionConfiguration.DefaultRemoteNameInGit, AssemblyFileVersioningScheme = AssemblyFileVersioningScheme.MajorMinorPatch, AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch, CommitDateFormat = "yyyy-MM-dd", diff --git a/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs b/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs index e060d39936..96a6a8a3f0 100644 --- a/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs +++ b/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs @@ -11,7 +11,6 @@ private GitHubFlowConfigurationBuilder() { WithConfiguration(new() { - RemoteNameInGit = GitVersionConfiguration.DefaultRemoteNameInGit, AssemblyFileVersioningScheme = AssemblyFileVersioningScheme.MajorMinorPatch, AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch, CommitDateFormat = "yyyy-MM-dd", diff --git a/src/GitVersion.Core/Core/GitVersionContextFactory.cs b/src/GitVersion.Core/Core/GitVersionContextFactory.cs index e50492b3a5..96b132147f 100644 --- a/src/GitVersion.Core/Core/GitVersionContextFactory.cs +++ b/src/GitVersion.Core/Core/GitVersionContextFactory.cs @@ -26,17 +26,6 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions) currentBranch = branchForCommit ?? currentBranch; } - if (currentBranch.IsRemote) - { - var remoteNameInGit = configuration.RemoteNameInGit; - if (string.IsNullOrEmpty(remoteNameInGit) || !currentBranch.Name.Friendly.StartsWith(remoteNameInGit)) - { - throw new InvalidOperationException( - $"The remote branch name '{currentBranch.Name.Friendly}' is not valid. Please use another branch or change the configuration." - ); - } - } - var currentCommitTaggedVersion = this.repositoryStore.GetCurrentCommitTaggedVersion(currentCommit, configuration.LabelPrefix, configuration.SemanticVersionFormat, handleDetachedBranch: currentBranch.IsDetachedHead); var numberOfUncommittedChanges = this.repositoryStore.GetNumberOfUncommittedChanges(); diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index ae93da3d6e..25efaf661a 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -165,7 +165,7 @@ 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! branchConfiguration) -> void -GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(string! remoteNameInGit, 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! versionFilters, bool tracksReleaseBranches, bool isReleaseBranch, bool isMainline, string? commitDateFormat, bool updateBuildNumber, GitVersion.SemanticVersionFormat semanticVersionFormat, int preReleaseWeight, int labelPreReleaseWeight) -> 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! 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 GitVersion.Configuration.EffectiveConfiguration.IsReleaseBranch.get -> bool @@ -180,7 +180,6 @@ GitVersion.Configuration.EffectiveConfiguration.NoBumpMessage.get -> string? GitVersion.Configuration.EffectiveConfiguration.PatchVersionBumpMessage.get -> string? GitVersion.Configuration.EffectiveConfiguration.PreReleaseWeight.get -> int GitVersion.Configuration.EffectiveConfiguration.PreventIncrementOfMergedBranchVersion.get -> bool -GitVersion.Configuration.EffectiveConfiguration.RemoteNameInGit.get -> string? GitVersion.Configuration.EffectiveConfiguration.SemanticVersionFormat.get -> GitVersion.SemanticVersionFormat GitVersion.Configuration.EffectiveConfiguration.SemanticVersionFormat.set -> void GitVersion.Configuration.EffectiveConfiguration.TrackMergeMessage.get -> bool @@ -207,9 +206,9 @@ GitVersion.Configuration.GitVersionConfiguration.CommitDateFormat.set -> void GitVersion.Configuration.GitVersionConfiguration.GitVersionConfiguration() -> void GitVersion.Configuration.GitVersionConfiguration.Ignore.get -> GitVersion.Configuration.IgnoreConfiguration! GitVersion.Configuration.GitVersionConfiguration.Ignore.set -> void -GitVersion.Configuration.GitVersionConfiguration.LabelPrefix.get -> string? GitVersion.Configuration.GitVersionConfiguration.LabelPreReleaseWeight.get -> int? GitVersion.Configuration.GitVersionConfiguration.LabelPreReleaseWeight.set -> void +GitVersion.Configuration.GitVersionConfiguration.LabelPrefix.get -> string? GitVersion.Configuration.GitVersionConfiguration.LabelPrefix.set -> void GitVersion.Configuration.GitVersionConfiguration.MajorVersionBumpMessage.get -> string? GitVersion.Configuration.GitVersionConfiguration.MajorVersionBumpMessage.set -> void @@ -223,8 +222,6 @@ GitVersion.Configuration.GitVersionConfiguration.NoBumpMessage.get -> string? GitVersion.Configuration.GitVersionConfiguration.NoBumpMessage.set -> void GitVersion.Configuration.GitVersionConfiguration.PatchVersionBumpMessage.get -> string? GitVersion.Configuration.GitVersionConfiguration.PatchVersionBumpMessage.set -> void -GitVersion.Configuration.GitVersionConfiguration.RemoteNameInGit.get -> string? -GitVersion.Configuration.GitVersionConfiguration.RemoteNameInGit.set -> void GitVersion.Configuration.GitVersionConfiguration.SemanticVersionFormat.get -> GitVersion.SemanticVersionFormat GitVersion.Configuration.GitVersionConfiguration.SemanticVersionFormat.set -> void GitVersion.Configuration.GitVersionConfiguration.UpdateBuildNumber.get -> bool diff --git a/src/GitVersion.Output.Tests/Output/AssemblyInfoFileUpdaterTests.cs b/src/GitVersion.Output.Tests/Output/AssemblyInfoFileUpdaterTests.cs index 88f07fa95b..2af8d14bb0 100644 --- a/src/GitVersion.Output.Tests/Output/AssemblyInfoFileUpdaterTests.cs +++ b/src/GitVersion.Output.Tests/Output/AssemblyInfoFileUpdaterTests.cs @@ -442,7 +442,7 @@ private void VerifyAssemblyInfoFile( this.fileSystem.ReadAllText(fileName).Returns(assemblyFileContent); }); - var configuration = new TestEffectiveConfiguration(assemblyVersioningScheme: versioningScheme); + var configuration = new TestEffectiveConfiguration(versioningScheme); var variables = this.variableProvider.GetVariablesFor(version, configuration, false); verify?.Invoke(this.fileSystem, variables); diff --git a/src/GitVersion.Output.Tests/Output/ProjectFileUpdaterTests.cs b/src/GitVersion.Output.Tests/Output/ProjectFileUpdaterTests.cs index f77570c43b..02a40684ef 100644 --- a/src/GitVersion.Output.Tests/Output/ProjectFileUpdaterTests.cs +++ b/src/GitVersion.Output.Tests/Output/ProjectFileUpdaterTests.cs @@ -300,7 +300,7 @@ private void VerifyAssemblyInfoFile( this.fileSystem.ReadAllText(fileName).Returns(projectFileContent); }); - var configuration = new TestEffectiveConfiguration(assemblyVersioningScheme: versioningScheme); + var configuration = new TestEffectiveConfiguration(versioningScheme); var variables = this.variableProvider.GetVariablesFor(version, configuration, false); verify?.Invoke(this.fileSystem, variables); From 7ffc693ea394434c91aa021700f2a83e87cc565f Mon Sep 17 00:00:00 2001 From: Hardy Hobeck Date: Mon, 27 Feb 2023 09:08:08 +0100 Subject: [PATCH 4/6] Remove complexity and make IncrementStrategy non-nullable --- ...riteOutEffectiveConfiguration.approved.txt | 2 +- ...itScenarios.CanSetNextVersion.approved.txt | 1 + .../MainlineDevelopmentMode.cs | 4 +- .../VersionInCurrentBranchNameScenarios.cs | 4 +- ...EffectiveBranchConfigurationFinderTests.cs | 73 +++++++------------ .../Configuration/BranchConfiguration.cs | 4 +- .../BranchConfigurationBuilder.cs | 4 +- .../Configuration/ConfigurationBuilderBase.cs | 4 +- .../Configuration/ConfigurationExtensions.cs | 1 - .../Configuration/EffectiveConfiguration.cs | 5 +- .../Configuration/GitVersionConfiguration.cs | 4 + .../Core/GitVersionContextFactory.cs | 5 ++ src/GitVersion.Core/Core/RepositoryStore.cs | 28 ++++--- src/GitVersion.Core/PublicAPI.Unshipped.txt | 4 +- .../EffectiveBranchConfigurationFinder.cs | 2 +- .../MainlineVersionCalculator.cs | 11 ++- 16 files changed, 79 insertions(+), 77 deletions(-) diff --git a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt index a864d80d99..31e89185aa 100644 --- a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt +++ b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt @@ -118,7 +118,7 @@ ignore: sha: [] mode: ContinuousDelivery label: '{BranchName}' -increment: Patch +increment: Inherit prevent-increment-of-merged-branch-version: false track-merge-target: false track-merge-message: true diff --git a/src/GitVersion.Core.Tests/Configuration/Init/InitScenarios.CanSetNextVersion.approved.txt b/src/GitVersion.Core.Tests/Configuration/Init/InitScenarios.CanSetNextVersion.approved.txt index 4867b41f53..f8d2dd9d75 100644 --- a/src/GitVersion.Core.Tests/Configuration/Init/InitScenarios.CanSetNextVersion.approved.txt +++ b/src/GitVersion.Core.Tests/Configuration/Init/InitScenarios.CanSetNextVersion.approved.txt @@ -5,3 +5,4 @@ semantic-version-format: Strict branches: {} ignore: sha: [] +increment: Inherit diff --git a/src/GitVersion.Core.Tests/IntegrationTests/MainlineDevelopmentMode.cs b/src/GitVersion.Core.Tests/IntegrationTests/MainlineDevelopmentMode.cs index e474adc92b..2a0ded56f9 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/MainlineDevelopmentMode.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/MainlineDevelopmentMode.cs @@ -443,11 +443,11 @@ public void VerifyIncrementConfigIsHonoured() .WithIncrement(IncrementStrategy.Minor) .WithBranch("main", builder => builder .WithVersioningMode(VersioningMode.Mainline) - .WithIncrement(null) + .WithIncrement(IncrementStrategy.Inherit) ) .WithBranch("feature", builder => builder .WithVersioningMode(VersioningMode.Mainline) - .WithIncrement(null) + .WithIncrement(IncrementStrategy.Inherit) ) .Build(); diff --git a/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs index d19aaa6cfc..c29ac66e85 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/VersionInCurrentBranchNameScenarios.cs @@ -52,7 +52,7 @@ public void TakesVersionFromNameOfRemoteReleaseBranchInOrigin() } [Test] - public void DoesNotTakeVersionFromNameOfRemoteReleaseBranchInCustomRemote() + public void TakesVersionFromNameOfRemoteReleaseBranchInCustom() { using var fixture = new RemoteRepositoryFixture(); fixture.LocalRepositoryFixture.Repository.Network.Remotes.Rename("origin", "upstream"); @@ -62,6 +62,6 @@ public void DoesNotTakeVersionFromNameOfRemoteReleaseBranchInCustomRemote() fixture.LocalRepositoryFixture.Checkout("upstream/release/2.0.0"); - fixture.LocalRepositoryFixture.AssertFullSemver("0.0.0-beta.1+6"); // This test fails with 2.0.0-beta.1+1 + fixture.LocalRepositoryFixture.AssertFullSemver("2.0.0-beta.1+1"); } } diff --git a/src/GitVersion.Core.Tests/VersionCalculation/EffectiveBranchConfigurationFinderTests.cs b/src/GitVersion.Core.Tests/VersionCalculation/EffectiveBranchConfigurationFinderTests.cs index eafcb511fc..370ed43dc7 100644 --- a/src/GitVersion.Core.Tests/VersionCalculation/EffectiveBranchConfigurationFinderTests.cs +++ b/src/GitVersion.Core.Tests/VersionCalculation/EffectiveBranchConfigurationFinderTests.cs @@ -200,28 +200,7 @@ public void UsesFirstBranchConfigWhenMultipleMatch(string branchName, IncrementS } [Test] - public void When_getting_configurations_of_an_orphaned_branch_Given_fallback_configuaration_without_increment_Then_result_should_be_empty() - { - // Arrange - var branchMock = GitToolsTestingExtensions.CreateMockBranch("develop", GitToolsTestingExtensions.CreateMockCommit()); - var configuration = GitFlowConfigurationBuilder.New - .WithIncrement(null) - .WithBranch("develop", builder => builder.WithIncrement(IncrementStrategy.Inherit)) - .Build(); - var repositoryStoreMock = Substitute.For(); - repositoryStoreMock.GetSourceBranches(branchMock, configuration, Arg.Any>()).Returns(Enumerable.Empty()); - - var unitUnderTest = new EffectiveBranchConfigurationFinder(Substitute.For(), repositoryStoreMock); - - // Act - var actual = unitUnderTest.GetConfigurations(branchMock, configuration).ToArray(); - - // Assert - actual.ShouldBeEmpty(); - } - - [Test] - public void When_getting_configurations_of_an_orphaned_branch_Given_fallback_configuration_with_increment_inherit_Then_result_should_have_increment_none() + public void When_getting_configurations_of_an_orphaned_branch_Given_fallback_configuration_with_increment_inherit_Then_result_should_be_empty() { // Arrange var branchMock = GitToolsTestingExtensions.CreateMockBranch("develop", GitToolsTestingExtensions.CreateMockCommit()); @@ -238,9 +217,7 @@ public void When_getting_configurations_of_an_orphaned_branch_Given_fallback_con var actual = unitUnderTest.GetConfigurations(branchMock, configuration).ToArray(); // Assert - actual.ShouldHaveSingleItem(); - actual[0].Branch.ShouldBe(branchMock); - actual[0].Value.Increment.ShouldBe(IncrementStrategy.None); + actual.ShouldBeEmpty(); } [TestCase(IncrementStrategy.None)] @@ -271,12 +248,13 @@ public void When_getting_configurations_of_an_orphaned_branch_Given_fallback_con } [Test] - public void When_getting_configurations_of_an_unknown_branch_Given_fallback_configuaration_without_increment_and_unknown_configuration_with_increment_inherit_Then_result_should_be_empty() + public void When_getting_configurations_of_an_unknown_branch_Given_fallback_and_unknown_configuration_with_increment_inherit_Then_result_should_be_empty() { // Arrange var branchMock = GitToolsTestingExtensions.CreateMockBranch("unknown", GitToolsTestingExtensions.CreateMockCommit()); var configuration = GitFlowConfigurationBuilder.New - .WithIncrement(null) + .WithIncrement(IncrementStrategy.Inherit) + .WithBranch("unknown", builder => builder.WithIncrement(IncrementStrategy.Inherit)) .Build(); var repositoryStoreMock = Substitute.For(); repositoryStoreMock.GetSourceBranches(branchMock, configuration, Arg.Any>()).Returns(Enumerable.Empty()); @@ -301,6 +279,7 @@ public void When_getting_configurations_of_an_unknown_branch_Given_fallback_conf var branchMock = GitToolsTestingExtensions.CreateMockBranch("unknown", GitToolsTestingExtensions.CreateMockCommit()); var configuration = GitFlowConfigurationBuilder.New .WithIncrement(fallbackIncrement) + .WithBranch("unknown", builder => builder.WithIncrement(IncrementStrategy.Inherit)) .Build(); var repositoryStoreMock = Substitute.For(); repositoryStoreMock.GetSourceBranches(branchMock, configuration, Arg.Any>()).Returns(Enumerable.Empty()); @@ -318,13 +297,13 @@ public void When_getting_configurations_of_an_unknown_branch_Given_fallback_conf [Theory] public void When_getting_configurations_of_an_unknown_branch_Given_fallback_configuaration_with_increment_and_develop_branch_with_increment_Then_result_should_have_develop_increment( - IncrementStrategy fallbackIncrement, IncrementStrategy developBranchIncrement) + IncrementStrategy fallbackIncrement, IncrementStrategy developIncrement) { // Arrange var unknownBranchMock = GitToolsTestingExtensions.CreateMockBranch("unknown", GitToolsTestingExtensions.CreateMockCommit()); var configuration = GitFlowConfigurationBuilder.New .WithIncrement(fallbackIncrement) - .WithBranch("develop", builder => builder.WithIncrement(developBranchIncrement)) + .WithBranch("develop", builder => builder.WithIncrement(developIncrement)) .Build(); var repositoryStoreMock = Substitute.For(); var developBranchMock = GitToolsTestingExtensions.CreateMockBranch("develop", GitToolsTestingExtensions.CreateMockCommit()); @@ -336,22 +315,24 @@ public void When_getting_configurations_of_an_unknown_branch_Given_fallback_conf var actual = unitUnderTest.GetConfigurations(unknownBranchMock, configuration).ToArray(); // Assert - actual.ShouldHaveSingleItem(); - actual[0].Branch.ShouldBe(developBranchMock); - - if (developBranchIncrement == IncrementStrategy.Inherit) + if (fallbackIncrement == IncrementStrategy.Inherit && developIncrement == IncrementStrategy.Inherit) { - if (fallbackIncrement == IncrementStrategy.Inherit) - { - fallbackIncrement = IncrementStrategy.None; - } - actual[0].Value.Increment.ShouldBe(fallbackIncrement); + actual.ShouldBeEmpty(); } else { - actual[0].Value.Increment.ShouldBe(developBranchIncrement); - } + actual.ShouldHaveSingleItem(); + actual[0].Branch.ShouldBe(developBranchMock); + if (developIncrement == IncrementStrategy.Inherit) + { + actual[0].Value.Increment.ShouldBe(fallbackIncrement); + } + else + { + actual[0].Value.Increment.ShouldBe(developIncrement); + } + } } [Theory] @@ -374,14 +355,16 @@ public void When_getting_configurations_of_an_unknown_branch_Given_fallback_conf var actual = unitUnderTest.GetConfigurations(unknownBranchMock, configuration).ToArray(); // Assert - actual.ShouldHaveSingleItem(); - actual[0].Branch.ShouldBe(developBranchMock); - if (fallbackIncrement == IncrementStrategy.Inherit) { - fallbackIncrement = IncrementStrategy.None; + actual.ShouldBeEmpty(); + } + else + { + actual.ShouldHaveSingleItem(); + actual[0].Branch.ShouldBe(developBranchMock); + actual[0].Value.Increment.ShouldBe(fallbackIncrement); } - actual[0].Value.Increment.ShouldBe(fallbackIncrement); } [TestCase(IncrementStrategy.None)] diff --git a/src/GitVersion.Core/Configuration/BranchConfiguration.cs b/src/GitVersion.Core/Configuration/BranchConfiguration.cs index 96f63404c6..cf3edfceef 100644 --- a/src/GitVersion.Core/Configuration/BranchConfiguration.cs +++ b/src/GitVersion.Core/Configuration/BranchConfiguration.cs @@ -42,7 +42,7 @@ public BranchConfiguration(BranchConfiguration branchConfiguration) [JsonPropertyName("increment")] [JsonPropertyDescription("The increment strategy for this branch. Can be 'Inherit', 'Patch', 'Minor', 'Major', 'None'.")] - public IncrementStrategy? Increment { get; set; } + public IncrementStrategy Increment { get; set; } [JsonPropertyName("prevent-increment-of-merged-branch-version")] [JsonPropertyDescription("Prevent increment of merged branch version.")] @@ -105,7 +105,7 @@ public BranchConfiguration Inherit(BranchConfiguration? parentConfig) var result = new BranchConfiguration(this); - if (result.Increment is null || result.Increment == IncrementStrategy.Inherit) + if (result.Increment == IncrementStrategy.Inherit) result.Increment = parentConfig.Increment; result.VersioningMode ??= parentConfig.VersioningMode; result.Label ??= parentConfig.Label; diff --git a/src/GitVersion.Core/Configuration/BranchConfigurationBuilder.cs b/src/GitVersion.Core/Configuration/BranchConfigurationBuilder.cs index 9dd2dface8..13d06d3f62 100644 --- a/src/GitVersion.Core/Configuration/BranchConfigurationBuilder.cs +++ b/src/GitVersion.Core/Configuration/BranchConfigurationBuilder.cs @@ -9,7 +9,7 @@ public class BranchConfigurationBuilder private string name; private VersioningMode? versioningMode; private string? label; - private IncrementStrategy? increment; + private IncrementStrategy increment; private bool? preventIncrementOfMergedBranchVersion; private string? labelNumberPattern; private bool? trackMergeTarget; @@ -45,7 +45,7 @@ public virtual BranchConfigurationBuilder WithLabel(string? value) return this; } - public virtual BranchConfigurationBuilder WithIncrement(IncrementStrategy? value) + public virtual BranchConfigurationBuilder WithIncrement(IncrementStrategy value) { this.increment = value; return this; diff --git a/src/GitVersion.Core/Configuration/ConfigurationBuilderBase.cs b/src/GitVersion.Core/Configuration/ConfigurationBuilderBase.cs index d4a894f093..e71114ea30 100644 --- a/src/GitVersion.Core/Configuration/ConfigurationBuilderBase.cs +++ b/src/GitVersion.Core/Configuration/ConfigurationBuilderBase.cs @@ -27,7 +27,7 @@ internal abstract class ConfigurationBuilderBase private readonly Dictionary branchConfigurationBuilders = new(); private VersioningMode? versioningMode; private string? label; - private IncrementStrategy? increment; + private IncrementStrategy increment = IncrementStrategy.Inherit; private bool? preventIncrementOfMergedBranchVersion; private string? labelNumberPattern; private bool? trackMergeTarget; @@ -232,7 +232,7 @@ public virtual TConfigurationBuilder WithLabel(string? value) return (TConfigurationBuilder)this; } - public virtual TConfigurationBuilder WithIncrement(IncrementStrategy? value) + public virtual TConfigurationBuilder WithIncrement(IncrementStrategy value) { this.increment = value; return (TConfigurationBuilder)this; diff --git a/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs b/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs index 3ce54c904e..13650c0376 100644 --- a/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs +++ b/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs @@ -54,7 +54,6 @@ private static IEnumerable GetBranchConfigurations(GitVersi public static BranchConfiguration GetFallbackBranchConfiguration(this GitVersionConfiguration configuration) { BranchConfiguration result = new(configuration); - if (result.Increment == IncrementStrategy.Inherit) result.Increment = IncrementStrategy.None; return result; } diff --git a/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs b/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs index bcc37e3549..48ac103a68 100644 --- a/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs +++ b/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs @@ -22,9 +22,6 @@ public EffectiveConfiguration(GitVersionConfiguration configuration, BranchConfi 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 (!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) throw new Exception("Configuration value for 'AssemblyVersioningScheme' has no value. (this should not happen, please report an issue)"); @@ -46,7 +43,7 @@ public EffectiveConfiguration(GitVersionConfiguration configuration, BranchConfi LabelPrefix = configuration.LabelPrefix; Label = branchConfiguration.Label ?? string.Empty; NextVersion = configuration.NextVersion; - Increment = branchConfiguration.Increment.Value; + Increment = branchConfiguration.Increment; BranchPrefixToTrim = branchConfiguration.Regex; PreventIncrementOfMergedBranchVersion = branchConfiguration.PreventIncrementOfMergedBranchVersion ?? false; LabelNumberPattern = branchConfiguration.LabelNumberPattern; diff --git a/src/GitVersion.Core/Configuration/GitVersionConfiguration.cs b/src/GitVersion.Core/Configuration/GitVersionConfiguration.cs index 131bb49534..d4530a586b 100644 --- a/src/GitVersion.Core/Configuration/GitVersionConfiguration.cs +++ b/src/GitVersion.Core/Configuration/GitVersionConfiguration.cs @@ -1,3 +1,7 @@ +using System.Globalization; +using GitVersion.Attributes; +using GitVersion.Extensions; + namespace GitVersion.Configuration; public class GitVersionConfiguration : BranchConfiguration diff --git a/src/GitVersion.Core/Core/GitVersionContextFactory.cs b/src/GitVersion.Core/Core/GitVersionContextFactory.cs index 96b132147f..b37658f95c 100644 --- a/src/GitVersion.Core/Core/GitVersionContextFactory.cs +++ b/src/GitVersion.Core/Core/GitVersionContextFactory.cs @@ -1,3 +1,8 @@ +using GitVersion.Common; +using GitVersion.Configuration; +using GitVersion.Extensions; +using Microsoft.Extensions.Options; + namespace GitVersion; public class GitVersionContextFactory : IGitVersionContextFactory diff --git a/src/GitVersion.Core/Core/RepositoryStore.cs b/src/GitVersion.Core/Core/RepositoryStore.cs index 79911e0105..062cfe1a8f 100644 --- a/src/GitVersion.Core/Core/RepositoryStore.cs +++ b/src/GitVersion.Core/Core/RepositoryStore.cs @@ -140,20 +140,30 @@ public IEnumerable GetSourceBranches(IBranch branch, GitVersionConfigur public IEnumerable GetSourceBranches(IBranch branch, GitVersionConfiguration configuration, IEnumerable excludedBranches) { + var returnedBranches = new HashSet(); + var referenceLookup = this.repository.Refs.ToLookup(r => r.TargetIdentifier); - var returnedBranches = new HashSet(); - if (referenceLookup.Any()) + foreach (var branchGrouping in FindCommitBranchesWasBranchedFrom(branch, configuration, excludedBranches) + .GroupBy(element => element.Commit, element => element.Branch)) { - foreach (var branchCommit in FindCommitBranchesWasBranchedFrom(branch, configuration, excludedBranches)) + bool referenceMatchFound = false; + var referenceNames = referenceLookup[branchGrouping.Key.Sha].Select(element => element.Name).ToHashSet(); + + foreach (var item in branchGrouping) + { + if (referenceNames.Contains(item.Name)) + { + if (returnedBranches.Add(item)) yield return item; + referenceMatchFound = true; + } + } + + if (!referenceMatchFound) { - foreach (var _ in referenceLookup[branchCommit.Commit.Sha] - .Where(r => r.Name.Friendly == branchCommit.Branch.Name.Friendly)) + foreach (var item in branchGrouping) { - if (returnedBranches.Add(branchCommit.Branch)) - { - yield return branchCommit.Branch; - } + if (returnedBranches.Add(item)) yield return item; } } } diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index 25efaf661a..8dc3b064c0 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -94,7 +94,7 @@ GitVersion.Configuration.BranchConfiguration.BranchConfiguration() -> void GitVersion.Configuration.BranchConfiguration.BranchConfiguration(GitVersion.Configuration.BranchConfiguration! branchConfiguration) -> void GitVersion.Configuration.BranchConfiguration.CommitMessageIncrementing.get -> GitVersion.VersionCalculation.CommitMessageIncrementMode? GitVersion.Configuration.BranchConfiguration.CommitMessageIncrementing.set -> void -GitVersion.Configuration.BranchConfiguration.Increment.get -> GitVersion.IncrementStrategy? +GitVersion.Configuration.BranchConfiguration.Increment.get -> GitVersion.IncrementStrategy GitVersion.Configuration.BranchConfiguration.Increment.set -> void GitVersion.Configuration.BranchConfiguration.Inherit(GitVersion.Configuration.BranchConfiguration? parentConfig) -> GitVersion.Configuration.BranchConfiguration! GitVersion.Configuration.BranchConfiguration.IsMainline.get -> bool? @@ -1188,7 +1188,7 @@ virtual GitVersion.Agents.BuildAgentBase.ShouldCleanUpRemotes() -> bool virtual GitVersion.Agents.BuildAgentBase.WriteIntegration(System.Action! writer, GitVersion.OutputVariables.VersionVariables! variables, bool updateBuildNumber = true) -> void virtual GitVersion.Configuration.BranchConfigurationBuilder.WithCommitMessageIncrementing(GitVersion.VersionCalculation.CommitMessageIncrementMode? value) -> GitVersion.Configuration.BranchConfigurationBuilder! virtual GitVersion.Configuration.BranchConfigurationBuilder.WithConfiguration(GitVersion.Configuration.BranchConfiguration! value) -> GitVersion.Configuration.BranchConfigurationBuilder! -virtual GitVersion.Configuration.BranchConfigurationBuilder.WithIncrement(GitVersion.IncrementStrategy? value) -> GitVersion.Configuration.BranchConfigurationBuilder! +virtual GitVersion.Configuration.BranchConfigurationBuilder.WithIncrement(GitVersion.IncrementStrategy value) -> GitVersion.Configuration.BranchConfigurationBuilder! virtual GitVersion.Configuration.BranchConfigurationBuilder.WithIsMainline(bool? value) -> GitVersion.Configuration.BranchConfigurationBuilder! virtual GitVersion.Configuration.BranchConfigurationBuilder.WithIsReleaseBranch(bool? value) -> GitVersion.Configuration.BranchConfigurationBuilder! virtual GitVersion.Configuration.BranchConfigurationBuilder.WithIsSourceBranchFor(System.Collections.Generic.IEnumerable? values) -> GitVersion.Configuration.BranchConfigurationBuilder! diff --git a/src/GitVersion.Core/VersionCalculation/EffectiveBranchConfigurationFinder.cs b/src/GitVersion.Core/VersionCalculation/EffectiveBranchConfigurationFinder.cs index 37af488151..0ad2e3ebe6 100644 --- a/src/GitVersion.Core/VersionCalculation/EffectiveBranchConfigurationFinder.cs +++ b/src/GitVersion.Core/VersionCalculation/EffectiveBranchConfigurationFinder.cs @@ -45,7 +45,7 @@ private IEnumerable 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 skipTraversingOfOrphanedBranches = configuration.Increment == null; + var skipTraversingOfOrphanedBranches = configuration.Increment == IncrementStrategy.Inherit; this.log.Info( $"An orphaned branch '{branch}' has been detected and will be skipped={skipTraversingOfOrphanedBranches}." ); diff --git a/src/GitVersion.Core/VersionCalculation/MainlineVersionCalculator.cs b/src/GitVersion.Core/VersionCalculation/MainlineVersionCalculator.cs index 16de07d847..23a2f91014 100644 --- a/src/GitVersion.Core/VersionCalculation/MainlineVersionCalculator.cs +++ b/src/GitVersion.Core/VersionCalculation/MainlineVersionCalculator.cs @@ -300,13 +300,12 @@ private VersionField TryFindIncrementFromMergeMessage(ICommit? mergeCommit) { var mergeMessage = new MergeMessage(mergeCommit.Message, context.Configuration); var configuration = context.Configuration.GetBranchConfiguration(mergeMessage.MergedBranch); - if (configuration.Increment != null && configuration.Increment != IncrementStrategy.Inherit) + if (configuration.Increment != IncrementStrategy.Inherit) { - return configuration.Increment.Value.ToVersionField(); + return configuration.Increment.ToVersionField(); } } - // Fallback to configuration increment value return FindDefaultIncrementForBranch(context); } @@ -314,7 +313,11 @@ private static VersionField FindDefaultIncrementForBranch(GitVersionContext cont => FindDefaultIncrementForBranch(context, context.CurrentBranch); private static VersionField FindDefaultIncrementForBranch(GitVersionContext context, IBranch branch) - => context.Configuration.GetEffectiveConfiguration(branch).Increment.ToVersionField(); + { + var increment = context.Configuration.GetEffectiveConfiguration(branch).Increment; + if (increment == IncrementStrategy.Inherit) increment = IncrementStrategy.Patch; + return increment.ToVersionField(); + } private static ICommit GetMergedHead(ICommit mergeCommit) { From cd1a0eff7a60ecd4af9b1767cfb5e2a85a348837 Mon Sep 17 00:00:00 2001 From: Hardy Hobeck Date: Mon, 27 Feb 2023 10:17:06 +0100 Subject: [PATCH 5/6] Fix unit tests --- .../Init/InitScenarios.CanSetNextVersion.approved.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitVersion.Core.Tests/Configuration/Init/InitScenarios.CanSetNextVersion.approved.txt b/src/GitVersion.Core.Tests/Configuration/Init/InitScenarios.CanSetNextVersion.approved.txt index f8d2dd9d75..71bbab7634 100644 --- a/src/GitVersion.Core.Tests/Configuration/Init/InitScenarios.CanSetNextVersion.approved.txt +++ b/src/GitVersion.Core.Tests/Configuration/Init/InitScenarios.CanSetNextVersion.approved.txt @@ -5,4 +5,4 @@ semantic-version-format: Strict branches: {} ignore: sha: [] -increment: Inherit +increment: None From 0ed09e98cb3edf2e32703d6740ef277f81751ba4 Mon Sep 17 00:00:00 2001 From: Hardy Hobeck Date: Mon, 27 Feb 2023 12:13:00 +0100 Subject: [PATCH 6/6] remove back sdk version to 7.0.103 --- global.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.json b/global.json index aa2aca45d2..ad12120c01 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "7.0.200" + "version": "7.0.103" } } \ No newline at end of file