2017-08-19 17:47:04 +02:00
|
|
|
// Package git provides an integration with the git command
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2018-01-28 16:03:46 +02:00
|
|
|
"bytes"
|
2017-08-19 17:47:04 +02:00
|
|
|
"errors"
|
|
|
|
"os/exec"
|
2017-10-16 19:43:26 +02:00
|
|
|
"strings"
|
2018-01-27 19:50:44 +02:00
|
|
|
|
|
|
|
"github.com/apex/log"
|
2017-08-19 17:47:04 +02:00
|
|
|
)
|
|
|
|
|
2017-10-16 19:43:26 +02:00
|
|
|
// IsRepo returns true if current folder is a git repository
|
|
|
|
func IsRepo() bool {
|
|
|
|
out, err := Run("rev-parse", "--is-inside-work-tree")
|
|
|
|
return err == nil && strings.TrimSpace(out) == "true"
|
|
|
|
}
|
|
|
|
|
2017-08-19 17:47:04 +02:00
|
|
|
// Run runs a git command and returns its output or errors
|
2018-01-28 16:03:46 +02:00
|
|
|
func Run(args ...string) (string, error) {
|
2017-11-26 16:09:12 +02:00
|
|
|
/* #nosec */
|
2017-08-19 17:47:04 +02:00
|
|
|
var cmd = exec.Command("git", args...)
|
2018-01-27 19:50:44 +02:00
|
|
|
log.WithField("args", args).Debug("running git")
|
2018-01-28 16:03:46 +02:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return "", errors.New(stderr.String())
|
2017-08-19 17:47:04 +02:00
|
|
|
}
|
2018-01-28 16:03:46 +02:00
|
|
|
log.WithField("output", stdout.String()).Debug("git result")
|
|
|
|
return stdout.String(), nil
|
2017-08-19 17:47:04 +02:00
|
|
|
}
|
2017-10-16 00:21:35 +02:00
|
|
|
|
|
|
|
// Clean the output
|
|
|
|
func Clean(output string, err error) (string, error) {
|
|
|
|
return strings.Replace(strings.Split(output, "\n")[0], "'", "", -1), err
|
|
|
|
}
|