From f02f4191738940a6358838ea5e0d0a552978a11f Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Sat, 28 Feb 2026 23:33:08 -0600 Subject: [PATCH] Fix README symlink resolution in subdirectories like .github (#36775) Fixes #36774. --------- Co-authored-by: wxiaoguang --- modules/git/commit.go | 44 ------------ routers/web/repo/view_readme.go | 2 +- routers/web/repo/view_readme_test.go | 70 +++++++++++++++++++ tests/integration/git_general_test.go | 16 ++--- .../git_helper_for_declarative_test.go | 54 ++++++++++++-- tests/integration/pull_merge_test.go | 4 +- tests/integration/ssh_key_test.go | 4 +- 7 files changed, 133 insertions(+), 61 deletions(-) create mode 100644 routers/web/repo/view_readme_test.go diff --git a/modules/git/commit.go b/modules/git/commit.go index b98d36d946..dfecfe6057 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -86,50 +86,6 @@ func (c *Commit) GetCommitByPath(relpath string) (*Commit, error) { return c.repo.getCommitByPathWithID(c.ID, relpath) } -// AddChanges marks local changes to be ready for commit. -func AddChanges(ctx context.Context, repoPath string, all bool, files ...string) error { - cmd := gitcmd.NewCommand().AddArguments("add") - if all { - cmd.AddArguments("--all") - } - cmd.AddDashesAndList(files...) - _, _, err := cmd.WithDir(repoPath).RunStdString(ctx) - return err -} - -// CommitChangesOptions the options when a commit created -type CommitChangesOptions struct { - Committer *Signature - Author *Signature - Message string -} - -// CommitChanges commits local changes with given committer, author and message. -// If author is nil, it will be the same as committer. -func CommitChanges(ctx context.Context, repoPath string, opts CommitChangesOptions) error { - cmd := gitcmd.NewCommand() - if opts.Committer != nil { - cmd.AddOptionValues("-c", "user.name="+opts.Committer.Name) - cmd.AddOptionValues("-c", "user.email="+opts.Committer.Email) - } - cmd.AddArguments("commit") - - if opts.Author == nil { - opts.Author = opts.Committer - } - if opts.Author != nil { - cmd.AddOptionFormat("--author='%s <%s>'", opts.Author.Name, opts.Author.Email) - } - cmd.AddOptionFormat("--message=%s", opts.Message) - - _, _, err := cmd.WithDir(repoPath).RunStdString(ctx) - // No stderr but exit status 1 means nothing to commit. - if gitcmd.IsErrorExitCode(err, 1) { - return nil - } - return err -} - // CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize func (c *Commit) CommitsByRange(page, pageSize int, not, since, until string) ([]*Commit, error) { return c.repo.commitsByRangeWithTime(c.ID, page, pageSize, not, since, until) diff --git a/routers/web/repo/view_readme.go b/routers/web/repo/view_readme.go index 830709422e..eba3ffc36f 100644 --- a/routers/web/repo/view_readme.go +++ b/routers/web/repo/view_readme.go @@ -102,7 +102,7 @@ func findReadmeFileInEntries(ctx *context.Context, parentDir string, entries []* return "", nil, err } - subfolder, readmeFile, err := findReadmeFileInEntries(ctx, parentDir, childEntries, false) + subfolder, readmeFile, err := findReadmeFileInEntries(ctx, path.Join(parentDir, subTreeEntry.Name()), childEntries, false) if err != nil && !git.IsErrNotExist(err) { return "", nil, err } diff --git a/routers/web/repo/view_readme_test.go b/routers/web/repo/view_readme_test.go new file mode 100644 index 0000000000..5f61b03fb8 --- /dev/null +++ b/routers/web/repo/view_readme_test.go @@ -0,0 +1,70 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package repo + +import ( + "fmt" + "path" + "testing" + + "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/git/gitcmd" + "code.gitea.io/gitea/services/contexttest" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestFindReadmeFileInEntriesWithSymlinkInSubfolder(t *testing.T) { + for _, subdir := range []string{".github", ".gitea", "docs"} { + t.Run(subdir, func(t *testing.T) { + repoPath := t.TempDir() + stdin := fmt.Sprintf(`commit refs/heads/master +author Test 1700000000 +0000 +committer Test 1700000000 +0000 +data <