This repository has no description
1.3 kB
60 lines
1package git
2
3import (
4 "fmt"
5 "os/exec"
6 "syscall"
7)
8
9const (
10 fieldSeparator = "\x1f" // ASCII Unit Separator
11 recordSeparator = "\x1e" // ASCII Record Separator
12)
13
14func (g *GitRepo) runGitCmd(command string, extraArgs ...string) ([]byte, error) {
15 var args []string
16 args = append(args, command)
17 args = append(args, extraArgs...)
18
19 cmd := exec.Command("git", args...)
20
21 if g.sandbox != nil {
22 var wrapErr error
23 cmd, wrapErr = g.sandbox.Wrap(g.path, cmd)
24 if wrapErr != nil {
25 return nil, fmt.Errorf("sandbox wrap: %w", wrapErr)
26 }
27 } else {
28 cmd.Dir = g.path
29 }
30
31 if cmd.SysProcAttr == nil {
32 cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
33 }
34
35 out, err := cmd.Output()
36 if err != nil {
37 if exitErr, ok := err.(*exec.ExitError); ok {
38 return nil, fmt.Errorf("%w, stderr: %s", err, string(exitErr.Stderr))
39 }
40 return nil, err
41 }
42
43 return out, nil
44}
45
46func (g *GitRepo) revList(extraArgs ...string) ([]byte, error) {
47 return g.runGitCmd("rev-list", extraArgs...)
48}
49
50func (g *GitRepo) forEachRef(extraArgs ...string) ([]byte, error) {
51 return g.runGitCmd("for-each-ref", extraArgs...)
52}
53
54func (g *GitRepo) revParse(extraArgs ...string) ([]byte, error) {
55 return g.runGitCmd("rev-parse", extraArgs...)
56}
57
58func (g *GitRepo) mergeBase(extraArgs ...string) ([]byte, error) {
59 return g.runGitCmd("merge-base", extraArgs...)
60}