1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/git/git.go

69 lines
1.5 KiB
Go
Raw Normal View History

2017-08-19 17:47:04 +02:00
// Package git provides an integration with the git command
package git
import (
"bytes"
2017-08-19 17:47:04 +02:00
"errors"
"os/exec"
"strings"
"github.com/apex/log"
2017-08-19 17:47:04 +02:00
)
// IsRepo returns true if current folder is a git repository.
2018-08-15 05:26:57 +02:00
func IsRepo() bool {
out, err := Run("rev-parse", "--is-inside-work-tree")
2018-08-15 05:26:57 +02:00
return err == nil && strings.TrimSpace(out) == "true"
}
// RunEnv runs a git command with the specified env vars and returns its output or errors.
func RunEnv(env map[string]string, args ...string) (string, error) {
2018-07-04 08:44:51 +02:00
// TODO: use exex.CommandContext here and refactor.
var extraArgs = []string{
"-c", "log.showSignature=false",
}
args = append(extraArgs, args...)
/* #nosec */
2017-08-19 17:47:04 +02:00
var cmd = exec.Command("git", args...)
if env != nil {
cmd.Env = []string{}
for k, v := range env {
cmd.Env = append(cmd.Env, k+"="+v)
}
}
stdout := bytes.Buffer{}
stderr := bytes.Buffer{}
cmd.Stdout = &stdout
cmd.Stderr = &stderr
log.WithField("args", args).Debug("running git")
err := cmd.Run()
log.WithField("stdout", stdout.String()).
WithField("stderr", stderr.String()).
2018-07-04 08:41:36 +02:00
Debug("git result")
2018-07-04 08:41:36 +02:00
if err != nil {
return "", errors.New(stderr.String())
2017-08-19 17:47:04 +02:00
}
return stdout.String(), nil
2017-08-19 17:47:04 +02:00
}
// Run runs a git command and returns its output or errors.
func Run(args ...string) (string, error) {
return RunEnv(nil, args...)
}
// Clean the output.
func Clean(output string, err error) (string, error) {
output = strings.Replace(strings.Split(output, "\n")[0], "'", "", -1)
if err != nil {
err = errors.New(strings.TrimSuffix(err.Error(), "\n"))
}
return output, err
}