Skip to content

Commit 39baa3d

Browse files
committed
temp
1 parent c4e27cb commit 39baa3d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+791
-1150
lines changed

models/activities/action.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (a *Action) LoadActUser(ctx context.Context) {
200200
}
201201
}
202202

203-
func (a *Action) loadRepo(ctx context.Context) {
203+
func (a *Action) LoadRepo(ctx context.Context) {
204204
if a.Repo != nil {
205205
return
206206
}
@@ -250,7 +250,7 @@ func (a *Action) GetActDisplayNameTitle(ctx context.Context) string {
250250

251251
// GetRepoUserName returns the name of the action repository owner.
252252
func (a *Action) GetRepoUserName(ctx context.Context) string {
253-
a.loadRepo(ctx)
253+
a.LoadRepo(ctx)
254254
if a.Repo == nil {
255255
return "(non-existing-repo)"
256256
}
@@ -265,7 +265,7 @@ func (a *Action) ShortRepoUserName(ctx context.Context) string {
265265

266266
// GetRepoName returns the name of the action repository.
267267
func (a *Action) GetRepoName(ctx context.Context) string {
268-
a.loadRepo(ctx)
268+
a.LoadRepo(ctx)
269269
if a.Repo == nil {
270270
return "(non-existing-repo)"
271271
}
@@ -644,7 +644,7 @@ func NotifyWatchers(ctx context.Context, actions ...*Action) error {
644644
}
645645

646646
if repoChanged {
647-
act.loadRepo(ctx)
647+
act.LoadRepo(ctx)
648648
repo = act.Repo
649649

650650
// check repo owner exist.

models/issues/comment_code.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"context"
88

99
"code.gitea.io/gitea/models/db"
10+
"code.gitea.io/gitea/models/renderhelper"
1011
user_model "code.gitea.io/gitea/models/user"
11-
"code.gitea.io/gitea/modules/markup"
1212
"code.gitea.io/gitea/modules/markup/markdown"
1313

1414
"xorm.io/builder"
@@ -112,12 +112,8 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu
112112
}
113113

114114
var err error
115-
rctx := markup.NewRenderContext(ctx).
116-
WithRepoFacade(issue.Repo).
117-
WithLinks(markup.Links{Base: issue.Repo.Link()}).
118-
WithMetas(issue.Repo.ComposeMetas(ctx))
119-
if comment.RenderedContent, err = markdown.RenderString(rctx,
120-
comment.Content); err != nil {
115+
rctx := renderhelper.NewRenderContextRepoComment(ctx, issue.Repo)
116+
if comment.RenderedContent, err = markdown.RenderString(rctx, comment.Content); err != nil {
121117
return nil, err
122118
}
123119
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package renderhelper
5+
6+
import (
7+
"context"
8+
"io"
9+
10+
"code.gitea.io/gitea/modules/git"
11+
"code.gitea.io/gitea/modules/gitrepo"
12+
"code.gitea.io/gitea/modules/log"
13+
)
14+
15+
type commitChecker struct {
16+
ctx context.Context
17+
commitCache map[string]bool
18+
gitRepoFacade gitrepo.Repository
19+
20+
gitRepo *git.Repository
21+
gitRepoCloser io.Closer
22+
}
23+
24+
func newCommitChecker(ctx context.Context, gitRepo gitrepo.Repository) *commitChecker {
25+
return &commitChecker{ctx: ctx, commitCache: make(map[string]bool), gitRepoFacade: gitRepo}
26+
}
27+
28+
func (c *commitChecker) Close() error {
29+
if c.gitRepoCloser != nil {
30+
return c.gitRepoCloser.Close()
31+
}
32+
return nil
33+
}
34+
35+
func (c *commitChecker) IsCommitIDExisting(commitID string) bool {
36+
exist, inCache := c.commitCache[commitID]
37+
if inCache {
38+
return exist
39+
}
40+
41+
if c.gitRepo == nil {
42+
r, closer, err := gitrepo.RepositoryFromContextOrOpen(c.ctx, c.gitRepoFacade)
43+
if err != nil {
44+
log.Error("unable to open repository: %s Error: %v", gitrepo.RepoGitURL(c.gitRepoFacade), err)
45+
return false
46+
}
47+
c.gitRepo, c.gitRepoCloser = r, closer
48+
}
49+
50+
exist = c.gitRepo.IsReferenceExist(commitID) // Don't use IsObjectExist since it doesn't support short hashs with gogit edition.
51+
c.commitCache[commitID] = exist
52+
return exist
53+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package renderhelper
5+
6+
import (
7+
"context"
8+
"html/template"
9+
10+
repo_model "code.gitea.io/gitea/models/repo"
11+
"code.gitea.io/gitea/modules/markup"
12+
"code.gitea.io/gitea/modules/util"
13+
)
14+
15+
type RepoComment struct {
16+
ctx *markup.RenderContext
17+
opts RepoCommentOptions
18+
19+
commitChecker *commitChecker
20+
repoLink string
21+
}
22+
23+
func (r *RepoComment) CleanUp() {
24+
_ = r.commitChecker.Close()
25+
}
26+
27+
func (r *RepoComment) IsUsernameMentionable(username string) bool {
28+
return markup.DefaultRenderHelperFuncs.IsUsernameMentionable(r.ctx, username)
29+
}
30+
31+
func (r *RepoComment) IsCommitIDExisting(commitID string) bool {
32+
return r.commitChecker.IsCommitIDExisting(commitID)
33+
}
34+
35+
func (r *RepoComment) RenderRepoFileCodePreview(options markup.RenderCodePreviewOptions) (template.HTML, error) {
36+
return markup.DefaultRenderHelperFuncs.RenderRepoFileCodePreview(r.ctx, options)
37+
}
38+
39+
func (r *RepoComment) ResolveLink(link string, likeType markup.LinkType) (finalLink string) {
40+
switch likeType {
41+
case markup.LinkTypeApp:
42+
finalLink = r.ctx.ResolveLinkApp(link)
43+
default:
44+
finalLink = r.ctx.ResolveLinkRelative(r.repoLink, r.opts.CurrentRefPath, link)
45+
}
46+
return finalLink
47+
}
48+
49+
var _ markup.RenderHelper = (*RepoComment)(nil)
50+
51+
type RepoCommentOptions struct {
52+
CurrentRefPath string // eg: "branch/main" or "commit/11223344"
53+
}
54+
55+
func NewRenderContextRepoComment(ctx context.Context, repo *repo_model.Repository, opts ...RepoCommentOptions) *markup.RenderContext {
56+
helper := &RepoComment{
57+
repoLink: repo.Link(),
58+
opts: util.OptionalArg(opts),
59+
commitChecker: newCommitChecker(ctx, repo),
60+
}
61+
rctx := markup.NewRenderContext(ctx).WithHelper(helper).WithMetas(repo.ComposeMetas(ctx))
62+
helper.ctx = rctx
63+
return rctx
64+
}

models/renderhelper/repo_file.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package renderhelper
5+
6+
import (
7+
"context"
8+
"html/template"
9+
"path"
10+
11+
repo_model "code.gitea.io/gitea/models/repo"
12+
"code.gitea.io/gitea/modules/markup"
13+
"code.gitea.io/gitea/modules/util"
14+
)
15+
16+
type RepoFile struct {
17+
ctx *markup.RenderContext
18+
opts RepoFileOptions
19+
repo *repo_model.Repository
20+
21+
commitChecker *commitChecker
22+
repoLink string
23+
}
24+
25+
func (r *RepoFile) CleanUp() {
26+
_ = r.commitChecker.Close()
27+
}
28+
29+
func (r *RepoFile) IsUsernameMentionable(username string) bool {
30+
return markup.DefaultRenderHelperFuncs.IsUsernameMentionable(r.ctx, username)
31+
}
32+
33+
func (r *RepoFile) IsCommitIDExisting(commitID string) bool {
34+
return r.commitChecker.IsCommitIDExisting(commitID)
35+
}
36+
37+
func (r *RepoFile) RenderRepoFileCodePreview(options markup.RenderCodePreviewOptions) (template.HTML, error) {
38+
return "", nil
39+
}
40+
41+
func (r *RepoFile) ResolveLink(link string, likeType markup.LinkType) string {
42+
finalLink := link
43+
switch likeType {
44+
case markup.LinkTypeApp:
45+
finalLink = r.ctx.ResolveLinkApp(link)
46+
case markup.LinkTypeDefault:
47+
finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "src", r.opts.CurrentRefPath), r.opts.CurrentTreePath, link)
48+
case markup.LinkTypeRaw:
49+
finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "raw", r.opts.CurrentRefPath), r.opts.CurrentTreePath, link)
50+
case markup.LinkTypeMedia:
51+
finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "media", r.opts.CurrentRefPath), r.opts.CurrentTreePath, link)
52+
}
53+
return finalLink
54+
}
55+
56+
var _ markup.RenderHelper = (*RepoFile)(nil)
57+
58+
type RepoFileOptions struct {
59+
CurrentRefPath string // eg: "branch/main"
60+
CurrentTreePath string // eg: "path/to/file" in the repo
61+
}
62+
63+
func NewRenderContextRepoFile(ctx context.Context, repo *repo_model.Repository, opts ...RepoFileOptions) *markup.RenderContext {
64+
helper := &RepoFile{
65+
repo: repo,
66+
repoLink: repo.Link(),
67+
opts: util.OptionalArg(opts),
68+
commitChecker: newCommitChecker(ctx, repo),
69+
}
70+
rctx := markup.NewRenderContext(ctx).WithHelper(helper).WithMetas(markup.ComposeSimpleDocumentMetas())
71+
helper.ctx = rctx
72+
return rctx
73+
}

models/renderhelper/repo_wiki.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package renderhelper
5+
6+
import (
7+
"context"
8+
"html/template"
9+
"path"
10+
11+
repo_model "code.gitea.io/gitea/models/repo"
12+
"code.gitea.io/gitea/modules/markup"
13+
"code.gitea.io/gitea/modules/util"
14+
)
15+
16+
type RepoWiki struct {
17+
ctx *markup.RenderContext
18+
opts RepoWikiOptions
19+
repo *repo_model.Repository
20+
21+
commitChecker *commitChecker
22+
repoLink string
23+
}
24+
25+
func (r *RepoWiki) CleanUp() {
26+
_ = r.commitChecker.Close()
27+
}
28+
29+
func (r *RepoWiki) IsUsernameMentionable(username string) bool {
30+
return markup.DefaultRenderHelperFuncs.IsUsernameMentionable(r.ctx, username)
31+
}
32+
33+
func (r *RepoWiki) IsCommitIDExisting(commitID string) bool {
34+
return r.commitChecker.IsCommitIDExisting(commitID)
35+
}
36+
37+
func (r *RepoWiki) RenderRepoFileCodePreview(options markup.RenderCodePreviewOptions) (template.HTML, error) {
38+
return "", nil
39+
}
40+
41+
func (r *RepoWiki) ResolveLink(link string, likeType markup.LinkType) string {
42+
finalLink := link
43+
switch likeType {
44+
case markup.LinkTypeApp:
45+
finalLink = r.ctx.ResolveLinkApp(link)
46+
case markup.LinkTypeDefault:
47+
finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "wiki", r.opts.currentRefPath), r.opts.currentTreePath, link)
48+
case markup.LinkTypeMedia:
49+
finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "raw", r.opts.currentRefPath), r.opts.currentTreePath, link)
50+
// wiki doesn't use src or raw
51+
}
52+
return finalLink
53+
}
54+
55+
var _ markup.RenderHelper = (*RepoWiki)(nil)
56+
57+
type RepoWikiOptions struct {
58+
// these options are not used at the moment because Wiki doesn't support sub-path, nor branch
59+
currentRefPath string // eg: "branch/main"
60+
currentTreePath string // eg: "path/to/file" in the repo
61+
}
62+
63+
func NewRenderContextRepoWiki(ctx context.Context, repo *repo_model.Repository, opts ...RepoWikiOptions) *markup.RenderContext {
64+
helper := &RepoWiki{
65+
repo: repo,
66+
repoLink: repo.Link(),
67+
opts: util.OptionalArg(opts),
68+
commitChecker: newCommitChecker(ctx, repo),
69+
}
70+
rctx := markup.NewRenderContext(ctx).WithHelper(helper).WithMetas(repo.ComposeWikiMetas(ctx))
71+
helper.ctx = rctx
72+
return rctx
73+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package renderhelper
5+
6+
import (
7+
"context"
8+
9+
"code.gitea.io/gitea/modules/markup"
10+
)
11+
12+
type SimpleDocument struct {
13+
*markup.SimpleRenderHelper
14+
ctx *markup.RenderContext
15+
baseLink string
16+
}
17+
18+
func (r *SimpleDocument) ResolveLink(link string, likeType markup.LinkType) string {
19+
return r.ctx.ResolveLinkRelative(r.baseLink, "", link)
20+
}
21+
22+
var _ markup.RenderHelper = (*SimpleDocument)(nil)
23+
24+
func NewRenderContextSimpleDocument(ctx context.Context, baseLink string) *markup.RenderContext {
25+
helper := &SimpleDocument{baseLink: baseLink}
26+
rctx := markup.NewRenderContext(ctx).WithHelper(helper).WithMetas(markup.ComposeSimpleDocumentMetas())
27+
helper.ctx = rctx
28+
return rctx
29+
}

modules/markup/csv/csv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (r Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.W
133133
// Check if maxRows or maxSize is reached, and if true, warn.
134134
if (row >= maxRows && maxRows != 0) || (rd.InputOffset() >= maxSize && maxSize != 0) {
135135
warn := `<table class="data-table"><tr><td>`
136-
rawLink := ` <a href="` + ctx.RenderOptions.Links.RawLink() + `/` + util.PathEscapeSegments(ctx.RenderOptions.RelativePath) + `">`
136+
rawLink := ` <a href="` + ctx.RenderHelper.ResolveLink(util.PathEscapeSegments(ctx.RenderOptions.RelativePath), markup.LinkTypeRaw) + `">`
137137

138138
// Try to get the user translation
139139
if locale, ok := ctx.Value(translation.ContextKey).(translation.Locale); ok {

modules/markup/external/external.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func envMark(envName string) string {
7979
func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
8080
var (
8181
command = strings.NewReplacer(
82-
envMark("GITEA_PREFIX_SRC"), ctx.RenderOptions.Links.SrcLink(),
83-
envMark("GITEA_PREFIX_RAW"), ctx.RenderOptions.Links.RawLink(),
82+
envMark("GITEA_PREFIX_SRC"), ctx.RenderHelper.ResolveLink("", markup.LinkTypeDefault),
83+
envMark("GITEA_PREFIX_RAW"), ctx.RenderHelper.ResolveLink("", markup.LinkTypeRaw),
8484
).Replace(p.Command)
8585
commands = strings.Fields(command)
8686
args = commands[1:]
@@ -112,14 +112,14 @@ func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.
112112
args = append(args, f.Name())
113113
}
114114

115-
processCtx, _, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("Render [%s] for %s", commands[0], ctx.RenderOptions.Links.SrcLink()))
115+
processCtx, _, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("Render [%s] for %s", commands[0], ctx.RenderHelper.ResolveLink("", markup.LinkTypeDefault)))
116116
defer finished()
117117

118118
cmd := exec.CommandContext(processCtx, commands[0], args...)
119119
cmd.Env = append(
120120
os.Environ(),
121-
"GITEA_PREFIX_SRC="+ctx.RenderOptions.Links.SrcLink(),
122-
"GITEA_PREFIX_RAW="+ctx.RenderOptions.Links.RawLink(),
121+
"GITEA_PREFIX_SRC="+ctx.RenderHelper.ResolveLink("", markup.LinkTypeDefault),
122+
"GITEA_PREFIX_RAW="+ctx.RenderHelper.ResolveLink("", markup.LinkTypeRaw),
123123
)
124124
if !p.IsInputFile {
125125
cmd.Stdin = input

0 commit comments

Comments
 (0)