@@ -44,7 +44,6 @@ const DefaultLocale = "C"
4444type Command struct {
4545 prog string
4646 args []string
47- parentContext context.Context
4847 globalArgsLength int
4948 brokenArgs []string
5049}
@@ -82,7 +81,7 @@ func (c *Command) LogString() string {
8281
8382// NewCommand creates and returns a new Git Command based on given command and arguments.
8483// Each argument should be safe to be trusted. User-provided arguments should be passed to AddDynamicArguments instead.
85- func NewCommand (ctx context. Context , args ... internal.CmdArg ) * Command {
84+ func NewCommand (args ... internal.CmdArg ) * Command {
8685 // Make an explicit copy of globalCommandArgs, otherwise append might overwrite it
8786 cargs := make ([]string , 0 , len (globalCommandArgs )+ len (args ))
8887 for _ , arg := range globalCommandArgs {
@@ -94,31 +93,23 @@ func NewCommand(ctx context.Context, args ...internal.CmdArg) *Command {
9493 return & Command {
9594 prog : GitExecutable ,
9695 args : cargs ,
97- parentContext : ctx ,
9896 globalArgsLength : len (globalCommandArgs ),
9997 }
10098}
10199
102- // NewCommandContextNoGlobals creates and returns a new Git Command based on given command and arguments only with the specify args and don't care global command args
100+ // NewCommandNoGlobals creates and returns a new Git Command based on given command and arguments only with the specified args and don't use global command args
103101// Each argument should be safe to be trusted. User-provided arguments should be passed to AddDynamicArguments instead.
104- func NewCommandContextNoGlobals ( ctx context. Context , args ... internal.CmdArg ) * Command {
102+ func NewCommandNoGlobals ( args ... internal.CmdArg ) * Command {
105103 cargs := make ([]string , 0 , len (args ))
106104 for _ , arg := range args {
107105 cargs = append (cargs , string (arg ))
108106 }
109107 return & Command {
110- prog : GitExecutable ,
111- args : cargs ,
112- parentContext : ctx ,
108+ prog : GitExecutable ,
109+ args : cargs ,
113110 }
114111}
115112
116- // SetParentContext sets the parent context for this command
117- func (c * Command ) SetParentContext (ctx context.Context ) * Command {
118- c .parentContext = ctx
119- return c
120- }
121-
122113// isSafeArgumentValue checks if the argument is safe to be used as a value (not an option)
123114func isSafeArgumentValue (s string ) bool {
124115 return s == "" || s [0 ] != '-'
@@ -277,11 +268,11 @@ func CommonCmdServEnvs() []string {
277268var ErrBrokenCommand = errors .New ("git command is broken" )
278269
279270// Run runs the command with the RunOpts
280- func (c * Command ) Run (opts * RunOpts ) error {
281- return c .run (1 , opts )
271+ func (c * Command ) Run (ctx context. Context , opts * RunOpts ) error {
272+ return c .run (ctx , 1 , opts )
282273}
283274
284- func (c * Command ) run (skip int , opts * RunOpts ) error {
275+ func (c * Command ) run (ctx context. Context , skip int , opts * RunOpts ) error {
285276 if len (c .brokenArgs ) != 0 {
286277 log .Error ("git command is broken: %s, broken args: %s" , c .LogString (), strings .Join (c .brokenArgs , " " ))
287278 return ErrBrokenCommand
@@ -305,19 +296,18 @@ func (c *Command) run(skip int, opts *RunOpts) error {
305296 desc := fmt .Sprintf ("git.Run(by:%s, repo:%s): %s" , callerInfo , logArgSanitize (opts .Dir ), cmdLogString )
306297 log .Debug ("git.Command: %s" , desc )
307298
308- _ , span := gtprof .GetTracer ().Start (c . parentContext , gtprof .TraceSpanGitRun )
299+ _ , span := gtprof .GetTracer ().Start (ctx , gtprof .TraceSpanGitRun )
309300 defer span .End ()
310301 span .SetAttributeString (gtprof .TraceAttrFuncCaller , callerInfo )
311302 span .SetAttributeString (gtprof .TraceAttrGitCommand , cmdLogString )
312303
313- var ctx context.Context
314304 var cancel context.CancelFunc
315305 var finished context.CancelFunc
316306
317307 if opts .UseContextTimeout {
318- ctx , cancel , finished = process .GetManager ().AddContext (c . parentContext , desc )
308+ ctx , cancel , finished = process .GetManager ().AddContext (ctx , desc )
319309 } else {
320- ctx , cancel , finished = process .GetManager ().AddContextTimeout (c . parentContext , timeout , desc )
310+ ctx , cancel , finished = process .GetManager ().AddContextTimeout (ctx , timeout , desc )
321311 }
322312 defer finished ()
323313
@@ -410,8 +400,8 @@ func IsErrorExitCode(err error, code int) bool {
410400}
411401
412402// RunStdString runs the command with options and returns stdout/stderr as string. and store stderr to returned error (err combined with stderr).
413- func (c * Command ) RunStdString (opts * RunOpts ) (stdout , stderr string , runErr RunStdError ) {
414- stdoutBytes , stderrBytes , err := c .runStdBytes (opts )
403+ func (c * Command ) RunStdString (ctx context. Context , opts * RunOpts ) (stdout , stderr string , runErr RunStdError ) {
404+ stdoutBytes , stderrBytes , err := c .runStdBytes (ctx , opts )
415405 stdout = util .UnsafeBytesToString (stdoutBytes )
416406 stderr = util .UnsafeBytesToString (stderrBytes )
417407 if err != nil {
@@ -422,11 +412,11 @@ func (c *Command) RunStdString(opts *RunOpts) (stdout, stderr string, runErr Run
422412}
423413
424414// RunStdBytes runs the command with options and returns stdout/stderr as bytes. and store stderr to returned error (err combined with stderr).
425- func (c * Command ) RunStdBytes (opts * RunOpts ) (stdout , stderr []byte , runErr RunStdError ) {
426- return c .runStdBytes (opts )
415+ func (c * Command ) RunStdBytes (ctx context. Context , opts * RunOpts ) (stdout , stderr []byte , runErr RunStdError ) {
416+ return c .runStdBytes (ctx , opts )
427417}
428418
429- func (c * Command ) runStdBytes (opts * RunOpts ) (stdout , stderr []byte , runErr RunStdError ) {
419+ func (c * Command ) runStdBytes (ctx context. Context , opts * RunOpts ) (stdout , stderr []byte , runErr RunStdError ) {
430420 if opts == nil {
431421 opts = & RunOpts {}
432422 }
@@ -449,7 +439,7 @@ func (c *Command) runStdBytes(opts *RunOpts) (stdout, stderr []byte, runErr RunS
449439 PipelineFunc : opts .PipelineFunc ,
450440 }
451441
452- err := c .run (2 , newOpts )
442+ err := c .run (ctx , 2 , newOpts )
453443 stderr = stderrBuf .Bytes ()
454444 if err != nil {
455445 return nil , stderr , & runStdError {err : err , stderr : util .UnsafeBytesToString (stderr )}
0 commit comments