@@ -92,13 +92,13 @@ func AddChanges(repoPath string, all bool, files ...string) error {
9292}
9393
9494// 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 {
9696 cmd := NewCommandNoGlobals (append (globalArgs , "add" )... )
9797 if all {
9898 cmd .AddArguments ("--all" )
9999 }
100- cmd .AddArguments ( "--" )
101- _ , _ , err := cmd .AddArguments ( files ... ). RunStdString (& RunOpts {Dir : repoPath })
100+ cmd .AddSlashedArguments ( files ... )
101+ _ , _ , err := cmd .RunStdString (& RunOpts {Dir : repoPath })
102102 return err
103103}
104104
@@ -112,27 +112,27 @@ type CommitChangesOptions struct {
112112// CommitChanges commits local changes with given committer, author and message.
113113// If author is nil, it will be the same as committer.
114114func CommitChanges (repoPath string , opts CommitChangesOptions ) error {
115- cargs := make ([]string , len (globalCommandArgs ))
115+ cargs := make ([]CmdArg , len (globalCommandArgs ))
116116 copy (cargs , globalCommandArgs )
117117 return CommitChangesWithArgs (repoPath , cargs , opts )
118118}
119119
120120// CommitChangesWithArgs commits local changes with given committer, author and message.
121121// 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 {
123123 cmd := NewCommandNoGlobals (args ... )
124124 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 ) )
126126 }
127127 cmd .AddArguments ("commit" )
128128
129129 if opts .Author == nil {
130130 opts .Author = opts .Committer
131131 }
132132 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 ) ))
134134 }
135- cmd .AddArguments ("-m" , opts .Message )
135+ cmd .AddArguments ("-m" ). AddDynamicArguments ( opts .Message )
136136
137137 _ , _ , err := cmd .RunStdString (& RunOpts {Dir : repoPath })
138138 // No stderr but exit status 1 means nothing to commit.
@@ -144,15 +144,13 @@ func CommitChangesWithArgs(repoPath string, args []string, opts CommitChangesOpt
144144
145145// AllCommitsCount returns count of all commits in repository
146146func AllCommitsCount (ctx context.Context , repoPath string , hidePRRefs bool , files ... string ) (int64 , error ) {
147- args := [] string { "--all" , "--count" }
147+ cmd := NewCommand ( ctx , "rev-list" )
148148 if hidePRRefs {
149- args = append ([] string { "--exclude=" + PullPrefix + "*" }, args ... )
149+ cmd . AddArguments ( "--exclude=" + PullPrefix + "*" )
150150 }
151- cmd := NewCommand (ctx , "rev-list" )
152- cmd .AddArguments (args ... )
151+ cmd .AddArguments ("--all" , "--count" )
153152 if len (files ) > 0 {
154- cmd .AddArguments ("--" )
155- cmd .AddArguments (files ... )
153+ cmd .AddSlashedArguments (files ... )
156154 }
157155
158156 stdout , _ , err := cmd .RunStdString (& RunOpts {Dir : repoPath })
@@ -168,8 +166,7 @@ func CommitsCountFiles(ctx context.Context, repoPath string, revision, relpath [
168166 cmd := NewCommand (ctx , "rev-list" , "--count" )
169167 cmd .AddDynamicArguments (revision ... )
170168 if len (relpath ) > 0 {
171- cmd .AddArguments ("--" )
172- cmd .AddArguments (relpath ... )
169+ cmd .AddSlashedArguments (relpath ... )
173170 }
174171
175172 stdout , _ , err := cmd .RunStdString (& RunOpts {Dir : repoPath })
@@ -209,7 +206,7 @@ func (c *Commit) HasPreviousCommit(commitHash SHA1) (bool, error) {
209206 return false , nil
210207 }
211208
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 })
213210 if err == nil {
214211 return true , nil
215212 }
@@ -392,15 +389,12 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
392389
393390// GetBranchName gets the closest branch name (as returned by 'git name-rev --name-only')
394391func (c * Commit ) GetBranchName () (string , error ) {
395- args := []string {
396- "name-rev" ,
397- }
392+ cmd := NewCommand (c .repo .Ctx , "name-rev" )
398393 if CheckGitVersionAtLeast ("2.13.0" ) == nil {
399- args = append ( args , "--exclude" , "refs/tags/*" )
394+ cmd . AddArguments ( "--exclude" , "refs/tags/*" )
400395 }
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 })
404398 if err != nil {
405399 // handle special case where git can not describe commit
406400 if strings .Contains (err .Error (), "cannot describe" ) {
@@ -426,7 +420,7 @@ func (c *Commit) LoadBranchName() (err error) {
426420
427421// GetTagName gets the current tag name for given commit
428422func (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 })
430424 if err != nil {
431425 // handle special case where there is no tag for this commit
432426 if strings .Contains (err .Error (), "no tag exactly matches" ) {
@@ -503,9 +497,7 @@ func GetCommitFileStatus(ctx context.Context, repoPath, commitID string) (*Commi
503497 }()
504498
505499 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 {
509501 Dir : repoPath ,
510502 Stdout : w ,
511503 Stderr : stderr ,
@@ -521,7 +513,7 @@ func GetCommitFileStatus(ctx context.Context, repoPath, commitID string) (*Commi
521513
522514// GetFullCommitID returns full length (40) of commit ID by given short SHA in a repository.
523515func 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 })
525517 if err != nil {
526518 if strings .Contains (err .Error (), "exit status 128" ) {
527519 return "" , ErrNotExist {shortID , "" }
0 commit comments