forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'giteaofficial/main'
* giteaofficial/main: Make `owner/repo/pulls` handlers use "PR reader" permission (go-gitea#32254) make `show stats` work when only one file changed (go-gitea#32244) Update scheduled tasks even if changes are pushed by "ActionsUser" (go-gitea#32246) Support migrating GitHub/GitLab PR draft status (go-gitea#32242) Only rename a user when they should receive a different name (go-gitea#32247) Fix dropdown content overflow (go-gitea#31610) Make git push options accept short name (go-gitea#32245) Allow code search by filename (go-gitea#32210) Allow maintainers to view and edit files of private repos when "Allow maintainers to edit" is enabled (go-gitea#32215) Use per package global lock for container uploads instead of memory lock (go-gitea#31860)
- Loading branch information
Showing
61 changed files
with
1,067 additions
and
146 deletions.
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
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
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,101 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package path | ||
|
||
import ( | ||
"slices" | ||
"strings" | ||
|
||
"github.com/blevesearch/bleve/v2/analysis" | ||
"github.com/blevesearch/bleve/v2/registry" | ||
) | ||
|
||
const ( | ||
Name = "gitea/path" | ||
) | ||
|
||
type TokenFilter struct{} | ||
|
||
func NewTokenFilter() *TokenFilter { | ||
return &TokenFilter{} | ||
} | ||
|
||
func TokenFilterConstructor(config map[string]any, cache *registry.Cache) (analysis.TokenFilter, error) { | ||
return NewTokenFilter(), nil | ||
} | ||
|
||
func (s *TokenFilter) Filter(input analysis.TokenStream) analysis.TokenStream { | ||
if len(input) == 1 { | ||
// if there is only one token, we dont need to generate the reversed chain | ||
return generatePathTokens(input, false) | ||
} | ||
|
||
normal := generatePathTokens(input, false) | ||
reversed := generatePathTokens(input, true) | ||
|
||
return append(normal, reversed...) | ||
} | ||
|
||
// Generates path tokens from the input tokens. | ||
// This mimics the behavior of the path hierarchy tokenizer in ES. It takes the input tokens and combine them, generating a term for each component | ||
// in tree (e.g., foo/bar/baz.md will generate foo, foo/bar, and foo/bar/baz.md). | ||
// | ||
// If the reverse flag is set, the order of the tokens is reversed (the same input will generate baz.md, baz.md/bar, baz.md/bar/foo). This is useful | ||
// to efficiently search for filenames without supplying the fullpath. | ||
func generatePathTokens(input analysis.TokenStream, reversed bool) analysis.TokenStream { | ||
terms := make([]string, 0, len(input)) | ||
longestTerm := 0 | ||
|
||
if reversed { | ||
slices.Reverse(input) | ||
} | ||
|
||
for i := 0; i < len(input); i++ { | ||
var sb strings.Builder | ||
sb.WriteString(string(input[0].Term)) | ||
|
||
for j := 1; j < i; j++ { | ||
sb.WriteString("/") | ||
sb.WriteString(string(input[j].Term)) | ||
} | ||
|
||
term := sb.String() | ||
|
||
if longestTerm < len(term) { | ||
longestTerm = len(term) | ||
} | ||
|
||
terms = append(terms, term) | ||
} | ||
|
||
output := make(analysis.TokenStream, 0, len(terms)) | ||
|
||
for _, term := range terms { | ||
var start, end int | ||
|
||
if reversed { | ||
start = 0 | ||
end = len(term) | ||
} else { | ||
start = longestTerm - len(term) | ||
end = longestTerm | ||
} | ||
|
||
token := analysis.Token{ | ||
Position: 1, | ||
Start: start, | ||
End: end, | ||
Type: analysis.AlphaNumeric, | ||
Term: []byte(term), | ||
} | ||
|
||
output = append(output, &token) | ||
} | ||
|
||
return output | ||
} | ||
|
||
func init() { | ||
registry.RegisterTokenFilter(Name, TokenFilterConstructor) | ||
} |
Oops, something went wrong.