From f769c8bf431ae1a2c4dd44a4f32fa24ad21ea689 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 9 Jan 2025 23:11:33 -0800 Subject: [PATCH] Make template simpler --- services/context/repo.go | 18 ++++++++++++++++++ templates/repo/home_branch_dropdown.tmpl | 16 ++-------------- templates/repo/view_file_tree_sidebar.tmpl | 17 ++--------------- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/services/context/repo.go b/services/context/repo.go index 94b2972c2b..3e83233316 100644 --- a/services/context/repo.go +++ b/services/context/repo.go @@ -21,6 +21,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" @@ -743,6 +744,21 @@ const ( RepoRefBlob ) +func RefTypeName(refType RepoRefType) string { + switch refType { + case RepoRefBranch: + return "branch" + case RepoRefTag: + return "tag" + case RepoRefCommit: + return "commit" + case RepoRefBlob: + return "blob" + default: + return "unknown" + } +} + const headRefName = "HEAD" // RepoRef handles repository reference names when the ref name is not @@ -988,6 +1004,8 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func ctx.Data["BranchName"] = ctx.Repo.BranchName ctx.Data["RefName"] = ctx.Repo.RefName + ctx.Data["RefShortName"] = util.Iif(refType == RepoRefCommit, base.ShortSha(ctx.Repo.RefName), ctx.Repo.RefName) + ctx.Data["RefType"] = RefTypeName(refType) ctx.Data["BranchNameSubURL"] = ctx.Repo.BranchNameSubURL() ctx.Data["TagName"] = ctx.Repo.TagName ctx.Data["CommitID"] = ctx.Repo.CommitID diff --git a/templates/repo/home_branch_dropdown.tmpl b/templates/repo/home_branch_dropdown.tmpl index 08255f482b..79fef3d09d 100644 --- a/templates/repo/home_branch_dropdown.tmpl +++ b/templates/repo/home_branch_dropdown.tmpl @@ -1,21 +1,9 @@ -{{- /* for repo home (default branch) and /owner/repo/src/branch/the-name */ -}} -{{- $branchDropdownCurrentRefType := "branch" -}} -{{- $branchDropdownCurrentRefShortName := .ctxData.BranchName -}} -{{- if .IsViewTag -}} - {{- /* for /owner/repo/src/tag/the-name */ -}} - {{- $branchDropdownCurrentRefType = "tag" -}} - {{- $branchDropdownCurrentRefShortName = .ctxData.TagName -}} -{{- else if .IsViewCommit -}} - {{- /* for /owner/repo/src/commit/000000 */ -}} - {{- $branchDropdownCurrentRefType = "commit" -}} - {{- $branchDropdownCurrentRefShortName = ShortSha .ctxData.CommitID -}} -{{- end -}} {{template "repo/branch_dropdown" dict "Repository" .ctxData.Repository "ShowTabBranches" true "ShowTabTags" true - "CurrentRefType" $branchDropdownCurrentRefType - "CurrentRefShortName" $branchDropdownCurrentRefShortName + "CurrentRefType" .ctxData.RefType + "CurrentRefShortName" .ctxData.RefShortName "CurrentTreePath" .ctxData.TreePath "RefLinkTemplate" "{RepoLink}/src/{RefType}/{RefShortName}/{TreePath}" "AllowCreateNewRef" .ctxData.CanCreateBranch diff --git a/templates/repo/view_file_tree_sidebar.tmpl b/templates/repo/view_file_tree_sidebar.tmpl index 0c9e1816d1..9e0aa74571 100644 --- a/templates/repo/view_file_tree_sidebar.tmpl +++ b/templates/repo/view_file_tree_sidebar.tmpl @@ -1,16 +1,3 @@ -{{- /* for repo home (default branch) and /owner/repo/src/branch/the-name */ -}} -{{- $branchDropdownCurrentRefType := "branch" -}} -{{- $branchDropdownCurrentRefShortName := .BranchName -}} -{{- if .IsViewTag -}} - {{- /* for /owner/repo/src/tag/the-name */ -}} - {{- $branchDropdownCurrentRefType = "tag" -}} - {{- $branchDropdownCurrentRefShortName = .TagName -}} -{{- else if .IsViewCommit -}} - {{- /* for /owner/repo/src/commit/000000 */ -}} - {{- $branchDropdownCurrentRefType = "commit" -}} - {{- $branchDropdownCurrentRefShortName = ShortSha .CommitID -}} -{{- end -}} -