1package repository
 2
 3import (
 4	"bytes"
 5	"fmt"
 6	"io"
 7	"os/exec"
 8	"strings"
 9)
10
11// gitCli is a helper to launch CLI git commands
12type gitCli struct {
13	path string
14}
15
16// Run the given git command with the given I/O reader/writers, returning an error if it fails.
17func (cli gitCli) runGitCommandWithIO(stdin io.Reader, stdout, stderr io.Writer, args ...string) error {
18	// make sure that the working directory for the command
19	// always exist, in particular when running "git init".
20	path := strings.TrimSuffix(cli.path, ".git")
21
22	// fmt.Printf("[%s] Running git %s\n", path, strings.Join(args, " "))
23
24	cmd := exec.Command("git", args...)
25	cmd.Dir = path
26	cmd.Stdin = stdin
27	cmd.Stdout = stdout
28	cmd.Stderr = stderr
29
30	return cmd.Run()
31}
32
33// Run the given git command and return its stdout, or an error if the command fails.
34func (cli gitCli) runGitCommandRaw(stdin io.Reader, args ...string) (string, string, error) {
35	var stdout bytes.Buffer
36	var stderr bytes.Buffer
37	err := cli.runGitCommandWithIO(stdin, &stdout, &stderr, args...)
38	return strings.TrimSpace(stdout.String()), strings.TrimSpace(stderr.String()), err
39}
40
41// Run the given git command and return its stdout, or an error if the command fails.
42func (cli gitCli) runGitCommandWithStdin(stdin io.Reader, args ...string) (string, error) {
43	stdout, stderr, err := cli.runGitCommandRaw(stdin, args...)
44	if err != nil {
45		if stderr == "" {
46			stderr = "Error running git command: " + strings.Join(args, " ")
47		}
48		err = fmt.Errorf(stderr)
49	}
50	return stdout, err
51}
52
53// Run the given git command and return its stdout, or an error if the command fails.
54func (cli gitCli) runGitCommand(args ...string) (string, error) {
55	return cli.runGitCommandWithStdin(nil, args...)
56}