mirror of https://github.com/go-gitea/gitea.git
enable literal string for code search (#33590)
Close: #33588 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Giteabot <teabot@gitea.io>pull/33609/head^2
parent
fc1b383da9
commit
950abfe8ee
@ -0,0 +1,59 @@
|
|||||||
|
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package gitgrep
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/git"
|
||||||
|
code_indexer "code.gitea.io/gitea/modules/indexer/code"
|
||||||
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
)
|
||||||
|
|
||||||
|
func indexSettingToGitGrepPathspecList() (list []string) {
|
||||||
|
for _, expr := range setting.Indexer.IncludePatterns {
|
||||||
|
list = append(list, ":(glob)"+expr.PatternString())
|
||||||
|
}
|
||||||
|
for _, expr := range setting.Indexer.ExcludePatterns {
|
||||||
|
list = append(list, ":(glob,exclude)"+expr.PatternString())
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
func PerformSearch(ctx context.Context, page int, repoID int64, gitRepo *git.Repository, ref git.RefName, keyword string, isFuzzy bool) (searchResults []*code_indexer.Result, total int, err error) {
|
||||||
|
// TODO: it should also respect ParseKeywordAsPhrase and clarify the "fuzzy" behavior
|
||||||
|
res, err := git.GrepSearch(ctx, gitRepo, keyword, git.GrepOptions{
|
||||||
|
ContextLineNumber: 1,
|
||||||
|
IsFuzzy: isFuzzy,
|
||||||
|
RefName: ref.String(),
|
||||||
|
PathspecList: indexSettingToGitGrepPathspecList(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
// TODO: if no branch exists, it reports: exit status 128, fatal: this operation must be run in a work tree.
|
||||||
|
return nil, 0, fmt.Errorf("git.GrepSearch: %w", err)
|
||||||
|
}
|
||||||
|
commitID, err := gitRepo.GetRefCommitID(ref.String())
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, fmt.Errorf("gitRepo.GetRefCommitID: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
total = len(res)
|
||||||
|
pageStart := min((page-1)*setting.UI.RepoSearchPagingNum, len(res))
|
||||||
|
pageEnd := min(page*setting.UI.RepoSearchPagingNum, len(res))
|
||||||
|
res = res[pageStart:pageEnd]
|
||||||
|
for _, r := range res {
|
||||||
|
searchResults = append(searchResults, &code_indexer.Result{
|
||||||
|
RepoID: repoID,
|
||||||
|
Filename: r.Filename,
|
||||||
|
CommitID: commitID,
|
||||||
|
// UpdatedUnix: not supported yet
|
||||||
|
// Language: not supported yet
|
||||||
|
// Color: not supported yet
|
||||||
|
Lines: code_indexer.HighlightSearchResultCode(r.Filename, "", r.LineNumbers, strings.Join(r.LineCodes, "\n")),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return searchResults, total, nil
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
package repo
|
package gitgrep
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
@ -0,0 +1,30 @@
|
|||||||
|
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseKeywordAsPhrase(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
keyword string
|
||||||
|
phrase string
|
||||||
|
isPhrase bool
|
||||||
|
}{
|
||||||
|
{``, "", false},
|
||||||
|
{`a`, "", false},
|
||||||
|
{`"`, "", false},
|
||||||
|
{`"a`, "", false},
|
||||||
|
{`"a"`, "a", true},
|
||||||
|
{`""\"""`, `"\""`, true},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
phrase, isPhrase := ParseKeywordAsPhrase(c.keyword)
|
||||||
|
assert.Equal(t, c.phrase, phrase, "keyword=%q", c.keyword)
|
||||||
|
assert.Equal(t, c.isPhrase, isPhrase, "keyword=%q", c.keyword)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue