Skip to content

Commit 53c868c

Browse files
committed
Adjust log depth of git command
1 parent a226849 commit 53c868c

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

modules/git/gitcmd/command.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

226229
func commonBaseEnvs() []string {
@@ -265,10 +268,6 @@ var ErrBrokenCommand = errors.New("git command is broken")
265268

266269
// Run runs the command with the RunOpts
267270
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 {
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).
401401
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)
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).
413417
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) {
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)}

modules/gitrepo/command.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ func RunCmdString(ctx context.Context, repo Repository, cmd *gitcmd.Command, opt
2525
o(&opt)
2626
}
2727
res, _, err := cmd.RunStdString(ctx, &gitcmd.RunOpts{
28-
Dir: repoPath(repo),
29-
Env: opt.Env,
28+
Dir: repoPath(repo),
29+
Env: opt.Env,
30+
LogDepth: 1,
3031
})
3132
return res, err
3233
}

modules/log/logger_global.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func Debug(format string, v ...any) {
3333
Log(1, DEBUG, format, v...)
3434
}
3535

36+
func DebugWithSkip(skip int, format string, v ...any) {
37+
Log(skip+1, DEBUG, format, v...)
38+
}
39+
3640
func IsDebug() bool {
3741
return GetLevel() <= DEBUG
3842
}

0 commit comments

Comments
 (0)