Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

search.go 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "path"
  7. "strings"
  8. "code.gitea.io/gitea/modules/base"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/search"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/Unknwon/paginater"
  13. )
  14. const tplSearch base.TplName = "repo/search"
  15. // Search render repository search page
  16. func Search(ctx *context.Context) {
  17. if !setting.Indexer.RepoIndexerEnabled {
  18. ctx.Redirect(ctx.Repo.RepoLink, 302)
  19. return
  20. }
  21. keyword := strings.TrimSpace(ctx.Query("q"))
  22. page := ctx.QueryInt("page")
  23. if page <= 0 {
  24. page = 1
  25. }
  26. total, searchResults, err := search.PerformSearch(ctx.Repo.Repository.ID, keyword, page, setting.UI.RepoSearchPagingNum)
  27. if err != nil {
  28. ctx.Handle(500, "SearchResults", err)
  29. return
  30. }
  31. ctx.Data["Keyword"] = keyword
  32. pager := paginater.New(total, setting.UI.RepoSearchPagingNum, page, 5)
  33. ctx.Data["Page"] = pager
  34. ctx.Data["SourcePath"] = setting.AppSubURL + "/" +
  35. path.Join(ctx.Repo.Repository.Owner.Name, ctx.Repo.Repository.Name, "src", ctx.Repo.Repository.DefaultBranch)
  36. ctx.Data["SearchResults"] = searchResults
  37. ctx.Data["RequireHighlightJS"] = true
  38. ctx.Data["PageIsViewCode"] = true
  39. ctx.HTML(200, tplSearch)
  40. }