aboutsummaryrefslogtreecommitdiffstats
path: root/modules/git/repo.go
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2022-10-23 22:44:45 +0800
committerGitHub <noreply@github.com>2022-10-23 22:44:45 +0800
commitdcd9fc7ee894700f702f3847d7d2a41d6a009b7e (patch)
tree76b1d6fdb2a760f3da57bbe566146d1c79ab5a87 /modules/git/repo.go
parent4eeea7b30ee5d90ed4e9410ec5c7d0252ada3a3b (diff)
downloadgitea-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.go')
-rw-r--r--modules/git/repo.go28
1 files changed, 14 insertions, 14 deletions
diff --git a/modules/git/repo.go b/modules/git/repo.go
index 3176e27695..575b686e29 100644
--- a/modules/git/repo.go
+++ b/modules/git/repo.go
@@ -59,7 +59,7 @@ func (repo *Repository) parsePrettyFormatLogToList(logs []byte) ([]*Commit, erro
// IsRepoURLAccessible checks if given repository URL is accessible.
func IsRepoURLAccessible(ctx context.Context, url string) bool {
- _, _, err := NewCommand(ctx, "ls-remote", "-q", "-h", url, "HEAD").RunStdString(nil)
+ _, _, err := NewCommand(ctx, "ls-remote", "-q", "-h").AddDynamicArguments(url, "HEAD").RunStdString(nil)
return err == nil
}
@@ -112,13 +112,11 @@ type CloneRepoOptions struct {
// Clone clones original repository to target path.
func Clone(ctx context.Context, from, to string, opts CloneRepoOptions) error {
- cargs := make([]string, len(globalCommandArgs))
- copy(cargs, globalCommandArgs)
- return CloneWithArgs(ctx, from, to, cargs, opts)
+ return CloneWithArgs(ctx, globalCommandArgs, from, to, opts)
}
// CloneWithArgs original repository to target path.
-func CloneWithArgs(ctx context.Context, from, to string, args []string, opts CloneRepoOptions) (err error) {
+func CloneWithArgs(ctx context.Context, args []CmdArg, from, to string, opts CloneRepoOptions) (err error) {
toDir := path.Dir(to)
if err = os.MkdirAll(toDir, os.ModePerm); err != nil {
return err
@@ -144,15 +142,15 @@ func CloneWithArgs(ctx context.Context, from, to string, args []string, opts Clo
cmd.AddArguments("--no-checkout")
}
if opts.Depth > 0 {
- cmd.AddArguments("--depth", strconv.Itoa(opts.Depth))
+ cmd.AddArguments("--depth").AddDynamicArguments(strconv.Itoa(opts.Depth))
}
if opts.Filter != "" {
- cmd.AddArguments("--filter", opts.Filter)
+ cmd.AddArguments("--filter").AddDynamicArguments(opts.Filter)
}
if len(opts.Branch) > 0 {
- cmd.AddArguments("-b", opts.Branch)
+ cmd.AddArguments("-b").AddDynamicArguments(opts.Branch)
}
- cmd.AddArguments("--", from, to)
+ cmd.AddDashesAndList(from, to)
if strings.Contains(from, "://") && strings.Contains(from, "@") {
cmd.SetDescription(fmt.Sprintf("clone branch %s from %s to %s (shared: %t, mirror: %t, depth: %d)", opts.Branch, util.SanitizeCredentialURLs(from), to, opts.Shared, opts.Mirror, opts.Depth))
@@ -203,10 +201,12 @@ func Push(ctx context.Context, repoPath string, opts PushOptions) error {
if opts.Mirror {
cmd.AddArguments("--mirror")
}
- cmd.AddArguments("--", opts.Remote)
+ remoteBranchArgs := []string{opts.Remote}
if len(opts.Branch) > 0 {
- cmd.AddArguments(opts.Branch)
+ remoteBranchArgs = append(remoteBranchArgs, opts.Branch)
}
+ cmd.AddDashesAndList(remoteBranchArgs...)
+
if strings.Contains(opts.Remote, "://") && strings.Contains(opts.Remote, "@") {
cmd.SetDescription(fmt.Sprintf("push branch %s to %s (force: %t, mirror: %t)", opts.Branch, util.SanitizeCredentialURLs(opts.Remote), opts.Force, opts.Mirror))
} else {
@@ -276,7 +276,7 @@ type DivergeObject struct {
func checkDivergence(ctx context.Context, repoPath, baseBranch, targetBranch string) (int, error) {
branches := fmt.Sprintf("%s..%s", baseBranch, targetBranch)
- cmd := NewCommand(ctx, "rev-list", "--count", branches)
+ cmd := NewCommand(ctx, "rev-list", "--count").AddDynamicArguments(branches)
stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath})
if err != nil {
return -1, err
@@ -319,7 +319,7 @@ func (repo *Repository) CreateBundle(ctx context.Context, commit string, out io.
return err
}
- _, _, err = NewCommand(ctx, "reset", "--soft", commit).RunStdString(&RunOpts{Dir: tmp, Env: env})
+ _, _, err = NewCommand(ctx, "reset", "--soft").AddDynamicArguments(commit).RunStdString(&RunOpts{Dir: tmp, Env: env})
if err != nil {
return err
}
@@ -330,7 +330,7 @@ func (repo *Repository) CreateBundle(ctx context.Context, commit string, out io.
}
tmpFile := filepath.Join(tmp, "bundle")
- _, _, err = NewCommand(ctx, "bundle", "create", tmpFile, "bundle", "HEAD").RunStdString(&RunOpts{Dir: tmp, Env: env})
+ _, _, err = NewCommand(ctx, "bundle", "create").AddDynamicArguments(tmpFile, "bundle", "HEAD").RunStdString(&RunOpts{Dir: tmp, Env: env})
if err != nil {
return err
}