From 4330b522ea7e1e5e7d048ab2238b59b8a18eae79 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 2 May 2023 01:04:18 -0300 Subject: [PATCH] feat: IsGitDirty template variable (#3967) will only ever be true on snapshots or when ran with `--skip-validate`. This should be useful as a ldflag, for example. --- internal/pipe/git/git.go | 1 + internal/pipe/git/git_test.go | 3 +++ internal/tmpl/tmpl.go | 2 ++ internal/tmpl/tmpl_test.go | 2 ++ pkg/context/context.go | 1 + www/docs/customization/templates.md | 1 + 6 files changed, 10 insertions(+) diff --git a/internal/pipe/git/git.go b/internal/pipe/git/git.go index 950063c7e..29deabf8e 100644 --- a/internal/pipe/git/git.go +++ b/internal/pipe/git/git.go @@ -164,6 +164,7 @@ func getGitInfo(ctx *context.Context) (context.GitInfo, error) { TagSubject: subject, TagContents: contents, TagBody: body, + Dirty: CheckDirty(ctx) != nil, }, nil } diff --git a/internal/pipe/git/git_test.go b/internal/pipe/git/git_test.go index 4c540ef3a..d2c3f0bc4 100644 --- a/internal/pipe/git/git_test.go +++ b/internal/pipe/git/git_test.go @@ -114,10 +114,12 @@ func TestDirty(t *testing.T) { t.Run("skip validate is set", func(t *testing.T) { ctx := testctx.New(testctx.SkipValidate) testlib.AssertSkipped(t, Pipe{}.Run(ctx)) + require.True(t, ctx.Git.Dirty) }) t.Run("snapshot", func(t *testing.T) { ctx := testctx.New(testctx.Snapshot) testlib.AssertSkipped(t, Pipe{}.Run(ctx)) + require.True(t, ctx.Git.Dirty) }) } @@ -235,6 +237,7 @@ func TestValidState(t *testing.T) { require.Equal(t, "v0.0.3", ctx.Git.CurrentTag) require.Equal(t, "git@github.com:foo/bar.git", ctx.Git.URL) require.NotEmpty(t, ctx.Git.FirstCommit) + require.False(t, ctx.Git.Dirty) } func TestSnapshotNoTags(t *testing.T) { diff --git a/internal/tmpl/tmpl.go b/internal/tmpl/tmpl.go index b87de9320..f9edf1a6a 100644 --- a/internal/tmpl/tmpl.go +++ b/internal/tmpl/tmpl.go @@ -45,6 +45,7 @@ const ( tagContents = "TagContents" tagBody = "TagBody" releaseURL = "ReleaseURL" + isGitDirty = "IsGitDirty" major = "Major" minor = "Minor" patch = "Patch" @@ -98,6 +99,7 @@ func New(ctx *context.Context) *Template { commitDate: ctx.Git.CommitDate.UTC().Format(time.RFC3339), commitTimestamp: ctx.Git.CommitDate.UTC().Unix(), gitURL: ctx.Git.URL, + isGitDirty: ctx.Git.Dirty, env: ctx.Env, date: ctx.Date.UTC().Format(time.RFC3339), timestamp: ctx.Date.UTC().Unix(), diff --git a/internal/tmpl/tmpl_test.go b/internal/tmpl/tmpl_test.go index 90b8bda61..720109f39 100644 --- a/internal/tmpl/tmpl_test.go +++ b/internal/tmpl/tmpl_test.go @@ -35,6 +35,7 @@ func TestWithArtifact(t *testing.T) { TagSubject: "awesome release", TagContents: "awesome release\n\nanother line", TagBody: "another line", + Dirty: true, }), testctx.WithEnv(map[string]string{ "FOO": "bar", @@ -88,6 +89,7 @@ func TestWithArtifact(t *testing.T) { "1678327562": `{{ .Timestamp }}`, "snapshot true": `snapshot {{.IsSnapshot}}`, "draft true": `draft {{.IsDraft}}`, + "dirty true": `dirty {{.IsGitDirty}}`, "remove this": "{{ filter .Env.MULTILINE \".*remove.*\" }}", "something with\nmultiple lines\nto test things": "{{ reverseFilter .Env.MULTILINE \".*remove.*\" }}", diff --git a/pkg/context/context.go b/pkg/context/context.go index 0e95be54f..24aa5acdf 100644 --- a/pkg/context/context.go +++ b/pkg/context/context.go @@ -32,6 +32,7 @@ type GitInfo struct { TagSubject string TagContents string TagBody string + Dirty bool } // Env is the environment variables. diff --git a/www/docs/customization/templates.md b/www/docs/customization/templates.md index 97104f1f4..6bc623ca7 100644 --- a/www/docs/customization/templates.md +++ b/www/docs/customization/templates.md @@ -27,6 +27,7 @@ Key |Description `.CommitDate` |the UTC commit date in RFC 3339 format `.CommitTimestamp` |the UTC commit date in Unix format `.GitURL` |the git remote url +`.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] `.Patch` |the patch part of the version[^tag-is-semver]