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

Fixes a bug introduced in https://github.com/go-gitea/gitea/pull/22177
which allows finding READMEs like
docs/docs/docs/.gitea/.github/docs/README.md

Fixes https://github.com/go-gitea/gitea/issues/23694
This commit is contained in:
Nick 2023-04-10 23:00:19 -04:00 committed by GitHub
parent b7b887ba00
commit ef7fd781f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 5 deletions

View File

@ -58,7 +58,7 @@ const (
// entries == ctx.Repo.Commit.SubTree(ctx.Repo.TreePath).ListEntries() // entries == ctx.Repo.Commit.SubTree(ctx.Repo.TreePath).ListEntries()
// //
// FIXME: There has to be a more efficient way of doing this // 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, tryWellKnownDirs bool) (string, *git.TreeEntry, error) {
// Create a list of extensions in priority order // Create a list of extensions in priority order
// 1. Markdown files - with and without localisation - e.g. README.en-us.md or README.md // 1. Markdown files - with and without localisation - e.g. README.en-us.md or README.md
// 2. Txt files - e.g. README.txt // 2. Txt files - e.g. README.txt
@ -69,7 +69,7 @@ func findReadmeFileInEntries(ctx *context.Context, entries []*git.TreeEntry) (st
docsEntries := make([]*git.TreeEntry, 3) // (one of docs/, .gitea/ or .github/) docsEntries := make([]*git.TreeEntry, 3) // (one of docs/, .gitea/ or .github/)
for _, entry := range entries { for _, entry := range entries {
if entry.IsDir() { if tryWellKnownDirs && entry.IsDir() {
// as a special case for the top-level repo introduction README, // 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, ... // 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) // (note that docsEntries is ignored unless we are at the root)
@ -130,7 +130,7 @@ func findReadmeFileInEntries(ctx *context.Context, entries []*git.TreeEntry) (st
return "", nil, err return "", nil, err
} }
subfolder, readmeFile, err := findReadmeFileInEntries(ctx, childEntries) subfolder, readmeFile, err := findReadmeFileInEntries(ctx, childEntries, false)
if err != nil && !git.IsErrNotExist(err) { if err != nil && !git.IsErrNotExist(err) {
return "", nil, err return "", nil, err
} }
@ -164,7 +164,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
return return
} }
subfolder, readmeFile, err := findReadmeFileInEntries(ctx, entries) subfolder, readmeFile, err := findReadmeFileInEntries(ctx, entries, true)
if err != nil { if err != nil {
ctx.ServerError("findReadmeFileInEntries", err) ctx.ServerError("findReadmeFileInEntries", err)
return return

View File

@ -361,7 +361,7 @@ func TestViewRepoDirectoryReadme(t *testing.T) {
} }
missing("sp-ace", "/user2/readme-test/src/branch/sp-ace/") 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("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/") missing("symlink-loop", "/user2/readme-test/src/branch/symlink-loop/")
} }