2017-08-19 12:47:04 -03:00
|
|
|
// Package git provides an integration with the git command
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2020-08-14 10:12:55 -03:00
|
|
|
"bytes"
|
2022-04-12 08:35:19 -03:00
|
|
|
"context"
|
2017-08-19 12:47:04 -03:00
|
|
|
"errors"
|
|
|
|
"os/exec"
|
2017-10-16 15:43:26 -02:00
|
|
|
"strings"
|
2018-01-27 15:50:44 -02:00
|
|
|
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
2017-08-19 12:47:04 -03:00
|
|
|
)
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// IsRepo returns true if current folder is a git repository.
|
2022-04-12 08:35:19 -03:00
|
|
|
func IsRepo(ctx context.Context) bool {
|
|
|
|
out, err := Run(ctx, "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
|
|
|
}
|
|
|
|
|
2022-04-12 08:35:19 -03:00
|
|
|
func RunWithEnv(ctx context.Context, env []string, args ...string) (string, error) {
|
2021-04-25 14:20:49 -03:00
|
|
|
extraArgs := []string{
|
2019-02-06 20:14:04 +01:00
|
|
|
"-c", "log.showSignature=false",
|
|
|
|
}
|
|
|
|
args = append(extraArgs, args...)
|
2017-11-26 12:09:12 -02:00
|
|
|
/* #nosec */
|
2022-04-12 08:35:19 -03:00
|
|
|
cmd := exec.CommandContext(ctx, "git", args...)
|
2020-07-06 16:09:22 -04:00
|
|
|
|
2020-08-14 10:12:55 -03:00
|
|
|
stdout := bytes.Buffer{}
|
|
|
|
stderr := bytes.Buffer{}
|
|
|
|
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
2022-01-20 14:59:39 -03:00
|
|
|
cmd.Env = append(cmd.Env, env...)
|
2020-08-14 10:12:55 -03:00
|
|
|
|
2018-01-27 15:50:44 -02:00
|
|
|
log.WithField("args", args).Debug("running git")
|
2020-08-14 10:12:55 -03:00
|
|
|
err := cmd.Run()
|
|
|
|
|
|
|
|
log.WithField("stdout", stdout.String()).
|
|
|
|
WithField("stderr", stderr.String()).
|
2018-07-03 23:41:36 -07:00
|
|
|
Debug("git result")
|
2020-08-14 10:12:55 -03:00
|
|
|
|
2018-07-03 23:41:36 -07:00
|
|
|
if err != nil {
|
2020-08-14 10:12:55 -03:00
|
|
|
return "", errors.New(stderr.String())
|
2017-08-19 12:47:04 -03:00
|
|
|
}
|
2020-08-14 10:12:55 -03:00
|
|
|
|
|
|
|
return stdout.String(), nil
|
2017-08-19 12:47:04 -03:00
|
|
|
}
|
2017-10-15 20:21:35 -02:00
|
|
|
|
2022-01-20 14:59:39 -03:00
|
|
|
// Run runs a git command and returns its output or errors.
|
2022-04-12 08:35:19 -03:00
|
|
|
func Run(ctx context.Context, args ...string) (string, error) {
|
|
|
|
return RunWithEnv(ctx, []string{}, args...)
|
2022-01-20 14:59:39 -03:00
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Clean the output.
|
2017-10-15 20:21:35 -02:00
|
|
|
func Clean(output string, err error) (string, error) {
|
2020-09-21 10:13:03 -03:00
|
|
|
output = strings.ReplaceAll(strings.Split(output, "\n")[0], "'", "")
|
2018-02-25 20:17:45 -03:00
|
|
|
if err != nil {
|
|
|
|
err = errors.New(strings.TrimSuffix(err.Error(), "\n"))
|
|
|
|
}
|
|
|
|
return output, err
|
2017-10-15 20:21:35 -02:00
|
|
|
}
|
2022-10-13 23:59:20 -03:00
|
|
|
|
|
|
|
// CleanAllLines returns all the non empty lines of the output, cleaned up.
|
|
|
|
func CleanAllLines(output string, err error) ([]string, error) {
|
|
|
|
var result []string
|
|
|
|
for _, line := range strings.Split(output, "\n") {
|
|
|
|
l := strings.TrimSpace(strings.ReplaceAll(line, "'", ""))
|
|
|
|
if l == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = append(result, l)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New(strings.TrimSuffix(err.Error(), "\n"))
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|