aboutsummaryrefslogtreecommitdiffstats
path: root/modules/git/grep.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/git/grep.go')
-rw-r--r--modules/git/grep.go33
1 files changed, 25 insertions, 8 deletions
diff --git a/modules/git/grep.go b/modules/git/grep.go
index bf6b41a886..66711650c9 100644
--- a/modules/git/grep.go
+++ b/modules/git/grep.go
@@ -23,11 +23,19 @@ type GrepResult struct {
LineCodes []string
}
+type GrepModeType string
+
+const (
+ GrepModeExact GrepModeType = "exact"
+ GrepModeWords GrepModeType = "words"
+ GrepModeRegexp GrepModeType = "regexp"
+)
+
type GrepOptions struct {
RefName string
MaxResultLimit int
ContextLineNumber int
- IsFuzzy bool
+ GrepMode GrepModeType
MaxLineLength int // the maximum length of a line to parse, exceeding chars will be truncated
PathspecList []string
}
@@ -52,21 +60,30 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
2^@repo: go-gitea/gitea
*/
var results []*GrepResult
- cmd := NewCommand(ctx, "grep", "--null", "--break", "--heading", "--fixed-strings", "--line-number", "--ignore-case", "--full-name")
- cmd.AddOptionValues("--context", fmt.Sprint(opts.ContextLineNumber))
- if opts.IsFuzzy {
+ cmd := NewCommand("grep", "--null", "--break", "--heading", "--line-number", "--full-name")
+ cmd.AddOptionValues("--context", strconv.Itoa(opts.ContextLineNumber))
+ switch opts.GrepMode {
+ case GrepModeExact:
+ cmd.AddArguments("--fixed-strings")
+ cmd.AddOptionValues("-e", strings.TrimLeft(search, "-"))
+ case GrepModeRegexp:
+ cmd.AddArguments("--perl-regexp")
+ cmd.AddOptionValues("-e", strings.TrimLeft(search, "-"))
+ default: /* words */
words := strings.Fields(search)
- for _, word := range words {
+ cmd.AddArguments("--fixed-strings", "--ignore-case")
+ for i, word := range words {
cmd.AddOptionValues("-e", strings.TrimLeft(word, "-"))
+ if i < len(words)-1 {
+ cmd.AddOptionValues("--and")
+ }
}
- } else {
- cmd.AddOptionValues("-e", strings.TrimLeft(search, "-"))
}
cmd.AddDynamicArguments(util.IfZero(opts.RefName, "HEAD"))
cmd.AddDashesAndList(opts.PathspecList...)
opts.MaxResultLimit = util.IfZero(opts.MaxResultLimit, 50)
stderr := bytes.Buffer{}
- err = cmd.Run(&RunOpts{
+ err = cmd.Run(ctx, &RunOpts{
Dir: repo.Path,
Stdout: stdoutWriter,
Stderr: &stderr,