Skip to content

Commit dc9ec35

Browse files
committed
GitTools#2340 implemented 'easier approach'
1 parent 8b56734 commit dc9ec35

File tree

10 files changed

+60
-18
lines changed

10 files changed

+60
-18
lines changed

docs/input/docs/configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,3 +520,13 @@ If the `pre-release-weight` is set, it would be added to the `PreReleaseNumber`
520520
to get a final `AssemblySemFileVer`, otherwise a branch specific default for
521521
`pre-release-weight` will be used in the calculation. Related Issues
522522
[1145](https://github.com/GitTools/GitVersion/issues/1145), [1366](https://github.com/GitTools/GitVersion/issues/1366)
523+
524+
### prefer-branch-config-over-existing-tag
525+
526+
If this is set true, GitVersion will prefer the configured branch (pre-release) tag
527+
over an existing git tag, when creating another tag for the same commit.
528+
Example: tag `1.1.0-alpha.42` was created while building the `develop` branch.
529+
Then `release/1.1.0` was created off this commit, without generating an empty commit.
530+
Building a release-candidate and running GitVersion on the release branch now will:
531+
- when set to `false` (default): create no new tag, using `1.1.0-alpha.42` again
532+
- when set to `true`: new tag e.g. `1.1.0-beta.1` will be created

src/GitVersionCore.Tests/Configuration/ConfigProviderTests.CanWriteOutEffectiveConfiguration.approved.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ branches:
2525
is-release-branch: false
2626
is-mainline: false
2727
pre-release-weight: 0
28+
prefer-branch-config-over-existing-tag: false
2829
master:
2930
mode: ContinuousDelivery
3031
tag: ''
@@ -39,6 +40,7 @@ branches:
3940
is-release-branch: false
4041
is-mainline: true
4142
pre-release-weight: 55000
43+
prefer-branch-config-over-existing-tag: false
4244
release:
4345
mode: ContinuousDelivery
4446
tag: beta
@@ -55,6 +57,7 @@ branches:
5557
is-release-branch: true
5658
is-mainline: false
5759
pre-release-weight: 30000
60+
prefer-branch-config-over-existing-tag: false
5861
feature:
5962
mode: ContinuousDelivery
6063
tag: useBranchName
@@ -73,6 +76,7 @@ branches:
7376
is-release-branch: false
7477
is-mainline: false
7578
pre-release-weight: 30000
79+
prefer-branch-config-over-existing-tag: false
7680
pull-request:
7781
mode: ContinuousDelivery
7882
tag: PullRequest
@@ -92,6 +96,7 @@ branches:
9296
is-release-branch: false
9397
is-mainline: false
9498
pre-release-weight: 30000
99+
prefer-branch-config-over-existing-tag: false
95100
hotfix:
96101
mode: ContinuousDelivery
97102
tag: beta
@@ -107,6 +112,7 @@ branches:
107112
is-release-branch: false
108113
is-mainline: false
109114
pre-release-weight: 30000
115+
prefer-branch-config-over-existing-tag: false
110116
support:
111117
mode: ContinuousDelivery
112118
tag: ''

src/GitVersionCore.Tests/Helpers/TestEffectiveConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public TestEffectiveConfiguration(
4343
majorMessage, minorMessage, patchMessage, noBumpMessage,
4444
commitMessageMode, legacySemVerPadding, buildMetaDataPadding, commitsSinceVersionSourcePadding,
4545
versionFilters ?? Enumerable.Empty<IVersionFilter>(),
46-
tracksReleaseBranches, isRelease, commitDateFormat, updateBuildNumber, 0, 0)
46+
tracksReleaseBranches, isRelease, commitDateFormat, updateBuildNumber, 0, 0, false)
4747
{
4848
}
4949
}

