-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.go
More file actions
50 lines (44 loc) · 1.73 KB
/
Copy pathrun.go
File metadata and controls
50 lines (44 loc) · 1.73 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
package clone
import (
"context"
"os"
"os/exec"
"time"
)
const DefaultWaitDelay = 10 * time.Second
// remoteEnv is the hardening env every remote-touching Git invocation in this
// package sets. GIT_ALLOW_PROTOCOL is a hard whitelist that overrides
// protocol.*.allow config, so an ambient url.<base>.insteadOf that rewrites an
// https:// URL to file://, ssh://, or ext:: is refused after ValidateURL has
// already approved the input. A caller that needs another protocol (or a test
// using file:// via insteadOf) sets GIT_ALLOW_PROTOCOL in its own env; the
// value here defers to that. GIT_PROTOCOL_FROM_USER=0 is kept for older Git
// that predates GIT_ALLOW_PROTOCOL.
func remoteEnv() []string {
env := []string{
"GIT_TERMINAL_PROMPT=0",
"GIT_PROTOCOL_FROM_USER=0",
}
if os.Getenv("GIT_ALLOW_PROTOCOL") == "" {
env = append(env, "GIT_ALLOW_PROTOCOL=https")
}
return env
}
// Runner runs one Git invocation and returns its combined output.
type Runner func(ctx context.Context, dir string, env []string, args ...string) (string, error)
// Run executes Git with the production WaitDelay.
func Run(ctx context.Context, dir string, env []string, args ...string) (string, error) {
return RunnerWithWaitDelay(DefaultWaitDelay)(ctx, dir, env, args...)
}
// RunnerWithWaitDelay returns a Runner with a bounded wait for transport
// children that retain Git's output pipe after Git itself exits.
func RunnerWithWaitDelay(waitDelay time.Duration) Runner {
return func(ctx context.Context, dir string, env []string, args ...string) (string, error) {
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), env...)
cmd.WaitDelay = waitDelay
out, err := cmd.CombinedOutput()
return string(out), err
}
}