Skip to content

Commit 015efcd

Browse files
authored
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.
1 parent d612a24 commit 015efcd

File tree

19 files changed

+135
-48
lines changed

19 files changed

+135
-48
lines changed

models/issues/comment_code.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu
113113

114114
var err error
115115
if comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
116-
Ctx: ctx,
116+
Ctx: ctx,
117+
Repo: issue.Repo,
117118
Links: markup.Links{
118119
Base: issue.Repo.Link(),
119120
},

models/repo/repo.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,9 @@ func (repo *Repository) MustOwner(ctx context.Context) *user_model.User {
472472
func (repo *Repository) ComposeMetas(ctx context.Context) map[string]string {
473473
if len(repo.RenderingMetas) == 0 {
474474
metas := map[string]string{
475-
"user": repo.OwnerName,
476-
"repo": repo.Name,
477-
"repoPath": repo.RepoPath(),
478-
"mode": "comment",
475+
"user": repo.OwnerName,
476+
"repo": repo.Name,
477+
"mode": "comment",
479478
}
480479

481480
unit, err := repo.GetUnit(ctx, unit.TypeExternalTracker)

modules/gitrepo/url.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package gitrepo
5+
6+
func RepoGitURL(repo Repository) string {
7+
return repoPath(repo)
8+
}

modules/markup/html.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
"code.gitea.io/gitea/modules/base"
1818
"code.gitea.io/gitea/modules/emoji"
19-
"code.gitea.io/gitea/modules/git"
19+
"code.gitea.io/gitea/modules/gitrepo"
2020
"code.gitea.io/gitea/modules/log"
2121
"code.gitea.io/gitea/modules/markup/common"
2222
"code.gitea.io/gitea/modules/references"
@@ -1140,7 +1140,7 @@ func emojiProcessor(ctx *RenderContext, node *html.Node) {
11401140
// hashCurrentPatternProcessor renders SHA1 strings to corresponding links that
11411141
// are assumed to be in the same repository.
11421142
func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) {
1143-
if ctx.Metas == nil || ctx.Metas["user"] == "" || ctx.Metas["repo"] == "" || ctx.Metas["repoPath"] == "" {
1143+
if ctx.Metas == nil || ctx.Metas["user"] == "" || ctx.Metas["repo"] == "" || (ctx.Repo == nil && ctx.GitRepo == nil) {
11441144
return
11451145
}
11461146

@@ -1172,13 +1172,14 @@ func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) {
11721172
if !inCache {
11731173
if ctx.GitRepo == nil {
11741174
var err error
1175-
ctx.GitRepo, err = git.OpenRepository(ctx.Ctx, ctx.Metas["repoPath"])
1175+
var closer io.Closer
1176+
ctx.GitRepo, closer, err = gitrepo.RepositoryFromContextOrOpen(ctx.Ctx, ctx.Repo)
11761177
if err != nil {
1177-
log.Error("unable to open repository: %s Error: %v", ctx.Metas["repoPath"], err)
1178+
log.Error("unable to open repository: %s Error: %v", gitrepo.RepoGitURL(ctx.Repo), err)
11781179
return
11791180
}
11801181
ctx.AddCancel(func() {
1181-
ctx.GitRepo.Close()
1182+
closer.Close()
11821183
ctx.GitRepo = nil
11831184
})
11841185
}

modules/markup/html_test.go

+27-14
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
package markup_test
55

66
import (
7-
"context"
87
"io"
9-
"os"
108
"strings"
119
"testing"
1210

13-
"code.gitea.io/gitea/models/unittest"
1411
"code.gitea.io/gitea/modules/emoji"
1512
"code.gitea.io/gitea/modules/git"
16-
"code.gitea.io/gitea/modules/log"
13+
"code.gitea.io/gitea/modules/gitrepo"
1714
"code.gitea.io/gitea/modules/markup"
1815
"code.gitea.io/gitea/modules/markup/markdown"
1916
"code.gitea.io/gitea/modules/setting"
@@ -22,18 +19,33 @@ import (
2219
"github.com/stretchr/testify/assert"
2320
)
2421

25-
var localMetas = map[string]string{
26-
"user": "gogits",
27-
"repo": "gogs",
28-
"repoPath": "../../tests/gitea-repositories-meta/user13/repo11.git/",
22+
var (
23+
testRepoOwnerName = "user13"
24+
testRepoName = "repo11"
25+
localMetas = map[string]string{
26+
"user": testRepoOwnerName,
27+
"repo": testRepoName,
28+
}
29+
)
30+
31+
type mockRepo struct {
32+
OwnerName string
33+
RepoName string
34+
}
35+
36+
func (m *mockRepo) GetOwnerName() string {
37+
return m.OwnerName
38+
}
39+
40+
func (m *mockRepo) GetName() string {
41+
return m.RepoName
2942
}
3043

31-
func TestMain(m *testing.M) {
32-
unittest.InitSettings()
33-
if err := git.InitSimple(context.Background()); err != nil {
34-
log.Fatal("git init failed, err: %v", err)
44+
func newMockRepo(ownerName, repoName string) gitrepo.Repository {
45+
return &mockRepo{
46+
OwnerName: ownerName,
47+
RepoName: repoName,
3548
}
36-
os.Exit(m.Run())
3749
}
3850

3951
func TestRender_Commits(t *testing.T) {
@@ -46,14 +58,15 @@ func TestRender_Commits(t *testing.T) {
4658
AbsolutePrefix: true,
4759
Base: markup.TestRepoURL,
4860
},
61+
Repo: newMockRepo(testRepoOwnerName, testRepoName),
4962
Metas: localMetas,
5063
}, input)
5164
assert.NoError(t, err)
5265
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
5366
}
5467

5568
sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
56-
repo := markup.TestRepoURL
69+
repo := markup.TestAppURL + testRepoOwnerName + "/" + testRepoName + "/"
5770
commit := util.URLJoin(repo, "commit", sha)
5871
tree := util.URLJoin(repo, "tree", sha, "src")
5972

modules/markup/main_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package markup_test
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/unittest"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
unittest.MainTest(m)
14+
}

modules/markup/markdown/main_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package markdown
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"code.gitea.io/gitea/models/unittest"
11+
"code.gitea.io/gitea/modules/markup"
12+
)
13+
14+
func TestMain(m *testing.M) {
15+
markup.Init(&markup.ProcessorHelper{
16+
IsUsernameMentionable: func(ctx context.Context, username string) bool {
17+
return username == "r-lyeh"
18+
},
19+
})
20+
unittest.MainTest(m)
21+
}

modules/markup/markdown/markdown_test.go

+29-20
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ package markdown_test
66
import (
77
"context"
88
"html/template"
9-
"os"
109
"strings"
1110
"testing"
1211

13-
"code.gitea.io/gitea/models/unittest"
1412
"code.gitea.io/gitea/modules/git"
13+
"code.gitea.io/gitea/modules/gitrepo"
1514
"code.gitea.io/gitea/modules/log"
1615
"code.gitea.io/gitea/modules/markup"
1716
"code.gitea.io/gitea/modules/markup/markdown"
@@ -25,28 +24,36 @@ import (
2524
)
2625

2726
const (
28-
AppURL = "http://localhost:3000/"
29-
FullURL = AppURL + "gogits/gogs/"
27+
AppURL = "http://localhost:3000/"
28+
testRepoOwnerName = "user13"
29+
testRepoName = "repo11"
30+
FullURL = AppURL + testRepoOwnerName + "/" + testRepoName + "/"
3031
)
3132

3233
// these values should match the const above
3334
var localMetas = map[string]string{
34-
"user": "gogits",
35-
"repo": "gogs",
36-
"repoPath": "../../../tests/gitea-repositories-meta/user13/repo11.git/",
35+
"user": testRepoOwnerName,
36+
"repo": testRepoName,
3737
}
3838

39-
func TestMain(m *testing.M) {
40-
unittest.InitSettings()
41-
if err := git.InitSimple(context.Background()); err != nil {
42-
log.Fatal("git init failed, err: %v", err)
39+
type mockRepo struct {
40+
OwnerName string
41+
RepoName string
42+
}
43+
44+
func (m *mockRepo) GetOwnerName() string {
45+
return m.OwnerName
46+
}
47+
48+
func (m *mockRepo) GetName() string {
49+
return m.RepoName
50+
}
51+
52+
func newMockRepo(ownerName, repoName string) gitrepo.Repository {
53+
return &mockRepo{
54+
OwnerName: ownerName,
55+
RepoName: repoName,
4356
}
44-
markup.Init(&markup.ProcessorHelper{
45-
IsUsernameMentionable: func(ctx context.Context, username string) bool {
46-
return username == "r-lyeh"
47-
},
48-
})
49-
os.Exit(m.Run())
5057
}
5158

5259
func TestRender_StandardLinks(t *testing.T) {
@@ -133,11 +140,11 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
133140
<li><a href="` + baseURLContent + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
134141
<li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li>
135142
</ul>
136-
<p>See commit <a href="/gogits/gogs/commit/65f1bf27bc" rel="nofollow"><code>65f1bf27bc</code></a></p>
143+
<p>See commit <a href="/` + testRepoOwnerName + `/` + testRepoName + `/commit/65f1bf27bc" rel="nofollow"><code>65f1bf27bc</code></a></p>
137144
<p>Ideas and codes</p>
138145
<ul>
139146
<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>
140-
<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>
147+
<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>
141148
<li>Node graph editors <a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">https://github.com/ocornut/imgui/issues/306</a></li>
142149
<li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
143150
<li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
@@ -222,7 +229,7 @@ See commit 65f1bf27bc
222229
Ideas and codes
223230
224231
- Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786
225-
- Bezier widget (by @r-lyeh) ` + AppURL + `gogits/gogs/issues/786
232+
- Bezier widget (by @r-lyeh) ` + FullURL + `issues/786
226233
- Node graph editors https://github.com/ocornut/imgui/issues/306
227234
- [[Memory Editor|memory_editor_example]]
228235
- [[Plot var helper|plot_var_example]]`,
@@ -299,6 +306,7 @@ func TestTotal_RenderWiki(t *testing.T) {
299306
Links: markup.Links{
300307
Base: FullURL,
301308
},
309+
Repo: newMockRepo(testRepoOwnerName, testRepoName),
302310
Metas: localMetas,
303311
IsWiki: true,
304312
}, sameCases[i])
@@ -344,6 +352,7 @@ func TestTotal_RenderString(t *testing.T) {
344352
Base: FullURL,
345353
BranchPath: "master",
346354
},
355+
Repo: newMockRepo(testRepoOwnerName, testRepoName),
347356
Metas: localMetas,
348357
}, sameCases[i])
349358
assert.NoError(t, err)

modules/markup/renderer.go

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"sync"
1717

1818
"code.gitea.io/gitea/modules/git"
19+
"code.gitea.io/gitea/modules/gitrepo"
1920
"code.gitea.io/gitea/modules/setting"
2021
"code.gitea.io/gitea/modules/util"
2122

@@ -77,6 +78,7 @@ type RenderContext struct {
7778
Metas map[string]string
7879
DefaultLink string
7980
GitRepo *git.Repository
81+
Repo gitrepo.Repository
8082
ShaExistCache map[string]bool
8183
cancelFn func()
8284
SidebarTocNode ast.Node

routers/common/markup.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"strings"
1111

12+
repo_model "code.gitea.io/gitea/models/repo"
1213
"code.gitea.io/gitea/modules/markup"
1314
"code.gitea.io/gitea/modules/markup/markdown"
1415
"code.gitea.io/gitea/modules/setting"
@@ -66,7 +67,9 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPr
6667
}
6768

6869
meta := map[string]string{}
70+
var repoCtx *repo_model.Repository
6971
if repo != nil && repo.Repository != nil {
72+
repoCtx = repo.Repository
7073
if mode == "comment" {
7174
meta = repo.Repository.ComposeMetas(ctx)
7275
} else {
@@ -78,7 +81,8 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPr
7881
}
7982

8083
if err := markup.Render(&markup.RenderContext{
81-
Ctx: ctx,
84+
Ctx: ctx,
85+
Repo: repoCtx,
8286
Links: markup.Links{
8387
AbsolutePrefix: true,
8488
Base: urlPrefix,

routers/web/feed/convert.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ func releasesToFeedItems(ctx *context.Context, releases []*repo_model.Release) (
297297

298298
link := &feeds.Link{Href: rel.HTMLURL()}
299299
content, err = markdown.RenderString(&markup.RenderContext{
300-
Ctx: ctx,
300+
Ctx: ctx,
301+
Repo: rel.Repo,
301302
Links: markup.Links{
302303
Base: rel.Repo.Link(),
303304
},

routers/web/repo/commit.go

+1
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ func Diff(ctx *context.Context) {
382382
},
383383
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
384384
GitRepo: ctx.Repo.GitRepo,
385+
Repo: ctx.Repo.Repository,
385386
Ctx: ctx,
386387
}, template.HTMLEscapeString(string(charset.ToUTF8WithFallback(note.Message, charset.ConvertOpts{}))))
387388
if err != nil {

routers/web/repo/issue.go

+5
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,7 @@ func ViewIssue(ctx *context.Context) {
14661466
},
14671467
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
14681468
GitRepo: ctx.Repo.GitRepo,
1469+
Repo: ctx.Repo.Repository,
14691470
Ctx: ctx,
14701471
}, issue.Content)
14711472
if err != nil {
@@ -1622,6 +1623,7 @@ func ViewIssue(ctx *context.Context) {
16221623
},
16231624
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
16241625
GitRepo: ctx.Repo.GitRepo,
1626+
Repo: ctx.Repo.Repository,
16251627
Ctx: ctx,
16261628
}, comment.Content)
16271629
if err != nil {
@@ -1699,6 +1701,7 @@ func ViewIssue(ctx *context.Context) {
16991701
},
17001702
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
17011703
GitRepo: ctx.Repo.GitRepo,
1704+
Repo: ctx.Repo.Repository,
17021705
Ctx: ctx,
17031706
}, comment.Content)
17041707
if err != nil {
@@ -2276,6 +2279,7 @@ func UpdateIssueContent(ctx *context.Context) {
22762279
},
22772280
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
22782281
GitRepo: ctx.Repo.GitRepo,
2282+
Repo: ctx.Repo.Repository,
22792283
Ctx: ctx,
22802284
}, issue.Content)
22812285
if err != nil {
@@ -3196,6 +3200,7 @@ func UpdateCommentContent(ctx *context.Context) {
31963200
},
31973201
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
31983202
GitRepo: ctx.Repo.GitRepo,
3203+
Repo: ctx.Repo.Repository,
31993204
Ctx: ctx,
32003205
}, comment.Content)
32013206
if err != nil {

0 commit comments

Comments
 (0)