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

Avoid recursing into sub-sub-sub-docs folders when looking for READMEs. #23695

Merged
merged 6 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const (
// entries == ctx.Repo.Commit.SubTree(ctx.Repo.TreePath).ListEntries()
//
// FIXME: There has to be a more efficient way of doing this
func findReadmeFileInEntries(ctx *context.Context, entries []*git.TreeEntry) (string, *git.TreeEntry, error) {
func findReadmeFileInEntries(ctx *context.Context, entries []*git.TreeEntry, recurse bool) (string, *git.TreeEntry, error) {
kousu marked this conversation as resolved.
Show resolved Hide resolved
// Create a list of extensions in priority order
// 1. Markdown files - with and without localisation - e.g. README.en-us.md or README.md
// 2. Txt files - e.g. README.txt
Expand All @@ -69,7 +69,7 @@ func findReadmeFileInEntries(ctx *context.Context, entries []*git.TreeEntry) (st

docsEntries := make([]*git.TreeEntry, 3) // (one of docs/, .gitea/ or .github/)
for _, entry := range entries {
if entry.IsDir() {
if recurse && entry.IsDir() {
// as a special case for the top-level repo introduction README,
// fall back to subfolders, looking for e.g. docs/README.md, .gitea/README.zh-CN.txt, .github/README.txt, ...
// (note that docsEntries is ignored unless we are at the root)
Expand Down Expand Up @@ -130,7 +130,7 @@ func findReadmeFileInEntries(ctx *context.Context, entries []*git.TreeEntry) (st
return "", nil, err
}

subfolder, readmeFile, err := findReadmeFileInEntries(ctx, childEntries)
subfolder, readmeFile, err := findReadmeFileInEntries(ctx, childEntries, false)
if err != nil && !git.IsErrNotExist(err) {
return "", nil, err
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
return
}

subfolder, readmeFile, err := findReadmeFileInEntries(ctx, entries)
subfolder, readmeFile, err := findReadmeFileInEntries(ctx, entries, true)
if err != nil {
ctx.ServerError("findReadmeFileInEntries", err)
return
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func TestViewRepoDirectoryReadme(t *testing.T) {
}
missing("sp-ace", "/user2/readme-test/src/branch/sp-ace/")
missing("nested-special", "/user2/readme-test/src/branch/special-subdir-nested/subproject") // the special subdirs should only trigger on the repo root
// missing("special-subdir-nested", "/user2/readme-test/src/branch/special-subdir-nested/") // This is currently FAILING, due to a bug introduced in https://github.com/go-gitea/gitea/pull/22177
missing("special-subdir-nested", "/user2/readme-test/src/branch/special-subdir-nested/")
missing("symlink-loop", "/user2/readme-test/src/branch/symlink-loop/")
}

Expand Down