RunGitCommand executes a git command in the specified directory. This is exported for use in tests and other packages that need direct git access.
(ctx context.Context, dir string, args ...string)
| 31 | // RunGitCommand executes a git command in the specified directory. |
| 32 | // This is exported for use in tests and other packages that need direct git access. |
| 33 | func RunGitCommand(ctx context.Context, dir string, args ...string) (out string, rerr error) { |
| 34 | slog.Info(fmt.Sprintf("[%s] $ git %s", dir, strings.Join(args, " "))) |
| 35 | defer func() { |
| 36 | slog.Info(fmt.Sprintf("[%s] $ git %s (DONE)", dir, strings.Join(args, " ")), "err", rerr) |
| 37 | }() |
| 38 | |
| 39 | cmd := exec.CommandContext(ctx, "git", args...) |
| 40 | cmd.Dir = dir |
| 41 | |
| 42 | output, err := cmd.CombinedOutput() |
| 43 | if err != nil { |
| 44 | var exitErr *exec.ExitError |
| 45 | if errors.As(err, &exitErr) { |
| 46 | return "", fmt.Errorf("git command failed (exit code %d): %w\nOutput: %s", |
| 47 | exitErr.ExitCode(), err, string(output)) |
| 48 | } |
| 49 | return "", fmt.Errorf("git command failed: %w", err) |
| 50 | } |
| 51 | |
| 52 | return string(output), nil |
| 53 | } |
| 54 | |
| 55 | // RunInteractiveGitCommand executes a git command in the specified directory in interactive mode. |
| 56 | func RunInteractiveGitCommand(ctx context.Context, dir string, w io.Writer, args ...string) (rerr error) { |