Skip to content

Commit

Permalink
Add description for the three commands and also replace possible sens…
Browse files Browse the repository at this point in the history
…itive content when generating description automatically
  • Loading branch information
lunny committed Mar 27, 2022
1 parent d17eb33 commit 2dff616
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
21 changes: 9 additions & 12 deletions modules/git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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])
}
}
Expand Down
11 changes: 6 additions & 5 deletions modules/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions services/mirror/mirror_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions services/mirror/mirror_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 2dff616

Please sign in to comment.