Skip to content

Move repository sub menu data loading to the shared file and only display license on repository home page #32686

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion routers/web/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/utils"
"code.gitea.io/gitea/routers/web/repo/shared"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/forms"
release_service "code.gitea.io/gitea/services/release"
Expand All @@ -46,6 +47,10 @@ func Branches(ctx *context.Context) {
ctx.Data["PageIsViewCode"] = true
ctx.Data["PageIsBranches"] = true

if !shared.PrepareRepoSubMenu(ctx) {
return
}

page := ctx.FormInt("page")
if page <= 1 {
page = 1
Expand Down Expand Up @@ -89,7 +94,6 @@ func Branches(ctx *context.Context) {
pager := context.NewPagination(int(branchesCount), pageSize, page, 5)
pager.SetDefaultParams(ctx)
ctx.Data["Page"] = pager
ctx.Data["LicenseFileName"] = repo_service.LicenseFileName
ctx.HTML(http.StatusOK, tplBranch)
}

Expand Down
20 changes: 12 additions & 8 deletions routers/web/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/web/repo/shared"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/gitdiff"
repo_service "code.gitea.io/gitea/services/repository"
Expand Down Expand Up @@ -61,9 +62,7 @@ func Commits(ctx *context.Context) {
}
ctx.Data["PageIsViewCode"] = true

commitsCount, err := ctx.Repo.GetCommitsCount()
if err != nil {
ctx.ServerError("GetCommitsCount", err)
if !shared.PrepareRepoSubMenu(ctx) {
return
}

Expand Down Expand Up @@ -97,12 +96,11 @@ func Commits(ctx *context.Context) {
}
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
ctx.Data["CommitCount"] = commitsCount
ctx.Data["CommitCount"] = ctx.Repo.CommitsCount

pager := context.NewPagination(int(commitsCount), pageSize, page, 5)
pager := context.NewPagination(int(ctx.Repo.CommitsCount), pageSize, page, 5)
pager.SetDefaultParams(ctx)
ctx.Data["Page"] = pager
ctx.Data["LicenseFileName"] = repo_service.LicenseFileName
ctx.HTML(http.StatusOK, tplCommits)
}

Expand Down Expand Up @@ -197,6 +195,10 @@ func SearchCommits(ctx *context.Context) {
ctx.Data["PageIsCommits"] = true
ctx.Data["PageIsViewCode"] = true

if !shared.PrepareRepoSubMenu(ctx) {
return
}

query := ctx.FormTrim("q")
if len(query) == 0 {
ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL())
Expand All @@ -220,7 +222,6 @@ func SearchCommits(ctx *context.Context) {
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
ctx.Data["RefName"] = ctx.Repo.RefName
ctx.Data["LicenseFileName"] = repo_service.LicenseFileName
ctx.HTML(http.StatusOK, tplCommits)
}

Expand All @@ -232,6 +233,10 @@ func FileHistory(ctx *context.Context) {
return
}

if !shared.PrepareRepoSubMenu(ctx) {
return
}

commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ctx.Repo.RefName, fileName)
if err != nil {
ctx.ServerError("FileCommitsCount", err)
Expand Down Expand Up @@ -266,7 +271,6 @@ func FileHistory(ctx *context.Context) {
pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5)
pager.SetDefaultParams(ctx)
ctx.Data["Page"] = pager
ctx.Data["LicenseFileName"] = repo_service.LicenseFileName
ctx.HTML(http.StatusOK, tplCommits)
}

Expand Down
9 changes: 9 additions & 0 deletions routers/web/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/web/feed"
"code.gitea.io/gitea/routers/web/repo/shared"
shared_user "code.gitea.io/gitea/routers/web/shared/user"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/context/upload"
Expand Down Expand Up @@ -156,6 +157,10 @@ func Releases(ctx *context.Context) {
ctx.Data["CanCreateBranch"] = false
ctx.Data["HideBranchesInDropdown"] = true

if !shared.PrepareSubmenuTag(ctx) {
return
}

listOptions := db.ListOptions{
Page: ctx.FormInt("page"),
PageSize: ctx.FormInt("limit"),
Expand Down Expand Up @@ -207,6 +212,10 @@ func TagsList(ctx *context.Context) {
ctx.Data["HideBranchesInDropdown"] = true
ctx.Data["CanCreateRelease"] = ctx.Repo.CanWrite(unit.TypeReleases) && !ctx.Repo.Repository.IsArchived

if !shared.PrepareSubmenuTag(ctx) {
return
}

namePattern := ctx.FormTrim("q")

listOptions := db.ListOptions{
Expand Down
103 changes: 103 additions & 0 deletions routers/web/repo/shared/submenu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package shared

import (
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/optional"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/services/context"
repo_service "code.gitea.io/gitea/services/repository"
)

// PrepareRepoSubMenu prepares data for repository template repo/sub_menu.tmpl
func PrepareRepoSubMenu(ctx *context.Context) bool {
if !prepareSubmenuBranch(ctx) {
return false
}

if !PrepareSubmenuTag(ctx) {
return false
}

if !prepareSubmenuCommit(ctx) {
return false
}

// only show license on repository's home page
if ctx.Data["PageIsRepoHome"] == true {
if !prepareSubmenuLicense(ctx) {
return false
}
}

return true
}

func prepareSubmenuLicense(ctx *context.Context) bool {
repoLicenses, err := repo_model.GetRepoLicenses(ctx, ctx.Repo.Repository)
if err != nil {
ctx.ServerError("GetRepoLicenses", err)
return false
}
ctx.Data["DetectedRepoLicenses"] = repoLicenses.StringList()
ctx.Data["LicenseFileName"] = repo_service.LicenseFileName
return true
}

func prepareSubmenuBranch(ctx *context.Context) bool {
branchOpts := git_model.FindBranchOptions{
RepoID: ctx.Repo.Repository.ID,
IsDeletedBranch: optional.Some(false),
ListOptions: db.ListOptionsAll,
}
branchesTotal, err := db.Count[git_model.Branch](ctx, branchOpts)
if err != nil {
ctx.ServerError("CountBranches", err)
return false
}

// non-empty repo should have at least 1 branch, so this repository's branches haven't been synced yet
if branchesTotal == 0 { // fallback to do a sync immediately
branchesTotal, err = repo_module.SyncRepoBranches(ctx, ctx.Repo.Repository.ID, 0)
if err != nil {
ctx.ServerError("SyncRepoBranches", err)
return false
}
}

ctx.Data["BranchesCount"] = branchesTotal
return true
}

func PrepareSubmenuTag(ctx *context.Context) bool {
var err error
ctx.Data["NumTags"], err = db.Count[repo_model.Release](ctx, repo_model.FindReleasesOptions{
IncludeDrafts: true,
IncludeTags: true,
HasSha1: optional.Some(true), // only draft releases which are created with existing tags
RepoID: ctx.Repo.Repository.ID,
})
if err != nil {
ctx.ServerError("GetReleaseCountByRepoID", err)
return false
}
return true
}

func prepareSubmenuCommit(ctx *context.Context) bool {
var err error
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
if err != nil {
ctx.ServerError("GetCommitsCount", err)
return false
}
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
ctx.Repo.GitRepo.LastCommitCache = git.NewLastCommitCache(ctx.Repo.CommitsCount, ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, cache.GetCache())
return true
}
7 changes: 6 additions & 1 deletion routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"code.gitea.io/gitea/modules/typesniffer"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/web/feed"
"code.gitea.io/gitea/routers/web/repo/shared"
"code.gitea.io/gitea/services/context"
issue_service "code.gitea.io/gitea/services/issue"
repo_service "code.gitea.io/gitea/services/repository"
Expand Down Expand Up @@ -923,6 +924,7 @@ func prepareOpenWithEditorApps(ctx *context.Context) {

func renderHomeCode(ctx *context.Context) {
ctx.Data["PageIsViewCode"] = true
ctx.Data["PageIsRepoHome"] = true
ctx.Data["RepositoryUploadEnabled"] = setting.Repository.Upload.Enabled
prepareOpenWithEditorApps(ctx)

Expand Down Expand Up @@ -980,6 +982,10 @@ func renderHomeCode(ctx *context.Context) {
return
}

if !shared.PrepareRepoSubMenu(ctx) {
return
}

// Get current entry user currently looking at.
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
if err != nil {
Expand Down Expand Up @@ -1062,7 +1068,6 @@ func renderHomeCode(ctx *context.Context) {
ctx.Data["TreeLink"] = treeLink
ctx.Data["TreeNames"] = treeNames
ctx.Data["BranchLink"] = branchLink
ctx.Data["LicenseFileName"] = repo_service.LicenseFileName
ctx.HTML(http.StatusOK, tplRepoHome)
}

Expand Down
48 changes: 0 additions & 48 deletions services/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"code.gitea.io/gitea/modules/httplib"
code_indexer "code.gitea.io/gitea/modules/indexer/code"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/optional"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
Expand Down Expand Up @@ -396,13 +395,6 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) {
ctx.Repo.Repository = repo
ctx.Data["RepoName"] = ctx.Repo.Repository.Name
ctx.Data["IsEmptyRepo"] = ctx.Repo.Repository.IsEmpty

repoLicenses, err := repo_model.GetRepoLicenses(ctx, ctx.Repo.Repository)
if err != nil {
ctx.ServerError("GetRepoLicenses", err)
return
}
ctx.Data["DetectedRepoLicenses"] = repoLicenses.StringList()
}

// RepoAssignment returns a middleware to handle repository assignment
Expand Down Expand Up @@ -520,16 +512,6 @@ func RepoAssignment(ctx *Context) context.CancelFunc {
ctx.Data["RepoExternalIssuesLink"] = unit.ExternalTrackerConfig().ExternalTrackerURL
}

ctx.Data["NumTags"], err = db.Count[repo_model.Release](ctx, repo_model.FindReleasesOptions{
IncludeDrafts: true,
IncludeTags: true,
HasSha1: optional.Some(true), // only draft releases which are created with existing tags
RepoID: ctx.Repo.Repository.ID,
})
if err != nil {
ctx.ServerError("GetReleaseCountByRepoID", err)
return nil
}
ctx.Data["NumReleases"], err = db.Count[repo_model.Release](ctx, repo_model.FindReleasesOptions{
// only show draft releases for users who can write, read-only users shouldn't see draft releases.
IncludeDrafts: ctx.Repo.CanWrite(unit_model.TypeReleases),
Expand Down Expand Up @@ -654,28 +636,6 @@ func RepoAssignment(ctx *Context) context.CancelFunc {
return cancel
}

branchOpts := git_model.FindBranchOptions{
RepoID: ctx.Repo.Repository.ID,
IsDeletedBranch: optional.Some(false),
ListOptions: db.ListOptionsAll,
}
branchesTotal, err := db.Count[git_model.Branch](ctx, branchOpts)
if err != nil {
ctx.ServerError("CountBranches", err)
return cancel
}

// non-empty repo should have at least 1 branch, so this repository's branches haven't been synced yet
if branchesTotal == 0 { // fallback to do a sync immediately
branchesTotal, err = repo_module.SyncRepoBranches(ctx, ctx.Repo.Repository.ID, 0)
if err != nil {
ctx.ServerError("SyncRepoBranches", err)
return cancel
}
}

ctx.Data["BranchesCount"] = branchesTotal

// If no branch is set in the request URL, try to guess a default one.
if len(ctx.Repo.BranchName) == 0 {
if len(ctx.Repo.Repository.DefaultBranch) > 0 && gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
Expand Down Expand Up @@ -1038,14 +998,6 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func
ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit
ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch()

ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
if err != nil {
ctx.ServerError("GetCommitsCount", err)
return cancel
}
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
ctx.Repo.GitRepo.LastCommitCache = git.NewLastCommitCache(ctx.Repo.CommitsCount, ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, cache.GetCache())

return cancel
}
}
Expand Down
Loading