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:
Rafail Giavrimis
2026-09-24 09:46:24 +00:00
committed by GitHub
co-authored by silverwind wxiaoguang
parent 19ae1f842b
commit defc9d5ca3
4 changed files with 35 additions and 5 deletions
+10
View File
@@ -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
+17
View File
@@ -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)
}
{