Run executes a git command with the given args.
(ctx context.Context, args ...string)
| 202 | |
| 203 | // Run executes a git command with the given args. |
| 204 | func (cli *GitCLI) Run(ctx context.Context, args ...string) (_ []byte, rerr error) { |
| 205 | ctx, span := Tracer(ctx).Start(ctx, strings.Join(append([]string{"git"}, args...), " "), trace.WithAttributes( |
| 206 | attribute.Bool(telemetry.UIEncapsulatedAttr, true), |
| 207 | )) |
| 208 | defer telemetry.EndWithCause(span, &rerr) |
| 209 | |
| 210 | stdio := telemetry.SpanStdio(ctx, InstrumentationLibrary) |
| 211 | defer stdio.Close() |
| 212 | |
| 213 | gitBinary := "git" |
| 214 | if cli.git != "" { |
| 215 | gitBinary = cli.git |
| 216 | } |
| 217 | proxyEnvVars := [...]string{ |
| 218 | "HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", "ALL_PROXY", |
| 219 | "http_proxy", "https_proxy", "no_proxy", "all_proxy", |
| 220 | } |
| 221 | |
| 222 | var cmd *exec.Cmd |
| 223 | if cli.exec == nil { |
| 224 | cmd = exec.CommandContext(ctx, gitBinary) |
| 225 | } else { |
| 226 | cmd = exec.Command(gitBinary) |
| 227 | } |
| 228 | |
| 229 | cmd.Dir = cli.dir |
| 230 | if cmd.Dir == "" { |
| 231 | cmd.Dir = cli.workTree |
| 232 | } |
| 233 | |
| 234 | // Block sneaky repositories from using repos from the filesystem as submodules. |
| 235 | cmd.Args = append(cmd.Args, "-c", "protocol.file.allow=user") |
| 236 | if cli.workTree != "" { |
| 237 | cmd.Args = append(cmd.Args, "--work-tree", cli.workTree) |
| 238 | } |
| 239 | if cli.gitDir != "" { |
| 240 | cmd.Args = append(cmd.Args, "--git-dir", cli.gitDir) |
| 241 | } |
| 242 | cmd.Args = append(cmd.Args, cli.args...) |
| 243 | cmd.Args = append(cmd.Args, args...) |
| 244 | |
| 245 | buf := bytes.NewBuffer(nil) |
| 246 | errbuf := bytes.NewBuffer(nil) |
| 247 | cmd.Stdin = nil |
| 248 | cmd.Stdout = io.MultiWriter(buf, stdio.Stdout) |
| 249 | cmd.Stderr = io.MultiWriter(errbuf, stdio.Stderr) |
| 250 | if cli.streams != nil { |
| 251 | stdout, stderr, flush := cli.streams(ctx) |
| 252 | if stdout != nil { |
| 253 | cmd.Stdout = io.MultiWriter(stdout, cmd.Stdout) |
| 254 | } |
| 255 | if stderr != nil { |
| 256 | cmd.Stderr = io.MultiWriter(stderr, cmd.Stderr) |
| 257 | } |
| 258 | defer stdout.Close() |
| 259 | defer stderr.Close() |
| 260 | defer func() { |
| 261 | if rerr != nil { |
no test coverage detected