Debian Codegraph @

subprocess/subprocess.go

raw symbols SCIP
1 // Package subprocess provides helper functions for forking new processes
2 // NOTE: Subject to change, do not rely on this package from outside git-lfs source
3 package subprocessscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/
4
5 import (
6 "bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/"
7 "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/"
8 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
9 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
10 "os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/"
11 "regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/"
12 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
13 "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/"
14
15 "github.com/git-lfs/git-lfs/v3/trscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tr`/"
16 "github.com/rubyist/tracerxscip-go gomod github.com/rubyist/tracerx 787959303086 `github.com/rubyist/tracerx`/"
17 )
18
19 // BufferedExec starts up a command and creates a stdin pipe and a buffered
20 // stdout & stderr pipes, wrapped in a BufferedCmd. The stdout buffer will be
21 // of stdoutBufSize bytes.
22 func BufferedExecscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/BufferedExec().(name string, args ...string) (*BufferedCmdscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/BufferedCmd#, error) {
23 cmd, err := ExecCommandscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/ExecCommand().(name, args...)
24 if err != nil {
25 return nil, err
26 }
27 stdout, err := cmd.StdoutPipescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/Cmd#StdoutPipe().()
28 if err != nil {
29 return nil, err
30 }
31 stderr, err := cmd.StderrPipescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/Cmd#StderrPipe().()
32 if err != nil {
33 return nil, err
34 }
35
36 stdin, err := cmd.StdinPipescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/Cmd#StdinPipe().()
37 if err != nil {
38 return nil, err
39 }
40
41 if err := cmd.Startscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/Cmd#Start().(); err != nil {
42 return nil, err
43 }
44
45 return &BufferedCmdscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/BufferedCmd#{
46 cmd,
47 stdin,
48 bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/.NewReaderSizescip-go gomod github.com/golang/go/src go1.25.0 bufio/NewReaderSize().(stdout, stdoutBufSizescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/stdoutBufSize.),
49 bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/.NewReaderSizescip-go gomod github.com/golang/go/src go1.25.0 bufio/NewReaderSize().(stderr, stdoutBufSizescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/stdoutBufSize.),
50 }, nil
51 }
52
53 // StdoutBufferedExec starts up a command and creates a stdin pipe and a
54 // buffered stdout pipe, with stderr directed to /dev/null, wrapped in a
55 // BufferedCmd. The stdout buffer will be of stdoutBufSize bytes.
56 func StdoutBufferedExecscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/StdoutBufferedExec().(name string, args ...string) (*BufferedCmdscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/BufferedCmd#, error) {
57 cmd, err := ExecCommandscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/ExecCommand().(name, args...)
58 if err != nil {
59 return nil, err
60 }
61 stdout, err := cmd.StdoutPipescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/Cmd#StdoutPipe().()
62 if err != nil {
63 return nil, err
64 }
65
66 stdin, err := cmd.StdinPipescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/Cmd#StdinPipe().()
67 if err != nil {
68 return nil, err
69 }
70
71 if err := cmd.Startscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/Cmd#Start().(); err != nil {
72 return nil, err
73 }
74
75 return &BufferedCmdscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/BufferedCmd#{
76 cmd,
77 stdin,
78 bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/.NewReaderSizescip-go gomod github.com/golang/go/src go1.25.0 bufio/NewReaderSize().(stdout, stdoutBufSizescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/stdoutBufSize.),
79 nil,
80 }, nil
81 }
82
83 // SimpleExec is a small wrapper around os/exec.Command.
84 func SimpleExecscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/SimpleExec().(name string, args ...string) (string, error) {
85 cmd, err := ExecCommandscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/ExecCommand().(name, args...)
86 if err != nil {
87 return "", err
88 }
89 return Outputscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/Output().(cmd)
90 }
91
92 func Outputscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/Output().(cmd *Cmdscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/Cmd#) (string, error) {
93 out, err := cmd.Outputscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/Cmd#Output().()
94
95 if exitError, ok := err.(*execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.ExitErrorscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/ExitError#); ok {
96 errorOutput := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(string(exitError.Stderrscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/ExitError#Stderr.))
97 if errorOutput == "" {
98 // some commands might write nothing to stderr but something to stdout in error-conditions, in which case, we'll use that
99 // in the error string
100 errorOutput = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(string(out))
101 }
102
103 ran := cmd.Pathscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Path.
104 if len(cmd.Argsscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Args.) > 1 {
105 ran = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s %s", cmd.Pathscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Path., quotedArgsscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/quotedArgs().(cmd.Argsscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Args.[1:]))
106 }
107 formattedErr := errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().(trscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tr`/.Trscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tr`/Tr..Getscip-go gomod github.com/leonelquinteros/gotext v1.7.2 `github.com/leonelquinteros/gotext`/Locale#Get().("error running %s: '%s' '%s'", ran, errorOutput, stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(exitError.Errorscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/ExitError#Error().())))
108
109 // return "" as output in error case, for callers that don't care about errors but rely on "" returned, in-case stdout != ""
110 return "", formattedErr
111 }
112
113 return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Trimscip-go gomod github.com/golang/go/src go1.25.0 strings/Trim().(string(out), " \n"), err
114 }
115
116 var shellWordRescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/shellWordRe. = regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 regexp/MustCompile().(`\A[A-Za-z0-9_@/.-]+\z`)
117
118 // ShellQuoteSingle returns a string which is quoted suitably for sh.
119 func ShellQuoteSinglescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/ShellQuoteSingle().(str string) string {
120 // Quote anything that looks slightly complicated.
121 if shellWordRescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/shellWordRe..FindStringIndexscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#FindStringIndex().(str) == nil {
122 return "'" + stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Replacescip-go gomod github.com/golang/go/src go1.25.0 strings/Replace().(str, "'", "'\\''", -1) + "'"
123 }
124 return str
125 }
126
127 // ShellQuote returns a copied string slice where each element is quoted
128 // suitably for sh.
129 func ShellQuotescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/ShellQuote().(strs []string) []string {
130 dup := make([]string, 0, len(strs))
131
132 for _, str := range strs {
133 dup = append(dup, ShellQuoteSinglescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/ShellQuoteSingle().(str))
134 }
135 return dup
136 }
137
138 // FormatForShell takes a command name and an argument string and returns a
139 // command and arguments that pass this command to the shell. Note that neither
140 // the command nor the arguments are quoted. Consider FormatForShellQuoted
141 // instead.
142 func FormatForShellscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/FormatForShell().(name string, args string) (string, []string) {
143 return "sh", []string{"-c", name + " " + args}
144 }
145
146 // FormatForShellQuotedArgs takes a command name and an argument string and
147 // returns a command and arguments that pass this command to the shell. The
148 // arguments are escaped, but the name of the command is not.
149 func FormatForShellQuotedArgsscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/FormatForShellQuotedArgs().(name string, args []string) (string, []string) {
150 return FormatForShellscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/FormatForShell().(name, stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(ShellQuotescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/ShellQuote().(args), " "))
151 }
152
153 func FormatPercentSequencesscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/FormatPercentSequences().(pattern string, replacements map[string]string) string {
154 s := new(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#)
155 state := 0
156 for _, r := range pattern {
157 if state == 0 && r == '%' {
158 state = 1
159 continue
160 } else if state == 1 {
161 state = 0
162 if r == '%' {
163 s.WriteRunescip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteRune().('%')
164 } else if val, ok := replacements[string([]rune{r})]; ok {
165 s.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteString().(ShellQuoteSinglescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/ShellQuoteSingle().(val))
166 }
167 } else {
168 s.WriteRunescip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteRune().(r)
169 }
170 }
171 return s.Stringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#String().()
172 }
173
174 func Tracescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/Trace().(name string, args ...string) {
175 tracerxscip-go gomod github.com/rubyist/tracerx 787959303086 `github.com/rubyist/tracerx`/.Printfscip-go gomod github.com/rubyist/tracerx 787959303086 `github.com/rubyist/tracerx`/Printf().("exec: %s %s", name, quotedArgsscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/quotedArgs().(args))
176 }
177
178 func quotedArgsscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/quotedArgs().(args []string) string {
179 if len(args) == 0 {
180 return ""
181 }
182
183 quoted := make([]string, len(args))
184 for i, arg := range args {
185 quoted[i] = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("'%s'", arg)
186 }
187 return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(quoted, " ")
188 }
189
190 // An env for an exec.Command without GIT_TRACE and GIT_INTERNAL_SUPER_PREFIX
191 var envscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/env. []string
192 var envMuscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/envMu. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#
193 var traceEnvscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/traceEnv. = "GIT_TRACE="
194
195 // Don't pass GIT_INTERNAL_SUPER_PREFIX back to Git. Git passes this environment
196 // variable to child processes when submodule.recurse is set to true. However,
197 // passing that environment variable back to Git will cause it to append the
198 // --super-prefix command-line option to every Git call. This is problematic
199 // because many Git commands (including git config and git rev-parse) don't
200 // support --super-prefix and would immediately exit with an error as a result.
201 var superPrefixEnvscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/superPrefixEnv. = "GIT_INTERNAL_SUPER_PREFIX="
202
203 func fetchEnvironmentscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/fetchEnvironment().() []string {
204 envMuscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/envMu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().()
205 defer envMuscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/envMu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().()
206
207 return fetchEnvironmentInternalscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/fetchEnvironmentInternal().()
208 }
209
210 // fetchEnvironmentInternal should only be called from fetchEnvironment or
211 // ResetEnvironment, who will hold the required lock.
212 func fetchEnvironmentInternalscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/fetchEnvironmentInternal().() []string {
213 if envscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/env. != nil {
214 return envscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/env.
215 }
216
217 realEnv := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Environscip-go gomod github.com/golang/go/src go1.25.0 os/Environ().()
218 envscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/env. = make([]string, 0, len(realEnv))
219
220 for _, kv := range realEnv {
221 if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(kv, traceEnvscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/traceEnv.) || stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(kv, superPrefixEnvscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/superPrefixEnv.) {
222 continue
223 }
224 envscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/env. = append(envscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/env., kv)
225 }
226 return envscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/env.
227 }
228
229 // ResetEnvironment resets the cached environment that's used in subprocess
230 // calls.
231 func ResetEnvironmentscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/ResetEnvironment().() {
232 envMuscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/envMu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().()
233 defer envMuscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/envMu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().()
234
235 envscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/env. = nil
236 // Reinitialize the environment settings.
237 fetchEnvironmentInternalscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/subprocess`/fetchEnvironmentInternal().()
238 }