From bdd78dfdcb2d7ea0971c616769b917905ceec5bd Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 13 Jan 2025 17:11:57 -0800 Subject: [PATCH] Use the old infrustructure about getting refullname --- modules/git/ref.go | 13 ------------- routers/web/repo/tree.go | 11 +++++------ routers/web/web.go | 5 +++-- templates/repo/view_file_tree_sidebar.tmpl | 1 + web_src/js/features/repo-view-file-tree-sidebar.ts | 5 ++--- 5 files changed, 11 insertions(+), 24 deletions(-) diff --git a/modules/git/ref.go b/modules/git/ref.go index 7f3c2d465790b..f20a175e422a8 100644 --- a/modules/git/ref.go +++ b/modules/git/ref.go @@ -84,19 +84,6 @@ func RefNameFromCommit(shortName string) RefName { return RefName(shortName) } -func RefNameFromTypeAndShortName(tp RefType, shortName string) RefName { - switch tp { - case RefTypeBranch: - return RefNameFromBranch(shortName) - case RefTypeTag: - return RefNameFromTag(shortName) - case RefTypeCommit: - return RefNameFromCommit(shortName) - default: - return "" - } -} - func (ref RefName) String() string { return string(ref) } diff --git a/routers/web/repo/tree.go b/routers/web/repo/tree.go index 0a7767aacb84c..facf4bfa8f986 100644 --- a/routers/web/repo/tree.go +++ b/routers/web/repo/tree.go @@ -55,20 +55,19 @@ func isExcludedEntry(entry *git.TreeEntry) bool { } func Tree(ctx *context.Context) { - treePath := ctx.PathParam("*") recursive := ctx.FormBool("recursive") - refFullName := git.RefNameFromTypeAndShortName(git.RefType(ctx.FormTrim("ref_type")), ctx.FormTrim("ref_name")) - if refFullName == "" { - ctx.Error(http.StatusBadRequest, "RefNameFromTypeAndShortName", "ref_type or ref_name is invalid") + + if ctx.Repo.RefFullName == "" { + ctx.Error(http.StatusBadRequest, "RefFullName", "ref_name is invalid") return } var results []*files_service.TreeViewNode var err error if !recursive { - results, err = files_service.GetTreeList(ctx, ctx.Repo.Repository, treePath, refFullName, false) + results, err = files_service.GetTreeList(ctx, ctx.Repo.Repository, ctx.Repo.TreePath, ctx.Repo.RefFullName, false) } else { - results, err = files_service.GetTreeInformation(ctx, ctx.Repo.Repository, treePath, refFullName) + results, err = files_service.GetTreeInformation(ctx, ctx.Repo.Repository, ctx.Repo.TreePath, ctx.Repo.RefFullName) } if err != nil { ctx.ServerError("GetTreeInformation", err) diff --git a/routers/web/web.go b/routers/web/web.go index effeddb9d2f7d..0d291a0e586bc 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1163,8 +1163,9 @@ func registerRoutes(m *web.Router) { m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.TreeList) }) m.Group("/tree", func() { - m.Get("", repo.Tree) - m.Get("/*", repo.Tree) + m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.Tree) + m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.Tree) + m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.Tree) }) m.Get("/compare", repo.MustBeNotEmpty, repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff) m.Combo("/compare/*", repo.MustBeNotEmpty, repo.SetEditorconfigIfExists). diff --git a/templates/repo/view_file_tree_sidebar.tmpl b/templates/repo/view_file_tree_sidebar.tmpl index c2ee6336c393b..48b8edfaa5235 100644 --- a/templates/repo/view_file_tree_sidebar.tmpl +++ b/templates/repo/view_file_tree_sidebar.tmpl @@ -13,5 +13,6 @@ data-tree-path="{{$.TreePath}}" data-current-ref-type="{{.RefFullName.RefType}}" data-current-ref-short-name="{{.RefFullName.ShortName}}" + data-current-ref-type-name-sub-url="{{.RefTypeNameSubURL}}" > diff --git a/web_src/js/features/repo-view-file-tree-sidebar.ts b/web_src/js/features/repo-view-file-tree-sidebar.ts index 7fc97137134ce..e97741ad339dd 100644 --- a/web_src/js/features/repo-view-file-tree-sidebar.ts +++ b/web_src/js/features/repo-view-file-tree-sidebar.ts @@ -31,9 +31,8 @@ async function toggleSidebar(visibility, isSigned) { async function loadChildren(item, recursive?: boolean) { const fileTree = document.querySelector('#view-file-tree'); const apiBaseUrl = fileTree.getAttribute('data-api-base-url'); - const refType = fileTree.getAttribute('data-current-ref-type'); - const refName = fileTree.getAttribute('data-current-ref-short-name'); - const response = await GET(`${apiBaseUrl}/tree/${item ? item.path : ''}?ref_type=${refType}&ref_name=${refName}&recursive=${recursive ?? false}`); + const refTypeNameSubURL = fileTree.getAttribute('data-current-ref-type-name-sub-url'); + const response = await GET(`${apiBaseUrl}/tree/${refTypeNameSubURL}/${item ? item.path : ''}?recursive=${recursive ?? false}`); const json = await response.json(); if (json instanceof Array) { return json.map((i) => ({