1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2024-12-31 01:53:50 +02:00

feat: add new template fields

This commit is contained in:
Carlos Alexandro Becker 2018-10-03 17:44:31 -03:00 committed by Carlos Alexandro Becker
parent 5c504024b1
commit af99acf244
5 changed files with 56 additions and 25 deletions

View File

@ -1,12 +1,12 @@
package git
import (
"fmt"
"os/exec"
"regexp"
"strings"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/deprecate"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/tmpl"
@ -26,6 +26,9 @@ func (Pipe) Run(ctx *context.Context) error {
if _, err := exec.LookPath("git"); err != nil {
return ErrNoGit
}
if ctx.Config.Git.ShortHash {
deprecate.Notice("git.short_hash")
}
info, err := getInfo(ctx)
if err != nil {
return err
@ -39,8 +42,10 @@ func (Pipe) Run(ctx *context.Context) error {
}
var fakeInfo = context.GitInfo{
CurrentTag: "v0.0.0",
Commit: "none",
CurrentTag: "v0.0.0",
Commit: "none",
ShortCommit: "none",
FullCommit: "none",
}
func getInfo(ctx *context.Context) (context.GitInfo, error) {
@ -63,20 +68,32 @@ func getInfo(ctx *context.Context) (context.GitInfo, error) {
}
func getGitInfo(ctx *context.Context) (context.GitInfo, error) {
commit, err := getCommit(ctx)
short, err := getShortCommit()
if err != nil {
return context.GitInfo{}, errors.Wrap(err, "couldn't get current commit")
}
full, err := getFullCommit()
if err != nil {
return context.GitInfo{}, errors.Wrap(err, "couldn't get current commit")
}
var commit = full
if ctx.Config.Git.ShortHash {
commit = short
}
tag, err := getTag()
if err != nil {
return context.GitInfo{
Commit: commit,
CurrentTag: "v0.0.0",
Commit: commit,
FullCommit: full,
ShortCommit: short,
CurrentTag: "v0.0.0",
}, ErrNoTag
}
return context.GitInfo{
CurrentTag: tag,
Commit: commit,
CurrentTag: tag,
Commit: commit,
FullCommit: full,
ShortCommit: short,
}, nil
}
@ -118,12 +135,12 @@ func validate(ctx *context.Context) error {
return nil
}
func getCommit(ctx *context.Context) (string, error) {
format := "%H"
if ctx.Config.Git.ShortHash {
format = "%h"
}
return git.Clean(git.Run("show", fmt.Sprintf("--format='%s'", format), "HEAD"))
func getShortCommit() (string, error) {
return git.Clean(git.Run("show", "--format='%h'", "HEAD"))
}
func getFullCommit() (string, error) {
return git.Clean(git.Run("show", "--format='%H'", "HEAD"))
}
func getTag() (string, error) {

View File

@ -25,6 +25,8 @@ const (
version = "Version"
tag = "Tag"
commit = "Commit"
shortCommit = "ShortCommit"
fullCommit = "FullCommit"
major = "Major"
minor = "Minor"
patch = "Patch"
@ -48,6 +50,8 @@ func New(ctx *context.Context) *Template {
version: ctx.Version,
tag: ctx.Git.CurrentTag,
commit: ctx.Git.Commit,
shortCommit: ctx.Git.ShortCommit,
fullCommit: ctx.Git.FullCommit,
env: ctx.Env,
date: time.Now().UTC().Format(time.RFC3339),
timestamp: time.Now().UTC().Unix(),

View File

@ -18,15 +18,21 @@ func TestWithArtifact(t *testing.T) {
}
ctx.Version = "1.0.0"
ctx.Git.CurrentTag = "v1.0.0"
ctx.Git.Commit = "commit"
ctx.Git.FullCommit = "fullcommit"
ctx.Git.ShortCommit = "shortcommit"
for expect, tmpl := range map[string]string{
"bar": "{{.Env.FOO}}",
"Linux": "{{.Os}}",
"amd64": "{{.Arch}}",
"6": "{{.Arm}}",
"1.0.0": "{{.Version}}",
"v1.0.0": "{{.Tag}}",
"binary": "{{.Binary}}",
"proj": "{{.ProjectName}}",
"bar": "{{.Env.FOO}}",
"Linux": "{{.Os}}",
"amd64": "{{.Arch}}",
"6": "{{.Arm}}",
"1.0.0": "{{.Version}}",
"v1.0.0": "{{.Tag}}",
"commit": "{{.Commit}}",
"fullcommit": "{{.FullCommit}}",
"shortcommit": "{{.ShortCommit}}",
"binary": "{{.Binary}}",
"proj": "{{.ProjectName}}",
} {
tmpl := tmpl
expect := expect

View File

@ -18,8 +18,10 @@ import (
// GitInfo includes tags and diffs used in some point
type GitInfo struct {
CurrentTag string
Commit string
CurrentTag string
Commit string
ShortCommit string
FullCommit string
}
// Context carries along some data through the pipes

View File

@ -18,7 +18,9 @@ On fields that support templating, this fields are always available:
| `.ProjectName` | the project name |
| `.Version` | the version being released (`v` prefix stripped) |
| `.Tag` | the current git tag |
| `.Commit` | the git commit hash |
| `.ShortCommit` | the git commit short hash |
| `.FullCommit` | the git commit full hash |
| `.Commit` | the git commit hash (deprecated) |
| `.Major` | the major part of the version |
| `.Minor` | the minor part of the version |
| `.Patch` | the patch part of the version |