@@ -221,6 +221,9 @@ type RunOpts struct {
221221 Stdin io.Reader
222222
223223 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
224227}
225228
226229func commonBaseEnvs () []string {
@@ -265,10 +268,6 @@ var ErrBrokenCommand = errors.New("git command is broken")
265268
266269// Run runs the command with the RunOpts
267270func (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 {
272271 if len (c .brokenArgs ) != 0 {
273272 log .Error ("git command is broken: %s, broken args: %s" , c .LogString (), strings .Join (c .brokenArgs , " " ))
274273 return ErrBrokenCommand
@@ -284,13 +283,14 @@ func (c *Command) run(ctx context.Context, skip int, opts *RunOpts) error {
284283 }
285284
286285 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 )
288288 if pos := strings .LastIndex (callerInfo , "/" ); pos >= 0 {
289289 callerInfo = callerInfo [pos + 1 :]
290290 }
291291 // these logs are for debugging purposes only, so no guarantee of correctness or stability
292292 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 )
294294
295295 _ , span := gtprof .GetTracer ().Start (ctx , gtprof .TraceSpanGitRun )
296296 defer span .End ()
@@ -399,7 +399,11 @@ func IsErrorExitCode(err error, code int) bool {
399399
400400// RunStdString runs the command with options and returns stdout/stderr as string. and store stderr to returned error (err combined with stderr).
401401func (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 )
403407 stdout = util .UnsafeBytesToString (stdoutBytes )
404408 stderr = util .UnsafeBytesToString (stderrBytes )
405409 if err != nil {
@@ -411,13 +415,10 @@ func (c *Command) RunStdString(ctx context.Context, opts *RunOpts) (stdout, stde
411415
412416// RunStdBytes runs the command with options and returns stdout/stderr as bytes. and store stderr to returned error (err combined with stderr).
413417func (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 ) {
418418 if opts == nil {
419419 opts = & RunOpts {}
420420 }
421+ opts .LogDepth += 1
421422 if opts .Stdout != nil || opts .Stderr != nil {
422423 // we must panic here, otherwise there would be bugs if developers set Stdin/Stderr by mistake, and it would be very difficult to debug
423424 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
435436 Stderr : stderrBuf ,
436437 Stdin : opts .Stdin ,
437438 PipelineFunc : opts .PipelineFunc ,
439+ LogDepth : opts .LogDepth ,
438440 }
439441
440- err := c .run (ctx , 2 , newOpts )
442+ err := c .Run (ctx , newOpts )
441443 stderr = stderrBuf .Bytes ()
442444 if err != nil {
443445 return nil , stderr , & runStdError {err : err , stderr : util .UnsafeBytesToString (stderr )}
0 commit comments