-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enable literal string for code search #33590
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dcb58f0
enable literal string for code search
darren e87e418
simplify code per comments
darren fca84ac
fix elasticsearch, refactor gitgrep
wxiaoguang d51d736
fix tests
wxiaoguang fe16c6a
Merge branch 'main' into bleve-literal-search
GiteaBot d78e335
Merge branch 'main' into bleve-literal-search
GiteaBot dc151b5
Merge branch 'main' into bleve-literal-search
GiteaBot e959b58
Merge branch 'main' into bleve-literal-search
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
2 changes: 1 addition & 1 deletion
2
routers/web/repo/search_test.go → modules/indexer/code/gitgrep/gitgrep_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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) { | ||
phrase, isPhrase := ParseKeywordAsPhrase(`a`) | ||
assert.Empty(t, phrase) | ||
assert.False(t, isPhrase) | ||
|
||
phrase, isPhrase = ParseKeywordAsPhrase(`"a"`) | ||
assert.Equal(t, "a", phrase) | ||
assert.True(t, isPhrase) | ||
|
||
phrase, isPhrase = ParseKeywordAsPhrase(`""\"""`) | ||
assert.Equal(t, `"\""`, phrase) | ||
assert.True(t, isPhrase) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me like an unintended regression from https://github.com/go-gitea/gitea/pull/32210/files#diff-e1866799c5d7d231d9df896b84d4a9fa54d404d150e26ac6b205eaf0fea3790bL243-R270 that the default changed from NewMatchPhraseQuery to NewMatchQuery. When searching for something like
swaggerParameterBodies
it is currently searching separately onswagger
,parameter
andbodies
.So I wonder if it shouldn't simply be switched back to NewMatchPhraseQuery, without requiring the quotes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree (I seldom use the "code search" feature, and just tried to help to make this PR's code more general).
@jpraet @lunny Would you like to propose some PRs to fix the regression? And maybe make the elasticsearch have the same behavior (I will improve the git grep search later). Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bsofiato can you confirm the switch from MatchPhraseQuery to MatchQuery for bleve in #32210 was unintentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jpraet , I made that PR some time ago, but I don't recall being intentional (i.e., thinking about the PR at hand -- search by filename --, it doesn't make much sense to change from MatchPhraseQuery to MatchQuery).