Skip to content

Commit 69cb49a

Browse files
committed
Adjust behavior of --nextVersion parameter for prepare-release command
Previously, the value of the nextVersion parameter would completely override the version from version.json. With this change the nextVersion parameter only sets the "version part" of the next version and retains the prerelease tags from version.json respectively the firstUnstableTag setting
1 parent cfbb295 commit 69cb49a

File tree

4 files changed

+43
-34
lines changed

4 files changed

+43
-34
lines changed

doc/nbgv-cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ or override the version increment setting.
8787
To explicitly set the next version, run
8888

8989
```ps1
90-
nbgv prepare-release --nextVersion 2.0-beta
90+
nbgv prepare-release --nextVersion 2.0
9191
```
9292

9393
To override the `versionIncrement` setting from `version.json`, run

src/NerdBank.GitVersioning.Tests/ReleaseManagerTests.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using ReleasePreparationError = Nerdbank.GitVersioning.ReleaseManager.ReleasePreparationError;
1212
using ReleasePreparationException = Nerdbank.GitVersioning.ReleaseManager.ReleasePreparationException;
1313
using ReleaseVersionIncrement = Nerdbank.GitVersioning.VersionOptions.ReleaseVersionIncrement;
14+
using Version = System.Version;
1415

1516
public class ReleaseManagerTests : RepoTestBase
1617
{
@@ -232,11 +233,11 @@ public void PrepeareRelease_ReleaseBranchWithVersionDecrement(string initialVers
232233
// versions without prerelease tags
233234
[InlineData("1.2", null, ReleaseVersionIncrement.Minor, "alpha", null, null, null, "v1.2", "1.2", "1.3-alpha")]
234235
[InlineData("1.2", null, ReleaseVersionIncrement.Major, "alpha", null, null, null, "v1.2", "1.2", "2.0-alpha")]
235-
// explicitly set next version (firstUnstableTag setting will be ignored)
236-
[InlineData("1.2-beta", null, null, null, null, "4.5", null, "v1.2", "1.2", "4.5")]
237-
[InlineData("1.2-beta", null, null, null, null, "4.5-pre", null, "v1.2", "1.2", "4.5-pre")]
238-
[InlineData("1.2-beta.{height}", null, null, null, null, "4.5-pre.{height}", null, "v1.2", "1.2", "4.5-pre.{height}")]
239-
// explicitly set version increment overriding the setting from version.json
236+
// explicitly set next version
237+
[InlineData("1.2-beta", null, null, null, null, "4.5", null, "v1.2", "1.2", "4.5-alpha")]
238+
[InlineData("1.2-beta.{height}", null, null, null, null, "4.5", null, "v1.2", "1.2", "4.5-alpha.{height}")]
239+
[InlineData("1.2-beta.{height}", null, null, "pre", null, "4.5.6", null, "v1.2", "1.2", "4.5.6-pre.{height}")]
240+
// explicitly set version increment overriding the setting from ReleaseOptions
240241
[InlineData("1.2-beta", null, ReleaseVersionIncrement.Minor, null, null, null, ReleaseVersionIncrement.Major, "v1.2", "1.2", "2.0-alpha")]
241242
[InlineData("1.2.3-beta", null, ReleaseVersionIncrement.Minor, null, null, null, ReleaseVersionIncrement.Build, "v1.2.3", "1.2.3", "1.2.4-alpha")]
242243
public void PrepareRelease_Master(
@@ -299,7 +300,7 @@ public void PrepareRelease_Master(
299300

300301
// prepare release
301302
var releaseManager = new ReleaseManager();
302-
releaseManager.PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion == null ? null : SemanticVersion.Parse(nextVersion)), parameterVersionIncrement);
303+
releaseManager.PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion == null ? null : Version.Parse(nextVersion)), parameterVersionIncrement);
303304

304305
// check if a branch was created
305306
Assert.Contains(this.Repo.Branches, branch => branch.FriendlyName == expectedBranchName);
@@ -375,7 +376,7 @@ public void PrepareRelease_MasterWithVersionDecrement(string initialVersion, str
375376
// running PrepareRelease should result in an error
376377
// because we're trying to add a prerelease tag to a version without prerelease tag
377378
this.AssertError(
378-
() => new ReleaseManager().PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion == null ? null : SemanticVersion.Parse(nextVersion))),
379+
() => new ReleaseManager().PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion == null ? null : Version.Parse(nextVersion))),
379380
ReleasePreparationError.VersionDecrement);
380381
}
381382

src/NerdBank.GitVersioning/ReleaseManager.cs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
using System;
44
using System.Collections.Generic;
55
using System.IO;
6-
using System.Linq;
76
using LibGit2Sharp;
87
using Validation;
8+
using Version = System.Version;
99
using static Nerdbank.GitVersioning.VersionOptions;
1010

