Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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.PrepareForRepoSubMenu(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
16 changes: 13 additions & 3 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,6 +62,10 @@ func Commits(ctx *context.Context) {
}
ctx.Data["PageIsViewCode"] = true

if !shared.PrepareForRepoSubMenu(ctx) {
return
}

commitsCount, err := ctx.Repo.GetCommitsCount()
if err != nil {
ctx.ServerError("GetCommitsCount", err)
Expand Down Expand Up @@ -102,7 +107,6 @@ func Commits(ctx *context.Context) {
pager := context.NewPagination(int(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 +201,10 @@ func SearchCommits(ctx *context.Context) {
ctx.Data["PageIsCommits"] = true
ctx.Data["PageIsViewCode"] = true

if !shared.PrepareForRepoSubMenu(ctx) {
return
}

query := ctx.FormTrim("q")
if len(query) == 0 {
ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL())
Expand All @@ -220,12 +228,15 @@ 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)
}

// FileHistory show a file's reversions
func FileHistory(ctx *context.Context) {
if !shared.PrepareForRepoSubMenu(ctx) {
return
}

fileName := ctx.Repo.TreePath
if len(fileName) == 0 {
Commits(ctx)
Expand Down Expand Up @@ -266,7 +277,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
15 changes: 15 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,13 @@ func Releases(ctx *context.Context) {
ctx.Data["CanCreateBranch"] = false
ctx.Data["HideBranchesInDropdown"] = true

// repo/release_tag_header.tmpl will render sub menu depending on the different permission
if !ctx.Repo.CanRead(unit.TypeReleases) && ctx.Repo.CanRead(unit.TypeCode) {
if !shared.PrepareForRepoSubMenu(ctx) {
return
}
}

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

// repo/release_tag_header.tmpl will render sub menu depending on the different permission
if !ctx.Repo.CanRead(unit.TypeReleases) && ctx.Repo.CanRead(unit.TypeCode) {
if !shared.PrepareForRepoSubMenu(ctx) {
return
}
}

namePattern := ctx.FormTrim("q")

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

package shared

import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/services/context"
repo_service "code.gitea.io/gitea/services/repository"
)

// PrepareForRepoSubMenu will prepare all sub menus' data for some repositories pages
// TODO: moving prepae commits count, branches count, tags count and repository size to this function
func PrepareForRepoSubMenu(ctx *context.Context) bool {
return prepareForLicenses(ctx)
}

func prepareForLicenses(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
}
6 changes: 5 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 @@ -980,6 +981,10 @@ func renderHomeCode(ctx *context.Context) {
return
}

if !shared.PrepareForRepoSubMenu(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 +1067,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
7 changes: 0 additions & 7 deletions services/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,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
Loading