@@ -221,6 +221,9 @@ type RunOpts struct {
221
221
Stdin io.Reader
222
222
223
223
PipelineFunc func (context.Context , context.CancelFunc ) error
224
+
225
+ // for debugging purpose only, it means current function call depth, every caller should +1
226
+ LogDepth int
224
227
}
225
228
226
229
func commonBaseEnvs () []string {
@@ -265,10 +268,6 @@ var ErrBrokenCommand = errors.New("git command is broken")
265
268
266
269
// Run runs the command with the RunOpts
267
270
func (c * Command ) Run (ctx context.Context , opts * RunOpts ) error {
268
- return c .run (ctx , 1 , opts )
269
- }
270
-
271
- func (c * Command ) run (ctx context.Context , skip int , opts * RunOpts ) error {
272
271
if len (c .brokenArgs ) != 0 {
273
272
log .Error ("git command is broken: %s, broken args: %s" , c .LogString (), strings .Join (c .brokenArgs , " " ))
274
273
return ErrBrokenCommand
@@ -284,13 +283,14 @@ func (c *Command) run(ctx context.Context, skip int, opts *RunOpts) error {
284
283
}
285
284
286
285
cmdLogString := c .LogString ()
287
- callerInfo := util .CallerFuncName (1 /* util */ + 1 /* this */ + skip /* parent */ )
286
+ depth := 1 /* util */ + 1 /* this */ + opts .LogDepth /* parent */
287
+ callerInfo := util .CallerFuncName (depth )
288
288
if pos := strings .LastIndex (callerInfo , "/" ); pos >= 0 {
289
289
callerInfo = callerInfo [pos + 1 :]
290
290
}
291
291
// these logs are for debugging purposes only, so no guarantee of correctness or stability
292
292
desc := fmt .Sprintf ("git.Run(by:%s, repo:%s): %s" , callerInfo , logArgSanitize (opts .Dir ), cmdLogString )
293
- log .Debug ( "git.Command: %s" , desc )
293
+ log .DebugWithSkip ( depth - 1 , "git.Command: %s" , desc )
294
294
295
295
_ , span := gtprof .GetTracer ().Start (ctx , gtprof .TraceSpanGitRun )
296
296
defer span .End ()
@@ -399,7 +399,11 @@ func IsErrorExitCode(err error, code int) bool {
399
399
400
400
// RunStdString runs the command with options and returns stdout/stderr as string. and store stderr to returned error (err combined with stderr).
401
401
func (c * Command ) RunStdString (ctx context.Context , opts * RunOpts ) (stdout , stderr string , runErr RunStdError ) {
402
- stdoutBytes , stderrBytes , err := c .runStdBytes (ctx , opts )
402
+ if opts == nil {
403
+ opts = & RunOpts {}
404
+ }
405
+ opts .LogDepth += 1
406
+ stdoutBytes , stderrBytes , err := c .RunStdBytes (ctx , opts )
403
407
stdout = util .UnsafeBytesToString (stdoutBytes )
404
408
stderr = util .UnsafeBytesToString (stderrBytes )
405
409
if err != nil {
@@ -411,13 +415,10 @@ func (c *Command) RunStdString(ctx context.Context, opts *RunOpts) (stdout, stde
411
415
412
416
// RunStdBytes runs the command with options and returns stdout/stderr as bytes. and store stderr to returned error (err combined with stderr).
413
417
func (c * Command ) RunStdBytes (ctx context.Context , opts * RunOpts ) (stdout , stderr []byte , runErr RunStdError ) {
414
- return c .runStdBytes (ctx , opts )
415
- }
416
-
417
- func (c * Command ) runStdBytes (ctx context.Context , opts * RunOpts ) (stdout , stderr []byte , runErr RunStdError ) {
418
418
if opts == nil {
419
419
opts = & RunOpts {}
420
420
}
421
+ opts .LogDepth += 1
421
422
if opts .Stdout != nil || opts .Stderr != nil {
422
423
// we must panic here, otherwise there would be bugs if developers set Stdin/Stderr by mistake, and it would be very difficult to debug
423
424
panic ("stdout and stderr field must be nil when using RunStdBytes" )
@@ -435,9 +436,10 @@ func (c *Command) runStdBytes(ctx context.Context, opts *RunOpts) (stdout, stder
435
436
Stderr : stderrBuf ,
436
437
Stdin : opts .Stdin ,
437
438
PipelineFunc : opts .PipelineFunc ,
439
+ LogDepth : opts .LogDepth ,
438
440
}
439
441
440
- err := c .run (ctx , 2 , newOpts )
442
+ err := c .Run (ctx , newOpts )
441
443
stderr = stderrBuf .Bytes ()
442
444
if err != nil {
443
445
return nil , stderr , & runStdError {err : err , stderr : util .UnsafeBytesToString (stderr )}
0 commit comments