fix(template): address regression in string functions for template engine (#142)

It appears that `Sprig` flips the inputs of the `contains`, `hasPrefix`, `replace`
and `hasSuffix` `strings` methods. This appears to be the cause of the regression.

See: https://github.com/Masterminds/sprig/blob/master/functions.go#L149-L152

This results in a regression against the previous implementations of the template
functions.

Signed-off-by: Derek Smith <derek@clokwork.net>
This commit is contained in:
Derek Smith 2021-04-13 16:27:42 -05:00 committed by GitHub
parent 7cc56b1256
commit fdd421b057
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View file

@ -439,9 +439,22 @@ Options to detect notes contained in commit bodies.
## Templates
The `git-chglog` template uses the `text/template` package and enhanced templating functions provided by [Sprig](http://masterminds.github.io/sprig). For basic usage please refer to the following.
The `git-chglog` template uses the `text/template` package and enhanced templating functions provided by [Sprig]. For basic usage please refer to the following.
> [text/template](https://golang.org/pkg/text/template/)
- [text/template](https://golang.org/pkg/text/template/)
- [Sprig]
We have implemented the following custom template functions. These override functions provided by [Sprig].
| Name | Signature | Description |
| :----------- | :-------------------------------------------- | :---------------------------------------------------------------------------- |
| `contains` | `func(s, substr string) bool` | Reports whether `substr` is within `s` using `strings.Contains` |
| `datetime` | `func(layout string, input time.Time) string` | Generate a formatted Date string based on layout |
| `hasPrefix` | `func(s, prefix string) bool` | Tests whether the string `s` begins with `prefix` using `strings.HasPrefix` |
| `hasSuffix` | `func(s, suffix string) bool` | Tests whether the string `s` ends with `suffix`. using `strings.HasPrefix` |
| `indent` | `func(s string, n int) string` | Indent all lines of `s` by `n` spaces |
| `replace` | `func(s, old, new string, n int) string` | Replace `old` with `new` within string `s`, `n` times using `strings.Replace` |
| `upperFirst` | `func(s string) string` | Upper case the first character of a string |
If you are not satisfied with the prepared template please try customizing one.
@ -729,3 +742,4 @@ See [CHANGELOG.md](./CHANGELOG.md)
[golangci-lint]: https://golangci-lint.run/usage/install/#local-installation
[issues]: https://github.com/git-chglog/git-chglog/issues
[git-chglog/artwork]: https://github.com/git-chglog/artwork
[Sprig]: http://masterminds.github.io/sprig

View file

@ -340,6 +340,13 @@ func (gen *Generator) render(w io.Writer, unreleased *Unreleased, versions []*Ve
pad := strings.Repeat(" ", n)
return pad + strings.ReplaceAll(s, "\n", "\n"+pad)
},
// While Sprig provides these functions, they change the standard input
// order which leads to a regression. For an example see:
// https://github.com/Masterminds/sprig/blob/master/functions.go#L149
"contains": strings.Contains,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix,
"replace": strings.Replace,
}
fname := filepath.Base(gen.config.Template)