1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

feat(tmpl): .GitTreeState and .IsGitClean

Avoids having too much template if blocks inside the configuration file.
This commit is contained in:
Carlos Alexandro Becker 2024-01-18 08:07:38 -03:00
parent d15dab3a79
commit 1ba3138c67
No known key found for this signature in database
3 changed files with 12 additions and 0 deletions

View File

@ -46,6 +46,8 @@ const (
tagBody = "TagBody"
releaseURL = "ReleaseURL"
isGitDirty = "IsGitDirty"
isGitClean = "IsGitClean"
gitTreeState = "GitTreeState"
major = "Major"
minor = "Minor"
patch = "Patch"
@ -83,6 +85,10 @@ const (
func New(ctx *context.Context) *Template {
sv := ctx.Semver
rawVersionV := fmt.Sprintf("%d.%d.%d", sv.Major, sv.Minor, sv.Patch)
treeState := "clean"
if ctx.Git.Dirty {
treeState = "dirty"
}
fields := map[string]interface{}{}
for k, v := range map[string]interface{}{
@ -101,6 +107,8 @@ func New(ctx *context.Context) *Template {
commitTimestamp: ctx.Git.CommitDate.UTC().Unix(),
gitURL: ctx.Git.URL,
isGitDirty: ctx.Git.Dirty,
isGitClean: !ctx.Git.Dirty,
gitTreeState: treeState,
env: ctx.Env,
date: ctx.Date.UTC().Format(time.RFC3339),
timestamp: ctx.Date.UTC().Unix(),

View File

@ -92,6 +92,8 @@ func TestWithArtifact(t *testing.T) {
"nightly false": `nightly {{.IsNightly}}`,
"draft true": `draft {{.IsDraft}}`,
"dirty true": `dirty {{.IsGitDirty}}`,
"clean false": `clean {{.IsGitClean}}`,
"state dirty": `state {{.GitTreeState}}`,
"env bar: barrrrr": `env bar: {{ envOrDefault "BAR" "barrrrr" }}`,
"env foo: bar": `env foo: {{ envOrDefault "FOO" "barrrrr" }}`,

View File

@ -25,6 +25,8 @@ In fields that support templates, these fields are always available:
| `.CommitDate` | the UTC commit date in RFC 3339 format |
| `.CommitTimestamp` | the UTC commit date in Unix format |
| `.GitURL` | the git remote url |
| `.GitTreeState` | either 'clean' or 'dirty'. Since v1.24 |
| `.IsGitClean` | whether or not current git state is clean. Since v1.24 |
| `.IsGitDirty` | whether or not current git state is dirty. Since v1.19 |
| `.Major` | the major part of the version[^tag-is-semver] |
| `.Minor` | the minor part of the version[^tag-is-semver] |