Repository: goreleaser/goreleaser
Pull request #3968 (2023-05-02 03:31:29)

fix: pass tree state as ldflag
needs #3967 

cloudflare-pages[bot]: ## Deploying with &nbsp;<a href="https://pages.dev"><img alt="Cloudflare Pages" src="https://user-images.githubusercontent.com/23264/106598434-9e719e00-654f-11eb-9e59-6167043cfa01.png" width="16"></a> &nbsp;Cloudflare Pages

<table><tr><td><strong>Latest commit:</strong> </td><td>
<code>774e795</code>
</td></tr>
<tr><td><strong>Status:</strong></td><td>&nbsp;✅&nbsp; Deploy successful!</td></tr>
<tr><td><strong>Preview URL:</strong></td><td>
<a href='https://a0eff296.goreleaser.pages.dev'>https://a0eff296.goreleaser.pages.dev</a>
</td></tr>
<tr><td><strong>Branch Preview URL:</strong></td><td>
<a href='https://pass-dirty.goreleaser.pages.dev'>https://pass-dirty.goreleaser.pages.dev</a>
</td></tr>
</table>

[View logs](https://dash.cloudflare.com/?to=/:account/pages/view/goreleaser/a0eff296-c3b3-4465-977d-1c5cd27fdcde)

codecov[bot]: ## [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/3968?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#3968](https://codecov.io/gh/goreleaser/goreleaser/pull/3968?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (774e795) into [main](https://codecov.io/gh/goreleaser/goreleaser/commit/59e7f1bd5affadf6522409f5978347382d5e96fd?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (59e7f1b) will **decrease** coverage by `0.03%`.
> The diff coverage is `0.00%`.

```diff
@@            Coverage Diff             @@
##             main    #3968      +/-   ##
==========================================
- Coverage   83.84%   83.82%   -0.03%     
==========================================
  Files         127      127              
  Lines       11250    11253       +3     
==========================================
  Hits         9433     9433              
- Misses       1457     1459       +2     
- Partials      360      361       +1     
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/3968?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [main.go](https://codecov.io/gh/goreleaser/goreleaser/pull/3968?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-bWFpbi5nbw==) | `62.85% <0.00%> (-5.90%)` | :arrow_down: |



Patch:
diff --git a/.goreleaser.yaml b/.goreleaser.yaml
index 1d431735..e1b0cc56 100644
--- a/.goreleaser.yaml
+++ b/.goreleaser.yaml
@@ -37,7 +37,7 @@ builds:
   flags:
     - -trimpath
   ldflags:
-    - -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{ .CommitDate }} -X main.builtBy=goreleaser
+    - -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{ .CommitDate }} -X main.builtBy=goreleaser -X main.treeState={{ .IsGitDirty }}
 
 universal_binaries:
 - replace: false
diff --git a/internal/pipe/git/git.go b/internal/pipe/git/git.go
index 950063c7..29deabf8 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/tmpl/tmpl.go b/internal/tmpl/tmpl.go
index b87de932..f9edf1a6 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/main.go b/main.go
index 0b96724c..22d6a42b 100644
--- a/main.go
+++ b/main.go
@@ -15,10 +15,11 @@ import (
 
 // nolint: gochecknoglobals
 var (
-	version = ""
-	commit  = ""
-	date    = ""
-	builtBy = ""
+	version   = ""
+	commit    = ""
+	treeState = ""
+	date      = ""
+	builtBy   = ""
 )
 
 func init() {
@@ -55,6 +56,9 @@ func buildVersion(version, commit, date, builtBy string) goversion.Info {
 			if commit != "" {
 				i.GitCommit = commit
 			}
+			if treeState != "" {
+				i.GitTreeState = treeState
+			}
 			if date != "" {
 				i.BuildDate = date
 			}
diff --git a/pkg/context/context.go b/pkg/context/context.go
index 0e95be54..24aa5acd 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 97104f1f..6bc623ca 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]


Test patch:
diff --git a/internal/pipe/git/git_test.go b/internal/pipe/git/git_test.go
index 4c540ef3..d2c3f0bc 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_test.go b/internal/tmpl/tmpl_test.go
index 90b8bda6..720109f3 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.*\" }}",