@@ -92,13 +92,13 @@ func AddChanges(repoPath string, all bool, files ...string) error {
92
92
}
93
93
94
94
// AddChangesWithArgs marks local changes to be ready for commit.
95
- func AddChangesWithArgs (repoPath string , globalArgs []string , all bool , files ... string ) error {
95
+ func AddChangesWithArgs (repoPath string , globalArgs []CmdArg , all bool , files ... string ) error {
96
96
cmd := NewCommandNoGlobals (append (globalArgs , "add" )... )
97
97
if all {
98
98
cmd .AddArguments ("--all" )
99
99
}
100
- cmd .AddArguments ( "--" )
101
- _ , _ , err := cmd .AddArguments ( files ... ). RunStdString (& RunOpts {Dir : repoPath })
100
+ cmd .AddSlashedArguments ( files ... )
101
+ _ , _ , err := cmd .RunStdString (& RunOpts {Dir : repoPath })
102
102
return err
103
103
}
104
104
@@ -112,27 +112,27 @@ type CommitChangesOptions struct {
112
112
// CommitChanges commits local changes with given committer, author and message.
113
113
// If author is nil, it will be the same as committer.
114
114
func CommitChanges (repoPath string , opts CommitChangesOptions ) error {
115
- cargs := make ([]string , len (globalCommandArgs ))
115
+ cargs := make ([]CmdArg , len (globalCommandArgs ))
116
116
copy (cargs , globalCommandArgs )
117
117
return CommitChangesWithArgs (repoPath , cargs , opts )
118
118
}
119
119
120
120
// CommitChangesWithArgs commits local changes with given committer, author and message.
121
121
// If author is nil, it will be the same as committer.
122
- func CommitChangesWithArgs (repoPath string , args []string , opts CommitChangesOptions ) error {
122
+ func CommitChangesWithArgs (repoPath string , args []CmdArg , opts CommitChangesOptions ) error {
123
123
cmd := NewCommandNoGlobals (args ... )
124
124
if opts .Committer != nil {
125
- cmd .AddArguments ("-c" , "user.name=" + opts .Committer .Name , "-c" , "user.email=" + opts .Committer .Email )
125
+ cmd .AddArguments ("-c" , CmdArg ( "user.name=" + opts .Committer .Name ) , "-c" , CmdArg ( "user.email=" + opts .Committer .Email ) )
126
126
}
127
127
cmd .AddArguments ("commit" )
128
128
129
129
if opts .Author == nil {
130
130
opts .Author = opts .Committer
131
131
}
132
132
if opts .Author != nil {
133
- cmd .AddArguments (fmt .Sprintf ("--author='%s <%s>'" , opts .Author .Name , opts .Author .Email ))
133
+ cmd .AddArguments (CmdArg ( fmt .Sprintf ("--author='%s <%s>'" , opts .Author .Name , opts .Author .Email ) ))
134
134
}
135
- cmd .AddArguments ("-m" , opts .Message )
135
+ cmd .AddArguments ("-m" ). AddDynamicArguments ( opts .Message )
136
136
137
137
_ , _ , err := cmd .RunStdString (& RunOpts {Dir : repoPath })
138
138
// No stderr but exit status 1 means nothing to commit.
@@ -144,15 +144,13 @@ func CommitChangesWithArgs(repoPath string, args []string, opts CommitChangesOpt
144
144
145
145
// AllCommitsCount returns count of all commits in repository
146
146
func AllCommitsCount (ctx context.Context , repoPath string , hidePRRefs bool , files ... string ) (int64 , error ) {
147
- args := [] string { "--all" , "--count" }
147
+ cmd := NewCommand ( ctx , "rev-list" )
148
148
if hidePRRefs {
149
- args = append ([] string { "--exclude=" + PullPrefix + "*" }, args ... )
149
+ cmd . AddArguments ( "--exclude=" + PullPrefix + "*" )
150
150
}
151
- cmd := NewCommand (ctx , "rev-list" )
152
- cmd .AddArguments (args ... )
151
+ cmd .AddArguments ("--all" , "--count" )
153
152
if len (files ) > 0 {
154
- cmd .AddArguments ("--" )
155
- cmd .AddArguments (files ... )
153
+ cmd .AddSlashedArguments (files ... )
156
154
}
157
155
158
156
stdout , _ , err := cmd .RunStdString (& RunOpts {Dir : repoPath })
@@ -168,8 +166,7 @@ func CommitsCountFiles(ctx context.Context, repoPath string, revision, relpath [
168
166
cmd := NewCommand (ctx , "rev-list" , "--count" )
169
167
cmd .AddDynamicArguments (revision ... )
170
168
if len (relpath ) > 0 {
171
- cmd .AddArguments ("--" )
172
- cmd .AddArguments (relpath ... )
169
+ cmd .AddSlashedArguments (relpath ... )
173
170
}
174
171
175
172
stdout , _ , err := cmd .RunStdString (& RunOpts {Dir : repoPath })
@@ -209,7 +206,7 @@ func (c *Commit) HasPreviousCommit(commitHash SHA1) (bool, error) {
209
206
return false , nil
210
207
}
211
208
212
- _ , _ , err := NewCommand (c .repo .Ctx , "merge-base" , "--is-ancestor" , that , this ).RunStdString (& RunOpts {Dir : c .repo .Path })
209
+ _ , _ , err := NewCommand (c .repo .Ctx , "merge-base" , "--is-ancestor" ). AddDynamicArguments ( that , this ).RunStdString (& RunOpts {Dir : c .repo .Path })
213
210
if err == nil {
214
211
return true , nil
215
212
}
@@ -392,15 +389,12 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
392
389
393
390
// GetBranchName gets the closest branch name (as returned by 'git name-rev --name-only')
394
391
func (c * Commit ) GetBranchName () (string , error ) {
395
- args := []string {
396
- "name-rev" ,
397
- }
392
+ cmd := NewCommand (c .repo .Ctx , "name-rev" )
398
393
if CheckGitVersionAtLeast ("2.13.0" ) == nil {
399
- args = append ( args , "--exclude" , "refs/tags/*" )
394
+ cmd . AddArguments ( "--exclude" , "refs/tags/*" )
400
395
}
401
- args = append (args , "--name-only" , "--no-undefined" , c .ID .String ())
402
-
403
- data , _ , err := NewCommand (c .repo .Ctx , args ... ).RunStdString (& RunOpts {Dir : c .repo .Path })
396
+ cmd .AddArguments ("--name-only" , "--no-undefined" ).AddDynamicArguments (c .ID .String ())
397
+ data , _ , err := cmd .RunStdString (& RunOpts {Dir : c .repo .Path })
404
398
if err != nil {
405
399
// handle special case where git can not describe commit
406
400
if strings .Contains (err .Error (), "cannot describe" ) {
@@ -426,7 +420,7 @@ func (c *Commit) LoadBranchName() (err error) {
426
420
427
421
// GetTagName gets the current tag name for given commit
428
422
func (c * Commit ) GetTagName () (string , error ) {
429
- data , _ , err := NewCommand (c .repo .Ctx , "describe" , "--exact-match" , "--tags" , "--always" , c .ID .String ()).RunStdString (& RunOpts {Dir : c .repo .Path })
423
+ data , _ , err := NewCommand (c .repo .Ctx , "describe" , "--exact-match" , "--tags" , "--always" ). AddDynamicArguments ( c .ID .String ()).RunStdString (& RunOpts {Dir : c .repo .Path })
430
424
if err != nil {
431
425
// handle special case where there is no tag for this commit
432
426
if strings .Contains (err .Error (), "no tag exactly matches" ) {
@@ -503,9 +497,7 @@ func GetCommitFileStatus(ctx context.Context, repoPath, commitID string) (*Commi
503
497
}()
504
498
505
499
stderr := new (bytes.Buffer )
506
- args := []string {"log" , "--name-status" , "-c" , "--pretty=format:" , "--parents" , "--no-renames" , "-z" , "-1" , commitID }
507
-
508
- err := NewCommand (ctx , args ... ).Run (& RunOpts {
500
+ err := NewCommand (ctx , "log" , "--name-status" , "-c" , "--pretty=format:" , "--parents" , "--no-renames" , "-z" , "-1" ).AddDynamicArguments (commitID ).Run (& RunOpts {
509
501
Dir : repoPath ,
510
502
Stdout : w ,
511
503
Stderr : stderr ,
@@ -521,7 +513,7 @@ func GetCommitFileStatus(ctx context.Context, repoPath, commitID string) (*Commi
521
513
522
514
// GetFullCommitID returns full length (40) of commit ID by given short SHA in a repository.
523
515
func GetFullCommitID (ctx context.Context , repoPath , shortID string ) (string , error ) {
524
- commitID , _ , err := NewCommand (ctx , "rev-parse" , shortID ).RunStdString (& RunOpts {Dir : repoPath })
516
+ commitID , _ , err := NewCommand (ctx , "rev-parse" ). AddDynamicArguments ( shortID ).RunStdString (& RunOpts {Dir : repoPath })
525
517
if err != nil {
526
518
if strings .Contains (err .Error (), "exit status 128" ) {
527
519
return "" , ErrNotExist {shortID , "" }
0 commit comments