From 34349c085ca9fa1a0ff731d132ebb47fd92b9d49 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 14 Apr 2025 03:27:31 +0800 Subject: [PATCH] Fix incorrect file links (#34189) Fix #34188 The name "FileName" is ambiguous: sometimes it is "base name without path", sometimes it is "full name with path". The ambiguous name causes various problems. This PR clarifies the usage: `FileTreePath`: the full name with path. --- routers/web/repo/blame.go | 6 +++--- routers/web/repo/commit.go | 9 ++++----- routers/web/repo/editor.go | 1 - routers/web/repo/view_file.go | 2 +- routers/web/repo/view_readme.go | 2 +- templates/repo/blame.tmpl | 2 +- templates/repo/commits_list.tmpl | 6 +++--- templates/repo/home_sidebar_top.tmpl | 2 +- templates/repo/view_file.tmpl | 6 +++--- 9 files changed, 17 insertions(+), 19 deletions(-) diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go index e125267524..e304633f95 100644 --- a/routers/web/repo/blame.go +++ b/routers/web/repo/blame.go @@ -8,6 +8,7 @@ import ( gotemplate "html/template" "net/http" "net/url" + "path" "strconv" "strings" @@ -69,7 +70,7 @@ func RefBlame(ctx *context.Context) { blob := entry.Blob() fileSize := blob.Size() ctx.Data["FileSize"] = fileSize - ctx.Data["FileName"] = blob.Name() + ctx.Data["FileTreePath"] = ctx.Repo.TreePath tplName := tplRepoViewContent if !ctx.FormBool("only_content") { @@ -285,8 +286,7 @@ func renderBlame(ctx *context.Context, blameParts []*git.BlamePart, commitNames if i != len(lines)-1 { line += "\n" } - fileName := fmt.Sprintf("%v", ctx.Data["FileName"]) - line, lexerNameForLine := highlight.Code(fileName, language, line) + line, lexerNameForLine := highlight.Code(path.Base(ctx.Repo.TreePath), language, line) // set lexer name to the first detected lexer. this is certainly suboptimal and // we should instead highlight the whole file at once diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 3fd1eacb58..3569a356d1 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -215,13 +215,12 @@ func SearchCommits(ctx *context.Context) { // FileHistory show a file's reversions func FileHistory(ctx *context.Context) { - fileName := ctx.Repo.TreePath - if len(fileName) == 0 { + if ctx.Repo.TreePath == "" { Commits(ctx) return } - commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ctx.Repo.RefFullName.ShortName(), fileName) // FIXME: legacy code used ShortName + commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(ctx.Repo.RefFullName.ShortName(), ctx.Repo.TreePath) if err != nil { ctx.ServerError("FileCommitsCount", err) return @@ -238,7 +237,7 @@ func FileHistory(ctx *context.Context) { commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange( git.CommitsByFileAndRangeOptions{ Revision: ctx.Repo.RefFullName.ShortName(), // FIXME: legacy code used ShortName - File: fileName, + File: ctx.Repo.TreePath, Page: page, }) if err != nil { @@ -253,7 +252,7 @@ func FileHistory(ctx *context.Context) { ctx.Data["Username"] = ctx.Repo.Owner.Name ctx.Data["Reponame"] = ctx.Repo.Repository.Name - ctx.Data["FileName"] = fileName + ctx.Data["FileTreePath"] = ctx.Repo.TreePath ctx.Data["CommitCount"] = commitsCount pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5) diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index c181aad050..c925b61151 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -160,7 +160,6 @@ func editFile(ctx *context.Context, isNewFile bool) { defer dataRc.Close() ctx.Data["FileSize"] = blob.Size() - ctx.Data["FileName"] = blob.Name() buf := make([]byte, 1024) n, _ := util.ReadAtMost(dataRc, buf) diff --git a/routers/web/repo/view_file.go b/routers/web/repo/view_file.go index ff0e1b4d54..3df6051975 100644 --- a/routers/web/repo/view_file.go +++ b/routers/web/repo/view_file.go @@ -52,7 +52,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) { ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefFullName.ShortName()) ctx.Data["FileIsSymlink"] = entry.IsLink() - ctx.Data["FileName"] = blob.Name() + ctx.Data["FileTreePath"] = ctx.Repo.TreePath ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath) if ctx.Repo.TreePath == ".editorconfig" { diff --git a/routers/web/repo/view_readme.go b/routers/web/repo/view_readme.go index 606ee7ff79..459cf0a616 100644 --- a/routers/web/repo/view_readme.go +++ b/routers/web/repo/view_readme.go @@ -162,7 +162,7 @@ func prepareToRenderReadmeFile(ctx *context.Context, subfolder string, readmeFil defer dataRc.Close() ctx.Data["FileIsText"] = fInfo.isTextFile - ctx.Data["FileName"] = path.Join(subfolder, readmeFile.Name()) + ctx.Data["FileTreePath"] = path.Join(subfolder, readmeFile.Name()) ctx.Data["FileSize"] = fInfo.fileSize ctx.Data["IsLFSFile"] = fInfo.isLFSFile diff --git a/templates/repo/blame.tmpl b/templates/repo/blame.tmpl index 05f79612bd..9596fe837a 100644 --- a/templates/repo/blame.tmpl +++ b/templates/repo/blame.tmpl @@ -10,7 +10,7 @@ {{end}} {{end}} -
+

{{template "repo/file_info" .}} diff --git a/templates/repo/commits_list.tmpl b/templates/repo/commits_list.tmpl index 50e6708dcf..17c7240ee4 100644 --- a/templates/repo/commits_list.tmpl +++ b/templates/repo/commits_list.tmpl @@ -70,15 +70,15 @@ {{/* at the moment, wiki doesn't support these "view" links like "view at history point" */}} {{if not $.PageIsWiki}} {{/* view single file diff */}} - {{if $.FileName}} + {{if $.FileTreePath}} {{svg "octicon-file-diff"}} {{end}} {{/* view at history point */}} {{$viewCommitLink := printf "%s/src/commit/%s" $commitRepoLink (PathEscape .ID.String)}} - {{if $.FileName}}{{$viewCommitLink = printf "%s/%s" $viewCommitLink (PathEscapeSegments $.FileName)}}{{end}} + {{if $.FileTreePath}}{{$viewCommitLink = printf "%s/%s" $viewCommitLink (PathEscapeSegments $.FileTreePath)}}{{end}} {{svg "octicon-file-code"}} {{end}} diff --git a/templates/repo/home_sidebar_top.tmpl b/templates/repo/home_sidebar_top.tmpl index 8d3d7c6f86..8c2089c839 100644 --- a/templates/repo/home_sidebar_top.tmpl +++ b/templates/repo/home_sidebar_top.tmpl @@ -45,7 +45,7 @@ {{end}} {{if .ReadmeExist}} - + {{svg "octicon-book"}} {{ctx.Locale.Tr "readme"}} {{end}} diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 51a8f7d501..f01adccadc 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -1,4 +1,4 @@ -
+
{{- if .FileError}}
{{.FileError}}
@@ -27,7 +27,7 @@
{{if .ReadmeInList}} {{svg "octicon-book" 16 "tw-mr-2"}} - {{.FileName}} + {{.FileTreePath}} {{else}} {{template "repo/file_info" .}} {{end}} @@ -78,7 +78,7 @@ {{end}} {{if and .ReadmeInList .CanEditReadmeFile}} - {{svg "octicon-pencil"}} + {{svg "octicon-pencil"}} {{end}}