Skip to content

Commit 19fc799

Browse files
authored
Merge branch 'main' into noprop
2 parents 8676402 + 3cd6494 commit 19fc799

Some content is hidden

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

41 files changed

+357
-329
lines changed

.eslintrc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ rules:
487487
no-jquery/no-visibility: [2]
488488
no-jquery/no-when: [2]
489489
no-jquery/no-wrap: [2]
490-
no-jquery/variable-pattern: [0]
490+
no-jquery/variable-pattern: [2]
491491
no-label-var: [2]
492492
no-labels: [0] # handled by no-restricted-syntax
493493
no-lone-blocks: [2]

modules/indexer/code/bleve/bleve.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (b *Indexer) addUpdate(ctx context.Context, batchWriter git.WriteCloserErro
142142
return err
143143
}
144144
if size, err = strconv.ParseInt(strings.TrimSpace(stdout), 10, 64); err != nil {
145-
return fmt.Errorf("Misformatted git cat-file output: %w", err)
145+
return fmt.Errorf("misformatted git cat-file output: %w", err)
146146
}
147147
}
148148

@@ -233,26 +233,26 @@ func (b *Indexer) Delete(_ context.Context, repoID int64) error {
233233

234234
// Search searches for files in the specified repo.
235235
// Returns the matching file-paths
236-
func (b *Indexer) Search(ctx context.Context, repoIDs []int64, language, keyword string, page, pageSize int, isFuzzy bool) (int64, []*internal.SearchResult, []*internal.SearchResultLanguages, error) {
236+
func (b *Indexer) Search(ctx context.Context, opts *internal.SearchOptions) (int64, []*internal.SearchResult, []*internal.SearchResultLanguages, error) {
237237
var (
238238
indexerQuery query.Query
239239
keywordQuery query.Query
240240
)
241241

242-
if isFuzzy {
243-
phraseQuery := bleve.NewMatchPhraseQuery(keyword)
242+
if opts.IsKeywordFuzzy {
243+
phraseQuery := bleve.NewMatchPhraseQuery(opts.Keyword)
244244
phraseQuery.FieldVal = "Content"
245245
phraseQuery.Analyzer = repoIndexerAnalyzer
246246
keywordQuery = phraseQuery
247247
} else {
248-
prefixQuery := bleve.NewPrefixQuery(keyword)
248+
prefixQuery := bleve.NewPrefixQuery(opts.Keyword)
249249
prefixQuery.FieldVal = "Content"
250250
keywordQuery = prefixQuery
251251
}
252252

253-
if len(repoIDs) > 0 {
254-
repoQueries := make([]query.Query, 0, len(repoIDs))
255-
for _, repoID := range repoIDs {
253+
if len(opts.RepoIDs) > 0 {
254+
repoQueries := make([]query.Query, 0, len(opts.RepoIDs))
255+
for _, repoID := range opts.RepoIDs {
256256
repoQueries = append(repoQueries, inner_bleve.NumericEqualityQuery(repoID, "RepoID"))
257257
}
258258

@@ -266,8 +266,8 @@ func (b *Indexer) Search(ctx context.Context, repoIDs []int64, language, keyword
266266

267267
// Save for reuse without language filter
268268
facetQuery := indexerQuery
269-
if len(language) > 0 {
270-
languageQuery := bleve.NewMatchQuery(language)
269+
if len(opts.Language) > 0 {
270+
languageQuery := bleve.NewMatchQuery(opts.Language)
271271
languageQuery.FieldVal = "Language"
272272
languageQuery.Analyzer = analyzer_keyword.Name
273273

@@ -277,12 +277,12 @@ func (b *Indexer) Search(ctx context.Context, repoIDs []int64, language, keyword
277277
)
278278
}
279279

280-
from := (page - 1) * pageSize
280+
from, pageSize := opts.GetSkipTake()
281281
searchRequest := bleve.NewSearchRequestOptions(indexerQuery, pageSize, from, false)
282282
searchRequest.Fields = []string{"Content", "RepoID", "Language", "CommitID", "UpdatedAt"}
283283
searchRequest.IncludeLocations = true
284284

285-
if len(language) == 0 {
285+
if len(opts.Language) == 0 {
286286
searchRequest.AddFacet("languages", bleve.NewFacetRequest("Language", 10))
287287
}
288288

@@ -326,7 +326,7 @@ func (b *Indexer) Search(ctx context.Context, repoIDs []int64, language, keyword
326326
}
327327

328328
searchResultLanguages := make([]*internal.SearchResultLanguages, 0, 10)
329-
if len(language) > 0 {
329+
if len(opts.Language) > 0 {
330330
// Use separate query to go get all language counts
331331
facetRequest := bleve.NewSearchRequestOptions(facetQuery, 1, 0, false)
332332
facetRequest.Fields = []string{"Content", "RepoID", "Language", "CommitID", "UpdatedAt"}

modules/indexer/code/elasticsearch/elasticsearch.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -281,35 +281,31 @@ func extractAggs(searchResult *elastic.SearchResult) []*internal.SearchResultLan
281281
}
282282

283283
// Search searches for codes and language stats by given conditions.
284-
func (b *Indexer) Search(ctx context.Context, repoIDs []int64, language, keyword string, page, pageSize int, isFuzzy bool) (int64, []*internal.SearchResult, []*internal.SearchResultLanguages, error) {
284+
func (b *Indexer) Search(ctx context.Context, opts *internal.SearchOptions) (int64, []*internal.SearchResult, []*internal.SearchResultLanguages, error) {
285285
searchType := esMultiMatchTypePhrasePrefix
286-
if isFuzzy {
286+
if opts.IsKeywordFuzzy {
287287
searchType = esMultiMatchTypeBestFields
288288
}
289289

290-
kwQuery := elastic.NewMultiMatchQuery(keyword, "content").Type(searchType)
290+
kwQuery := elastic.NewMultiMatchQuery(opts.Keyword, "content").Type(searchType)
291291
query := elastic.NewBoolQuery()
292292
query = query.Must(kwQuery)
293-
if len(repoIDs) > 0 {
294-
repoStrs := make([]any, 0, len(repoIDs))
295-
for _, repoID := range repoIDs {
293+
if len(opts.RepoIDs) > 0 {
294+
repoStrs := make([]any, 0, len(opts.RepoIDs))
295+
for _, repoID := range opts.RepoIDs {
296296
repoStrs = append(repoStrs, repoID)
297297
}
298298
repoQuery := elastic.NewTermsQuery("repo_id", repoStrs...)
299299
query = query.Must(repoQuery)
300300
}
301301

302302
var (
303-
start int
304-
kw = "<em>" + keyword + "</em>"
305-
aggregation = elastic.NewTermsAggregation().Field("language").Size(10).OrderByCountDesc()
303+
start, pageSize = opts.GetSkipTake()
304+
kw = "<em>" + opts.Keyword + "</em>"
305+
aggregation = elastic.NewTermsAggregation().Field("language").Size(10).OrderByCountDesc()
306306
)
307307

308-
if page > 0 {
309-
start = (page - 1) * pageSize
310-
}
311-
312-
if len(language) == 0 {
308+
if len(opts.Language) == 0 {
313309
searchResult, err := b.inner.Client.Search().
314310
Index(b.inner.VersionedIndexName()).
315311
Aggregation("language", aggregation).
@@ -330,7 +326,7 @@ func (b *Indexer) Search(ctx context.Context, repoIDs []int64, language, keyword
330326
return convertResult(searchResult, kw, pageSize)
331327
}
332328

333-
langQuery := elastic.NewMatchQuery("language", language)
329+
langQuery := elastic.NewMatchQuery("language", opts.Language)
334330
countResult, err := b.inner.Client.Search().
335331
Index(b.inner.VersionedIndexName()).
336332
Aggregation("language", aggregation).

modules/indexer/code/git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func getRepoChanges(ctx context.Context, repo *repo_model.Repository, revision s
3232

3333
needGenesis := len(status.CommitSha) == 0
3434
if !needGenesis {
35-
hasAncestorCmd := git.NewCommand(ctx, "merge-base").AddDynamicArguments(repo.CodeIndexerStatus.CommitSha, revision)
35+
hasAncestorCmd := git.NewCommand(ctx, "merge-base").AddDynamicArguments(status.CommitSha, revision)
3636
stdout, _, _ := hasAncestorCmd.RunStdString(&git.RunOpts{Dir: repo.RepoPath()})
3737
needGenesis = len(stdout) == 0
3838
}

modules/indexer/code/indexer_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"testing"
1010

11+
"code.gitea.io/gitea/models/db"
1112
"code.gitea.io/gitea/models/unittest"
1213
"code.gitea.io/gitea/modules/git"
1314
"code.gitea.io/gitea/modules/indexer/code/bleve"
@@ -70,7 +71,15 @@ func testIndexer(name string, t *testing.T, indexer internal.Indexer) {
7071

7172
for _, kw := range keywords {
7273
t.Run(kw.Keyword, func(t *testing.T) {
73-
total, res, langs, err := indexer.Search(context.TODO(), kw.RepoIDs, "", kw.Keyword, 1, 10, true)
74+
total, res, langs, err := indexer.Search(context.TODO(), &internal.SearchOptions{
75+
RepoIDs: kw.RepoIDs,
76+
Keyword: kw.Keyword,
77+
Paginator: &db.ListOptions{
78+
Page: 1,
79+
PageSize: 10,
80+
},
81+
IsKeywordFuzzy: true,
82+
})
7483
assert.NoError(t, err)
7584
assert.Len(t, kw.IDs, int(total))
7685
assert.Len(t, langs, kw.Langs)

modules/indexer/code/internal/indexer.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99

10+
"code.gitea.io/gitea/models/db"
1011
repo_model "code.gitea.io/gitea/models/repo"
1112
"code.gitea.io/gitea/modules/indexer/internal"
1213
)
@@ -16,7 +17,17 @@ type Indexer interface {
1617
internal.Indexer
1718
Index(ctx context.Context, repo *repo_model.Repository, sha string, changes *RepoChanges) error
1819
Delete(ctx context.Context, repoID int64) error
19-
Search(ctx context.Context, repoIDs []int64, language, keyword string, page, pageSize int, isFuzzy bool) (int64, []*SearchResult, []*SearchResultLanguages, error)
20+
Search(ctx context.Context, opts *SearchOptions) (int64, []*SearchResult, []*SearchResultLanguages, error)
21+
}
22+
23+
type SearchOptions struct {
24+
RepoIDs []int64
25+
Keyword string
26+
Language string
27+
28+
IsKeywordFuzzy bool
29+
30+
db.Paginator
2031
}
2132

2233
// NewDummyIndexer returns a dummy indexer
@@ -38,6 +49,6 @@ func (d *dummyIndexer) Delete(ctx context.Context, repoID int64) error {
3849
return fmt.Errorf("indexer is not ready")
3950
}
4051

41-
func (d *dummyIndexer) Search(ctx context.Context, repoIDs []int64, language, keyword string, page, pageSize int, isFuzzy bool) (int64, []*SearchResult, []*SearchResultLanguages, error) {
52+
func (d *dummyIndexer) Search(ctx context.Context, opts *SearchOptions) (int64, []*SearchResult, []*SearchResultLanguages, error) {
4253
return 0, nil, nil, fmt.Errorf("indexer is not ready")
4354
}

modules/indexer/code/search.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type ResultLine struct {
3232

3333
type SearchResultLanguages = internal.SearchResultLanguages
3434

35+
type SearchOptions = internal.SearchOptions
36+
3537
func indices(content string, selectionStartIndex, selectionEndIndex int) (int, int) {
3638
startIndex := selectionStartIndex
3739
numLinesBefore := 0
@@ -125,12 +127,12 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
125127

126128
// PerformSearch perform a search on a repository
127129
// if isFuzzy is true set the Damerau-Levenshtein distance from 0 to 2
128-
func PerformSearch(ctx context.Context, repoIDs []int64, language, keyword string, page, pageSize int, isFuzzy bool) (int, []*Result, []*internal.SearchResultLanguages, error) {
129-
if len(keyword) == 0 {
130+
func PerformSearch(ctx context.Context, opts *SearchOptions) (int, []*Result, []*SearchResultLanguages, error) {
131+
if opts == nil || len(opts.Keyword) == 0 {
130132
return 0, nil, nil, nil
131133
}
132134

133-
total, results, resultLanguages, err := (*globalIndexer.Load()).Search(ctx, repoIDs, language, keyword, page, pageSize, isFuzzy)
135+
total, results, resultLanguages, err := (*globalIndexer.Load()).Search(ctx, opts)
134136
if err != nil {
135137
return 0, nil, nil, err
136138
}

modules/markup/markdown/ast.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,6 @@ func NewColorPreview(color []byte) *ColorPreview {
175175
}
176176
}
177177

178-
// IsColorPreview returns true if the given node implements the ColorPreview interface,
179-
// otherwise false.
180-
func IsColorPreview(node ast.Node) bool {
181-
_, ok := node.(*ColorPreview)
182-
return ok
183-
}
184-
185178
// Attention is an inline for an attention
186179
type Attention struct {
187180
ast.BaseInline

modules/markup/markdown/goldmark.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
216216
attentionType := strings.ToLower(strings.TrimPrefix(string(secondTextNode.Segment.Value(reader.Source())), "!"))
217217

218218
// color the blockquote
219-
v.SetAttributeString("class", []byte("gt-py-3 attention attention-"+attentionType))
219+
v.SetAttributeString("class", []byte("attention-header attention-"+attentionType))
220220

221221
// create an emphasis to make it bold
222222
attentionParagraph := ast.NewParagraph()
@@ -364,27 +364,21 @@ func (r *HTMLRenderer) renderCodeSpan(w util.BufWriter, source []byte, n ast.Nod
364364
// renderAttention renders a quote marked with i.e. "> **Note**" or "> **Warning**" with a corresponding svg
365365
func (r *HTMLRenderer) renderAttention(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
366366
if entering {
367-
_, _ = w.WriteString(`<span class="gt-mr-2 gt-vm attention-`)
368367
n := node.(*Attention)
369-
_, _ = w.WriteString(strings.ToLower(n.AttentionType))
370-
_, _ = w.WriteString(`">`)
371-
372-
var octiconType string
368+
var octiconName string
373369
switch n.AttentionType {
374-
case "note":
375-
octiconType = "info"
376370
case "tip":
377-
octiconType = "light-bulb"
371+
octiconName = "light-bulb"
378372
case "important":
379-
octiconType = "report"
373+
octiconName = "report"
380374
case "warning":
381-
octiconType = "alert"
375+
octiconName = "alert"
382376
case "caution":
383-
octiconType = "stop"
377+
octiconName = "stop"
378+
default: // including "note"
379+
octiconName = "info"
384380
}
385-
_, _ = w.WriteString(string(svg.RenderHTML("octicon-" + octiconType)))
386-
} else {
387-
_, _ = w.WriteString("</span>\n")
381+
_, _ = w.WriteString(string(svg.RenderHTML("octicon-"+octiconName, 16, "attention-icon attention-"+n.AttentionType)))
388382
}
389383
return ast.WalkContinue, nil
390384
}

modules/markup/sanitizer.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ func createDefaultPolicy() *bluemonday.Policy {
6464
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^color-preview$`)).OnElements("span")
6565

6666
// For attention
67-
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^gt-py-3 attention attention-\w+$`)).OnElements("blockquote")
67+
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^attention-header attention-\w+$`)).OnElements("blockquote")
6868
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^attention-\w+$`)).OnElements("strong")
69-
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^gt-mr-2 gt-vm attention-\w+$`)).OnElements("span", "strong")
70-
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^svg octicon-(\w|-)+$`)).OnElements("svg")
69+
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^attention-icon attention-\w+ svg octicon-[\w-]+$`)).OnElements("svg")
7170
policy.AllowAttrs("viewBox", "width", "height", "aria-hidden").OnElements("svg")
7271
policy.AllowAttrs("fill-rule", "d").OnElements("path")
7372

@@ -105,18 +104,12 @@ func createDefaultPolicy() *bluemonday.Policy {
105104
// Allow icons
106105
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^icon(\s+[\p{L}\p{N}_-]+)+$`)).OnElements("i")
107106

108-
// Allow unlabelled labels
109-
policy.AllowNoAttrs().OnElements("label")
110-
111107
// Allow classes for emojis
112108
policy.AllowAttrs("class").Matching(regexp.MustCompile(`emoji`)).OnElements("img")
113109

114110
// Allow icons, emojis, chroma syntax and keyword markup on span
115111
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^((icon(\s+[\p{L}\p{N}_-]+)+)|(emoji)|(language-math display)|(language-math inline))$|^([a-z][a-z0-9]{0,2})$|^` + keywordClass + `$`)).OnElements("span")
116112

117-
// Allow 'style' attribute on text elements.
118-
policy.AllowAttrs("style").OnElements("span", "p")
119-
120113
// Allow 'color' and 'background-color' properties for the style attribute on text elements.
121114
policy.AllowStyles("color", "background-color").OnElements("span", "p")
122115

@@ -144,7 +137,7 @@ func createDefaultPolicy() *bluemonday.Policy {
144137

145138
generalSafeElements := []string{
146139
"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "br", "b", "i", "strong", "em", "a", "pre", "code", "img", "tt",
147-
"div", "ins", "del", "sup", "sub", "p", "ol", "ul", "table", "thead", "tbody", "tfoot", "blockquote",
140+
"div", "ins", "del", "sup", "sub", "p", "ol", "ul", "table", "thead", "tbody", "tfoot", "blockquote", "label",
148141
"dl", "dt", "dd", "kbd", "q", "samp", "var", "hr", "ruby", "rt", "rp", "li", "tr", "td", "th", "s", "strike", "summary",
149142
"details", "caption", "figure", "figcaption",
150143
"abbr", "bdo", "cite", "dfn", "mark", "small", "span", "time", "video", "wbr",

routers/web/admin/repos.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package admin
55

66
import (
7+
"fmt"
78
"net/http"
89
"net/url"
910
"strings"
@@ -84,7 +85,7 @@ func UnadoptedRepos(ctx *context.Context) {
8485
if !doSearch {
8586
pager := context.NewPagination(0, opts.PageSize, opts.Page, 5)
8687
pager.SetDefaultParams(ctx)
87-
pager.AddParam(ctx, "search", "search")
88+
pager.AddParamString("search", fmt.Sprint(doSearch))
8889
ctx.Data["Page"] = pager
8990
ctx.HTML(http.StatusOK, tplUnadoptedRepos)
9091
return
@@ -98,7 +99,7 @@ func UnadoptedRepos(ctx *context.Context) {
9899
ctx.Data["Dirs"] = repoNames
99100
pager := context.NewPagination(count, opts.PageSize, opts.Page, 5)
100101
pager.SetDefaultParams(ctx)
101-
pager.AddParam(ctx, "search", "search")
102+
pager.AddParamString("search", fmt.Sprint(doSearch))
102103
ctx.Data["Page"] = pager
103104
ctx.HTML(http.StatusOK, tplUnadoptedRepos)
104105
}

routers/web/explore/code.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package explore
66
import (
77
"net/http"
88

9+
"code.gitea.io/gitea/models/db"
910
repo_model "code.gitea.io/gitea/models/repo"
1011
"code.gitea.io/gitea/modules/base"
1112
code_indexer "code.gitea.io/gitea/modules/indexer/code"
@@ -76,7 +77,16 @@ func Code(ctx *context.Context) {
7677
)
7778

7879
if (len(repoIDs) > 0) || isAdmin {
79-
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, repoIDs, language, keyword, page, setting.UI.RepoSearchPagingNum, isFuzzy)
80+
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, &code_indexer.SearchOptions{
81+
RepoIDs: repoIDs,
82+
Keyword: keyword,
83+
IsKeywordFuzzy: isFuzzy,
84+
Language: language,
85+
Paginator: &db.ListOptions{
86+
Page: page,
87+
PageSize: setting.UI.RepoSearchPagingNum,
88+
},
89+
})
8090
if err != nil {
8191
if code_indexer.IsAvailable(ctx) {
8292
ctx.ServerError("SearchResults", err)
@@ -127,7 +137,7 @@ func Code(ctx *context.Context) {
127137

128138
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
129139
pager.SetDefaultParams(ctx)
130-
pager.AddParam(ctx, "l", "Language")
140+
pager.AddParamString("l", language)
131141
ctx.Data["Page"] = pager
132142

133143
ctx.HTML(http.StatusOK, tplExploreCode)

0 commit comments

Comments
 (0)