@@ -46,7 +46,7 @@ type Command struct {
4646 brokenArgs []string
4747 cmd * exec.Cmd // for debug purpose only
4848 configArgs []string
49- opts * runOpts
49+ opts runOpts
5050}
5151
5252func logArgSanitize (arg string ) string {
@@ -269,136 +269,61 @@ func CommonCmdServEnvs() []string {
269269
270270var ErrBrokenCommand = errors .New ("git command is broken" )
271271
272- type stringWriter struct {
273- str * string
274- }
275-
276- func (w * stringWriter ) Write (p []byte ) (n int , err error ) {
277- * w .str += util .UnsafeBytesToString (p )
278- return len (p ), nil
279- }
280-
281- func (c * Command ) WithStringOutput (output * string ) * Command {
282- if output != nil {
283- if c .opts == nil {
284- c .opts = & runOpts {}
285- }
286- c .opts .Stdout = & stringWriter {str : output }
287- }
288- return c
289- }
290-
291- func (c * Command ) WithStringErr (stderr * string ) * Command {
292- if stderr != nil {
293- if c .opts == nil {
294- c .opts = & runOpts {}
295- }
296- c .opts .Stderr = & stringWriter {str : stderr }
297- }
298- return c
299- }
300-
301- type bytesWriter struct {
302- buf * []byte
303- }
304-
305- func (w * bytesWriter ) Write (p []byte ) (n int , err error ) {
306- * w .buf = append (* w .buf , p ... )
307- return len (p ), nil
308- }
309-
310- func (c * Command ) WithBytesOutput (output * []byte ) * Command {
311- if output != nil {
312- if c .opts == nil {
313- c .opts = & runOpts {}
314- }
315- c .opts .Stdout = & bytesWriter {buf : output }
316- }
317- return c
318- }
319-
320272func (c * Command ) WithDir (dir string ) * Command {
321273 if dir != "" {
322- if c .opts == nil {
323- c .opts = & runOpts {}
324- }
325274 c .opts .Dir = dir
326275 }
327276 return c
328277}
329278
330279func (c * Command ) WithEnv (env []string ) * Command {
331280 if env != nil {
332- if c .opts == nil {
333- c .opts = & runOpts {}
334- }
335281 c .opts .Env = env
336282 }
337283 return c
338284}
339285
340286func (c * Command ) WithTimeout (timeout time.Duration ) * Command {
341287 if timeout > 0 {
342- if c .opts == nil {
343- c .opts = & runOpts {}
344- }
345288 c .opts .Timeout = timeout
346289 }
347290 return c
348291}
349292
350293func (c * Command ) WithStdout (stdout io.Writer ) * Command {
351294 if stdout != nil {
352- if c .opts == nil {
353- c .opts = & runOpts {}
354- }
355295 c .opts .Stdout = stdout
356296 }
357297 return c
358298}
359299
360300func (c * Command ) WithStderr (stderr io.Writer ) * Command {
361301 if stderr != nil {
362- if c .opts == nil {
363- c .opts = & runOpts {}
364- }
365302 c .opts .Stderr = stderr
366303 }
367304 return c
368305}
369306
370307func (c * Command ) WithStdin (stdin io.Reader ) * Command {
371308 if stdin != nil {
372- if c .opts == nil {
373- c .opts = & runOpts {}
374- }
375309 c .opts .Stdin = stdin
376310 }
377311 return c
378312}
379313
380314func (c * Command ) WithPipelineFunc (f func (context.Context , context.CancelFunc ) error ) * Command {
381315 if f != nil {
382- if c .opts == nil {
383- c .opts = & runOpts {}
384- }
385316 c .opts .PipelineFunc = f
386317 }
387318 return c
388319}
389320
390321func (c * Command ) WithUseContextTimeout (useContextTimeout bool ) * Command {
391- if c .opts == nil {
392- c .opts = & runOpts {}
393- }
394322 c .opts .UseContextTimeout = useContextTimeout
395323 return c
396324}
397325
398326func (c * Command ) WithLogSkipStep (stepSkip int ) * Command {
399- if c .opts == nil {
400- c .opts = & runOpts {}
401- }
402327 c .opts .LogSkip += stepSkip
403328 return c
404329}
@@ -409,9 +334,6 @@ func (c *Command) Run(ctx context.Context) error {
409334 log .Error ("git command is broken: %s, broken args: %s" , c .LogString (), strings .Join (c .brokenArgs , " " ))
410335 return ErrBrokenCommand
411336 }
412- if c .opts == nil {
413- c .opts = & runOpts {}
414- }
415337
416338 // We must not change the provided options
417339 timeout := c .opts .Timeout
@@ -547,10 +469,6 @@ func (c *Command) RunStdString(ctx context.Context) (stdout, stderr string, runE
547469
548470// RunStdBytes runs the command and returns stdout/stderr as bytes. and store stderr to returned error (err combined with stderr).
549471func (c * Command ) RunStdBytes (ctx context.Context ) (stdout , stderr []byte , runErr RunStdError ) {
550- if c .opts == nil {
551- c .opts = & runOpts {}
552- }
553-
554472 if c .opts .Stdout != nil || c .opts .Stderr != nil {
555473 // we must panic here, otherwise there would be bugs if developers set Stdin/Stderr by mistake, and it would be very difficult to debug
556474 panic ("stdout and stderr field must be nil when using RunStdBytes" )
0 commit comments