2017-10-27 11:40:54 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2017-10-27 11:40:54 +05:30
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2021-04-05 21:00:52 +05:30
|
|
|
"net/http"
|
2017-10-27 11:40:54 +05:30
|
|
|
|
2024-03-16 16:02:45 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2017-10-27 11:40:54 +05:30
|
|
|
"code.gitea.io/gitea/modules/base"
|
2019-12-23 18:01:16 +05:30
|
|
|
code_indexer "code.gitea.io/gitea/modules/indexer/code"
|
2017-10-27 11:40:54 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-02-27 12:42:22 +05:30
|
|
|
"code.gitea.io/gitea/services/context"
|
2024-02-20 16:35:42 +05:30
|
|
|
"code.gitea.io/gitea/services/repository/files"
|
2017-10-27 11:40:54 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const tplSearch base.TplName = "repo/search"
|
|
|
|
|
|
|
|
// Search render repository search page
|
|
|
|
func Search(ctx *context.Context) {
|
2021-08-11 20:38:52 +05:30
|
|
|
language := ctx.FormTrim("l")
|
|
|
|
keyword := ctx.FormTrim("q")
|
2022-10-11 04:42:03 +05:30
|
|
|
|
|
|
|
queryType := ctx.FormTrim("t")
|
2024-03-09 07:09:27 +05:30
|
|
|
isFuzzy := queryType != "match"
|
2022-10-11 04:42:03 +05:30
|
|
|
|
|
|
|
ctx.Data["Keyword"] = keyword
|
|
|
|
ctx.Data["Language"] = language
|
|
|
|
ctx.Data["queryType"] = queryType
|
|
|
|
ctx.Data["PageIsViewCode"] = true
|
|
|
|
|
|
|
|
if keyword == "" {
|
|
|
|
ctx.HTML(http.StatusOK, tplSearch)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-20 16:35:42 +05:30
|
|
|
ctx.Data["SourcePath"] = ctx.Repo.Repository.Link()
|
|
|
|
|
2021-07-29 07:12:15 +05:30
|
|
|
page := ctx.FormInt("page")
|
2017-10-27 11:40:54 +05:30
|
|
|
if page <= 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
2021-01-27 15:30:35 +05:30
|
|
|
|
2024-02-20 16:35:42 +05:30
|
|
|
if setting.Indexer.RepoIndexerEnabled {
|
|
|
|
ctx.Data["CodeIndexerEnabled"] = true
|
|
|
|
|
2024-03-16 16:02:45 +05:30
|
|
|
total, searchResults, searchResultLanguages, err := code_indexer.PerformSearch(ctx, &code_indexer.SearchOptions{
|
|
|
|
RepoIDs: []int64{ctx.Repo.Repository.ID},
|
|
|
|
Keyword: keyword,
|
|
|
|
IsKeywordFuzzy: isFuzzy,
|
|
|
|
Language: language,
|
|
|
|
Paginator: &db.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: setting.UI.RepoSearchPagingNum,
|
|
|
|
},
|
|
|
|
})
|
2024-02-20 16:35:42 +05:30
|
|
|
if err != nil {
|
|
|
|
if code_indexer.IsAvailable(ctx) {
|
|
|
|
ctx.ServerError("SearchResults", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["CodeIndexerUnavailable"] = true
|
|
|
|
} else {
|
|
|
|
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable(ctx)
|
2022-01-27 14:00:51 +05:30
|
|
|
}
|
2024-02-20 16:35:42 +05:30
|
|
|
|
|
|
|
ctx.Data["SearchResults"] = searchResults
|
|
|
|
ctx.Data["SearchResultLanguages"] = searchResultLanguages
|
|
|
|
|
|
|
|
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
|
|
|
|
pager.SetDefaultParams(ctx)
|
|
|
|
pager.AddParam(ctx, "l", "Language")
|
|
|
|
ctx.Data["Page"] = pager
|
2022-01-27 14:00:51 +05:30
|
|
|
} else {
|
2024-02-20 16:35:42 +05:30
|
|
|
data, err := files.NewRepoGrep(ctx, ctx.Repo.Repository, keyword)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("NewRepoGrep", err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-11 04:42:03 +05:30
|
|
|
|
2024-02-20 16:35:42 +05:30
|
|
|
ctx.Data["CodeIndexerEnabled"] = false
|
|
|
|
ctx.Data["SearchResults"] = data
|
2019-04-20 09:45:19 +05:30
|
|
|
|
2024-02-20 16:35:42 +05:30
|
|
|
pager := context.NewPagination(len(data), setting.UI.RepoSearchPagingNum, page, 5)
|
|
|
|
pager.SetDefaultParams(ctx)
|
|
|
|
ctx.Data["Page"] = pager
|
|
|
|
}
|
2019-04-20 09:45:19 +05:30
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSearch)
|
2017-10-27 11:40:54 +05:30
|
|
|
}
|