src/GitVersionCore.Tests/IntegrationTests/BranchWithoutCommitScenarios.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void BetaBranchCreatedButStillOnTaggedAlphaCommitShouldCreateBetaVersion(
4646
{
4747
Branches =
4848
{
49-
{ "release", new BranchConfig { Tag = "beta" } },
49+
{ "release", new BranchConfig { Tag = "beta", PreferBranchConfigOverTag = true } },
5050
{ "develop", new BranchConfig { Tag = "alpha" } }
5151
}
5252
};
@@ -58,9 +58,10 @@ public void BetaBranchCreatedButStillOnTaggedAlphaCommitShouldCreateBetaVersion(
5858

5959
fixture.BranchTo("release/1.1.0"); // about to be released, no additional empty commit in this scenario!
6060
fixture.Checkout("release/1.1.0"); // still on the same commit, but another branch, choosing to build same code as beta now
61-
62-
// Assert
63-
fixture.AssertFullSemver("1.1.0-beta.1", config); //Tag is an "alpha" tag from develop branch, only "beta" tags should count when on release branch. If no beta tag found, build new beta version on release branch.
61+
62+
// Assert
63+
fixture.AssertFullSemver("1.1.0-beta.1+0", config); //was 1.1.0-alpha.1, with the new config param 'prefer-branch-config-over-existing-tag' set to true should be 1.1.0-beta.1.
64+
//Tag is an "alpha" tag from develop branch, only "beta" tags should count when on release branch. If no beta tag found, build new beta version on release branch.
6465

6566
fixture.Checkout("develop"); // back to develop
6667
fixture.AssertFullSemver("1.1.0-alpha.1", config); //will be 1.1.0-alpha.1 based on tag (as before)

src/GitVersionCore.Tests/Model/CommitDateTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void CommitDateFormatTest(string format, string expectedOutcome)
2929
},
3030
new EffectiveConfiguration(
3131
AssemblyVersioningScheme.MajorMinorPatch, AssemblyFileVersioningScheme.MajorMinorPatch, "", "", "", VersioningMode.ContinuousDelivery, "", "", "", IncrementStrategy.Inherit,
32-
"", true, "", "", false, "", "", "", "", CommitMessageIncrementMode.Enabled, 4, 4, 4, Enumerable.Empty<IVersionFilter>(), false, true, format, false, 0, 0)
32+
"", true, "", "", false, "", "", "", "", CommitMessageIncrementMode.Enabled, 4, 4, 4, Enumerable.Empty<IVersionFilter>(), false, true, format, false, 0, 0, false)
3333
);
3434

3535
Assert.That(formatValues.CommitDate, Is.EqualTo(expectedOutcome));

src/GitVersionCore/Configuration/ConfigExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public static EffectiveConfiguration CalculateEffectiveConfiguration(this Config
7979
var preventIncrementForMergedBranchVersion = currentBranchConfig.PreventIncrementOfMergedBranchVersion.Value;
8080
var trackMergeTarget = currentBranchConfig.TrackMergeTarget.Value;
8181
var preReleaseWeight = currentBranchConfig.PreReleaseWeight ?? 0;
82+
var preferBranchConfigOverTag = currentBranchConfig.PreferBranchConfigOverTag ?? false;
8283

8384
var nextVersion = configuration.NextVersion;
8485
var assemblyVersioningScheme = configuration.AssemblyVersioningScheme.Value;
@@ -114,7 +115,8 @@ public static EffectiveConfiguration CalculateEffectiveConfiguration(this Config
114115
commitDateFormat,
115116
updateBuildNumber,
116117
preReleaseWeight,
117-
tagPreReleaseWeight);
118+
tagPreReleaseWeight,
119+
preferBranchConfigOverTag);
118120
}
119121

120122
public static string GetBranchSpecificTag(this EffectiveConfiguration configuration, ILog log, string branchFriendlyName, string branchNameOverride)

src/GitVersionCore/Model/Configuration/BranchConfig.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public BranchConfig(BranchConfig branchConfiguration)
3232
SourceBranches = branchConfiguration.SourceBranches;
3333
IsSourceBranchFor = branchConfiguration.IsSourceBranchFor;
3434
PreReleaseWeight = branchConfiguration.PreReleaseWeight;
35+
PreferBranchConfigOverTag = branchConfiguration.PreferBranchConfigOverTag;
3536
}
3637

3738
[YamlMember(Alias = "mode")]
@@ -79,6 +80,13 @@ public BranchConfig(BranchConfig branchConfiguration)
7980
[YamlMember(Alias = "pre-release-weight")]
8081
public int? PreReleaseWeight { get; set; }
8182

83+
/// <summary>
84+
/// If true, the NextVersionCalculator will check that an existing tag is matching the configured tag for this branch.
85+
/// If the tag isn't matching, a new tag with the configured string will be created.
86+
/// </summary>
87+
[YamlMember(Alias = "prefer-branch-config-over-existing-tag")]
88+
public bool? PreferBranchConfigOverTag { get; set; }
89+
8290
/// <summary>
8391
/// The name given to this configuration in the config file.
8492
/// </summary>
@@ -103,6 +111,7 @@ public void MergeTo([NotNull] BranchConfig targetConfig)
103111
targetConfig.IsReleaseBranch = this.IsReleaseBranch ?? targetConfig.IsReleaseBranch;
104112
targetConfig.IsMainline = this.IsMainline ?? targetConfig.IsMainline;
105113
targetConfig.PreReleaseWeight = this.PreReleaseWeight ?? targetConfig.PreReleaseWeight;
114+
targetConfig.PreferBranchConfigOverTag = this.PreferBranchConfigOverTag ?? targetConfig.PreferBranchConfigOverTag;
106115
}
107116

