diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2022-10-23 22:44:45 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-23 22:44:45 +0800 |
commit | dcd9fc7ee894700f702f3847d7d2a41d6a009b7e (patch) | |
tree | 76b1d6fdb2a760f3da57bbe566146d1c79ab5a87 /modules/git/repo_attribute.go | |
parent | 4eeea7b30ee5d90ed4e9410ec5c7d0252ada3a3b (diff) | |
download | gitea-dcd9fc7ee894700f702f3847d7d2a41d6a009b7e.tar.gz gitea-dcd9fc7ee894700f702f3847d7d2a41d6a009b7e.zip |
Refactor git command arguments and make all arguments to be safe to be used (#21535)
Follow #21464
Make all git command arguments strictly safe. Most changes are one-to-one replacing, keep all existing logic.
Diffstat (limited to 'modules/git/repo_attribute.go')
-rw-r--r-- | modules/git/repo_attribute.go | 26 |
1 files changed, 9 insertions, 17 deletions
diff --git a/modules/git/repo_attribute.go b/modules/git/repo_attribute.go index 21ff93e498..60dc993dc2 100644 --- a/modules/git/repo_attribute.go +++ b/modules/git/repo_attribute.go @@ -20,7 +20,7 @@ import ( type CheckAttributeOpts struct { CachedOnly bool AllAttributes bool - Attributes []string + Attributes []CmdArg Filenames []string IndexFile string WorkTree string @@ -44,31 +44,23 @@ func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[ stdOut := new(bytes.Buffer) stdErr := new(bytes.Buffer) - cmdArgs := []string{"check-attr", "-z"} + cmd := NewCommand(repo.Ctx, "check-attr", "-z") if opts.AllAttributes { - cmdArgs = append(cmdArgs, "-a") + cmd.AddArguments("-a") } else { for _, attribute := range opts.Attributes { if attribute != "" { - cmdArgs = append(cmdArgs, attribute) + cmd.AddArguments(attribute) } } } if opts.CachedOnly { - cmdArgs = append(cmdArgs, "--cached") - } - - cmdArgs = append(cmdArgs, "--") - - for _, arg := range opts.Filenames { - if arg != "" { - cmdArgs = append(cmdArgs, arg) - } + cmd.AddArguments("--cached") } - cmd := NewCommand(repo.Ctx, cmdArgs...) + cmd.AddDashesAndList(opts.Filenames...) if err := cmd.Run(&RunOpts{ Env: env, @@ -106,7 +98,7 @@ func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[ // CheckAttributeReader provides a reader for check-attribute content that can be long running type CheckAttributeReader struct { // params - Attributes []string + Attributes []CmdArg Repo *Repository IndexFile string WorkTree string @@ -122,7 +114,7 @@ type CheckAttributeReader struct { // Init initializes the CheckAttributeReader func (c *CheckAttributeReader) Init(ctx context.Context) error { - cmdArgs := []string{"check-attr", "--stdin", "-z"} + cmdArgs := []CmdArg{"check-attr", "--stdin", "-z"} if len(c.IndexFile) > 0 { cmdArgs = append(cmdArgs, "--cached") @@ -401,7 +393,7 @@ func (repo *Repository) CheckAttributeReader(commitID string) (*CheckAttributeRe } checker := &CheckAttributeReader{ - Attributes: []string{"linguist-vendored", "linguist-generated", "linguist-language", "gitlab-language"}, + Attributes: []CmdArg{"linguist-vendored", "linguist-generated", "linguist-language", "gitlab-language"}, Repo: repo, IndexFile: indexFilename, WorkTree: worktree, |