Skip to content

Fix incorrect file links #34189

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

Merged
merged 1 commit into from
Apr 13, 2025
Merged
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: 3 additions & 3 deletions routers/web/repo/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
gotemplate "html/template"
"net/http"
"net/url"
"path"
"strconv"
"strings"

Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions routers/web/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion routers/web/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/view_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/view_readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion templates/repo/blame.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</div>
{{end}}
{{end}}
<div class="{{TabSizeClass .Editorconfig .FileName}} non-diff-file-content">
<div class="{{TabSizeClass .Editorconfig .FileTreePath}} non-diff-file-content">
<h4 class="file-header ui top attached header tw-flex tw-items-center tw-justify-between tw-flex-wrap">
<div class="file-header-left tw-flex tw-items-center tw-py-2 tw-pr-4">
{{template "repo/file_info" .}}
Expand Down
6 changes: 3 additions & 3 deletions templates/repo/commits_list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
<a class="btn interact-bg tw-p-2 view-single-diff" data-tooltip-content="{{ctx.Locale.Tr "repo.commits.view_file_diff"}}"
href="{{$commitRepoLink}}/commit/{{.ID.String}}?files={{$.FileName}}"
href="{{$commitRepoLink}}/commit/{{.ID.String}}?files={{$.FileTreePath}}"
>{{svg "octicon-file-diff"}}</a>
{{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}}
<a class="btn interact-bg tw-p-2 view-commit-path" data-tooltip-content="{{ctx.Locale.Tr "repo.commits.view_path"}}" href="{{$viewCommitLink}}">{{svg "octicon-file-code"}}</a>
{{end}}
</td>
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/home_sidebar_top.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
{{end}}

{{if .ReadmeExist}}
<a class="flex-text-block muted" href="{{.TreeLink}}/{{.FileName}}">
<a class="flex-text-block muted" href="{{.RepoLink}}/src/{{.RefTypeNameSubURL}}/{{PathEscapeSegments .FileTreePath}}">
{{svg "octicon-book"}} {{ctx.Locale.Tr "readme"}}
</a>
{{end}}
Expand Down
6 changes: 3 additions & 3 deletions templates/repo/view_file.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div {{if .ReadmeInList}}id="readme" {{end}}class="{{TabSizeClass .Editorconfig .FileName}} non-diff-file-content">
<div {{if .ReadmeInList}}id="readme" {{end}}class="{{TabSizeClass .Editorconfig .FileTreePath}} non-diff-file-content">
{{- if .FileError}}
<div class="ui error message">
<div class="text left tw-whitespace-pre">{{.FileError}}</div>
Expand Down Expand Up @@ -27,7 +27,7 @@
<div class="file-header-left tw-flex tw-items-center tw-py-2 tw-pr-4">
{{if .ReadmeInList}}
{{svg "octicon-book" 16 "tw-mr-2"}}
<strong><a class="muted" href="#readme">{{.FileName}}</a></strong>
<strong><a class="muted" href="#readme">{{.FileTreePath}}</a></strong>
{{else}}
{{template "repo/file_info" .}}
{{end}}
Expand Down Expand Up @@ -78,7 +78,7 @@
<button class="ui mini basic button escape-button tw-mr-1">{{ctx.Locale.Tr "repo.escape_control_characters"}}</button>
{{end}}
{{if and .ReadmeInList .CanEditReadmeFile}}
<a class="btn-octicon" data-tooltip-content="{{ctx.Locale.Tr "repo.editor.edit_this_file"}}" href="{{.RepoLink}}/_edit/{{PathEscapeSegments .BranchName}}/{{PathEscapeSegments .TreePath}}/{{PathEscapeSegments .FileName}}">{{svg "octicon-pencil"}}</a>
<a class="btn-octicon" data-tooltip-content="{{ctx.Locale.Tr "repo.editor.edit_this_file"}}" href="{{.RepoLink}}/_edit/{{PathEscapeSegments .BranchName}}/{{PathEscapeSegments .FileTreePath}}">{{svg "octicon-pencil"}}</a>
{{end}}
</div>
</h4>
Expand Down