Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update documentation and change the design of the new feature commit …
…message pattern to customize all the message
  • Loading branch information
fmacavilca committed Nov 13, 2023
commit e9f35641b85f5e53dcbb3d22c4ca1e18c7e442b5
6 changes: 3 additions & 3 deletions doc/nbgv-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ In that case, the `NewBranch` property will be `null`.

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

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
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).

For example, running the following command on `master`

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

```
Custom commit message pattern - Set version to '1.0' custom message
Custom commit message pattern '1.0' custom message
```
**Note:** You must consider that commit message pattern must contain only one {0} and valid commit message characters.
**Note:** Must include {0} in the pattern to represent the version.

## Creating a version tag

Expand Down
7 changes: 6 additions & 1 deletion src/NerdBank.GitVersioning/ReleaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,17 @@ private void UpdateVersion(LibGit2Context context, SemanticVersion oldVersion, S
// Author a commit only if we effectively changed something.
if (!context.Repository.Head.Tip.Tree.Equals(context.Repository.Index.WriteToTree()))
{
string commitMessage = string.Format(commitMessagePattern, $"Set version to '{versionOptions.Version}'");
string commitMessage = this.GetCommitMessage(commitMessagePattern, versionOptions.Version);
context.Repository.Commit(commitMessage, signature, signature, new CommitOptions() { AllowEmptyCommit = false });
}
}
}

private string GetCommitMessage(string commitMessagePattern, SemanticVersion version)
{
return commitMessagePattern == "{0}" ? $"Set version to '{version}'" : string.Format(commitMessagePattern, $"'{version}'");
}

private Signature GetSignature(Repository repository)
{
Signature signature = repository.Config.BuildSignature(DateTimeOffset.Now);
Expand Down
40 changes: 9 additions & 31 deletions src/nbgv/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private static Parser BuildCommandLine()
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.");
var versionIncrement = new Option<string>("--versionIncrement", "Overrides the 'versionIncrement' setting set in version.json for determining the next version of the current branch.");
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);
var commitMessagePattern = new Option<string>("--commit-message-pattern", "Custom pattern to add a prefix or suffix to the default commit message.");
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.");
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.")
{
Arity = ArgumentArity.ZeroOrOne,
Expand Down Expand Up @@ -767,16 +767,18 @@ private static Task<int> OnPrepareReleaseCommand(string project, string nextVers
return Task.FromResult((int)ExitCodes.UnsupportedFormat);
}

// validate commit message pattern
if (AreCurlyBracesBalanced(commitMessagePattern))
if (string.IsNullOrEmpty(commitMessagePattern))
{
Console.Error.WriteLine("Commit message pattern contains unbalanced curly braces.");
return Task.FromResult((int)ExitCodes.InvalidCommitMessagePattern);
commitMessagePattern = "{0}";
}

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]\})*$"))
try
{
Console.Error.WriteLine("Commit message pattern must contain only one {0} and valid commit message characters.");
string.Format(commitMessagePattern, "FormatValidator");
}
catch (FormatException ex)
{
Console.Error.WriteLine($"InvalidCommitMessagePattern: {ex.Message}");
return Task.FromResult((int)ExitCodes.InvalidCommitMessagePattern);
}

Expand Down Expand Up @@ -906,29 +908,5 @@ private static void PrintCommits(bool quiet, GitContext context, IEnumerable<Lib
}
}
}

private static bool AreCurlyBracesBalanced(string text)
{
var curlyBraceCount = 0;

foreach (char c in text)
{
if (c == '{')
{
curlyBraceCount++;
}
else if (c == '}')
{
curlyBraceCount--;

if (curlyBraceCount < 0)
{
return true;
}
}
}

return curlyBraceCount != 0;
}
}
}
6 changes: 3 additions & 3 deletions test/Nerdbank.GitVersioning.Tests/ReleaseManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -639,9 +639,9 @@ public void PrepareRelease_ResetsVersionHeightOffset()
}

[Theory]
[InlineData("1.0-beta", "{0} Custom commit message pattern", "Set version to '1.0' Custom commit message pattern")]
[InlineData("1.0-beta", "Custom commit message pattern - {0} custom message", "Custom commit message pattern - Set version to '1.0' custom message")]
[InlineData("1.0-beta", "Custom commit message pattern - {0}", "Custom commit message pattern - Set version to '1.0'")]
[InlineData("1.0-beta", "{0} Custom commit message pattern", "'1.0' Custom commit message pattern")]
[InlineData("1.0-beta", "Custom commit message pattern - {0} custom message", "Custom commit message pattern - '1.0' custom message")]
[InlineData("1.0-beta", "Custom commit message pattern - {0}", "Custom commit message pattern - '1.0'")]
[InlineData("1.0-beta", "{0}", "Set version to '1.0'")]
public void PrepareRelease_WithCustomCommitMessagePattern(string initialVersion, string commitMessagePattern, string expectedCommitMessage)
{
Expand Down