2017-01-19 09:47:17 +01:00
|
|
|
// 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"
|
2017-01-19 09:47:17 +01:00
|
|
|
"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"
|
2017-01-19 09:47:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run uses the latest tag as source.
|
2017-01-19 10:24:04 +01:00
|
|
|
// Uncommitted changes are stashed.
|
2017-01-19 09:47:17 +01:00
|
|
|
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)
|
2017-01-19 09:47:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2017-01-19 09:47:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
2017-01-19 09:47:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 09:47:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|