forked from kevadesu/forgejo
git-grep: support regexp
This commit is contained in:
parent
0ccefbebfc
commit
6d6116857c
4 changed files with 99 additions and 10 deletions
|
@ -17,16 +17,55 @@ import (
|
|||
|
||||
const tplSearch base.TplName = "repo/search"
|
||||
|
||||
type searchMode int
|
||||
|
||||
const (
|
||||
ExactSearchMode searchMode = iota
|
||||
FuzzySearchMode
|
||||
RegExpSearchMode
|
||||
)
|
||||
|
||||
func searchModeFromString(s string) searchMode {
|
||||
switch s {
|
||||
case "fuzzy":
|
||||
return FuzzySearchMode
|
||||
case "regexp":
|
||||
return RegExpSearchMode
|
||||
default:
|
||||
return ExactSearchMode
|
||||
}
|
||||
}
|
||||
|
||||
func (m searchMode) String() string {
|
||||
switch m {
|
||||
case ExactSearchMode:
|
||||
return "exact"
|
||||
case FuzzySearchMode:
|
||||
return "fuzzy"
|
||||
case RegExpSearchMode:
|
||||
return "regexp"
|
||||
default:
|
||||
panic("cannot happen")
|
||||
}
|
||||
}
|
||||
|
||||
// Search render repository search page
|
||||
func Search(ctx *context.Context) {
|
||||
language := ctx.FormTrim("l")
|
||||
keyword := ctx.FormTrim("q")
|
||||
|
||||
isFuzzy := ctx.FormOptionalBool("fuzzy").ValueOrDefault(true)
|
||||
mode := ExactSearchMode
|
||||
if modeStr := ctx.FormString("mode"); len(modeStr) > 0 {
|
||||
mode = searchModeFromString(modeStr)
|
||||
} else if ctx.FormOptionalBool("fuzzy").ValueOrDefault(true) { // for backward compatibility in links
|
||||
mode = FuzzySearchMode
|
||||
}
|
||||
|
||||
ctx.Data["Keyword"] = keyword
|
||||
ctx.Data["Language"] = language
|
||||
ctx.Data["IsFuzzy"] = isFuzzy
|
||||
ctx.Data["IsFuzzy"] = mode == FuzzySearchMode
|
||||
ctx.Data["IsRegExp"] = mode == RegExpSearchMode
|
||||
ctx.Data["SearchMode"] = mode.String()
|
||||
ctx.Data["PageIsViewCode"] = true
|
||||
|
||||
if keyword == "" {
|
||||
|
@ -47,7 +86,7 @@ func Search(ctx *context.Context) {
|
|||
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, &code_indexer.SearchOptions{
|
||||
RepoIDs: []int64{ctx.Repo.Repository.ID},
|
||||
Keyword: keyword,
|
||||
IsKeywordFuzzy: isFuzzy,
|
||||
IsKeywordFuzzy: mode == FuzzySearchMode,
|
||||
Language: language,
|
||||
Paginator: &db.ListOptions{
|
||||
Page: page,
|
||||
|
@ -64,11 +103,17 @@ func Search(ctx *context.Context) {
|
|||
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable(ctx)
|
||||
}
|
||||
} else {
|
||||
res, err := git.GrepSearch(ctx, ctx.Repo.GitRepo, keyword, git.GrepOptions{
|
||||
grepOpt := git.GrepOptions{
|
||||
ContextLineNumber: 1,
|
||||
IsFuzzy: isFuzzy,
|
||||
RefName: ctx.Repo.RefName,
|
||||
})
|
||||
}
|
||||
switch mode {
|
||||
case FuzzySearchMode:
|
||||
grepOpt.Mode = git.FixedAnyGrepMode
|
||||
case RegExpSearchMode:
|
||||
grepOpt.Mode = git.RegExpGrepMode
|
||||
}
|
||||
res, err := git.GrepSearch(ctx, ctx.Repo.GitRepo, keyword, grepOpt)
|
||||
if err != nil {
|
||||
ctx.ServerError("GrepSearch", err)
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue