@@ -7,7 +7,6 @@ package git
77import (
88 "bytes"
99 "context"
10- "fmt"
1110 "io"
1211 "os"
1312 "strconv"
@@ -27,10 +26,10 @@ type CommandOptions struct {
2726// applied when the context does not already have a deadline.
2827const DefaultTimeout = time .Minute
2928
30- // gitCmd builds a *run.Command for "git" with the given arguments, environment
29+ // cmd builds a *run.Command for "git" with the given arguments, environment
3130// variables and working directory. If the context does not already have a
3231// deadline, DefaultTimeout will be applied automatically.
33- func gitCmd (ctx context.Context , dir string , args []string , envs []string ) (* run.Command , context.CancelFunc ) {
32+ func cmd (ctx context.Context , dir string , args []string , envs []string ) (* run.Command , context.CancelFunc ) {
3433 cancel := func () {}
3534
3635 // Apply default timeout if the context doesn't already have a deadline.
@@ -49,22 +48,22 @@ func gitCmd(ctx context.Context, dir string, args []string, envs []string) (*run
4948 parts = append (parts , run .Arg (arg ))
5049 }
5150
52- cmd := run .Cmd (ctx , parts ... )
51+ c := run .Cmd (ctx , parts ... )
5352 if dir != "" {
54- cmd = cmd .Dir (dir )
53+ c = c .Dir (dir )
5554 }
5655 if len (envs ) > 0 {
57- cmd = cmd .Environ (append (os .Environ (), envs ... ))
56+ c = c .Environ (append (os .Environ (), envs ... ))
5857 }
59- return cmd , cancel
58+ return c , cancel
6059}
6160
62- // gitRun executes a git command in the given directory and returns stdout as
61+ // exec executes a git command in the given directory and returns stdout as
6362// bytes. Stderr is included in the error message on failure. If the command's
6463// context does not have a deadline, DefaultTimeout will be applied
6564// automatically. It returns an ErrExecTimeout if the execution was timed out.
66- func gitRun (ctx context.Context , dir string , args []string , envs []string ) ([]byte , error ) {
67- cmd , cancel := gitCmd (ctx , dir , args , envs )
65+ func exec (ctx context.Context , dir string , args []string , envs []string ) ([]byte , error ) {
66+ c , cancel := cmd (ctx , dir , args , envs )
6867 defer cancel ()
6968
7069 var logBuf * bytes.Buffer
@@ -80,7 +79,7 @@ func gitRun(ctx context.Context, dir string, args []string, envs []string) ([]by
8079 // commands like "ls-tree -z"). The String/Lines methods process output
8180 // line-by-line which corrupts binary-ish output.
8281 stdout := new (bytes.Buffer )
83- err := cmd .StdOut ().Run ().Stream (stdout )
82+ err := c .StdOut ().Run ().Stream (stdout )
8483
8584 // Capture (partial) stdout for logging even on error, so failed commands
8685 // produce a useful log entry rather than an empty one.
@@ -102,11 +101,10 @@ func gitRun(ctx context.Context, dir string, args []string, envs []string) ([]by
102101 return stdout .Bytes (), nil
103102}
104103
105- // gitPipeline executes a git command in the given directory, streaming stdout
106- // to the given writer. If stderr writer is provided and the command fails,
107- // stderr content extracted from the error is written to it.
108- func gitPipeline (ctx context.Context , dir string , args []string , envs []string , stdout , stderr io.Writer ) error {
109- cmd , cancel := gitCmd (ctx , dir , args , envs )
104+ // pipe executes a git command in the given directory, streaming stdout to the
105+ // given writer.
106+ func pipe (ctx context.Context , dir string , args []string , envs []string , stdout io.Writer ) error {
107+ c , cancel := cmd (ctx , dir , args , envs )
110108 defer cancel ()
111109
112110 var buf * bytes.Buffer
@@ -125,11 +123,8 @@ func gitPipeline(ctx context.Context, dir string, args []string, envs []string,
125123 }()
126124 }
127125
128- streamErr := cmd .StdOut ().Run ().Stream (w )
126+ streamErr := c .StdOut ().Run ().Stream (w )
129127 if streamErr != nil {
130- if stderr != nil {
131- _ , _ = fmt .Fprint (stderr , extractStderr (streamErr ))
132- }
133128 return mapContextError (streamErr , ctx )
134129 }
135130 return nil
@@ -213,17 +208,3 @@ func isExitStatus(err error, code int) bool {
213208 exitCoder , ok := err .(run.ExitCoder )
214209 return ok && exitCoder .ExitCode () == code
215210}
216-
217- // extractStderr attempts to extract the stderr portion from a sourcegraph/run
218- // error. The error format is typically "exit status N: <stderr content>".
219- func extractStderr (err error ) string {
220- if err == nil {
221- return ""
222- }
223- msg := err .Error ()
224- // sourcegraph/run error format: "exit status N: <stderr>"
225- if idx := strings .Index (msg , ": " ); idx >= 0 && strings .HasPrefix (msg , "exit status" ) {
226- return msg [idx + 2 :]
227- }
228- return msg
229- }
0 commit comments