Use repo as of renderctx's member rather than a repoPath on metas (#29222)

Use a `gitrepo.Repository` in the markup's RenderContext but not store
the repository's path.
This commit is contained in:
Lunny Xiao 2024-05-30 15:04:01 +08:00 committed by GitHub
parent d612a24e3e
commit 015efcd8bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 135 additions and 48 deletions

View File

@ -114,6 +114,7 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu
var err error var err error
if comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{ if comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
Ctx: ctx, Ctx: ctx,
Repo: issue.Repo,
Links: markup.Links{ Links: markup.Links{
Base: issue.Repo.Link(), Base: issue.Repo.Link(),
}, },

View File

@ -474,7 +474,6 @@ func (repo *Repository) ComposeMetas(ctx context.Context) map[string]string {
metas := map[string]string{ metas := map[string]string{
"user": repo.OwnerName, "user": repo.OwnerName,
"repo": repo.Name, "repo": repo.Name,
"repoPath": repo.RepoPath(),
"mode": "comment", "mode": "comment",
} }

8
modules/gitrepo/url.go Normal file
View File

@ -0,0 +1,8 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package gitrepo
func RepoGitURL(repo Repository) string {
return repoPath(repo)
}

View File

@ -16,7 +16,7 @@ import (
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/emoji" "code.gitea.io/gitea/modules/emoji"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup/common" "code.gitea.io/gitea/modules/markup/common"
"code.gitea.io/gitea/modules/references" "code.gitea.io/gitea/modules/references"
@ -1140,7 +1140,7 @@ func emojiProcessor(ctx *RenderContext, node *html.Node) {
// hashCurrentPatternProcessor renders SHA1 strings to corresponding links that // hashCurrentPatternProcessor renders SHA1 strings to corresponding links that
// are assumed to be in the same repository. // are assumed to be in the same repository.
func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) { func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) {
if ctx.Metas == nil || ctx.Metas["user"] == "" || ctx.Metas["repo"] == "" || ctx.Metas["repoPath"] == "" { if ctx.Metas == nil || ctx.Metas["user"] == "" || ctx.Metas["repo"] == "" || (ctx.Repo == nil && ctx.GitRepo == nil) {
return return
} }
@ -1172,13 +1172,14 @@ func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) {
if !inCache { if !inCache {
if ctx.GitRepo == nil { if ctx.GitRepo == nil {
var err error var err error
ctx.GitRepo, err = git.OpenRepository(ctx.Ctx, ctx.Metas["repoPath"]) var closer io.Closer
ctx.GitRepo, closer, err = gitrepo.RepositoryFromContextOrOpen(ctx.Ctx, ctx.Repo)
if err != nil { if err != nil {
log.Error("unable to open repository: %s Error: %v", ctx.Metas["repoPath"], err) log.Error("unable to open repository: %s Error: %v", gitrepo.RepoGitURL(ctx.Repo), err)
return return
} }
ctx.AddCancel(func() { ctx.AddCancel(func() {
ctx.GitRepo.Close() closer.Close()
ctx.GitRepo = nil ctx.GitRepo = nil
}) })
} }

View File

@ -4,16 +4,13 @@
package markup_test package markup_test
import ( import (
"context"
"io" "io"
"os"
"strings" "strings"
"testing" "testing"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/emoji" "code.gitea.io/gitea/modules/emoji"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -22,18 +19,33 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
var localMetas = map[string]string{ var (
"user": "gogits", testRepoOwnerName = "user13"
"repo": "gogs", testRepoName = "repo11"
"repoPath": "../../tests/gitea-repositories-meta/user13/repo11.git/", localMetas = map[string]string{
"user": testRepoOwnerName,
"repo": testRepoName,
}
)
type mockRepo struct {
OwnerName string
RepoName string
} }
func TestMain(m *testing.M) { func (m *mockRepo) GetOwnerName() string {
unittest.InitSettings() return m.OwnerName
if err := git.InitSimple(context.Background()); err != nil { }
log.Fatal("git init failed, err: %v", err)
func (m *mockRepo) GetName() string {
return m.RepoName
}
func newMockRepo(ownerName, repoName string) gitrepo.Repository {
return &mockRepo{
OwnerName: ownerName,
RepoName: repoName,
} }
os.Exit(m.Run())
} }
func TestRender_Commits(t *testing.T) { func TestRender_Commits(t *testing.T) {
@ -46,6 +58,7 @@ func TestRender_Commits(t *testing.T) {
AbsolutePrefix: true, AbsolutePrefix: true,
Base: markup.TestRepoURL, Base: markup.TestRepoURL,
}, },
Repo: newMockRepo(testRepoOwnerName, testRepoName),
Metas: localMetas, Metas: localMetas,
}, input) }, input)
assert.NoError(t, err) assert.NoError(t, err)
@ -53,7 +66,7 @@ func TestRender_Commits(t *testing.T) {
} }
sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d" sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
repo := markup.TestRepoURL repo := markup.TestAppURL + testRepoOwnerName + "/" + testRepoName + "/"
commit := util.URLJoin(repo, "commit", sha) commit := util.URLJoin(repo, "commit", sha)
tree := util.URLJoin(repo, "tree", sha, "src") tree := util.URLJoin(repo, "tree", sha, "src")

View File

@ -0,0 +1,14 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markup_test
import (
"testing"
"code.gitea.io/gitea/models/unittest"
)
func TestMain(m *testing.M) {
unittest.MainTest(m)
}

View File

@ -0,0 +1,21 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markdown
import (
"context"
"testing"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/markup"
)
func TestMain(m *testing.M) {
markup.Init(&markup.ProcessorHelper{
IsUsernameMentionable: func(ctx context.Context, username string) bool {
return username == "r-lyeh"
},
})
unittest.MainTest(m)
}

View File

@ -6,12 +6,11 @@ package markdown_test
import ( import (
"context" "context"
"html/template" "html/template"
"os"
"strings" "strings"
"testing" "testing"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/markup/markdown"
@ -26,27 +25,35 @@ import (
const ( const (
AppURL = "http://localhost:3000/" AppURL = "http://localhost:3000/"
FullURL = AppURL + "gogits/gogs/" testRepoOwnerName = "user13"
testRepoName = "repo11"
FullURL = AppURL + testRepoOwnerName + "/" + testRepoName + "/"
) )
// these values should match the const above // these values should match the const above
var localMetas = map[string]string{ var localMetas = map[string]string{
"user": "gogits", "user": testRepoOwnerName,
"repo": "gogs", "repo": testRepoName,
"repoPath": "../../../tests/gitea-repositories-meta/user13/repo11.git/",
} }
func TestMain(m *testing.M) { type mockRepo struct {
unittest.InitSettings() OwnerName string
if err := git.InitSimple(context.Background()); err != nil { RepoName string
log.Fatal("git init failed, err: %v", err) }
func (m *mockRepo) GetOwnerName() string {
return m.OwnerName
}
func (m *mockRepo) GetName() string {
return m.RepoName
}
func newMockRepo(ownerName, repoName string) gitrepo.Repository {
return &mockRepo{
OwnerName: ownerName,
RepoName: repoName,
} }
markup.Init(&markup.ProcessorHelper{
IsUsernameMentionable: func(ctx context.Context, username string) bool {
return username == "r-lyeh"
},
})
os.Exit(m.Run())
} }
func TestRender_StandardLinks(t *testing.T) { func TestRender_StandardLinks(t *testing.T) {
@ -133,11 +140,11 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
<li><a href="` + baseURLContent + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li> <li><a href="` + baseURLContent + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
<li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li> <li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li>
</ul> </ul>
<p>See commit <a href="/gogits/gogs/commit/65f1bf27bc" rel="nofollow"><code>65f1bf27bc</code></a></p> <p>See commit <a href="/` + testRepoOwnerName + `/` + testRepoName + `/commit/65f1bf27bc" rel="nofollow"><code>65f1bf27bc</code></a></p>
<p>Ideas and codes</p> <p>Ideas and codes</p>
<ul> <ul>
<li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" class="ref-issue" rel="nofollow">ocornut/imgui#786</a></li> <li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" class="ref-issue" rel="nofollow">ocornut/imgui#786</a></li>
<li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/gogits/gogs/issues/786" class="ref-issue" rel="nofollow">#786</a></li> <li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="` + FullURL + `issues/786" class="ref-issue" rel="nofollow">#786</a></li>
<li>Node graph editors <a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">https://github.com/ocornut/imgui/issues/306</a></li> <li>Node graph editors <a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">https://github.com/ocornut/imgui/issues/306</a></li>
<li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li> <li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
<li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li> <li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
@ -222,7 +229,7 @@ See commit 65f1bf27bc
Ideas and codes Ideas and codes
- Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786 - Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786
- Bezier widget (by @r-lyeh) ` + AppURL + `gogits/gogs/issues/786 - Bezier widget (by @r-lyeh) ` + FullURL + `issues/786
- Node graph editors https://github.com/ocornut/imgui/issues/306 - Node graph editors https://github.com/ocornut/imgui/issues/306
- [[Memory Editor|memory_editor_example]] - [[Memory Editor|memory_editor_example]]
- [[Plot var helper|plot_var_example]]`, - [[Plot var helper|plot_var_example]]`,
@ -299,6 +306,7 @@ func TestTotal_RenderWiki(t *testing.T) {
Links: markup.Links{ Links: markup.Links{
Base: FullURL, Base: FullURL,
}, },
Repo: newMockRepo(testRepoOwnerName, testRepoName),
Metas: localMetas, Metas: localMetas,
IsWiki: true, IsWiki: true,
}, sameCases[i]) }, sameCases[i])
@ -344,6 +352,7 @@ func TestTotal_RenderString(t *testing.T) {
Base: FullURL, Base: FullURL,
BranchPath: "master", BranchPath: "master",
}, },
Repo: newMockRepo(testRepoOwnerName, testRepoName),
Metas: localMetas, Metas: localMetas,
}, sameCases[i]) }, sameCases[i])
assert.NoError(t, err) assert.NoError(t, err)

View File

@ -16,6 +16,7 @@ import (
"sync" "sync"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
@ -77,6 +78,7 @@ type RenderContext struct {
Metas map[string]string Metas map[string]string
DefaultLink string DefaultLink string
GitRepo *git.Repository GitRepo *git.Repository
Repo gitrepo.Repository
ShaExistCache map[string]bool ShaExistCache map[string]bool
cancelFn func() cancelFn func()
SidebarTocNode ast.Node SidebarTocNode ast.Node

View File

@ -9,6 +9,7 @@ import (
"net/http" "net/http"
"strings" "strings"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -66,7 +67,9 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPr
} }
meta := map[string]string{} meta := map[string]string{}
var repoCtx *repo_model.Repository
if repo != nil && repo.Repository != nil { if repo != nil && repo.Repository != nil {
repoCtx = repo.Repository
if mode == "comment" { if mode == "comment" {
meta = repo.Repository.ComposeMetas(ctx) meta = repo.Repository.ComposeMetas(ctx)
} else { } else {
@ -79,6 +82,7 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPr
if err := markup.Render(&markup.RenderContext{ if err := markup.Render(&markup.RenderContext{
Ctx: ctx, Ctx: ctx,
Repo: repoCtx,
Links: markup.Links{ Links: markup.Links{
AbsolutePrefix: true, AbsolutePrefix: true,
Base: urlPrefix, Base: urlPrefix,

View File

@ -298,6 +298,7 @@ func releasesToFeedItems(ctx *context.Context, releases []*repo_model.Release) (
link := &feeds.Link{Href: rel.HTMLURL()} link := &feeds.Link{Href: rel.HTMLURL()}
content, err = markdown.RenderString(&markup.RenderContext{ content, err = markdown.RenderString(&markup.RenderContext{
Ctx: ctx, Ctx: ctx,
Repo: rel.Repo,
Links: markup.Links{ Links: markup.Links{
Base: rel.Repo.Link(), Base: rel.Repo.Link(),
}, },

View File

@ -382,6 +382,7 @@ func Diff(ctx *context.Context) {
}, },
Metas: ctx.Repo.Repository.ComposeMetas(ctx), Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo, GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx, Ctx: ctx,
}, template.HTMLEscapeString(string(charset.ToUTF8WithFallback(note.Message, charset.ConvertOpts{})))) }, template.HTMLEscapeString(string(charset.ToUTF8WithFallback(note.Message, charset.ConvertOpts{}))))
if err != nil { if err != nil {

View File

@ -1466,6 +1466,7 @@ func ViewIssue(ctx *context.Context) {
}, },
Metas: ctx.Repo.Repository.ComposeMetas(ctx), Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo, GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx, Ctx: ctx,
}, issue.Content) }, issue.Content)
if err != nil { if err != nil {
@ -1622,6 +1623,7 @@ func ViewIssue(ctx *context.Context) {
}, },
Metas: ctx.Repo.Repository.ComposeMetas(ctx), Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo, GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx, Ctx: ctx,
}, comment.Content) }, comment.Content)
if err != nil { if err != nil {
@ -1699,6 +1701,7 @@ func ViewIssue(ctx *context.Context) {
}, },
Metas: ctx.Repo.Repository.ComposeMetas(ctx), Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo, GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx, Ctx: ctx,
}, comment.Content) }, comment.Content)
if err != nil { if err != nil {
@ -2276,6 +2279,7 @@ func UpdateIssueContent(ctx *context.Context) {
}, },
Metas: ctx.Repo.Repository.ComposeMetas(ctx), Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo, GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx, Ctx: ctx,
}, issue.Content) }, issue.Content)
if err != nil { if err != nil {
@ -3196,6 +3200,7 @@ func UpdateCommentContent(ctx *context.Context) {
}, },
Metas: ctx.Repo.Repository.ComposeMetas(ctx), Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo, GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx, Ctx: ctx,
}, comment.Content) }, comment.Content)
if err != nil { if err != nil {

View File

@ -86,6 +86,7 @@ func Milestones(ctx *context.Context) {
}, },
Metas: ctx.Repo.Repository.ComposeMetas(ctx), Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo, GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx, Ctx: ctx,
}, m.Content) }, m.Content)
if err != nil { if err != nil {
@ -282,6 +283,7 @@ func MilestoneIssuesAndPulls(ctx *context.Context) {
}, },
Metas: ctx.Repo.Repository.ComposeMetas(ctx), Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo, GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx, Ctx: ctx,
}, milestone.Content) }, milestone.Content)
if err != nil { if err != nil {

View File

@ -96,6 +96,7 @@ func Projects(ctx *context.Context) {
}, },
Metas: ctx.Repo.Repository.ComposeMetas(ctx), Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo, GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx, Ctx: ctx,
}, projects[i].Description) }, projects[i].Description)
if err != nil { if err != nil {
@ -357,6 +358,7 @@ func ViewProject(ctx *context.Context) {
}, },
Metas: ctx.Repo.Repository.ComposeMetas(ctx), Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo, GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx, Ctx: ctx,
}, project.Description) }, project.Description)
if err != nil { if err != nil {

View File

@ -119,6 +119,7 @@ func getReleaseInfos(ctx *context.Context, opts *repo_model.FindReleasesOptions)
}, },
Metas: ctx.Repo.Repository.ComposeMetas(ctx), Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo, GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx, Ctx: ctx,
}, r.Note) }, r.Note)
if err != nil { if err != nil {

View File

@ -262,6 +262,7 @@ func Milestones(ctx *context.Context) {
}, },
Metas: milestones[i].Repo.ComposeMetas(ctx), Metas: milestones[i].Repo.ComposeMetas(ctx),
Ctx: ctx, Ctx: ctx,
Repo: milestones[i].Repo,
}, milestones[i].Content) }, milestones[i].Content)
if err != nil { if err != nil {
ctx.ServerError("RenderString", err) ctx.ServerError("RenderString", err)

View File

@ -221,6 +221,7 @@ func composeIssueCommentMessages(ctx *mailCommentContext, lang string, recipient
// This is the body of the new issue or comment, not the mail body // This is the body of the new issue or comment, not the mail body
body, err := markdown.RenderString(&markup.RenderContext{ body, err := markdown.RenderString(&markup.RenderContext{
Ctx: ctx, Ctx: ctx,
Repo: ctx.Issue.Repo,
Links: markup.Links{ Links: markup.Links{
AbsolutePrefix: true, AbsolutePrefix: true,
Base: ctx.Issue.Repo.HTMLURL(), Base: ctx.Issue.Repo.HTMLURL(),

View File

@ -58,6 +58,7 @@ func mailNewRelease(ctx context.Context, lang string, tos []string, rel *repo_mo
var err error var err error
rel.RenderedNote, err = markdown.RenderString(&markup.RenderContext{ rel.RenderedNote, err = markdown.RenderString(&markup.RenderContext{
Ctx: ctx, Ctx: ctx,
Repo: rel.Repo,
Links: markup.Links{ Links: markup.Links{
Base: rel.Repo.HTMLURL(), Base: rel.Repo.HTMLURL(),
}, },