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