2017-08-19 12:47:04 -03:00
|
|
|
// Package git provides an integration with the git command
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os/exec"
|
2017-10-16 15:43:26 -02:00
|
|
|
"strings"
|
2018-01-27 15:50:44 -02:00
|
|
|
|
|
|
|
"github.com/apex/log"
|
2017-08-19 12:47:04 -03:00
|
|
|
)
|
|
|
|
|
2017-10-16 15:43:26 -02:00
|
|
|
// IsRepo returns true if current folder is a git repository
|
2018-08-15 00:26:57 -03:00
|
|
|
func IsRepo() bool {
|
2017-10-16 15:43:26 -02:00
|
|
|
out, err := Run("rev-parse", "--is-inside-work-tree")
|
2018-08-15 00:26:57 -03:00
|
|
|
return err == nil && strings.TrimSpace(out) == "true"
|
2017-10-16 15:43:26 -02:00
|
|
|
}
|
|
|
|
|
2017-08-19 12:47:04 -03:00
|
|
|
// Run runs a git command and returns its output or errors
|
2018-01-28 12:03:46 -02:00
|
|
|
func Run(args ...string) (string, error) {
|
2018-07-03 23:44:51 -07:00
|
|
|
// TODO: use exex.CommandContext here and refactor.
|
2017-11-26 12:09:12 -02:00
|
|
|
/* #nosec */
|
2017-08-19 12:47:04 -03:00
|
|
|
var cmd = exec.Command("git", args...)
|
2018-01-27 15:50:44 -02:00
|
|
|
log.WithField("args", args).Debug("running git")
|
2018-07-03 23:44:51 -07:00
|
|
|
bts, err := cmd.CombinedOutput()
|
|
|
|
log.WithField("output", string(bts)).
|
2018-07-03 23:41:36 -07:00
|
|
|
Debug("git result")
|
|
|
|
if err != nil {
|
2018-07-03 23:44:51 -07:00
|
|
|
return "", errors.New(string(bts))
|
2017-08-19 12:47:04 -03:00
|
|
|
}
|
2018-07-03 23:44:51 -07:00
|
|
|
return string(bts), nil
|
2017-08-19 12:47:04 -03:00
|
|
|
}
|
2017-10-15 20:21:35 -02:00
|
|
|
|
|
|
|
// Clean the output
|
|
|
|
func Clean(output string, err error) (string, error) {
|
2018-02-25 20:17:45 -03:00
|
|
|
output = strings.Replace(strings.Split(output, "\n")[0], "'", "", -1)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New(strings.TrimSuffix(err.Error(), "\n"))
|
|
|
|
}
|
|
|
|
return output, err
|
2017-10-15 20:21:35 -02:00
|
|
|
}
|