Repository: goreleaser/goreleaser
Pull request #1906 (2020-11-17 16:42:36)

feat: add support for git branch in templates
Add a `.Branch` field to the text templates populated by the current git
branch.

Signed-off-by: Ben Kochie <superq@gmail.com>

vercel[bot]: @SuperQ is attempting to deploy a commit to the **Goreleaser** Team on [Vercel](https://vercel.com).

A member of the Team first needs to [authorize it](https://vercel.com/git/authorize?team=Goreleaser&type=github&job=%7B%22headInfo%22%3A%7B%22sha%22%3A%22dd751456e225078582cd4a06ae5c797b9e9a32d6%22%7D%2C%22id%22%3A%22QmNi9mxoc1DedgARNeXSbAmfoFskLKk31u6Rjt3v7XWf4D%22%2C%22org%22%3A%22goreleaser%22%2C%22prId%22%3A1906%2C%22repo%22%3A%22goreleaser%22%7D).


codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1906?src=pr&el=h1) Report
> Merging [#1906](https://codecov.io/gh/goreleaser/goreleaser/pull/1906?src=pr&el=desc) (25adac2) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/a704baea0bea6e226c2241ed838d3f32aecde622?el=desc) (a704bae) will **decrease** coverage by `0.01%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/1906/graphs/tree.svg?width=650&height=150&src=pr&token=3bJvv6b7hN)](https://codecov.io/gh/goreleaser/goreleaser/pull/1906?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1906      +/-   ##
==========================================
- Coverage   81.78%   81.76%   -0.02%     
==========================================
  Files          73       73              
  Lines        3827     3834       +7     
==========================================
+ Hits         3130     3135       +5     
- Misses        573      574       +1     
- Partials      124      125       +1     
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/1906?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/context/context.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1906/diff?src=pr&el=tree#diff-cGtnL2NvbnRleHQvY29udGV4dC5nbw==) | `100.00% <ø> (ø)` | |
| [internal/pipe/git/git.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1906/diff?src=pr&el=tree#diff-aW50ZXJuYWwvcGlwZS9naXQvZ2l0Lmdv) | `86.59% <100.00%> (-1.32%)` | :arrow_down: |
| [internal/tmpl/tmpl.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1906/diff?src=pr&el=tree#diff-aW50ZXJuYWwvdG1wbC90bXBsLmdv) | `96.47% <100.00%> (+0.04%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1906?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1906?src=pr&el=footer). Last update [a704bae...25adac2](https://codecov.io/gh/goreleaser/goreleaser/pull/1906?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/internal/pipe/git/git.go b/internal/pipe/git/git.go
index 0aab8f30..2d2ba64a 100644
--- a/internal/pipe/git/git.go
+++ b/internal/pipe/git/git.go
@@ -39,6 +39,7 @@ func (Pipe) Run(ctx *context.Context) error {
 
 // nolint: gochecknoglobals
 var fakeInfo = context.GitInfo{
+	Branch:      "none",
 	CurrentTag:  "v0.0.0",
 	Commit:      "none",
 	ShortCommit: "none",
@@ -65,6 +66,10 @@ func getInfo(ctx *context.Context) (context.GitInfo, error) {
 }
 
 func getGitInfo() (context.GitInfo, error) {
+	branch, err := getBranch()
+	if err != nil {
+		return context.GitInfo{}, fmt.Errorf("couldn't get current branch: %w", err)
+	}
 	short, err := getShortCommit()
 	if err != nil {
 		return context.GitInfo{}, fmt.Errorf("couldn't get current commit: %w", err)
@@ -84,6 +89,7 @@ func getGitInfo() (context.GitInfo, error) {
 	tag, err := getTag()
 	if err != nil {
 		return context.GitInfo{
+			Branch:      branch,
 			Commit:      full,
 			FullCommit:  full,
 			ShortCommit: short,
@@ -93,6 +99,7 @@ func getGitInfo() (context.GitInfo, error) {
 		}, ErrNoTag
 	}
 	return context.GitInfo{
+		Branch:      branch,
 		CurrentTag:  tag,
 		Commit:      full,
 		FullCommit:  full,
@@ -123,6 +130,10 @@ func validate(ctx *context.Context) error {
 	return nil
 }
 
+func getBranch() (string, error) {
+	return git.Clean(git.Run("rev-parse", "--abbrev-ref", "HEAD", "--quiet"))
+}
+
 func getCommitDate() (time.Time, error) {
 	ct, err := git.Clean(git.Run("show", "--format='%ct'", "HEAD", "--quiet"))
 	if err != nil {
diff --git a/internal/tmpl/tmpl.go b/internal/tmpl/tmpl.go
index 42ab9828..0c83f9a5 100644
--- a/internal/tmpl/tmpl.go
+++ b/internal/tmpl/tmpl.go
@@ -29,6 +29,7 @@ const (
 	version         = "Version"
 	rawVersion      = "RawVersion"
 	tag             = "Tag"
+	branch          = "Branch"
 	commit          = "Commit"
 	shortCommit     = "ShortCommit"
 	fullCommit      = "FullCommit"
@@ -74,6 +75,7 @@ func New(ctx *context.Context) *Template {
 			version:         ctx.Version,
 			rawVersion:      rawVersionV,
 			tag:             ctx.Git.CurrentTag,
+			branch:          ctx.Git.Branch,
 			commit:          ctx.Git.Commit,
 			shortCommit:     ctx.Git.ShortCommit,
 			fullCommit:      ctx.Git.FullCommit,
diff --git a/pkg/context/context.go b/pkg/context/context.go
index 517f47d1..fa1f604e 100644
--- a/pkg/context/context.go
+++ b/pkg/context/context.go
@@ -18,6 +18,7 @@ import (
 
 // GitInfo includes tags and diffs used in some point.
 type GitInfo struct {
+	Branch      string
 	CurrentTag  string
 	Commit      string
 	ShortCommit string
diff --git a/www/docs/customization/templates.md b/www/docs/customization/templates.md
index 25c73b36..077f8b21 100644
--- a/www/docs/customization/templates.md
+++ b/www/docs/customization/templates.md
@@ -14,6 +14,7 @@ On fields that support templating, these fields are always available:
 |--------------------|------------------------------------------------------------------------------------------------------------------------------|
 | `.ProjectName`     | the project name                                                                                                             |
 | `.Version`         | the version being released (`v` prefix stripped),<br>or `{{ .Tag }}-SNAPSHOT-{{ .ShortCommit }}` in case of snapshot release |
+| `.Branch`          | the current git branch                                                                                                       |
 | `.Tag`             | the current git tag                                                                                                          |
 | `.ShortCommit`     | the git commit short hash                                                                                                    |
 | `.FullCommit`      | the git commit full hash                                                                                                     |


Test patch:
diff --git a/internal/pipe/git/git_test.go b/internal/pipe/git/git_test.go
index 7024d42e..d11f6d3a 100644
--- a/internal/pipe/git/git_test.go
+++ b/internal/pipe/git/git_test.go
@@ -41,6 +41,21 @@ func TestSingleCommit(t *testing.T) {
 	require.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
 }
 
+func TestBranch(t *testing.T) {
+	_, back := testlib.Mktmp(t)
+	defer back()
+	testlib.GitInit(t)
+	testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
+	testlib.GitCommit(t, "test-branch-commit")
+	testlib.GitTag(t, "test-branch-tag")
+	testlib.GitCheckoutBranch(t, "test-branch")
+	var ctx = &context.Context{
+		Config: config.Project{},
+	}
+	require.NoError(t, Pipe{}.Run(ctx))
+	require.Equal(t, "test-branch", ctx.Git.Branch)
+}
+
 func TestNoRemote(t *testing.T) {
 	_, back := testlib.Mktmp(t)
 	defer back()
diff --git a/internal/tmpl/tmpl_test.go b/internal/tmpl/tmpl_test.go
index 4f8125be..4964ab6f 100644
--- a/internal/tmpl/tmpl_test.go
+++ b/internal/tmpl/tmpl_test.go
@@ -26,6 +26,7 @@ func TestWithArtifact(t *testing.T) {
 		Minor: 2,
 		Patch: 3,
 	}
+	ctx.Git.Branch = "test-branch"
 	ctx.Git.Commit = "commit"
 	ctx.Git.FullCommit = "fullcommit"
 	ctx.Git.ShortCommit = "shortcommit"
@@ -38,6 +39,7 @@ func TestWithArtifact(t *testing.T) {
 		"1.2.3":       "{{.Version}}",
 		"v1.2.3":      "{{.Tag}}",
 		"1-2-3":       "{{.Major}}-{{.Minor}}-{{.Patch}}",
+		"test-branch": "{{.Branch}}",
 		"commit":      "{{.Commit}}",
 		"fullcommit":  "{{.FullCommit}}",
 		"shortcommit": "{{.ShortCommit}}",