1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/pipe/git/git.go
Paul Tyng 0d4f605388
feat: deterministic / reproducible build support (#1641)
* Make checksum ordering consistent

* Use consistent time for build date

* Add commit date to templates

* Add config option for build mod timestamp

* Make goreleaser builds reproducible

* Fix error in wording

* Update www/docs/customization/build.md
2020-07-06 17:09:22 -03:00

161 lines
3.6 KiB
Go

package git
import (
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/apex/log"
"github.com/pkg/errors"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/pkg/context"
)
// Pipe that sets up git state.
type Pipe struct{}
func (Pipe) String() string {
return "getting and validating git state"
}
// Run the pipe.
func (Pipe) Run(ctx *context.Context) error {
if _, err := exec.LookPath("git"); err != nil {
return ErrNoGit
}
info, err := getInfo(ctx)
if err != nil {
return err
}
ctx.Git = info
log.Infof("releasing %s, commit %s", info.CurrentTag, info.Commit)
ctx.Version = strings.TrimPrefix(ctx.Git.CurrentTag, "v")
return validate(ctx)
}
// nolint: gochecknoglobals
var fakeInfo = context.GitInfo{
CurrentTag: "v0.0.0",
Commit: "none",
ShortCommit: "none",
FullCommit: "none",
}
func getInfo(ctx *context.Context) (context.GitInfo, error) {
if !git.IsRepo() && ctx.Snapshot {
log.Warn("accepting to run without a git repo because this is a snapshot")
return fakeInfo, nil
}
if !git.IsRepo() {
return context.GitInfo{}, ErrNotRepository
}
info, err := getGitInfo()
if err != nil && ctx.Snapshot {
log.WithError(err).Warn("ignoring errors because this is a snapshot")
if info.Commit == "" {
info = fakeInfo
}
return info, nil
}
return info, err
}
func getGitInfo() (context.GitInfo, error) {
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")
}
date, err := getCommitDate()
if err != nil {
return context.GitInfo{}, errors.Wrap(err, "couldn't get commit date")
}
url, err := getURL()
if err != nil {
return context.GitInfo{}, errors.Wrap(err, "couldn't get remote URL")
}
tag, err := getTag()
if err != nil {
return context.GitInfo{
Commit: full,
FullCommit: full,
ShortCommit: short,
CommitDate: date,
URL: url,
CurrentTag: "v0.0.0",
}, ErrNoTag
}
return context.GitInfo{
CurrentTag: tag,
Commit: full,
FullCommit: full,
ShortCommit: short,
CommitDate: date,
URL: url,
}, nil
}
func validate(ctx *context.Context) error {
if ctx.Snapshot {
return pipe.ErrSnapshotEnabled
}
if ctx.SkipValidate {
return pipe.ErrSkipValidateEnabled
}
out, err := git.Run("status", "--porcelain")
if strings.TrimSpace(out) != "" || err != nil {
return ErrDirty{status: out}
}
_, err = git.Clean(git.Run("describe", "--exact-match", "--tags", "--match", ctx.Git.CurrentTag))
if err != nil {
return ErrWrongRef{
commit: ctx.Git.Commit,
tag: ctx.Git.CurrentTag,
}
}
return nil
}
func getCommitDate() (time.Time, error) {
ct, err := git.Clean(git.Run("show", "--format='%ct'", "HEAD", "--quiet"))
if err != nil {
return time.Time{}, err
}
if ct == "" {
return time.Time{}, nil
}
i, err := strconv.ParseInt(ct, 10, 64)
if err != nil {
return time.Time{}, err
}
t := time.Unix(i, 0).UTC()
return t, nil
}
func getShortCommit() (string, error) {
return git.Clean(git.Run("show", "--format='%h'", "HEAD", "--quiet"))
}
func getFullCommit() (string, error) {
return git.Clean(git.Run("show", "--format='%H'", "HEAD", "--quiet"))
}
func getTag() (string, error) {
if tag := os.Getenv("GORELEASER_CURRENT_TAG"); tag != "" {
return tag, nil
}
return git.Clean(git.Run("describe", "--tags", "--abbrev=0"))
}
func getURL() (string, error) {
return git.Clean(git.Run("ls-remote", "--get-url"))
}