1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-26 04:22:05 +02:00

86 lines
2.0 KiB
Go
Raw Normal View History

// Package source provides pipes to take care of using the correct source files.
// For the releasing process we need the files of the tag we are releasing.
package source
import (
"bytes"
2017-01-19 10:24:04 +01:00
"errors"
"fmt"
"log"
"os/exec"
"github.com/goreleaser/goreleaser/context"
)
// Pipe to use the latest Git tag as source.
type Pipe struct {
dirty bool
wrongBranch bool
}
// Description of the pipe
func (p *Pipe) Description() string {
2017-01-19 10:04:41 +01:00
return "Using source from latest tag"
}
// Run uses the latest tag as source.
2017-01-19 10:24:04 +01:00
// Uncommitted changes are stashed.
func (p *Pipe) Run(ctx *context.Context) error {
cmd := exec.Command("git", "diff-index", "--quiet", "HEAD", "--")
err := cmd.Run()
dirty := err != nil
if dirty {
2017-01-19 10:04:41 +01:00
log.Println("Stashing changes")
2017-01-19 10:24:04 +01:00
if err = run("git", "stash", "--include-untracked", "--quiet"); err != nil {
return fmt.Errorf("failed stashing changes: %v", err)
}
}
p.dirty = dirty
cmd = exec.Command("git", "describe", "--exact-match", "--match", ctx.Git.CurrentTag)
err = cmd.Run()
wrongBranch := err != nil
if wrongBranch {
2017-01-19 10:30:15 +01:00
log.Println("Checking out tag", ctx.Git.CurrentTag)
2017-01-19 10:24:04 +01:00
if err = run("git", "checkout", ctx.Git.CurrentTag); err != nil {
return fmt.Errorf("failed changing branch: %v", err)
}
}
p.wrongBranch = wrongBranch
return nil
}
// Clean switches back to the original branch and restores changes.
func (p *Pipe) Clean(ctx *context.Context) {
if p.wrongBranch {
2017-01-19 10:04:41 +01:00
log.Println("Checking out original branch")
2017-01-19 10:24:04 +01:00
if err := run("git", "checkout", "-"); err != nil {
log.Println("failed changing branch: ", err.Error())
}
}
if p.dirty {
2017-01-19 10:04:41 +01:00
log.Println("Popping stashed changes")
2017-01-19 10:24:04 +01:00
if err := run("git", "stash", "pop"); err != nil {
log.Println("failed popping stashed changes:", err.Error())
}
}
}
2017-01-19 10:24:04 +01:00
func run(bin string, args ...string) error {
cmd := exec.Command(bin, args...)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()
if err != nil {
return errors.New(out.String())
}
return nil
}