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

90 lines
2.1 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"
"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.
// Uncommited 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")
cmd = exec.Command("git", "stash", "--include-untracked", "--quiet")
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stdout
err = cmd.Run()
if err != nil {
return fmt.Errorf("failed stashing changes: %s", stdout.String())
}
}
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:04:41 +01:00
log.Println("Checking out tag")
cmd = exec.Command("git", "checkout", ctx.Git.CurrentTag)
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stdout
if err = cmd.Run(); err != nil {
return fmt.Errorf("failed changing branch: %s", stdout.String())
}
}
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")
cmd := exec.Command("git", "checkout", "-")
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stdout
if err := cmd.Run(); err != nil {
log.Printf("failed changing branch: %s\n", stdout.String())
}
}
if p.dirty {
2017-01-19 10:04:41 +01:00
log.Println("Popping stashed changes")
cmd := exec.Command("git", "stash", "pop")
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stdout
if err := cmd.Run(); err != nil {
log.Printf("failed popping stashed changes: %s\n", stdout.String())
}
}
}