108117
public BranchConfig Apply([NotNull] BranchConfig overrides)
@@ -124,6 +133,7 @@ public static BranchConfig CreateDefaultBranchConfig(string name)
124133
TracksReleaseBranches = false,
125134
IsReleaseBranch = false,
126135
IsMainline = false,
136+
PreferBranchConfigOverTag = false,
127137
};
128138
}
129139
}

src/GitVersionCore/Model/Configuration/EffectiveConfiguration.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public EffectiveConfiguration(
3636
string commitDateFormat,
3737
bool updateBuildNumber,
3838
int preReleaseWeight,
39-
int tagPreReleaseWeight)
39+
int tagPreReleaseWeight,
40+
bool preferBranchConfigOverTag)
4041
{
4142
AssemblyVersioningScheme = assemblyVersioningScheme;
4243
AssemblyFileVersioningScheme = assemblyFileVersioningScheme;
@@ -68,6 +69,7 @@ public EffectiveConfiguration(
6869
UpdateBuildNumber = updateBuildNumber;
6970
PreReleaseWeight = preReleaseWeight;
7071
TagPreReleaseWeight = tagPreReleaseWeight;
72+
PreferBranchConfigOverTag = preferBranchConfigOverTag;
7173
}
7274

7375
public bool TracksReleaseBranches { get; private set; }
@@ -128,5 +130,7 @@ public EffectiveConfiguration(
128130
public int PreReleaseWeight { get; private set; }
129131

130132
public int TagPreReleaseWeight { get; private set; }
133+
134+
public bool PreferBranchConfigOverTag { get; private set; }
131135
}
132136
}

src/GitVersionCore/VersionCalculation/BaseVersionCalculators/VersionInBranchNameVersionStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override IEnumerable<BaseVersion> GetVersions()
2828
return GetVersions(tagPrefixRegex, currentBranch);
2929
}
3030

31-
internal IEnumerable<BaseVersion> GetVersions(string tagPrefixRegex Branch currentBranch)
31+
internal IEnumerable<BaseVersion> GetVersions(string tagPrefixRegex, Branch currentBranch)
3232
{
3333
if (!Context.FullConfiguration.IsReleaseBranch(currentBranch.NameWithoutOrigin()))
3434
{

src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,27 @@ public SemanticVersion FindVersion()
4242

4343
SemanticVersion taggedSemanticVersion = null;
4444

45+
var branchConfigHasPreReleaseTagConfigured = !string.IsNullOrEmpty(context.Configuration.Tag);
4546
if (context.IsCurrentCommitTagged)
4647
{
47-
// Will always be 0, don't bother with the +0 on tags
48-
var semanticVersionBuildMetaData = mainlineVersionCalculator.CreateVersionBuildMetaData(context.CurrentCommit);
49-
semanticVersionBuildMetaData.CommitsSinceTag = null;
50-
51-
var semanticVersion = new SemanticVersion(context.CurrentCommitTaggedVersion)
48+
// If configured, check if another tag with different branch tag is needed
49+
if (context.Configuration.PreferBranchConfigOverTag && branchConfigHasPreReleaseTagConfigured
50+
&& !context.CurrentCommitTaggedVersion.PreReleaseTag.Name.Equals(context.Configuration.Tag))
5251
{
53-
BuildMetaData = semanticVersionBuildMetaData
54-
};
55-
taggedSemanticVersion = semanticVersion;
52+
log.Info($"Current commit tag does not match the current branch configured expectation. Create another one.");
53+
}
54+
else
55+
{
56+
// Will always be 0, don't bother with the +0 on tags
57+
var semanticVersionBuildMetaData = mainlineVersionCalculator.CreateVersionBuildMetaData(context.CurrentCommit);
58+
semanticVersionBuildMetaData.CommitsSinceTag = null;
59+
60+
var semanticVersion = new SemanticVersion(context.CurrentCommitTaggedVersion)
61+
{
62+
BuildMetaData = semanticVersionBuildMetaData
63+
};
64+
taggedSemanticVersion = semanticVersion;
65+
}
5666
}
5767

5868
var baseVersion = baseVersionCalculator.GetBaseVersion();
@@ -68,7 +78,6 @@ public SemanticVersion FindVersion()
6878
}
6979

7080
var hasPreReleaseTag = semver.PreReleaseTag.HasTag();
71-
var branchConfigHasPreReleaseTagConfigured = !string.IsNullOrEmpty(context.Configuration.Tag);
7281
var preReleaseTagDoesNotMatchConfiguration = hasPreReleaseTag && branchConfigHasPreReleaseTagConfigured && semver.PreReleaseTag.Name != context.Configuration.Tag;
7382
if (!semver.PreReleaseTag.HasTag() && branchConfigHasPreReleaseTagConfigured || preReleaseTagDoesNotMatchConfiguration)
7483
{

0 commit comments

Comments
 (0)