Skip to content

Commit

Permalink
feat: add support for git branch in templates (#1906)
Browse files Browse the repository at this point in the history
Add a `.Branch` field to the text templates populated by the current git
branch.

Signed-off-by: Ben Kochie <superq@gmail.com>

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
SuperQ and kodiakhq[bot] committed Nov 18, 2020
1 parent fa9bb22 commit f417d77
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/pipe/git/git.go
Expand Up @@ -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",
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -93,6 +99,7 @@ func getGitInfo() (context.GitInfo, error) {
}, ErrNoTag
}
return context.GitInfo{
Branch: branch,
CurrentTag: tag,
Commit: full,
FullCommit: full,
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions internal/pipe/git/git_test.go
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions internal/tmpl/tmpl.go
Expand Up @@ -29,6 +29,7 @@ const (
version = "Version"
rawVersion = "RawVersion"
tag = "Tag"
branch = "Branch"
commit = "Commit"
shortCommit = "ShortCommit"
fullCommit = "FullCommit"
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions internal/tmpl/tmpl_test.go
Expand Up @@ -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"
Expand All @@ -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}}",
Expand Down
1 change: 1 addition & 0 deletions pkg/context/context.go
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions www/docs/customization/templates.md
Expand Up @@ -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 |
Expand Down

1 comment on commit f417d77

@vercel
Copy link

@vercel vercel bot commented on f417d77 Nov 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.