Skip to content

Commit 2dff616

Browse files
committed
Add description for the three commands and also replace possible sensitive content when generating description automatically
1 parent d17eb33 commit 2dff616

File tree

4 files changed

+21
-23
lines changed

4 files changed

+21
-23
lines changed

modules/git/command.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,6 @@ func (c *Command) AddArguments(args ...string) *Command {
9292
return c
9393
}
9494

95-
// AddURLArgument adds an argument which is a url which means password should be shadow when display in UI
96-
func (c *Command) AddURLArgument(arg string) *Command {
97-
c.urlArgIndexes = append(c.urlArgIndexes, len(c.args))
98-
c.args = append(c.args, arg)
99-
return c
100-
}
101-
10295
// RunInDirTimeoutEnvPipeline executes the command in given directory with given timeout,
10396
// it pipes stdout and stderr to given io.Writer.
10497
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 {
149142

150143
desc := c.desc
151144
if desc == "" {
152-
var args []string
153-
if len(c.urlArgIndexes) == 0 {
154-
args = c.args
155-
} else {
145+
args := c.args
146+
var argSensitiveURLIndexes []int
147+
for i, arg := range c.args {
148+
if strings.Index(arg, "://") != -1 && strings.IndexByte(arg, '@') != -1 {
149+
argSensitiveURLIndexes = append(argSensitiveURLIndexes, i)
150+
}
151+
}
152+
if len(argSensitiveURLIndexes) > 0 {
156153
args = make([]string, len(c.args))
157154
copy(args, c.args)
158-
for _, urlArgIndex := range c.urlArgIndexes {
155+
for _, urlArgIndex := range argSensitiveURLIndexes {
159156
args[urlArgIndex] = util.NewStringURLSanitizer(args[urlArgIndex], true).Replace(args[urlArgIndex])
160157
}
161158
}

modules/git/repo.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"time"
2020

2121
"code.gitea.io/gitea/modules/proxy"
22+
"code.gitea.io/gitea/modules/util"
2223
)
2324

2425
// 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
152153
if len(opts.Branch) > 0 {
153154
cmd.AddArguments("-b", opts.Branch)
154155
}
155-
cmd.AddArguments("--")
156-
cmd.AddURLArgument(from)
157-
cmd.AddArguments(to)
156+
cmd.AddArguments("--", from, to).
157+
SetDescription(fmt.Sprintf("clone from %s to %s", util.NewStringURLSanitizer(from, true).Replace(from), to))
158158

159159
if opts.Timeout <= 0 {
160160
opts.Timeout = -1
@@ -199,11 +199,12 @@ func Push(ctx context.Context, repoPath string, opts PushOptions) error {
199199
if opts.Mirror {
200200
cmd.AddArguments("--mirror")
201201
}
202-
cmd.AddArguments("--")
203-
cmd.AddURLArgument(opts.Remote)
202+
cmd.AddArguments("--", opts.Remote)
204203
if len(opts.Branch) > 0 {
205204
cmd.AddArguments(opts.Branch)
206205
}
206+
cmd.SetDescription(fmt.Sprintf("push %s to %s", repoPath, util.NewStringURLSanitizer(opts.Remote, true).Replace(opts.Remote)))
207+
207208
var outbuf, errbuf strings.Builder
208209

209210
if opts.Timeout == 0 {

services/mirror/mirror_pull.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error
3838
return err
3939
}
4040

41-
cmd := git.NewCommand(ctx, "remote", "add", remoteName, "--mirror=fetch")
42-
cmd.AddURLArgument(addr)
41+
cmd := git.NewCommand(ctx, "remote", "add", remoteName, "--mirror=fetch", addr)
42+
cmd.SetDescription(fmt.Sprintf("add fetch remote %s to %s", util.NewStringURLSanitizer(addr, true).Replace(addr), repoPath))
4343
_, err = cmd.RunInDir(repoPath)
4444
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
4545
return err
@@ -54,8 +54,8 @@ func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error
5454
return err
5555
}
5656

57-
cmd = git.NewCommand(ctx, "remote", "add", remoteName, "--mirror=fetch")
58-
cmd.AddURLArgument(wikiRemotePath)
57+
cmd = git.NewCommand(ctx, "remote", "add", remoteName, "--mirror=fetch", wikiRemotePath)
58+
cmd.SetDescription(fmt.Sprintf("add fetch remote %s to %s", util.NewStringURLSanitizer(wikiRemotePath, true).Replace(wikiRemotePath), wikiPath))
5959
_, err = cmd.RunInDir(wikiPath)
6060
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
6161
return err

services/mirror/mirror_push.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ var stripExitStatus = regexp.MustCompile(`exit status \d+ - `)
2828
// AddPushMirrorRemote registers the push mirror remote.
2929
func AddPushMirrorRemote(ctx context.Context, m *repo_model.PushMirror, addr string) error {
3030
addRemoteAndConfig := func(addr, path string) error {
31-
cmd := git.NewCommand(ctx, "remote", "add", "--mirror=push", m.RemoteName)
32-
cmd.AddURLArgument(addr)
31+
cmd := git.NewCommand(ctx, "remote", "add", "--mirror=push", m.RemoteName, addr)
32+
cmd.SetDescription(fmt.Sprintf("add push remote %s to %s", util.NewStringURLSanitizer(addr, true).Replace(addr), path))
3333
if _, err := cmd.RunInDir(path); err != nil {
3434
return err
3535
}

0 commit comments

Comments
 (0)