1111
/// <summary>
@@ -116,7 +116,7 @@ public ReleaseManager(TextWriter outputWriter = null, TextWriter errorWriter = n
116116
/// If specified, value will be used instead of the increment specified in <c>version.json</c>.
117117
/// Parameter will be ignored if the current branch is a release branch.
118118
/// </param>
119-
public void PrepareRelease(string projectDirectory, string releaseUnstableTag = null, SemanticVersion nextVersion = null, ReleaseVersionIncrement? versionIncrement = null)
119+
public void PrepareRelease(string projectDirectory, string releaseUnstableTag = null, Version nextVersion = null, ReleaseVersionIncrement? versionIncrement = null)
120120
{
121121
Requires.NotNull(projectDirectory, nameof(projectDirectory));
122122

@@ -142,7 +142,6 @@ public void PrepareRelease(string projectDirectory, string releaseUnstableTag =
142142
var releaseVersion = string.IsNullOrEmpty(releaseUnstableTag)
143143
? versionOptions.Version.WithoutPrepreleaseTags()
144144
: versionOptions.Version.SetFirstPrereleaseTag(releaseUnstableTag);
145-
var nextDevVersion = this.GetNextDevVersion(versionOptions, nextVersion, versionIncrement);
146145

147146
// check if the current branch is the release branch
148147
if (string.Equals(originalBranchName, releaseBranchName, StringComparison.OrdinalIgnoreCase))
@@ -152,6 +151,8 @@ public void PrepareRelease(string projectDirectory, string releaseUnstableTag =
152151
return;
153152
}
154153

154+
var nextDevVersion = this.GetNextDevVersion(versionOptions, nextVersion, versionIncrement);
155+
155156
// check if the release branch already exists
156157
if (repository.Branches[releaseBranchName] != null)
157158
{
@@ -279,31 +280,38 @@ private static bool IsVersionDecrement(SemanticVersion oldVersion, SemanticVersi
279280
}
280281
}
281282

282-
private SemanticVersion GetNextDevVersion(VersionOptions versionOptions, SemanticVersion nextVersion, ReleaseVersionIncrement? versionIncrementOverride)
283+
private SemanticVersion GetNextDevVersion(VersionOptions versionOptions, Version nextVersionOverride, ReleaseVersionIncrement? versionIncrementOverride)
283284
{
284-
if (nextVersion != null)
285-
return nextVersion;
285+
var currentVersion = versionOptions.Version;
286286

287-
// determine the increment to use.
288-
// Use parameter if it has a value, otherwise use setting from version.json
289-
var versionIncrement = versionIncrementOverride ?? versionOptions.ReleaseOrDefault.VersionIncrementOrDefault;
290-
291-
// the increment is only valid if the current version has the required precision
292-
// increment settings "Major" and "Minor" are always valid
293-
// increment setting "Build" is only valid if the version has at lease three segments
294-
var isValidIncrement = versionIncrement != ReleaseVersionIncrement.Build ||
295-
versionOptions.Version.Version.Build >= 0;
296-
297-
// increment is ignored when the next version was specified explicitly
298-
if (!isValidIncrement)
287+
SemanticVersion nextDevVersion;
288+
if(nextVersionOverride != null)
299289
{
300-
this.stderr.WriteLine($"Cannot apply version increment 'build' to version '{versionOptions.Version}' because it only has major and minor segments");
301-
throw new ReleasePreparationException(ReleasePreparationError.InvalidVersionIncrementSetting);
290+
nextDevVersion = new SemanticVersion(nextVersionOverride, currentVersion.Prerelease, currentVersion.BuildMetadata);
291+
}
292+
else
293+
{
294+
// determine the increment to use.
295+
// Use parameter if it has a value, otherwise use setting from version.json
296+
var versionIncrement = versionIncrementOverride ?? versionOptions.ReleaseOrDefault.VersionIncrementOrDefault;
297+
298+
// the increment is only valid if the current version has the required precision
299+
// increment settings "Major" and "Minor" are always valid
300+
// increment setting "Build" is only valid if the version has at lease three segments
301+
var isValidIncrement = versionIncrement != ReleaseVersionIncrement.Build ||
302+
versionOptions.Version.Version.Build >= 0;
303+
304+
// increment is ignored when the next version was specified explicitly
305+
if (!isValidIncrement)
306+
{
307+
this.stderr.WriteLine($"Cannot apply version increment 'build' to version '{versionOptions.Version}' because it only has major and minor segments");
308+
throw new ReleasePreparationException(ReleasePreparationError.InvalidVersionIncrementSetting);
309+
}
310+
311+
nextDevVersion = currentVersion.Increment(versionIncrement);
302312
}
303313

304-
return versionOptions.Version
305-
.Increment(versionIncrement)
306-
.SetFirstPrereleaseTag(versionOptions.ReleaseOrDefault.FirstUnstableTagOrDefault);
314+
return nextDevVersion.SetFirstPrereleaseTag(versionOptions.ReleaseOrDefault.FirstUnstableTagOrDefault);
307315
}
308316
}
309317
}

src/nbgv/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,12 +592,12 @@ private static ExitCodes OnPrepareReleaseCommand(string projectPath, string prer
592592
}
593593

594594
// parse nextVersion if parameter was specified
595-
SemanticVersion nextVersionParsed = default;
595+
Version nextVersionParsed = default;
596596
if (!string.IsNullOrEmpty(nextVersion))
597597
{
598-
if (!SemanticVersion.TryParse(nextVersion, out nextVersionParsed))
598+
if (!Version.TryParse(nextVersion, out nextVersionParsed))
599599
{
600-
Console.Error.WriteLine($"\"{nextVersion}\" is not a semver-compliant version spec.");
600+
Console.Error.WriteLine($"\"{nextVersion}\" is not a valid version spec.");
601601
return ExitCodes.InvalidVersionSpec;
602602
}
603603
}

0 commit comments

Comments
 (0)