Skip to content
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

Improve tree not found page #26570

Merged
merged 27 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9057b19
improve
yp05327 Aug 18, 2023
8126566
improve
yp05327 Aug 18, 2023
514f5f6
Update options/locale/locale_en-US.ini
yp05327 Aug 18, 2023
4f10f70
improve
yp05327 Aug 18, 2023
927b41a
Update modules/context/context_response.go
wxiaoguang Aug 18, 2023
1e6be71
improve
yp05327 Aug 18, 2023
99de298
improve
yp05327 Aug 21, 2023
56cfc57
improve
yp05327 Aug 22, 2023
b3a1ffd
Merge branch 'main' into improve-tree-not-found
yp05327 Sep 25, 2023
d532eab
support different ref type redirect
yp05327 Sep 25, 2023
ae103ab
improve
yp05327 Sep 25, 2023
8a12915
Merge branch 'main' into improve-tree-not-found
delvh Sep 27, 2023
842e41f
Update routers/web/repo/helper.go
yp05327 Sep 28, 2023
865742c
Update options/locale/locale_en-US.ini
yp05327 Sep 28, 2023
ea92ea2
Use the new `ctx.Locale.Tr` instead of `.locale.Tr`
delvh Sep 28, 2023
bb0fcf8
Merge branch 'main' into improve-tree-not-found
GiteaBot Sep 28, 2023
a04e9b8
Merge branch 'main' into improve-tree-not-found
GiteaBot Sep 28, 2023
9438a4c
Merge branch 'main' into improve-tree-not-found
GiteaBot Sep 28, 2023
01179ad
Merge branch 'main' into improve-tree-not-found
GiteaBot Sep 28, 2023
096262e
Merge branch 'main' into improve-tree-not-found
GiteaBot Sep 28, 2023
2491af3
Merge branch 'main' into improve-tree-not-found
GiteaBot Sep 28, 2023
ca53832
Merge branch 'main' into improve-tree-not-found
GiteaBot Sep 28, 2023
88a32c9
Merge branch 'main' into improve-tree-not-found
GiteaBot Sep 29, 2023
d3a9d96
Merge branch 'main' into improve-tree-not-found
GiteaBot Sep 29, 2023
871e47b
Merge branch 'main' into improve-tree-not-found
GiteaBot Sep 29, 2023
c682f0b
Fix lint
lunny Sep 29, 2023
36a8f2b
Merge branch 'main' into improve-tree-not-found
lunny Sep 29, 2023
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
5 changes: 5 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ step2 = Step 2:

error = Error
error404 = The page you are trying to reach either <strong>does not exist</strong> or <strong>you are not authorized</strong> to view it.
go_back = Go Back

never = Never
unknown = Unknown
Expand Down Expand Up @@ -1012,6 +1013,10 @@ blame.ignore_revs = Ignoring revisions in <a href="%s">.git-blame-ignore-revs</a
blame.ignore_revs.failed = Failed to ignore revisions in <a href="%s">.git-blame-ignore-revs</a>.
author_search_tooltip = Shows a maximum of 30 users

tree_path_not_found_commit = Path %[1]s doesn't exist in commit %[2]s
tree_path_not_found_branch = Path %[1]s doesn't exist in branch %[2]s
tree_path_not_found_tag = Path %[1]s doesn't exist in tag %[2]s

transfer.accept = Accept Transfer
transfer.accept_desc = Transfer to "%s"
transfer.reject = Reject Transfer
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func RefBlame(ctx *context.Context) {
// Get current entry user currently looking at.
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
if err != nil {
ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
return
}

Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
if !isNewFile {
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
if err != nil {
ctx.NotFoundOrServerError("GetTreeEntryByPath", git.IsErrNotExist, err)
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
return
}

Expand Down
22 changes: 22 additions & 0 deletions routers/web/repo/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
package repo

import (
"net/url"
"sort"

"code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
)

func MakeSelfOnTop(doer *user.User, users []*user.User) []*user.User {
Expand All @@ -20,3 +23,22 @@ func MakeSelfOnTop(doer *user.User, users []*user.User) []*user.User {
}
return users
}

func HandleGitError(ctx *context.Context, msg string, err error) {
if git.IsErrNotExist(err) {
refType := ""
switch {
case ctx.Repo.IsViewBranch:
refType = "branch"
case ctx.Repo.IsViewTag:
refType = "tag"
case ctx.Repo.IsViewCommit:
refType = "commit"
}
ctx.Data["NotFoundPrompt"] = ctx.Locale.Tr("repo.tree_path_not_found_" + refType, ctx.Repo.TreePath, url.PathEscape(ctx.Repo.RefName))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@delvh
There's a risk when refType is empty? I'm not sure what will happen when the key word not exists.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, in that case, the user will see the message repo.tree_path_not_found_ instead of Path <path> is not contained within <type> <name>.
In which case, we then know that we need to adapt this switch…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I don't even know how a user would trigger something like that.
What other ref types are there except branches, commits, and tags?

ctx.Data["NotFoundGoBackURL"] = ctx.Repo.RepoLink + "/src/" + refType + "/" + url.PathEscape(ctx.Repo.RefName)
ctx.NotFound(msg, err)
} else {
ctx.ServerError(msg, err)
}
}
10 changes: 5 additions & 5 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ func checkCitationFile(ctx *context.Context, entry *git.TreeEntry) {
}
tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
if err != nil {
ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err)
HandleGitError(ctx, "Repo.Commit.SubTree", err)
return
}
allEntries, err := tree.ListEntries()
Expand Down Expand Up @@ -783,7 +783,7 @@ func LastCommit(ctx *context.Context) {
func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entries {
tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
if err != nil {
ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err)
HandleGitError(ctx, "Repo.Commit.SubTree", err)
return nil
}

Expand All @@ -792,12 +792,12 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri
// Get current entry user currently looking at.
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
if err != nil {
ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
return nil
}

if !entry.IsDir() {
ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
return nil
}

Expand Down Expand Up @@ -963,7 +963,7 @@ func renderCode(ctx *context.Context) {
// Get current entry user currently looking at.
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
if err != nil {
ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
return
}

Expand Down
4 changes: 3 additions & 1 deletion templates/status/404.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
{{if .IsRepo}}{{template "repo/header" .}}{{end}}
<div class="ui container center">
<p style="margin-top: 100px"><img src="{{AssetUrlPrefix}}/img/404.png" alt="404"></p>
<p>{{if .NotFoundPrompt}}{{.NotFoundPrompt}}{{else}}{{.locale.Tr "error404" | Safe}}{{end}}</p>
{{if .NotFoundGoBackURL}}<a class="ui button green" href="{{.NotFoundGoBackURL}}">{{.locale.Tr "go_back"}}</a>{{end}}
delvh marked this conversation as resolved.
Show resolved Hide resolved

<div class="divider"></div>
<br>
<p>{{ctx.Locale.Tr "error404" | Safe}}</p>
{{if .ShowFooterVersion}}<p>{{ctx.Locale.Tr "admin.config.app_ver"}}: {{AppVer}}</p>{{end}}
</div>
</div>
Expand Down
Loading