Skip to content

Commit b63cb81

Browse files
committed
Touch-up on usage of new switch
1 parent e9f3564 commit b63cb81

File tree

4 files changed

+39
-40
lines changed

4 files changed

+39
-40
lines changed

doc/nbgv-cli.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ In that case, the `NewBranch` property will be `null`.
167167

168168
### Customizing the `prepare-release` commit message
169169

170-
By default, the `prepare-release` command generates a commit message with the format "Set version to {version}". So with this new option, You can add a prefix or suffix to the default commit message (which will replace just the version).
170+
By default, the `prepare-release` command generates a commit message with the format "Set version to {version}".
171+
A switch allows you to customize the commit message, using `{0}` as a placeholder for the version.
171172

172-
For example, running the following command on `master`
173+
For example, running the following command:
173174

174175
```
175176
nbgv prepare-release --commit-message-pattern "Custom commit message pattern - {0} custom message"
@@ -178,9 +179,8 @@ nbgv prepare-release --commit-message-pattern "Custom commit message pattern - {
178179
So your commit message is going to be this:
179180

180181
```
181-
Custom commit message pattern '1.0' custom message
182+
Custom commit message pattern - 1.0 custom message
182183
```
183-
**Note:** Must include {0} in the pattern to represent the version.
184184

185185
## Creating a version tag
186186

src/NerdBank.GitVersioning/ReleaseManager.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System.Globalization;
45
using LibGit2Sharp;
56
using Nerdbank.GitVersioning.LibGit2;
67
using Newtonsoft.Json;
@@ -127,10 +128,10 @@ public enum ReleaseManagerOutputMode
127128
/// <param name="outputMode">
128129
/// The output format to use for writing to stdout.
129130
/// </param>
130-
/// <param name="commitMessagePattern">
131-
/// Custom pattern to add a prefix or suffix to the default commit message.
131+
/// <param name="unformattedCommitMessage">
132+
/// An optional, custom message to use for the commit that sets the new version number. May use <c>{0}</c> to substitute the new version number.
132133
/// </param>
133-
public void PrepareRelease(string projectDirectory, string releaseUnstableTag = null, Version nextVersion = null, VersionOptions.ReleaseVersionIncrement? versionIncrement = null, ReleaseManagerOutputMode outputMode = default, string commitMessagePattern = "{0}")
134+
public void PrepareRelease(string projectDirectory, string releaseUnstableTag = null, Version nextVersion = null, VersionOptions.ReleaseVersionIncrement? versionIncrement = null, ReleaseManagerOutputMode outputMode = default, string unformattedCommitMessage = null)
134135
{
135136
Requires.NotNull(projectDirectory, nameof(projectDirectory));
136137

@@ -171,7 +172,7 @@ public void PrepareRelease(string projectDirectory, string releaseUnstableTag =
171172
this.WriteToOutput(releaseInfo);
172173
}
173174

174-
this.UpdateVersion(context, versionOptions.Version, releaseVersion, commitMessagePattern);
175+
this.UpdateVersion(context, versionOptions.Version, releaseVersion, unformattedCommitMessage);
175176
return;
176177
}
177178

@@ -195,7 +196,7 @@ public void PrepareRelease(string projectDirectory, string releaseUnstableTag =
195196
// create release branch and update version
196197
Branch releaseBranch = repository.CreateBranch(releaseBranchName);
197198
global::LibGit2Sharp.Commands.Checkout(repository, releaseBranch);
198-
this.UpdateVersion(context, versionOptions.Version, releaseVersion, commitMessagePattern);
199+
this.UpdateVersion(context, versionOptions.Version, releaseVersion, unformattedCommitMessage);
199200

200201
if (outputMode == ReleaseManagerOutputMode.Text)
201202
{
@@ -204,7 +205,7 @@ public void PrepareRelease(string projectDirectory, string releaseUnstableTag =
204205

205206
// update version on main branch
206207
global::LibGit2Sharp.Commands.Checkout(repository, originalBranchName);
207-
this.UpdateVersion(context, versionOptions.Version, nextDevVersion, commitMessagePattern);
208+
this.UpdateVersion(context, versionOptions.Version, nextDevVersion, unformattedCommitMessage);
208209

209210
if (outputMode == ReleaseManagerOutputMode.Text)
210211
{
@@ -264,7 +265,7 @@ private string GetReleaseBranchName(VersionOptions versionOptions)
264265
return branchNameFormat.Replace("{version}", versionOptions.Version.Version.ToString());
265266
}
266267

267-
private void UpdateVersion(LibGit2Context context, SemanticVersion oldVersion, SemanticVersion newVersion, string commitMessagePattern)
268+
private void UpdateVersion(LibGit2Context context, SemanticVersion oldVersion, SemanticVersion newVersion, string unformattedCommitMessage)
268269
{
269270
Requires.NotNull(context, nameof(context));
270271

@@ -293,17 +294,17 @@ private void UpdateVersion(LibGit2Context context, SemanticVersion oldVersion, S
293294
// Author a commit only if we effectively changed something.
294295
if (!context.Repository.Head.Tip.Tree.Equals(context.Repository.Index.WriteToTree()))
295296
{
296-
string commitMessage = this.GetCommitMessage(commitMessagePattern, versionOptions.Version);
297+
if (string.IsNullOrEmpty(unformattedCommitMessage))
298+
{
299+
unformattedCommitMessage = "Set version to '{0}'";
300+
}
301+
302+
string commitMessage = string.Format(CultureInfo.CurrentCulture, unformattedCommitMessage, versionOptions.Version);
297303
context.Repository.Commit(commitMessage, signature, signature, new CommitOptions() { AllowEmptyCommit = false });
298304
}
299305
}
300306
}
301307

302-
private string GetCommitMessage(string commitMessagePattern, SemanticVersion version)
303-
{
304-
return commitMessagePattern == "{0}" ? $"Set version to '{version}'" : string.Format(commitMessagePattern, $"'{version}'");
305-
}
306-
307308
private Signature GetSignature(Repository repository)
308309
{
309310
Signature signature = repository.Config.BuildSignature(DateTimeOffset.Now);

src/nbgv/Program.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private enum ExitCodes
7070
ShallowClone,
7171
InternalError,
7272
InvalidTagNameSetting,
73-
InvalidCommitMessagePattern,
73+
InvalidunformattedCommitMessage,
7474
}
7575

7676
private static bool AlwaysUseLibGit2 => string.Equals(Environment.GetEnvironmentVariable("NBGV_GitEngine"), "LibGit2", StringComparison.Ordinal);
@@ -219,7 +219,7 @@ private static Parser BuildCommandLine()
219219
var nextVersion = new Option<string>("--nextVersion", "The version to set for the current branch. If omitted, the next version is determined automatically by incrementing the current version.");
220220
var versionIncrement = new Option<string>("--versionIncrement", "Overrides the 'versionIncrement' setting set in version.json for determining the next version of the current branch.");
221221
var format = new Option<string>(new[] { "--format", "-f" }, $"The format to write information about the release. Allowed values are: {string.Join(", ", SupportedFormats)}. The default is {DefaultOutputFormat}.").FromAmong(SupportedFormats);
222-
var commitMessagePattern = new Option<string>("--commit-message-pattern", "Custom pattern to add a prefix or suffix to the default commit message. Must include '{0}' in the pattern to represent the version.");
222+
var unformattedCommitMessage = new Option<string>("--commit-message-pattern", "A custom message to use for the commit that changes the version number. May include {0} for the version number. If not specified, the default is \"Set version to '{0}'\".");
223223
var tagArgument = new Argument<string>("tag", "The prerelease tag to apply on the release branch (if any). If not specified, any existing prerelease tag will be removed. The preceding hyphen may be omitted.")
224224
{
225225
Arity = ArgumentArity.ZeroOrOne,
@@ -230,11 +230,11 @@ private static Parser BuildCommandLine()
230230
nextVersion,
231231
versionIncrement,
232232
format,
233-
commitMessagePattern,
233+
unformattedCommitMessage,
234234
tagArgument,
235235
};
236236

237-
prepareRelease.SetHandler(OnPrepareReleaseCommand, project, nextVersion, versionIncrement, format, tagArgument, commitMessagePattern);
237+
prepareRelease.SetHandler(OnPrepareReleaseCommand, project, nextVersion, versionIncrement, format, tagArgument, unformattedCommitMessage);
238238
}
239239

240240
var root = new RootCommand($"{ThisAssembly.AssemblyTitle} v{ThisAssembly.AssemblyInformationalVersion}")
@@ -714,7 +714,7 @@ private static Task<int> OnCloudCommand(string project, string[] metadata, strin
714714
return Task.FromResult((int)ExitCodes.OK);
715715
}
716716

717-
private static Task<int> OnPrepareReleaseCommand(string project, string nextVersion, string versionIncrement, string format, string tag, string commitMessagePattern)
717+
private static Task<int> OnPrepareReleaseCommand(string project, string nextVersion, string versionIncrement, string format, string tag, string unformattedCommitMessage)
718718
{
719719
// validate project path property
720720
string searchPath = GetSpecifiedOrCurrentDirectoryPath(project);
@@ -767,26 +767,24 @@ private static Task<int> OnPrepareReleaseCommand(string project, string nextVers
767767
return Task.FromResult((int)ExitCodes.UnsupportedFormat);
768768
}
769769

770-
if (string.IsNullOrEmpty(commitMessagePattern))
770+
if (!string.IsNullOrEmpty(unformattedCommitMessage))
771771
{
772-
commitMessagePattern = "{0}";
773-
}
774-
775-
try
776-
{
777-
string.Format(commitMessagePattern, "FormatValidator");
778-
}
779-
catch (FormatException ex)
780-
{
781-
Console.Error.WriteLine($"InvalidCommitMessagePattern: {ex.Message}");
782-
return Task.FromResult((int)ExitCodes.InvalidCommitMessagePattern);
772+
try
773+
{
774+
string.Format(unformattedCommitMessage, "FormatValidator");
775+
}
776+
catch (FormatException ex)
777+
{
778+
Console.Error.WriteLine($"Invalid commit message pattern: {ex.Message}");
779+
return Task.FromResult((int)ExitCodes.InvalidunformattedCommitMessage);
780+
}
783781
}
784782

785783
// run prepare-release
786784
try
787785
{
788786
var releaseManager = new ReleaseManager(Console.Out, Console.Error);
789-
releaseManager.PrepareRelease(searchPath, tag, nextVersionParsed, versionIncrementParsed, outputMode, commitMessagePattern);
787+
releaseManager.PrepareRelease(searchPath, tag, nextVersionParsed, versionIncrementParsed, outputMode, unformattedCommitMessage);
790788
return Task.FromResult((int)ExitCodes.OK);
791789
}
792790
catch (ReleaseManager.ReleasePreparationException ex)

test/Nerdbank.GitVersioning.Tests/ReleaseManagerTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,10 @@ public void PrepareRelease_ResetsVersionHeightOffset()
639639
}
640640

641641
[Theory]
642-
[InlineData("1.0-beta", "{0} Custom commit message pattern", "'1.0' Custom commit message pattern")]
643-
[InlineData("1.0-beta", "Custom commit message pattern - {0} custom message", "Custom commit message pattern - '1.0' custom message")]
644-
[InlineData("1.0-beta", "Custom commit message pattern - {0}", "Custom commit message pattern - '1.0'")]
645-
[InlineData("1.0-beta", "{0}", "Set version to '1.0'")]
642+
[InlineData("1.0-beta", "{0} Custom commit message pattern", "1.0 Custom commit message pattern")]
643+
[InlineData("1.0-beta", "Custom commit message pattern - {0} custom message", "Custom commit message pattern - 1.0 custom message")]
644+
[InlineData("1.0-beta", "Custom commit message pattern - {0}", "Custom commit message pattern - 1.0")]
645+
[InlineData("1.0-beta", "{0}", "1.0")]
646646
public void PrepareRelease_WithCustomCommitMessagePattern(string initialVersion, string commitMessagePattern, string expectedCommitMessage)
647647
{
648648
// Create and configure the repository
@@ -657,7 +657,7 @@ public void PrepareRelease_WithCustomCommitMessagePattern(string initialVersion,
657657

658658
// Run PrepareRelease with the custom commit message pattern
659659
var releaseManager = new ReleaseManager();
660-
releaseManager.PrepareRelease(this.RepoPath, commitMessagePattern: commitMessagePattern);
660+
releaseManager.PrepareRelease(this.RepoPath, unformattedCommitMessage: commitMessagePattern);
661661

662662
// Verify that the commit message on the release branch matches the expected pattern
663663
string releaseBranchName = "v1.0";

0 commit comments

Comments
 (0)