Skip to content

Commit e9f3564

Browse files
committed
update documentation and change the design of the new feature commit message pattern to customize all the message
1 parent b5b8a5e commit e9f3564

File tree

4 files changed

+21
-38
lines changed

4 files changed

+21
-38
lines changed

doc/nbgv-cli.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ 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 commit message with this format "Set version to {version}". So with this new option. You can add a prefix or suffix to the default commit message
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).
171171

172172
For example, running the following command on `master`
173173

@@ -178,9 +178,9 @@ nbgv prepare-release --commit-message-pattern "Custom commit message pattern - {
178178
So your commit message is going to be this:
179179

180180
```
181-
Custom commit message pattern - Set version to '1.0' custom message
181+
Custom commit message pattern '1.0' custom message
182182
```
183-
**Note:** You must consider that commit message pattern must contain only one {0} and valid commit message characters.
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,17 @@ private void UpdateVersion(LibGit2Context context, SemanticVersion oldVersion, S
293293
// Author a commit only if we effectively changed something.
294294
if (!context.Repository.Head.Tip.Tree.Equals(context.Repository.Index.WriteToTree()))
295295
{
296-
string commitMessage = string.Format(commitMessagePattern, $"Set version to '{versionOptions.Version}'");
296+
string commitMessage = this.GetCommitMessage(commitMessagePattern, versionOptions.Version);
297297
context.Repository.Commit(commitMessage, signature, signature, new CommitOptions() { AllowEmptyCommit = false });
298298
}
299299
}
300300
}
301301

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

src/nbgv/Program.cs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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.");
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.");
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,
@@ -767,16 +767,18 @@ private static Task<int> OnPrepareReleaseCommand(string project, string nextVers
767767
return Task.FromResult((int)ExitCodes.UnsupportedFormat);
768768
}
769769

770-
// validate commit message pattern
771-
if (AreCurlyBracesBalanced(commitMessagePattern))
770+
if (string.IsNullOrEmpty(commitMessagePattern))
772771
{
773-
Console.Error.WriteLine("Commit message pattern contains unbalanced curly braces.");
774-
return Task.FromResult((int)ExitCodes.InvalidCommitMessagePattern);
772+
commitMessagePattern = "{0}";
775773
}
776774

777-
if (!Regex.IsMatch(commitMessagePattern, @"^(?=.*\{0\})(?!.*\{[1-9]\})(?!.*\{\{0\}\})(?!.*\{0\}.*\{0\})[^\x00-\x08\x0B\x0C\x0E-\x1F<>'\""\\]*(?:\{0\}[^\x00-\x08\x0B\x0C\x0E-\x1F<>'\""\\]*)*(?!\{[1-9]\})*$"))
775+
try
778776
{
779-
Console.Error.WriteLine("Commit message pattern must contain only one {0} and valid commit message characters.");
777+
string.Format(commitMessagePattern, "FormatValidator");
778+
}
779+
catch (FormatException ex)
780+
{
781+
Console.Error.WriteLine($"InvalidCommitMessagePattern: {ex.Message}");
780782
return Task.FromResult((int)ExitCodes.InvalidCommitMessagePattern);
781783
}
782784

@@ -906,29 +908,5 @@ private static void PrintCommits(bool quiet, GitContext context, IEnumerable<Lib
906908
}
907909
}
908910
}
909-
910-
private static bool AreCurlyBracesBalanced(string text)
911-
{
912-
var curlyBraceCount = 0;
913-
914-
foreach (char c in text)
915-
{
916-
if (c == '{')
917-
{
918-
curlyBraceCount++;
919-
}
920-
else if (c == '}')
921-
{
922-
curlyBraceCount--;
923-
924-
if (curlyBraceCount < 0)
925-
{
926-
return true;
927-
}
928-
}
929-
}
930-
931-
return curlyBraceCount != 0;
932-
}
933911
}
934912
}

test/Nerdbank.GitVersioning.Tests/ReleaseManagerTests.cs

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

641641
[Theory]
642-
[InlineData("1.0-beta", "{0} Custom commit message pattern", "Set version to '1.0' Custom commit message pattern")]
643-
[InlineData("1.0-beta", "Custom commit message pattern - {0} custom message", "Custom commit message pattern - Set version to '1.0' custom message")]
644-
[InlineData("1.0-beta", "Custom commit message pattern - {0}", "Custom commit message pattern - 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'")]
645645
[InlineData("1.0-beta", "{0}", "Set version to '1.0'")]
646646
public void PrepareRelease_WithCustomCommitMessagePattern(string initialVersion, string commitMessagePattern, string expectedCommitMessage)
647647
{

0 commit comments

Comments
 (0)