mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-25 06:03:40 +09:00
fix(git): keep leading dashes in git grep search patterns (#39404)
Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
co-authored by
silverwind
wxiaoguang
parent
19ae1f842b
commit
defc9d5ca3
@@ -172,6 +172,16 @@ func (c *Command) AddOptionFormat(opt string, args ...any) *Command {
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Command) AddOptionGrepExpr(s string) *Command {
|
||||
if len(c.args) == 0 || c.args[0] != "grep" {
|
||||
c.handlePreErrorBrokenCommand("(not grep command)")
|
||||
return c
|
||||
}
|
||||
// man git-grep: -e: This option has to be used for patterns starting with "-"
|
||||
c.args = append(c.args, "-e", s)
|
||||
return c
|
||||
}
|
||||
|
||||
// AddDynamicArguments adds new dynamic argument values to the command.
|
||||
// The arguments may come from user input and can not be trusted, so no leading '-' is allowed to avoid passing options.
|
||||
// TODO: in the future, this function can be renamed to AddArgumentValues
|
||||
|
||||
@@ -77,10 +77,27 @@ func TestRunWithContextStd(t *testing.T) {
|
||||
cmd := NewCommand()
|
||||
cmd.AddDynamicArguments("-test")
|
||||
assert.ErrorIs(t, cmd.Run(t.Context()), ErrBrokenCommand)
|
||||
assert.Empty(t, cmd.args)
|
||||
|
||||
cmd = NewCommand()
|
||||
cmd.AddDynamicArguments("--test")
|
||||
assert.ErrorIs(t, cmd.Run(t.Context()), ErrBrokenCommand)
|
||||
assert.Empty(t, cmd.args)
|
||||
|
||||
cmd = NewCommand()
|
||||
cmd.AddOptionGrepExpr("-x")
|
||||
assert.ErrorIs(t, cmd.Run(t.Context()), ErrBrokenCommand)
|
||||
assert.Empty(t, cmd.args)
|
||||
|
||||
cmd = NewCommand("any")
|
||||
cmd.AddOptionGrepExpr("-x")
|
||||
assert.ErrorIs(t, cmd.Run(t.Context()), ErrBrokenCommand)
|
||||
assert.Equal(t, []string{"any"}, cmd.args)
|
||||
|
||||
cmd = NewCommand("grep")
|
||||
cmd.AddOptionGrepExpr("-x")
|
||||
assert.NoError(t, cmd.Run(t.Context()))
|
||||
assert.Equal(t, []string{"grep", "-e", "-x"}, cmd.args)
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user