1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-04-25 12:24:44 +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
internal
pkg/context
www/content

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

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

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

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

@ -18,7 +18,9 @@ On fields that support templating, this fields are always available:
| `.ProjectName` | the project name | | `.ProjectName` | the project name |
| `.Version` | the version being released (`v` prefix stripped) | | `.Version` | the version being released (`v` prefix stripped) |
| `.Tag` | the current git tag | | `.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 | | `.Major` | the major part of the version |
| `.Minor` | the minor part of the version | | `.Minor` | the minor part of the version |
| `.Patch` | the patch part of the version | | `.Patch` | the patch part of the version |