-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
38 lines (32 loc) · 762 Bytes
/
git.go
File metadata and controls
38 lines (32 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package branchcleaner
import (
"bytes"
"fmt"
"os/exec"
"strings"
)
func runGit(repoPath string, args ...string) (string, error) {
cmd := exec.Command("git", args...)
cmd.Dir = repoPath
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
out := stdout.String()
if err != nil {
msg := strings.TrimSpace(stderr.String())
if msg == "" {
msg = strings.TrimSpace(out)
}
if msg != "" {
return out, fmt.Errorf("git %s: %s", strings.Join(args, " "), msg)
}
return out, fmt.Errorf("git %s: %w", strings.Join(args, " "), err)
}
return out, nil
}
func refExists(repoPath, ref string) bool {
_, err := runGit(repoPath, "show-ref", "--verify", "--quiet", ref)
return err == nil
}