@@ -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
232229func commonBaseEnvs () []string {
@@ -270,51 +267,37 @@ func CommonCmdServEnvs() []string {
270267var ErrBrokenCommand = errors .New ("git command is broken" )
271268
272269func (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
279274func (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
286279func (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
293284func (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
300289func (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
307294func (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
314299func (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).
459455func (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 )
0 commit comments