diff --git a/modules/git/command.go b/modules/git/command.go index fb375e4b6bb8..9b743241fa40 100644 --- a/modules/git/command.go +++ b/modules/git/command.go @@ -92,13 +92,6 @@ func (c *Command) AddArguments(args ...string) *Command { return c } -// AddURLArgument adds an argument which is a url which means password should be shadow when display in UI -func (c *Command) AddURLArgument(arg string) *Command { - c.urlArgIndexes = append(c.urlArgIndexes, len(c.args)) - c.args = append(c.args, arg) - return c -} - // RunInDirTimeoutEnvPipeline executes the command in given directory with given timeout, // it pipes stdout and stderr to given io.Writer. func (c *Command) RunInDirTimeoutEnvPipeline(env []string, timeout time.Duration, dir string, stdout, stderr io.Writer) error { @@ -149,13 +142,17 @@ func (c *Command) RunWithContext(rc *RunContext) error { desc := c.desc if desc == "" { - var args []string - if len(c.urlArgIndexes) == 0 { - args = c.args - } else { + args := c.args + var argSensitiveURLIndexes []int + for i, arg := range c.args { + if strings.Index(arg, "://") != -1 && strings.IndexByte(arg, '@') != -1 { + argSensitiveURLIndexes = append(argSensitiveURLIndexes, i) + } + } + if len(argSensitiveURLIndexes) > 0 { args = make([]string, len(c.args)) copy(args, c.args) - for _, urlArgIndex := range c.urlArgIndexes { + for _, urlArgIndex := range argSensitiveURLIndexes { args[urlArgIndex] = util.NewStringURLSanitizer(args[urlArgIndex], true).Replace(args[urlArgIndex]) } } diff --git a/modules/git/repo.go b/modules/git/repo.go index d245f816c1bb..d627e86f9e2d 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -19,6 +19,7 @@ import ( "time" "code.gitea.io/gitea/modules/proxy" + "code.gitea.io/gitea/modules/util" ) // GPGSettings represents the default GPG settings for this repository @@ -152,9 +153,8 @@ func CloneWithArgs(ctx context.Context, from, to string, args []string, opts Clo if len(opts.Branch) > 0 { cmd.AddArguments("-b", opts.Branch) } - cmd.AddArguments("--") - cmd.AddURLArgument(from) - cmd.AddArguments(to) + cmd.AddArguments("--", from, to). + SetDescription(fmt.Sprintf("clone from %s to %s", util.NewStringURLSanitizer(from, true).Replace(from), to)) if opts.Timeout <= 0 { opts.Timeout = -1 @@ -199,11 +199,12 @@ func Push(ctx context.Context, repoPath string, opts PushOptions) error { if opts.Mirror { cmd.AddArguments("--mirror") } - cmd.AddArguments("--") - cmd.AddURLArgument(opts.Remote) + cmd.AddArguments("--", opts.Remote) if len(opts.Branch) > 0 { cmd.AddArguments(opts.Branch) } + cmd.SetDescription(fmt.Sprintf("push %s to %s", repoPath, util.NewStringURLSanitizer(opts.Remote, true).Replace(opts.Remote))) + var outbuf, errbuf strings.Builder if opts.Timeout == 0 { diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index e2b49217c718..d5749cb0a71b 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -38,8 +38,8 @@ func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error return err } - cmd := git.NewCommand(ctx, "remote", "add", remoteName, "--mirror=fetch") - cmd.AddURLArgument(addr) + cmd := git.NewCommand(ctx, "remote", "add", remoteName, "--mirror=fetch", addr) + cmd.SetDescription(fmt.Sprintf("add fetch remote %s to %s", util.NewStringURLSanitizer(addr, true).Replace(addr), repoPath)) _, err = cmd.RunInDir(repoPath) if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") { return err @@ -54,8 +54,8 @@ func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error return err } - cmd = git.NewCommand(ctx, "remote", "add", remoteName, "--mirror=fetch") - cmd.AddURLArgument(wikiRemotePath) + cmd = git.NewCommand(ctx, "remote", "add", remoteName, "--mirror=fetch", wikiRemotePath) + cmd.SetDescription(fmt.Sprintf("add fetch remote %s to %s", util.NewStringURLSanitizer(wikiRemotePath, true).Replace(wikiRemotePath), wikiPath)) _, err = cmd.RunInDir(wikiPath) if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") { return err diff --git a/services/mirror/mirror_push.go b/services/mirror/mirror_push.go index b1e7b8ad3be3..64165da1d851 100644 --- a/services/mirror/mirror_push.go +++ b/services/mirror/mirror_push.go @@ -28,8 +28,8 @@ var stripExitStatus = regexp.MustCompile(`exit status \d+ - `) // AddPushMirrorRemote registers the push mirror remote. func AddPushMirrorRemote(ctx context.Context, m *repo_model.PushMirror, addr string) error { addRemoteAndConfig := func(addr, path string) error { - cmd := git.NewCommand(ctx, "remote", "add", "--mirror=push", m.RemoteName) - cmd.AddURLArgument(addr) + cmd := git.NewCommand(ctx, "remote", "add", "--mirror=push", m.RemoteName, addr) + cmd.SetDescription(fmt.Sprintf("add push remote %s to %s", util.NewStringURLSanitizer(addr, true).Replace(addr), path)) if _, err := cmd.RunInDir(path); err != nil { return err }