-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexec.go
More file actions
64 lines (59 loc) · 1.6 KB
/
Copy pathexec.go
File metadata and controls
64 lines (59 loc) · 1.6 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"errors"
"fmt"
"os/exec"
"strings"
)
func git(args ...string) (string, error) { return execCmd("git", args...) }
func gh(args ...string) (string, error) { return execCmd("gh", args...) }
func jj(args ...string) (string, error) { return execCmd("jj", args...) }
type execError struct {
exitCode int
err error
output string
}
func (e *execError) Error() string {
var b strings.Builder
b.WriteString(fmt.Sprintf("exit code %d", e.exitCode))
if e.output != "" {
b.WriteString("\n")
b.WriteString(strings.TrimSpace(e.output))
}
return b.String()
}
func execCmd(name string, args ...string) (string, error) {
if config.verbose {
var b strings.Builder
b.WriteString(name)
for _, arg := range args {
b.WriteString(" ")
if strings.Contains(arg, " ") {
b.WriteString(fmt.Sprintf("%q", arg))
} else {
b.WriteString(arg)
}
}
debugf(b.String())
}
// jj-workspace mode: GIT_DIR is exported in LoadConfig via os.Setenv so every
// git subprocess picks up the backing .git path. GIT_WORK_TREE is left unset
// on purpose so a stray checkout/reset can't clobber jj's working copy.
output, err := exec.Command(name, args...).CombinedOutput()
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
err = &execError{exitCode: exitErr.ExitCode(), err: err, output: string(output)}
} else {
err = &execError{exitCode: 199, err: err, output: string(output)}
}
}
if config.verbose {
if err != nil {
debugf(err.Error())
} else {
debugf(strings.TrimSpace(string(output)))
}
}
return strings.TrimSpace(string(output)), err
}