Skip to content

Commit 85681d6

Browse files
committed
fix caller info
1 parent bfa7f94 commit 85681d6

File tree

5 files changed

+42
-44
lines changed

5 files changed

+42
-44
lines changed

modules/git/gitcmd/command.go

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,7 @@ type runOpts struct {
223223

224224
PipelineFunc func(context.Context, context.CancelFunc) error
225225

226-
// for debugging purpose only, it indicates how many stack frames to skip to find the caller info
227-
// it's mainly used to find the caller function name for logging
228-
// it should be 0 when the caller is the direct caller of Run/RunStdString/RunStdBytes
229-
LogSkip int
226+
callerInfo string
230227
}
231228

232229
func commonBaseEnvs() []string {
@@ -270,51 +267,37 @@ func CommonCmdServEnvs() []string {
270267
var ErrBrokenCommand = errors.New("git command is broken")
271268

272269
func (c *Command) WithDir(dir string) *Command {
273-
if dir != "" {
274-
c.opts.Dir = dir
275-
}
270+
c.opts.Dir = dir
276271
return c
277272
}
278273

279274
func (c *Command) WithEnv(env []string) *Command {
280-
if env != nil {
281-
c.opts.Env = env
282-
}
275+
c.opts.Env = env
283276
return c
284277
}
285278

286279
func (c *Command) WithTimeout(timeout time.Duration) *Command {
287-
if timeout > 0 {
288-
c.opts.Timeout = timeout
289-
}
280+
c.opts.Timeout = timeout
290281
return c
291282
}
292283

293284
func (c *Command) WithStdout(stdout io.Writer) *Command {
294-
if stdout != nil {
295-
c.opts.Stdout = stdout
296-
}
285+
c.opts.Stdout = stdout
297286
return c
298287
}
299288

300289
func (c *Command) WithStderr(stderr io.Writer) *Command {
301-
if stderr != nil {
302-
c.opts.Stderr = stderr
303-
}
290+
c.opts.Stderr = stderr
304291
return c
305292
}
306293

307294
func (c *Command) WithStdin(stdin io.Reader) *Command {
308-
if stdin != nil {
309-
c.opts.Stdin = stdin
310-
}
295+
c.opts.Stdin = stdin
311296
return c
312297
}
313298

314299
func (c *Command) WithPipelineFunc(f func(context.Context, context.CancelFunc) error) *Command {
315-
if f != nil {
316-
c.opts.PipelineFunc = f
317-
}
300+
c.opts.PipelineFunc = f
318301
return c
319302
}
320303

@@ -323,8 +306,22 @@ func (c *Command) WithUseContextTimeout(useContextTimeout bool) *Command {
323306
return c
324307
}
325308

326-
func (c *Command) WithLogSkipStep(stepSkip int) *Command {
327-
c.opts.LogSkip += stepSkip
309+
// WithParentCallerInfo can be used to set the caller info (usually function name) of the parent function of the caller.
310+
// For most cases, "Run" family functions can get its caller info automatically
311+
// But if you need to call "Run" family functions in a wrapper function: "FeatureFunc -> GeneralWrapperFunc -> RunXxx",
312+
// then you can to call this function in GeneralWrapperFunc to set the caller info of FeatureFunc.
313+
func (c *Command) WithParentCallerInfo(optInfo ...string) *Command {
314+
if len(optInfo) > 0 {
315+
c.opts.callerInfo = optInfo[0]
316+
return c
317+
}
318+
skip := 1 /*parent "wrap/run" functions*/ + 1 /*this function*/
319+
callerFuncName := util.CallerFuncName(skip)
320+
callerInfo := callerFuncName
321+
if pos := strings.LastIndex(callerInfo, "/"); pos >= 0 {
322+
callerInfo = callerInfo[pos+1:]
323+
}
324+
c.opts.callerInfo = callerInfo
328325
return c
329326
}
330327

@@ -342,17 +339,16 @@ func (c *Command) Run(ctx context.Context) error {
342339
}
343340

344341
cmdLogString := c.LogString()
345-
callerInfo := util.CallerFuncName(1 /* util */ + 1 /* this */ + c.opts.LogSkip /* parent */)
346-
if pos := strings.LastIndex(callerInfo, "/"); pos >= 0 {
347-
callerInfo = callerInfo[pos+1:]
342+
if c.opts.callerInfo == "" {
343+
c.WithParentCallerInfo()
348344
}
349345
// these logs are for debugging purposes only, so no guarantee of correctness or stability
350-
desc := fmt.Sprintf("git.Run(by:%s, repo:%s): %s", callerInfo, logArgSanitize(c.opts.Dir), cmdLogString)
346+
desc := fmt.Sprintf("git.Run(by:%s, repo:%s): %s", c.opts.callerInfo, logArgSanitize(c.opts.Dir), cmdLogString)
351347
log.Debug("git.Command: %s", desc)
352348

353349
_, span := gtprof.GetTracer().Start(ctx, gtprof.TraceSpanGitRun)
354350
defer span.End()
355-
span.SetAttributeString(gtprof.TraceAttrFuncCaller, callerInfo)
351+
span.SetAttributeString(gtprof.TraceAttrFuncCaller, c.opts.callerInfo)
356352
span.SetAttributeString(gtprof.TraceAttrGitCommand, cmdLogString)
357353

358354
var cancel context.CancelFunc
@@ -457,7 +453,7 @@ func IsErrorExitCode(err error, code int) bool {
457453

458454
// RunStdString runs the command and returns stdout/stderr as string. and store stderr to returned error (err combined with stderr).
459455
func (c *Command) RunStdString(ctx context.Context) (stdout, stderr string, runErr RunStdError) {
460-
stdoutBytes, stderrBytes, err := c.WithLogSkipStep(1).RunStdBytes(ctx)
456+
stdoutBytes, stderrBytes, err := c.WithParentCallerInfo().RunStdBytes(ctx)
461457
stdout = util.UnsafeBytesToString(stdoutBytes)
462458
stderr = util.UnsafeBytesToString(stderrBytes)
463459
if err != nil {
@@ -477,7 +473,7 @@ func (c *Command) RunStdBytes(ctx context.Context) (stdout, stderr []byte, runEr
477473
stdoutBuf := &bytes.Buffer{}
478474
stderrBuf := &bytes.Buffer{}
479475

480-
err := c.WithLogSkipStep(1).
476+
err := c.WithParentCallerInfo().
481477
WithStdout(stdoutBuf).
482478
WithStderr(stderrBuf).
483479
Run(ctx)

modules/gitrepo/command.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@ import (
1010
)
1111

1212
func RunCmd(ctx context.Context, repo Repository, cmd *gitcmd.Command) error {
13-
return cmd.WithDir(repoPath(repo)).
14-
WithLogSkipStep(1).
15-
Run(ctx)
13+
return cmd.WithDir(repoPath(repo)).WithParentCallerInfo().Run(ctx)
1614
}
1715

1816
func RunCmdString(ctx context.Context, repo Repository, cmd *gitcmd.Command) (string, error) {
19-
res, _, err := cmd.WithDir(repoPath(repo)).WithLogSkipStep(1).RunStdString(ctx)
17+
res, _, err := cmd.WithDir(repoPath(repo)).WithParentCallerInfo().RunStdString(ctx)
2018
return res, err
2119
}
2220

2321
func RunCmdBytes(ctx context.Context, repo Repository, cmd *gitcmd.Command) ([]byte, []byte, error) {
24-
return cmd.WithDir(repoPath(repo)).WithLogSkipStep(1).RunStdBytes(ctx)
22+
return cmd.WithDir(repoPath(repo)).WithParentCallerInfo().RunStdBytes(ctx)
2523
}

modules/util/runtime.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ package util
55

66
import "runtime"
77

8-
func CallerFuncName(skip int) string {
8+
func CallerFuncName(optSkipParent ...int) string {
99
pc := make([]uintptr, 1)
10-
runtime.Callers(skip+1, pc)
10+
skipParent := 0
11+
if len(optSkipParent) > 0 {
12+
skipParent = optSkipParent[0]
13+
}
14+
runtime.Callers(skipParent+1 /*this*/ +1 /*runtime*/, pc)
1115
funcName := runtime.FuncForPC(pc[0]).Name()
1216
return funcName
1317
}

modules/util/runtime_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func TestCallerFuncName(t *testing.T) {
14-
s := CallerFuncName(1)
14+
s := CallerFuncName()
1515
assert.Equal(t, "code.gitea.io/gitea/modules/util.TestCallerFuncName", s)
1616
}
1717

@@ -26,7 +26,7 @@ func BenchmarkCallerFuncName(b *testing.B) {
2626
// It is almost as fast as fmt.Sprintf
2727
b.Run("caller", func(b *testing.B) {
2828
for b.Loop() {
29-
CallerFuncName(1)
29+
CallerFuncName()
3030
}
3131
})
3232
}

services/pull/merge_prepare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (ctx *mergeContext) WithCmd(cmd *gitcmd.Command) *gitcmd.Command {
3737
ctx.errbuf.Reset()
3838
return cmd.WithEnv(ctx.env).
3939
WithDir(ctx.tmpBasePath).
40-
WithLogSkipStep(1).
40+
WithParentCallerInfo().
4141
WithStdout(ctx.outbuf).
4242
WithStderr(ctx.errbuf)
4343
}

0 commit comments

Comments
 (0)