Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for git branch in templates #1906

Merged
merged 4 commits into from Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"))
SuperQ marked this conversation as resolved.
Show resolved Hide resolved
}

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