@@ -44,7 +44,6 @@ const DefaultLocale = "C"
44
44
type Command struct {
45
45
prog string
46
46
args []string
47
- parentContext context.Context
48
47
globalArgsLength int
49
48
brokenArgs []string
50
49
}
@@ -82,7 +81,7 @@ func (c *Command) LogString() string {
82
81
83
82
// NewCommand creates and returns a new Git Command based on given command and arguments.
84
83
// 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 {
86
85
// Make an explicit copy of globalCommandArgs, otherwise append might overwrite it
87
86
cargs := make ([]string , 0 , len (globalCommandArgs )+ len (args ))
88
87
for _ , arg := range globalCommandArgs {
@@ -94,31 +93,23 @@ func NewCommand(ctx context.Context, args ...internal.CmdArg) *Command {
94
93
return & Command {
95
94
prog : GitExecutable ,
96
95
args : cargs ,
97
- parentContext : ctx ,
98
96
globalArgsLength : len (globalCommandArgs ),
99
97
}
100
98
}
101
99
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
103
101
// 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 {
105
103
cargs := make ([]string , 0 , len (args ))
106
104
for _ , arg := range args {
107
105
cargs = append (cargs , string (arg ))
108
106
}
109
107
return & Command {
110
- prog : GitExecutable ,
111
- args : cargs ,
112
- parentContext : ctx ,
108
+ prog : GitExecutable ,
109
+ args : cargs ,
113
110
}
114
111
}
115
112
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
-
122
113
// isSafeArgumentValue checks if the argument is safe to be used as a value (not an option)
123
114
func isSafeArgumentValue (s string ) bool {
124
115
return s == "" || s [0 ] != '-'
@@ -277,11 +268,11 @@ func CommonCmdServEnvs() []string {
277
268
var ErrBrokenCommand = errors .New ("git command is broken" )
278
269
279
270
// 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 )
282
273
}
283
274
284
- func (c * Command ) run (skip int , opts * RunOpts ) error {
275
+ func (c * Command ) run (ctx context. Context , skip int , opts * RunOpts ) error {
285
276
if len (c .brokenArgs ) != 0 {
286
277
log .Error ("git command is broken: %s, broken args: %s" , c .LogString (), strings .Join (c .brokenArgs , " " ))
287
278
return ErrBrokenCommand
@@ -305,19 +296,18 @@ func (c *Command) run(skip int, opts *RunOpts) error {
305
296
desc := fmt .Sprintf ("git.Run(by:%s, repo:%s): %s" , callerInfo , logArgSanitize (opts .Dir ), cmdLogString )
306
297
log .Debug ("git.Command: %s" , desc )
307
298
308
- _ , span := gtprof .GetTracer ().Start (c . parentContext , gtprof .TraceSpanGitRun )
299
+ _ , span := gtprof .GetTracer ().Start (ctx , gtprof .TraceSpanGitRun )
309
300
defer span .End ()
310
301
span .SetAttributeString (gtprof .TraceAttrFuncCaller , callerInfo )
311
302
span .SetAttributeString (gtprof .TraceAttrGitCommand , cmdLogString )
312
303
313
- var ctx context.Context
314
304
var cancel context.CancelFunc
315
305
var finished context.CancelFunc
316
306
317
307
if opts .UseContextTimeout {
318
- ctx , cancel , finished = process .GetManager ().AddContext (c . parentContext , desc )
308
+ ctx , cancel , finished = process .GetManager ().AddContext (ctx , desc )
319
309
} else {
320
- ctx , cancel , finished = process .GetManager ().AddContextTimeout (c . parentContext , timeout , desc )
310
+ ctx , cancel , finished = process .GetManager ().AddContextTimeout (ctx , timeout , desc )
321
311
}
322
312
defer finished ()
323
313
@@ -410,8 +400,8 @@ func IsErrorExitCode(err error, code int) bool {
410
400
}
411
401
412
402
// 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 )
415
405
stdout = util .UnsafeBytesToString (stdoutBytes )
416
406
stderr = util .UnsafeBytesToString (stderrBytes )
417
407
if err != nil {
@@ -422,11 +412,11 @@ func (c *Command) RunStdString(opts *RunOpts) (stdout, stderr string, runErr Run
422
412
}
423
413
424
414
// 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 )
427
417
}
428
418
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 ) {
430
420
if opts == nil {
431
421
opts = & RunOpts {}
432
422
}
@@ -449,7 +439,7 @@ func (c *Command) runStdBytes(opts *RunOpts) (stdout, stderr []byte, runErr RunS
449
439
PipelineFunc : opts .PipelineFunc ,
450
440
}
451
441
452
- err := c .run (2 , newOpts )
442
+ err := c .run (ctx , 2 , newOpts )
453
443
stderr = stderrBuf .Bytes ()
454
444
if err != nil {
455
445
return nil , stderr , & runStdError {err : err , stderr : util .UnsafeBytesToString (stderr )}
0 commit comments