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/repository | |
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/repository')
-rw-r--r-- | modules/repository/generate.go | 2 | ||||
-rw-r--r-- | modules/repository/init.go | 18 | ||||
-rw-r--r-- | modules/repository/push.go | 2 |
3 files changed, 11 insertions, 11 deletions
diff --git a/modules/repository/generate.go b/modules/repository/generate.go index 4d76d33993..a69dd12a78 100644 --- a/modules/repository/generate.go +++ b/modules/repository/generate.go @@ -211,7 +211,7 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r } repoPath := repo.RepoPath() - if stdout, _, err := git.NewCommand(ctx, "remote", "add", "origin", repoPath). + if stdout, _, err := git.NewCommand(ctx, "remote", "add", "origin").AddDynamicArguments(repoPath). SetDescription(fmt.Sprintf("generateRepoCommit (git remote add): %s to %s", templateRepoPath, tmpDir)). RunStdString(&git.RunOpts{Dir: tmpDir, Env: env}); err != nil { log.Error("Unable to add %v as remote origin to temporary repo to %s: stdout %s\nError: %v", repo, tmpDir, stdout, err) diff --git a/modules/repository/init.go b/modules/repository/init.go index 37ed0748b4..473729a599 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -228,7 +228,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir, ) // Clone to temporary path and do the init commit. - if stdout, _, err := git.NewCommand(ctx, "clone", repoPath, tmpDir). + if stdout, _, err := git.NewCommand(ctx, "clone").AddDynamicArguments(repoPath, tmpDir). SetDescription(fmt.Sprintf("prepareRepoCommit (git clone): %s to %s", repoPath, tmpDir)). RunStdString(&git.RunOpts{Dir: "", Env: env}); err != nil { log.Error("Failed to clone from %v into %s: stdout: %s\nError: %v", repo, tmpDir, stdout, err) @@ -317,14 +317,14 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi return fmt.Errorf("git add --all: %v", err) } - args := []string{ - "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), + cmd := git.NewCommand(ctx, + "commit", git.CmdArg(fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email)), "-m", "Initial commit", - } + ) sign, keyID, signer, _ := asymkey_service.SignInitialCommit(ctx, tmpPath, u) if sign { - args = append(args, "-S"+keyID) + cmd.AddArguments(git.CmdArg("-S" + keyID)) if repo.GetTrustModel() == repo_model.CommitterTrustModel || repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel { // need to set the committer to the KeyID owner @@ -332,7 +332,7 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi committerEmail = signer.Email } } else { - args = append(args, "--no-gpg-sign") + cmd.AddArguments("--no-gpg-sign") } env = append(env, @@ -340,10 +340,10 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi "GIT_COMMITTER_EMAIL="+committerEmail, ) - if stdout, _, err := git.NewCommand(ctx, args...). + if stdout, _, err := cmd. SetDescription(fmt.Sprintf("initRepoCommit (git commit): %s", tmpPath)). RunStdString(&git.RunOpts{Dir: tmpPath, Env: env}); err != nil { - log.Error("Failed to commit: %v: Stdout: %s\nError: %v", args, stdout, err) + log.Error("Failed to commit: %v: Stdout: %s\nError: %v", cmd.String(), stdout, err) return fmt.Errorf("git commit: %v", err) } @@ -351,7 +351,7 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi defaultBranch = setting.Repository.DefaultBranch } - if stdout, _, err := git.NewCommand(ctx, "push", "origin", "HEAD:"+defaultBranch). + if stdout, _, err := git.NewCommand(ctx, "push", "origin").AddDynamicArguments("HEAD:" + defaultBranch). SetDescription(fmt.Sprintf("initRepoCommit (git push): %s", tmpPath)). RunStdString(&git.RunOpts{Dir: tmpPath, Env: InternalPushingEnvironment(u, repo)}); err != nil { log.Error("Failed to push back to HEAD: Stdout: %s\nError: %v", stdout, err) diff --git a/modules/repository/push.go b/modules/repository/push.go index e1dbd5d2fc..4e4b4000b1 100644 --- a/modules/repository/push.go +++ b/modules/repository/push.go @@ -104,7 +104,7 @@ func IsForcePush(ctx context.Context, opts *PushUpdateOptions) (bool, error) { return false, nil } - output, _, err := git.NewCommand(ctx, "rev-list", "--max-count=1", opts.OldCommitID, "^"+opts.NewCommitID). + output, _, err := git.NewCommand(ctx, "rev-list", "--max-count=1").AddDynamicArguments(opts.OldCommitID, "^"+opts.NewCommitID). RunStdString(&git.RunOpts{Dir: repo_model.RepoPath(opts.RepoUserName, opts.RepoName)}) if err != nil { return false, err |