gitee.com/mirrors_opencollective/goreleaser@v0.45.0/pipeline/git/git.go (about)

     1  package git
     2  
     3  import (
     4  	"bytes"
     5  	"regexp"
     6  	"strings"
     7  	"text/template"
     8  	"time"
     9  
    10  	"github.com/apex/log"
    11  	"github.com/goreleaser/goreleaser/context"
    12  	"github.com/goreleaser/goreleaser/internal/git"
    13  	"github.com/goreleaser/goreleaser/pipeline"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  // Pipe for brew deployment
    18  type Pipe struct{}
    19  
    20  func (Pipe) String() string {
    21  	return "getting and validating git state"
    22  }
    23  
    24  // Run the pipe
    25  func (Pipe) Run(ctx *context.Context) (err error) {
    26  	tag, commit, err := getInfo()
    27  	if err != nil {
    28  		return
    29  	}
    30  	if tag == "" && !ctx.Snapshot {
    31  		return ErrNoTag
    32  	}
    33  	ctx.Git = context.GitInfo{
    34  		CurrentTag: tag,
    35  		Commit:     commit,
    36  	}
    37  	log.Infof("releasing %s, commit %s", tag, commit)
    38  	if err = setVersion(ctx, tag, commit); err != nil {
    39  		return
    40  	}
    41  	if !ctx.Validate {
    42  		return pipeline.Skip("--skip-validate is set")
    43  	}
    44  	return validate(ctx, commit, tag)
    45  }
    46  
    47  func setVersion(ctx *context.Context, tag, commit string) (err error) {
    48  	if ctx.Snapshot {
    49  		snapshotName, err := getSnapshotName(ctx, tag, commit)
    50  		if err != nil {
    51  			return errors.Wrap(err, "failed to generate snapshot name")
    52  		}
    53  		ctx.Version = snapshotName
    54  		return nil
    55  	}
    56  	// removes usual `v` prefix
    57  	ctx.Version = strings.TrimPrefix(tag, "v")
    58  	return
    59  }
    60  
    61  type snapshotNameData struct {
    62  	Commit    string
    63  	Tag       string
    64  	Timestamp int64
    65  }
    66  
    67  func getSnapshotName(ctx *context.Context, tag, commit string) (string, error) {
    68  	tmpl, err := template.New("snapshot").Parse(ctx.Config.Snapshot.NameTemplate)
    69  	var out bytes.Buffer
    70  	if err != nil {
    71  		return "", err
    72  	}
    73  	var data = snapshotNameData{
    74  		Commit:    commit,
    75  		Tag:       tag,
    76  		Timestamp: time.Now().Unix(),
    77  	}
    78  	err = tmpl.Execute(&out, data)
    79  	return out.String(), err
    80  }
    81  
    82  func validate(ctx *context.Context, commit, tag string) error {
    83  	out, err := git.Run("status", "--porcelain")
    84  	if strings.TrimSpace(out) != "" || err != nil {
    85  		return ErrDirty{out}
    86  	}
    87  	if ctx.Snapshot {
    88  		return nil
    89  	}
    90  	if !regexp.MustCompile("^[0-9.]+").MatchString(ctx.Version) {
    91  		return ErrInvalidVersionFormat{ctx.Version}
    92  	}
    93  	_, err = git.Clean(git.Run("describe", "--exact-match", "--tags", "--match", tag))
    94  	if err != nil {
    95  		return ErrWrongRef{commit, tag}
    96  	}
    97  	return nil
    98  }
    99  
   100  func getInfo() (tag, commit string, err error) {
   101  	tag, err = git.Clean(git.Run("describe", "--tags", "--abbrev=0"))
   102  	if err != nil {
   103  		log.WithError(err).Info("failed to retrieve current tag")
   104  	}
   105  	commit, err = git.Clean(git.Run("show", "--format='%H'", "HEAD"))
   106  	return
   107  }