refactor: lazy init global regexp variables (#40269)

Gitea binary is an all-in-one program.

It doesn't make sense to compile dozens or even hundreds of regexps for
git hook sub commands.
This commit is contained in:
wxiaoguang authored and GitHub committed 2026-11-07 11:56:56 +00:00
1 parent 6bc92f464a
commit a165fdb63c
52 files changed
+474 -282

No files matched your search

+4 -1
View File
@@ -13,6 +13,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"unicode"
"uuid"
@@ -666,7 +667,9 @@ func runMergeCommand(ctx *mergeContext, mergeStyle repo_model.MergeStyle, cmd *g
return nil
}
var escapedSymbols = regexp.MustCompile(`([*[?! \\])`)
var escapedSymbols = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile(`([*[?! \\])`)
})
// IsUserAllowedToMerge check if user is allowed to merge PR with given permissions and branch protections
func IsUserAllowedToMerge(ctx context.Context, pr *issues_model.PullRequest, p access_model.Permission, user *user_model.User) (bool, error) {
+1 -1
View File
@@ -220,7 +220,7 @@ func getDiffTree(ctx context.Context, repo git.RepositoryFacade, baseBranch, hea
for scanner.Scan() {
treePath := scanner.Text()
// escape '*', '?', '[', spaces and '!' prefix
treePath = escapedSymbols.ReplaceAllString(treePath, `\$1`)
treePath = escapedSymbols().ReplaceAllString(treePath, `\$1`)
// no necessary to escape the first '#' symbol because the first symbol is '/'
if _, err := fmt.Fprintf(out, "/%s\n", treePath); err != nil {
return err