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

+5 -2
View File
@@ -8,6 +8,7 @@ import (
"fmt"
"regexp"
"strings"
"sync"
"gitea.dev/actionslib/pkg/model"
@@ -75,7 +76,9 @@ func ParseWorkflowCallConfig(content []byte) (*model.WorkflowCall, error) {
const SecretsInherit = "inherit"
// callerSecretValueRegexp matches the `${{ secrets.NAME }}` form expected for each value in a caller's `secrets:` mapping.
var callerSecretValueRegexp = regexp.MustCompile(`^\s*\$\{\{\s*secrets\.([A-Za-z_][A-Za-z0-9_]*)\s*\}\}\s*$`)
var callerSecretValueRegexp = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile(`^\s*\$\{\{\s*secrets\.([A-Za-z_][A-Za-z0-9_]*)\s*\}\}\s*$`)
})
// ParseCallerSecrets decodes a caller's "secrets:" YAML node into one of two forms:
// - inherit == true: the caller wrote `secrets: inherit`; mapping is nil
@@ -101,7 +104,7 @@ func ParseCallerSecrets(node yaml.Node) (inherit bool, mapping map[string]string
if err := v.Decode(&sv); err != nil {
return false, nil, fmt.Errorf("decode secret %q: %w", k.Value, err)
}
matches := callerSecretValueRegexp.FindStringSubmatch(sv)
matches := callerSecretValueRegexp().FindStringSubmatch(sv)
if len(matches) != 2 {
return false, nil, fmt.Errorf("caller secret %q value must be of the form ${{ secrets.NAME }}", k.Value)
}
+16 -9
View File
@@ -10,6 +10,7 @@ import (
"path"
"regexp"
"strings"
"sync"
"gitea.dev/modules/markup"
"gitea.dev/modules/translation"
@@ -71,15 +72,24 @@ func determineDelimiter(ctx *markup.RenderContext, data []byte) rune {
return delimiter
}
// quoteRegexp follows the RFC-4180 CSV standard for when double-quotes are used to enclose fields, then a double-quote appearing inside a
// field must be escaped by preceding it with another double quote. https://www.ietf.org/rfc/rfc4180.txt
// This finds all quoted strings that have escaped quotes.
var quoteRegexp = regexp.MustCompile(`"[^"]*"`)
var globalVars = sync.OnceValue(func() (ret struct {
quoteRegexp *regexp.Regexp
beforeAfterQuotes *regexp.Regexp
},
) {
// quoteRegexp follows the RFC-4180 CSV standard for when double-quotes are used to enclose fields, then a double-quote appearing inside a
// field must be escaped by preceding it with another double quote. https://www.ietf.org/rfc/rfc4180.txt
// This finds all quoted strings that have escaped quotes.
ret.quoteRegexp = regexp.MustCompile(`"[^"]*"`)
// Looks for possible delimiters right before or after (with spaces after the former) double quotes with closing quotes
ret.beforeAfterQuotes = regexp.MustCompile(`([,@\t;|]{0,1}) *(?:"[^"]*")+([,@\t;|]{0,1})`)
return ret
})
// removeQuotedStrings uses the quoteRegexp to remove all quoted strings so that we can reliably have each row on one line
// (quoted strings often have new lines within the string)
func removeQuotedString(text string) string {
return quoteRegexp.ReplaceAllLiteralString(text, "")
return globalVars().quoteRegexp.ReplaceAllLiteralString(text, "")
}
// guessDelimiter takes up to maxLines of the CSV text, iterates through the possible delimiters, and sees if the CSV Reader reads it without throwing any errors.
@@ -133,13 +143,10 @@ func FormatError(err error, locale translation.Locale) (string, error) {
return "", err
}
// Looks for possible delimiters right before or after (with spaces after the former) double quotes with closing quotes
var beforeAfterQuotes = regexp.MustCompile(`([,@\t;|]{0,1}) *(?:"[^"]*")+([,@\t;|]{0,1})`)
// guessFromBeforeAfterQuotes guesses the limiter by finding a double quote that has a valid delimiter before it and a closing quote,
// or a double quote with a closing quote and a valid delimiter after it
func guessFromBeforeAfterQuotes(data []byte) rune {
rs := beforeAfterQuotes.FindStringSubmatch(string(data)) // returns first match, or nil if none
rs := globalVars().beforeAfterQuotes.FindStringSubmatch(string(data)) // returns first match, or nil if none
if rs != nil {
if rs[1] != "" {
return rune(rs[1][0]) // delimiter found left of quoted string
+26 -3
View File
@@ -74,20 +74,43 @@ func (c *CommitMessage) MessageTrailer() CommitMessageTrailerValues {
return c.trailerValues
}
var commitMessageTrailerSplit = sync.OnceValue(func() *regexp.Regexp {
var globalVars = sync.OnceValue(func() (ret struct {
commitMessageTrailerSplit *regexp.Regexp
hunkRegex *regexp.Regexp
refNamePatternInvalid *regexp.Regexp
patchCommits *regexp.Regexp
},
) {
// ref: https://git-scm.com/docs/git-interpret-trailers
// TODO: the regexp is not able to perfectly parse the all kinds of trailers
// It was just copied from legacy code, it is not exactly the same as how Git parses the trailer and not quite right in some cases.
// For the key characters: it follows RFC 822 field name syntax (or RFC 2822/RFC 5322): printable ASCII characters between 33 and 126 except the colon (:),
// but maybe we don't want to make it that complicated, so here we only support some common "symbol-like" characters.
return regexp.MustCompile(`(?s)^(?P<content>.*?)(?P<sep>^|^\n|^-{3,}\n+|\n+-{3,}\n+|\n{2,})(?P<trailer>(?:[A-Za-z0-9][-\w]*:[^\n]*(\n\s+[^\n]*)*\n?)*\n*)$`)
ret.commitMessageTrailerSplit = regexp.MustCompile(`(?s)^(?P<content>.*?)(?P<sep>^|^\n|^-{3,}\n+|\n+-{3,}\n+|\n{2,})(?P<trailer>(?:[A-Za-z0-9][-\w]*:[^\n]*(\n\s+[^\n]*)*\n?)*\n*)$`)
// Example: @@ -1,8 +1,9 @@ => [..., 1, 8, 1, 9]
ret.hunkRegex = regexp.MustCompile(`^@@ -(?P<beginOld>[0-9]+)(,(?P<endOld>[0-9]+))? \+(?P<beginNew>[0-9]+)(,(?P<endNew>[0-9]+))? @@`)
// refNamePatternInvalid is regular expression with unallowed characters in git reference name
// They cannot have ASCII control characters (i.e. bytes whose values are lower than \040, or \177 DEL), space, tilde ~, caret ^, or colon : anywhere.
// They cannot have question-mark ?, asterisk *, or open bracket [ anywhere
ret.refNamePatternInvalid = regexp.MustCompile(
`[\000-\037\177 \\~^:?*[]|` + // No absolutely invalid characters
`(?:^[/.])|` + // Not HasPrefix("/") or "."
`(?:/\.)|` + // no "/."
`(?:\.lock$)|(?:\.lock/)|` + // No ".lock/"" or ".lock" at the end
`(?:\.\.)|` + // no ".." anywhere
`(?://)|` + // no "//" anywhere
`(?:@{)|` + // no "@{"
`(?:[/.]$)|` + // no terminal '/' or '.'
`(?:^@$)`) // Not "@"
ret.patchCommits = regexp.MustCompile(`^From\s(\w+)\s`)
return ret
})
// CommitMessageSplitTrailer tries to split the message by the trailer separator
// content + sep + trailer will reconstruct the original message
func CommitMessageSplitTrailer(s string) (content, sep, trailer string) {
s = util.NormalizeStringEOL(s)
re := commitMessageTrailerSplit()
re := globalVars().commitMessageTrailerSplit
v := re.FindStringSubmatch(s)
if v == nil {
return s, "", ""
+2 -6
View File
@@ -9,7 +9,6 @@ import (
"context"
"fmt"
"io"
"regexp"
"strconv"
"strings"
@@ -132,9 +131,6 @@ func ParseDiffHunkString(diffHunk string) (leftLine, leftHunk, rightLine, rightH
return leftLine, leftHunk, rightLine, rightHunk
}
// Example: @@ -1,8 +1,9 @@ => [..., 1, 8, 1, 9]
var hunkRegex = regexp.MustCompile(`^@@ -(?P<beginOld>[0-9]+)(,(?P<endOld>[0-9]+))? \+(?P<beginNew>[0-9]+)(,(?P<endNew>[0-9]+))? @@`)
const cmdDiffHead = "diff --git "
func isHeader(lof string, inHunk bool) bool {
@@ -192,9 +188,9 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
break
}
// A map with named groups of our regex to recognize them later more easily
submatches := hunkRegex.FindStringSubmatch(lof)
submatches := globalVars().hunkRegex.FindStringSubmatch(lof)
groups := make(map[string]string)
for i, name := range hunkRegex.SubexpNames() {
for i, name := range globalVars().hunkRegex.SubexpNames() {
if i != 0 && name != "" {
groups[name] = submatches[i]
}
+2 -17
View File
@@ -5,7 +5,6 @@ package git
import (
"context"
"regexp"
"strconv"
"strings"
@@ -22,27 +21,13 @@ const (
pullSuffix = "/head"
)
// refNamePatternInvalid is regular expression with unallowed characters in git reference name
// They cannot have ASCII control characters (i.e. bytes whose values are lower than \040, or \177 DEL), space, tilde ~, caret ^, or colon : anywhere.
// They cannot have question-mark ?, asterisk *, or open bracket [ anywhere
var refNamePatternInvalid = regexp.MustCompile(
`[\000-\037\177 \\~^:?*[]|` + // No absolutely invalid characters
`(?:^[/.])|` + // Not HasPrefix("/") or "."
`(?:/\.)|` + // no "/."
`(?:\.lock$)|(?:\.lock/)|` + // No ".lock/"" or ".lock" at the end
`(?:\.\.)|` + // no ".." anywhere
`(?://)|` + // no "//" anywhere
`(?:@{)|` + // no "@{"
`(?:[/.]$)|` + // no terminal '/' or '.'
`(?:^@$)`) // Not "@"
// IsValidRefPattern ensures that the provided string could be a valid reference
func IsValidRefPattern(name string) bool {
return !refNamePatternInvalid.MatchString(name)
return !globalVars().refNamePatternInvalid.MatchString(name)
}
func SanitizeRefPattern(name string) string {
return refNamePatternInvalid.ReplaceAllString(name, "_")
return globalVars().refNamePatternInvalid.ReplaceAllString(name, "_")
}
// Reference represents a Git ref.
+1 -4
View File
@@ -11,7 +11,6 @@ import (
"errors"
"fmt"
"io"
"regexp"
"strings"
"gitea.dev/modules/git/gitcmd"
@@ -51,8 +50,6 @@ func (repo *Repository) GetDiffNumChangedFiles(ctx context.Context, base, head s
return w.numLines, nil
}
var patchCommits = regexp.MustCompile(`^From\s(\w+)\s`)
// GetDiff generates and returns patch data between given revisions, optimized for human readability
func (repo *Repository) GetDiff(ctx context.Context, compareArg string, w io.Writer) error {
return gitcmd.NewCommand("diff", "-p").AddDynamicArguments(compareArg).
@@ -119,7 +116,7 @@ func (repo *Repository) ReadPatchCommit(prID int64) (commitSHA string, err error
scanner := bufio.NewScanner(loadPatch)
scanner.Scan()
// Parse the Patch stats, sometimes Migration returns a 404 for the patch file
commitSHAGroups := patchCommits.FindStringSubmatch(scanner.Text())
commitSHAGroups := globalVars().patchCommits.FindStringSubmatch(scanner.Text())
if len(commitSHAGroups) != 0 {
commitSHA = commitSHAGroups[1]
} else {
+15 -7
View File
@@ -12,6 +12,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"gitea.dev/modules/packages"
"gitea.dev/modules/util"
@@ -38,6 +39,18 @@ const (
maxPKGINFOSize = 1024 * 1024
)
var globalVars = sync.OnceValue(func() (ret struct {
namePattern *regexp.Regexp
versionPattern *regexp.Regexp
},
) {
// https://man.archlinux.org/man/PKGBUILD.5
ret.namePattern = regexp.MustCompile(`\A[a-zA-Z0-9@._+-]+\z`)
// (epoch:pkgver-pkgrel)
ret.versionPattern = regexp.MustCompile(`\A(?:\d:)?[\w.+~]+(?:-[-\w.+~]+)?\z`)
return ret
})
var (
ErrMissingPKGINFOFile = util.NewInvalidArgumentErrorf(".PKGINFO file is missing")
ErrUnsupportedFormat = util.NewInvalidArgumentErrorf("unsupported package container format")
@@ -45,11 +58,6 @@ var (
ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid")
ErrInvalidArchitecture = util.NewInvalidArgumentErrorf("package architecture is invalid")
// https://man.archlinux.org/man/PKGBUILD.5
namePattern = regexp.MustCompile(`\A[a-zA-Z0-9@._+-]+\z`)
// (epoch:pkgver-pkgrel)
versionPattern = regexp.MustCompile(`\A(?:\d:)?[\w.+~]+(?:-[-\w.+~]+)?\z`)
// caps on the accumulated package file list (vars so tests can lower them); far above
// any legitimate package, but low enough to stop metadata amplification
maxFileEntries = 100000
@@ -251,10 +259,10 @@ func ParsePackageInfo(r io.Reader) (*Package, error) {
return nil, err
}
if !namePattern.MatchString(p.Name) {
if !globalVars().namePattern.MatchString(p.Name) {
return nil, ErrInvalidName
}
if !versionPattern.MatchString(p.Version) {
if !globalVars().versionPattern.MatchString(p.Version) {
return nil, ErrInvalidVersion
}
if p.FileMetadata.Architecture == "" {
+5 -5
View File
@@ -127,11 +127,11 @@ func TestParsePackageInfo(t *testing.T) {
})
t.Run("Regexp", func(t *testing.T) {
assert.Regexp(t, versionPattern, "1.2_3~4+5")
assert.Regexp(t, versionPattern, "1:2_3~4+5")
assert.NotRegexp(t, versionPattern, "a:1.0.0-1")
assert.NotRegexp(t, versionPattern, "0.0.1/1-1")
assert.NotRegexp(t, versionPattern, "1.0.0 -1")
assert.Regexp(t, globalVars().versionPattern, "1.2_3~4+5")
assert.Regexp(t, globalVars().versionPattern, "1:2_3~4+5")
assert.NotRegexp(t, globalVars().versionPattern, "a:1.0.0-1")
assert.NotRegexp(t, globalVars().versionPattern, "0.0.1/1-1")
assert.NotRegexp(t, globalVars().versionPattern, "1.0.0 -1")
})
t.Run("InvalidVersion", func(t *testing.T) {
+5 -2
View File
@@ -8,6 +8,7 @@ import (
"errors"
"io"
"regexp"
"sync"
"gitea.dev/modules/json"
"gitea.dev/modules/packages"
@@ -60,7 +61,9 @@ type Dependency struct {
Package *string `json:"package"`
}
var nameMatch = regexp.MustCompile(`\A[a-zA-Z][a-zA-Z0-9-_]{0,63}\z`)
var nameMatch = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile(`\A[a-zA-Z][a-zA-Z0-9-_]{0,63}\z`)
})
// ParsePackage reads the metadata and content of a package
func ParsePackage(r io.Reader) (*Package, error) {
@@ -120,7 +123,7 @@ func parsePackage(r io.Reader) (*Package, error) {
return nil, err
}
if !nameMatch.MatchString(meta.Name) {
if !nameMatch().MatchString(meta.Name) {
return nil, ErrInvalidName
}
+13 -5
View File
@@ -9,6 +9,7 @@ import (
"io"
"regexp"
"strings"
"sync"
"gitea.dev/modules/json"
"gitea.dev/modules/packages"
@@ -21,13 +22,20 @@ const (
SettingPublicPem = "chef.public_pem"
)
var globalVars = sync.OnceValue(func() (ret struct {
namePattern *regexp.Regexp
versionPattern *regexp.Regexp
},
) {
ret.namePattern = regexp.MustCompile(`\A\S+\z`)
ret.versionPattern = regexp.MustCompile(`\A\d+\.\d+(?:\.\d+)?\z`)
return ret
})
var (
ErrMissingMetadataFile = util.NewInvalidArgumentErrorf("metadata.json file is missing")
ErrInvalidName = util.NewInvalidArgumentErrorf("package name is invalid")
ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid")
namePattern = regexp.MustCompile(`\A\S+\z`)
versionPattern = regexp.MustCompile(`\A\d+\.\d+(?:\.\d+)?\z`)
)
// Package represents a Chef package
@@ -108,11 +116,11 @@ func ParseChefMetadata(r io.Reader) (*Package, error) {
return nil, err
}
if !namePattern.MatchString(cm.Name) {
if !globalVars().namePattern.MatchString(cm.Name) {
return nil, ErrInvalidName
}
if !versionPattern.MatchString(cm.Version) {
if !globalVars().versionPattern.MatchString(cm.Version) {
return nil, ErrInvalidVersion
}
+5 -2
View File
@@ -14,6 +14,7 @@ import (
"path"
"regexp"
"strings"
"sync"
"gitea.dev/modules/charset"
"gitea.dev/modules/json"
@@ -117,7 +118,9 @@ type Author struct {
Homepage string `json:"homepage,omitempty"`
}
var nameMatch = regexp.MustCompile(`\A[a-z0-9]([_\.-]?[a-z0-9]+)*/[a-z0-9](([_\.]?|-{0,2})[a-z0-9]+)*\z`)
var nameMatch = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile(`\A[a-z0-9]([_\.-]?[a-z0-9]+)*/[a-z0-9](([_\.]?|-{0,2})[a-z0-9]+)*\z`)
})
type ReadSeekAt interface {
io.Reader
@@ -237,7 +240,7 @@ func ParsePackage(r ReadSeekAt, optVersion ...string) (*PackageInfo, error) {
return nil, err
}
if !nameMatch.MatchString(cj.Name) {
if !nameMatch().MatchString(cj.Name) {
return nil, ErrInvalidName
}
+32 -16
View File
@@ -4,20 +4,36 @@
package conan
import (
"fmt"
"io"
"regexp"
"strings"
"sync"
)
var (
patternAuthor = compilePattern("author")
patternHomepage = compilePattern("homepage")
patternURL = compilePattern("url")
patternLicense = compilePattern("license")
patternDescription = compilePattern("description")
patternTopics = regexp.MustCompile(`(?im)^\s*topics\s*=\s*\((.+)\)`)
patternTopicList = regexp.MustCompile(`\s*['"](.+?)['"]\s*,?`)
)
var globalVars = sync.OnceValue(func() (ret struct {
patternAuthor *regexp.Regexp
patternHomepage *regexp.Regexp
patternURL *regexp.Regexp
patternLicense *regexp.Regexp
patternDescription *regexp.Regexp
patternTopics *regexp.Regexp
patternTopicList *regexp.Regexp
namePattern *regexp.Regexp
revisionPattern *regexp.Regexp
},
) {
ret.patternAuthor = compilePattern("author")
ret.patternHomepage = compilePattern("homepage")
ret.patternURL = compilePattern("url")
ret.patternLicense = compilePattern("license")
ret.patternDescription = compilePattern("description")
ret.patternTopics = regexp.MustCompile(`(?im)^\s*topics\s*=\s*\((.+)\)`)
ret.patternTopicList = regexp.MustCompile(`\s*['"](.+?)['"]\s*,?`)
ret.namePattern = regexp.MustCompile(fmt.Sprintf(`^[a-zA-Z0-9_][a-zA-Z0-9_\+\.-]{%d,%d}$`, minChars-1, maxChars-1))
ret.revisionPattern = regexp.MustCompile(fmt.Sprintf(`^[a-zA-Z0-9]{1,%d}$`, maxChars))
return ret
})
func compilePattern(name string) *regexp.Regexp {
return regexp.MustCompile(`(?im)^\s*` + name + `\s*=\s*['"\(](.+)['"\)]`)
@@ -31,29 +47,29 @@ func ParseConanfile(r io.Reader) (*Metadata, error) {
metadata := &Metadata{}
m := patternAuthor.FindSubmatch(buf)
m := globalVars().patternAuthor.FindSubmatch(buf)
if len(m) > 1 && len(m[1]) > 0 {
metadata.Author = string(m[1])
}
m = patternHomepage.FindSubmatch(buf)
m = globalVars().patternHomepage.FindSubmatch(buf)
if len(m) > 1 && len(m[1]) > 0 {
metadata.ProjectURL = string(m[1])
}
m = patternURL.FindSubmatch(buf)
m = globalVars().patternURL.FindSubmatch(buf)
if len(m) > 1 && len(m[1]) > 0 {
metadata.RepositoryURL = string(m[1])
}
m = patternLicense.FindSubmatch(buf)
m = globalVars().patternLicense.FindSubmatch(buf)
if len(m) > 1 && len(m[1]) > 0 {
metadata.License = strings.ReplaceAll(strings.ReplaceAll(string(m[1]), "'", ""), "\"", "")
}
m = patternDescription.FindSubmatch(buf)
m = globalVars().patternDescription.FindSubmatch(buf)
if len(m) > 1 && len(m[1]) > 0 {
metadata.Description = string(m[1])
}
m = patternTopics.FindSubmatch(buf)
m = globalVars().patternTopics.FindSubmatch(buf)
if len(m) > 1 && len(m[1]) > 0 {
m2 := patternTopicList.FindAllSubmatch(m[1], -1)
m2 := globalVars().patternTopicList.FindAllSubmatch(m[1], -1)
if len(m2) > 0 {
metadata.Keywords = make([]string, 0, len(m2))
for _, g := range m2 {
+7 -13
View File
@@ -5,7 +5,6 @@ package conan
import (
"fmt"
"regexp"
"strings"
"gitea.dev/modules/log"
@@ -21,12 +20,7 @@ const (
DefaultRevision = "0"
)
var (
namePattern = regexp.MustCompile(fmt.Sprintf(`^[a-zA-Z0-9_][a-zA-Z0-9_\+\.-]{%d,%d}$`, minChars-1, maxChars-1))
revisionPattern = regexp.MustCompile(fmt.Sprintf(`^[a-zA-Z0-9]{1,%d}$`, maxChars))
ErrValidation = util.NewInvalidArgumentErrorf("could not validate one or more reference fields")
)
var ErrValidation = util.NewInvalidArgumentErrorf("could not validate one or more reference fields")
// RecipeReference represents a recipe <Name>/<Version>@<User>/<Channel>#<Revision>
type RecipeReference struct {
@@ -51,7 +45,7 @@ func NewRecipeReference(name, version, user, channel, revision string) (*RecipeR
return nil, ErrValidation
}
if !namePattern.MatchString(name) {
if !globalVars().namePattern.MatchString(name) {
return nil, ErrValidation
}
@@ -59,13 +53,13 @@ func NewRecipeReference(name, version, user, channel, revision string) (*RecipeR
if v == "" {
return nil, ErrValidation
}
if user != "" && !namePattern.MatchString(user) {
if user != "" && !globalVars().namePattern.MatchString(user) {
return nil, ErrValidation
}
if channel != "" && !namePattern.MatchString(channel) {
if channel != "" && !globalVars().namePattern.MatchString(channel) {
return nil, ErrValidation
}
if revision != "" && !revisionPattern.MatchString(revision) {
if revision != "" && !globalVars().revisionPattern.MatchString(revision) {
return nil, ErrValidation
}
@@ -124,10 +118,10 @@ func NewPackageReference(recipe *RecipeReference, reference, revision string) (*
if recipe == nil {
return nil, ErrValidation
}
if reference == "" || !revisionPattern.MatchString(reference) {
if reference == "" || !globalVars().revisionPattern.MatchString(reference) {
return nil, ErrValidation
}
if revision != "" && !revisionPattern.MatchString(revision) {
if revision != "" && !globalVars().revisionPattern.MatchString(revision) {
return nil, ErrValidation
}
+18 -10
View File
@@ -13,6 +13,7 @@ import (
"path"
"regexp"
"strings"
"sync"
"gitea.dev/modules/base"
"gitea.dev/modules/packages"
@@ -34,12 +35,19 @@ var (
ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid")
)
var (
fieldPattern = regexp.MustCompile(`\A\S+:`)
namePattern = regexp.MustCompile(`\A[a-zA-Z][a-zA-Z0-9\.]*[a-zA-Z0-9]\z`)
versionPattern = regexp.MustCompile(`\A[0-9]+(?:[.\-][0-9]+)+\z`)
authorReplacePattern = regexp.MustCompile(`[\[\(].+?[\]\)]`)
)
var globalVars = sync.OnceValue(func() (ret struct {
fieldPattern *regexp.Regexp
namePattern *regexp.Regexp
versionPattern *regexp.Regexp
authorReplacePattern *regexp.Regexp
},
) {
ret.fieldPattern = regexp.MustCompile(`\A\S+:`)
ret.namePattern = regexp.MustCompile(`\A[a-zA-Z][a-zA-Z0-9\.]*[a-zA-Z0-9]\z`)
ret.versionPattern = regexp.MustCompile(`\A[0-9]+(?:[.\-][0-9]+)+\z`)
ret.authorReplacePattern = regexp.MustCompile(`[\[\(].+?[\]\)]`)
return ret
})
// Package represents a CRAN package
type Package struct {
@@ -162,7 +170,7 @@ func ParseDescription(r io.Reader) (*Package, error) {
if line == "" {
continue
}
if !fieldPattern.MatchString(line) {
if !globalVars().fieldPattern.MatchString(line) {
b.WriteRune(' ')
b.WriteString(line)
continue
@@ -202,12 +210,12 @@ func setField(p *Package, data string) error {
switch name {
case "Package":
if !namePattern.MatchString(value) {
if !globalVars().namePattern.MatchString(value) {
return ErrInvalidName
}
p.Name = value
case "Version":
if !versionPattern.MatchString(value) {
if !globalVars().versionPattern.MatchString(value) {
return ErrInvalidVersion
}
p.Version = value
@@ -220,7 +228,7 @@ func setField(p *Package, data string) error {
case "License":
p.Metadata.License = value
case "Author":
p.Metadata.Authors = splitAndTrim(authorReplacePattern.ReplaceAllString(value, ""))
p.Metadata.Authors = splitAndTrim(globalVars().authorReplacePattern.ReplaceAllString(value, ""))
case "Depends":
p.Metadata.Depends = splitAndTrim(value)
case "Imports":
+5 -2
View File
@@ -12,6 +12,7 @@ import (
"path/filepath"
"regexp"
"strings"
"sync"
"gitea.dev/modules/util"
"gitea.dev/modules/validation"
@@ -42,7 +43,9 @@ const (
PropertySymbolID = "nuget.symbol.id"
)
var idmatch = regexp.MustCompile(`\A\w+(?:[.-]\w+)*\z`)
var idmatch = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile(`\A\w+(?:[.-]\w+)*\z`)
})
const maxNuspecFileSize = 3 * 1024 * 1024
@@ -171,7 +174,7 @@ func ParseNuspecMetaData(archive *zip.Reader, r io.Reader) (*Package, error) {
return nil, err
}
if !idmatch.MatchString(p.Metadata.ID) {
if !idmatch().MatchString(p.Metadata.ID) {
return nil, ErrNuspecInvalidID
}
+5 -2
View File
@@ -9,6 +9,7 @@ import (
"io"
"regexp"
"strings"
"sync"
"gitea.dev/modules/util"
"gitea.dev/modules/validation"
@@ -24,7 +25,9 @@ var (
ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid")
)
var namePattern = regexp.MustCompile(`\A[a-zA-Z_][a-zA-Z0-9_]*\z`)
var namePattern = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile(`\A[a-zA-Z_][a-zA-Z0-9_]*\z`)
})
// https://github.com/dart-lang/pub-dev/blob/4d582302a8d10152a5cd6129f65bf4f4dbca239d/pkg/pub_package_reader/lib/pub_package_reader.dart#L143
const maxPubspecFileSize = 128 * 1024
@@ -118,7 +121,7 @@ func ParsePubspecMetadata(r io.Reader) (*Package, error) {
return nil, err
}
if !namePattern.MatchString(p.Name) {
if !namePattern().MatchString(p.Name) {
return nil, ErrInvalidName
}
+14 -6
View File
@@ -10,6 +10,7 @@ import (
"path"
"regexp"
"strings"
"sync"
"gitea.dev/modules/json"
"gitea.dev/modules/packages"
@@ -19,14 +20,21 @@ import (
"github.com/hashicorp/go-version"
)
var globalVars = sync.OnceValue(func() (ret struct {
manifestPattern *regexp.Regexp
toolsVersionPattern *regexp.Regexp
},
) {
ret.manifestPattern = regexp.MustCompile(`\APackage(?:@swift-(\d+(?:\.\d+)?(?:\.\d+)?))?\.swift\z`)
ret.toolsVersionPattern = regexp.MustCompile(`\A// swift-tools-version:(\d+(?:\.\d+)?(?:\.\d+)?)`)
return ret
})
var (
ErrMissingManifestFile = util.NewInvalidArgumentErrorf("Package.swift file is missing")
ErrManifestFileTooLarge = util.NewInvalidArgumentErrorf("Package.swift file is too large")
ErrManifestFilesTooLarge = util.NewInvalidArgumentErrorf("Package.swift files are too large")
ErrInvalidManifestVersion = util.NewInvalidArgumentErrorf("manifest version is invalid")
manifestPattern = regexp.MustCompile(`\APackage(?:@swift-(\d+(?:\.\d+)?(?:\.\d+)?))?\.swift\z`)
toolsVersionPattern = regexp.MustCompile(`\A// swift-tools-version:(\d+(?:\.\d+)?(?:\.\d+)?)`)
)
const (
@@ -133,7 +141,7 @@ func ParsePackage(sr io.ReaderAt, size int64, mr io.Reader) (*Package, error) {
var manifestFiles []*zip.File
manifestDir, manifestDepth := "", 0
for _, file := range zr.File {
if strings.HasSuffix(file.Name, "/") || !manifestPattern.MatchString(path.Base(file.Name)) {
if strings.HasSuffix(file.Name, "/") || !globalVars().manifestPattern.MatchString(path.Base(file.Name)) {
continue
}
dir, depth := path.Dir(file.Name), strings.Count(file.Name, "/")
@@ -156,7 +164,7 @@ func ParsePackage(sr io.ReaderAt, size int64, mr io.Reader) (*Package, error) {
}
for _, file := range manifestFiles {
manifestMatch := manifestPattern.FindStringSubmatch(path.Base(file.Name))
manifestMatch := globalVars().manifestPattern.FindStringSubmatch(path.Base(file.Name))
if file.UncompressedSize64 > maxManifestFileSize {
return nil, ErrManifestFileTooLarge
@@ -190,7 +198,7 @@ func ParsePackage(sr io.ReaderAt, size int64, mr io.Reader) (*Package, error) {
Content: string(content),
}
toolsMatch := toolsVersionPattern.FindStringSubmatch(manifest.Content)
toolsMatch := globalVars().toolsVersionPattern.FindStringSubmatch(manifest.Content)
if len(toolsMatch) == 2 {
v, err := version.NewSemver(toolsMatch[1])
if err != nil {
+14 -8
View File
@@ -15,6 +15,7 @@ import (
"regexp"
"slices"
"strings"
"sync"
packages_module "gitea.dev/modules/packages"
@@ -31,20 +32,25 @@ var (
const maxReadmeSize = 1024 * 1024 // larger READMEs are not stored, they would be rendered on every page view
// same rules as Terraform's module source address parser (github.com/hashicorp/terraform-registry-address)
var (
nameRe = regexp.MustCompile(`\A[0-9A-Za-z](?:[0-9A-Za-z_-]{0,62}[0-9A-Za-z])?\z`)
systemRe = regexp.MustCompile(`\A[0-9a-z]{1,64}\z`)
)
var globalVars = sync.OnceValue(func() (ret struct {
nameRe *regexp.Regexp
systemRe *regexp.Regexp
},
) {
// same rules as Terraform's module source address parser (github.com/hashicorp/terraform-registry-address)
ret.nameRe = regexp.MustCompile(`\A[0-9A-Za-z](?:[0-9A-Za-z_-]{0,62}[0-9A-Za-z])?\z`)
ret.systemRe = regexp.MustCompile(`\A[0-9a-z]{1,64}\z`)
return ret
})
func ValidateAddress(namespace, name, provider string) error {
if !nameRe.MatchString(namespace) {
if !globalVars().nameRe.MatchString(namespace) {
return ErrInvalidNamespace
}
if !nameRe.MatchString(name) {
if !globalVars().nameRe.MatchString(name) {
return ErrInvalidName
}
if !systemRe.MatchString(provider) {
if !globalVars().systemRe.MatchString(provider) {
return ErrInvalidProvider
}
return nil
+29 -17
View File
@@ -20,10 +20,19 @@ import (
"gitea.dev/modules/util"
)
var (
var globalVars = sync.OnceValue(func() (ret struct {
validNamePattern *regexp.Regexp
mentionPattern *regexp.Regexp
issueNumericPattern *regexp.Regexp
issueAlphanumericPattern *regexp.Regexp
crossReferenceIssueNumericPattern *regexp.Regexp
crossReferenceCommitPattern *regexp.Regexp
timeLogPattern *regexp.Regexp
},
) {
// validNamePattern performs only the most basic validation for user or repository names
// Repository name should contain only alphanumeric, dash ('-'), underscore ('_') and dot ('.') characters.
validNamePattern = regexp.MustCompile(`^[a-z0-9_.-]+$`)
ret.validNamePattern = regexp.MustCompile(`^[a-z0-9_.-]+$`)
// NOTE: All below regex matching do not perform any extra validation.
// Thus a link is produced even if the linked entity does not exist.
@@ -31,22 +40,25 @@ var (
// TODO: fix invalid linking issue
// mentionPattern matches all mentions in the form of "@user" or "@org/team"
mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[-\w][-.\w]*?|@[-\w][-.\w]*?/[-\w][-.\w]*?)(?:\s|$|[:,;.?!](\s|$)|'|\)|\])`)
ret.mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[-\w][-.\w]*?|@[-\w][-.\w]*?/[-\w][-.\w]*?)(?:\s|$|[:,;.?!](\s|$)|'|\)|\])`)
// issueNumericPattern matches string that references to a numeric issue, e.g. #1287
// TODO: REFERENCE-REGEXP-PATTERN: findValidReferenceIndex checks a non-ASCII trailing character
issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[|\'|\"|[^\x00-\x7f])([#!][0-9]+)(?:\s|$|\)|\]|\'|\"|[:;,.?!]\s|[:;,.?!]$|[^\x00-\x7f])`)
ret.issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[|\'|\"|[^\x00-\x7f])([#!][0-9]+)(?:\s|$|\)|\]|\'|\"|[:;,.?!]\s|[:;,.?!]$|[^\x00-\x7f])`)
// issueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234
issueAlphanumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[|\"|\')([A-Z]{1,10}-[1-9][0-9]*)(?:\s|$|\)|\]|:|\.(\s|$)|\"|\'|,)`)
ret.issueAlphanumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[|\"|\')([A-Z]{1,10}-[1-9][0-9]*)(?:\s|$|\)|\]|:|\.(\s|$)|\"|\'|,)`)
// crossReferenceIssueNumericPattern matches string that references a numeric issue in a different repository
// e.g. org/repo#12345
// TODO: REFERENCE-REGEXP-PATTERN: findValidReferenceIndex checks a non-ASCII trailing character
crossReferenceIssueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[|[^\x00-\x7f])([0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+[#!][0-9]+)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$|[^\x00-\x7f])`)
ret.crossReferenceIssueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[|[^\x00-\x7f])([0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+[#!][0-9]+)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$|[^\x00-\x7f])`)
// crossReferenceCommitPattern matches a string that references a commit in a different repository
// e.g. go-gitea/gitea@d8a994ef, go-gitea/gitea@d8a994ef243349f321568f9e36d5c3f444b99cae (7-40 characters)
crossReferenceCommitPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+)/([0-9a-zA-Z-_\.]+)@([0-9a-f]{7,64})(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)`)
ret.crossReferenceCommitPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+)/([0-9a-zA-Z-_\.]+)@([0-9a-f]{7,64})(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)`)
// timeLogPattern matches string for time tracking
timeLogPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@([0-9]+([\.,][0-9]+)?(w|d|m|h))+)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)`)
ret.timeLogPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@([0-9]+([\.,][0-9]+)?(w|d|m|h))+)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)`)
return ret
})
var (
issueCloseKeywordsPat, issueReopenKeywordsPat *regexp.Regexp
issueKeywordWindow int
issueKeywordsOnce sync.Once
@@ -225,7 +237,7 @@ func FindAllMentionsBytes(content []byte) []RefSpan {
ret := make([]RefSpan, 0, 5)
pos := 0
for {
match := mentionPattern.FindSubmatchIndex(content[pos:])
match := globalVars().mentionPattern.FindSubmatchIndex(content[pos:])
if match == nil {
break
}
@@ -238,7 +250,7 @@ func FindAllMentionsBytes(content []byte) []RefSpan {
// FindFirstMentionBytes matches the first mention in then given content
// and returns the location of the unvalidated username, including the @ prefix.
func FindFirstMentionBytes(content []byte) (bool, RefSpan) {
mention := mentionPattern.FindSubmatchIndex(content)
mention := globalVars().mentionPattern.FindSubmatchIndex(content)
if mention == nil {
return false, RefSpan{}
}
@@ -365,10 +377,10 @@ func FindRenderizableReferenceNumeric(content string, prOnly, crossLinkOnly bool
contentBytes := util.UnsafeStringToBytes(content)
var match []int
if !crossLinkOnly {
match = findValidReferenceIndex(issueNumericPattern, contentBytes, 0)
match = findValidReferenceIndex(globalVars().issueNumericPattern, contentBytes, 0)
}
if match == nil {
if match = findValidReferenceIndex(crossReferenceIssueNumericPattern, contentBytes, 0); match == nil {
if match = findValidReferenceIndex(globalVars().crossReferenceIssueNumericPattern, contentBytes, 0); match == nil {
return nil
}
}
@@ -390,7 +402,7 @@ func FindRenderizableReferenceNumeric(content string, prOnly, crossLinkOnly bool
// FindRenderizableCommitCrossReference returns the first unvalidated commit cross reference found in a string.
func FindRenderizableCommitCrossReference(content string) (bool, *RenderizableReference) {
m := crossReferenceCommitPattern.FindStringSubmatchIndex(content)
m := globalVars().crossReferenceCommitPattern.FindStringSubmatchIndex(content)
if len(m) < 8 {
return false, nil
}
@@ -436,7 +448,7 @@ func FindRenderizableReferenceRegexp(content string, pattern *regexp.Regexp) *Re
// FindRenderizableReferenceAlphanumeric returns the first alphanumeric unvalidated references found in a string.
func FindRenderizableReferenceAlphanumeric(content string) *RenderizableReference {
match := issueAlphanumericPattern.FindStringSubmatchIndex(content)
match := globalVars().issueAlphanumericPattern.FindStringSubmatchIndex(content)
if match == nil {
return nil
}
@@ -460,7 +472,7 @@ func findAllIssueReferencesBytes(content []byte, links []string, originalContent
// trailing spaces (\s#ref,\s), so if we get two consecutive references, the space
// from the second reference will be "eaten" by the first one:
// ...\s#ref1\s#ref2\s... --> ...`\s#ref1\s`, (not) `#ref2,\s...`
for _, pattern := range []*regexp.Regexp{issueNumericPattern, crossReferenceIssueNumericPattern} {
for _, pattern := range []*regexp.Regexp{globalVars().issueNumericPattern, globalVars().crossReferenceIssueNumericPattern} {
for match := findValidReferenceIndex(pattern, content, 0); match != nil; match = findValidReferenceIndex(pattern, content, match[3]) {
if ref := getCrossReference(content, match[2], match[3], false, false); ref != nil {
ret = append(ret, ref)
@@ -523,7 +535,7 @@ func findAllIssueReferencesBytes(content []byte, links []string, originalContent
pos := 0
for {
match := timeLogPattern.FindSubmatchIndex(content[pos:])
match := globalVars().timeLogPattern.FindSubmatchIndex(content[pos:])
if match == nil {
break
}
@@ -586,7 +598,7 @@ func getCrossReference(content []byte, start, end int, fromLink, prOnly bool) *r
return nil
}
owner, name := parts[0], parts[1]
if !validNamePattern.MatchString(owner) || !validNamePattern.MatchString(name) {
if !globalVars().validNamePattern.MatchString(owner) || !globalVars().validNamePattern.MatchString(name) {
return nil
}
action, location := findActionKeywords(content, start)
+6 -6
View File
@@ -483,11 +483,11 @@ func TestRegExp_mentionPattern(t *testing.T) {
}
for _, testCase := range trueTestCases {
found := mentionPattern.FindStringSubmatch(testCase.pat)
found := globalVars().mentionPattern.FindStringSubmatch(testCase.pat)
assert.Equal(t, testCase.exp, found[1])
}
for _, testCase := range falseTestCases {
res := mentionPattern.MatchString(testCase)
res := globalVars().mentionPattern.MatchString(testCase)
assert.False(t, res, "[%s] should be false", testCase)
}
}
@@ -515,10 +515,10 @@ func TestRegExp_issueNumericPattern(t *testing.T) {
}
for _, testCase := range trueTestCases {
assert.True(t, issueNumericPattern.MatchString(testCase))
assert.True(t, globalVars().issueNumericPattern.MatchString(testCase))
}
for _, testCase := range falseTestCases {
assert.False(t, issueNumericPattern.MatchString(testCase))
assert.False(t, globalVars().issueNumericPattern.MatchString(testCase))
}
}
@@ -552,10 +552,10 @@ func TestRegExp_issueAlphanumericPattern(t *testing.T) {
}
for _, testCase := range trueTestCases {
assert.True(t, issueAlphanumericPattern.MatchString(testCase))
assert.True(t, globalVars().issueAlphanumericPattern.MatchString(testCase))
}
for _, testCase := range falseTestCases {
assert.False(t, issueAlphanumericPattern.MatchString(testCase))
assert.False(t, globalVars().issueAlphanumericPattern.MatchString(testCase))
}
}
+11 -2
View File
@@ -9,6 +9,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"gitea.dev/modules/log"
)
@@ -20,7 +21,15 @@ const (
const escapeRegexpString = "_0[xX](([0-9a-fA-F][0-9a-fA-F])+)_"
var escapeRegex = regexp.MustCompile(escapeRegexpString)
var globalVars = sync.OnceValue(func() (ret struct {
escapeRegex *regexp.Regexp
extensionReg *regexp.Regexp
},
) {
ret.escapeRegex = regexp.MustCompile(escapeRegexpString)
ret.extensionReg = regexp.MustCompile(`^(\.[-\w]+)+$`)
return ret
})
func CollectEnvConfigKeys() (keys []string) {
for _, env := range os.Environ() {
@@ -47,7 +56,7 @@ func ClearEnvConfigKeys() {
func decodeEnvSectionKey(encoded string) (ok bool, section, key string) {
inKey := false
last := 0
escapeStringIndices := escapeRegex.FindAllStringIndex(encoded, -1)
escapeStringIndices := globalVars().escapeRegex.FindAllStringIndex(encoded, -1)
for _, unescapeIdx := range escapeStringIndices {
preceding := encoded[last:unescapeIdx[0]]
if !inKey {
+1 -6
View File
@@ -6,7 +6,6 @@ package setting
import (
"regexp"
"strings"
"sync"
"gitea.dev/modules/log"
"gitea.dev/modules/util"
@@ -222,14 +221,10 @@ func createMarkupSanitizerRule(name string, sec ConfigSection) (MarkupSanitizerR
return rule, true
}
var extensionReg = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile(`^(\.[-\w]+)+$`)
})
func fileExtensionsToPatterns(sectionName string, extensions []string) []string {
patterns := make([]string, 0, len(extensions))
for _, extension := range extensions {
if !extensionReg().MatchString(extension) {
if !globalVars().extensionReg.MatchString(extension) {
log.Warn("Config section %s file extension %s is invalid. Extension ignored", sectionName, extension)
} else {
patterns = append(patterns, "*"+extension)
+1 -1
View File
@@ -29,7 +29,7 @@ func TestSubjectBodySeparator(t *testing.T) {
{"Multiple\n---\n-------\n---\nSeparators", "Multiple\n", "\n-------\n---\nSeparators"},
{"Insufficient\n--\nSeparators", "", "Insufficient\n--\nSeparators"},
} {
loc := mailSubjectSplit.FindStringIndex(testCase.input)
loc := globalVars().mailSubjectSplit.FindStringIndex(testCase.input)
if loc == nil {
assert.Empty(t, testCase.subject, "no subject found, but one expected")
assert.Equal(t, testCase.body, testCase.input)
+29 -18
View File
@@ -14,6 +14,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
texttemplate "text/template"
@@ -81,10 +82,31 @@ type templateErrorPrettier struct {
assets *assetfs.LayeredFS
}
var reGenericTemplateError = regexp.MustCompile(`^template: (.*):([0-9]+): (.*)`)
var globalVars = sync.OnceValue(func() (ret struct {
reGenericTemplateError *regexp.Regexp
reFuncNotDefinedError *regexp.Regexp
reUnexpectedOperandError *regexp.Regexp
reExpectedEndError *regexp.Regexp
reTemplateExecutingError *regexp.Regexp
reTemplateExecutingErrorMsg *regexp.Regexp
mailSubjectSplit *regexp.Regexp
codeMatcher *regexp.Regexp
},
) {
ret.reGenericTemplateError = regexp.MustCompile(`^template: (.*):([0-9]+): (.*)`)
ret.reFuncNotDefinedError = regexp.MustCompile(`^template: (.*):([0-9]+): (function "(.*)" not defined)`)
ret.reUnexpectedOperandError = regexp.MustCompile(`^template: (.*):([0-9]+): (unexpected "(.*)" in operand)`)
ret.reExpectedEndError = regexp.MustCompile(`^template: (.*):([0-9]+): (expected end; found (.*))`)
ret.reTemplateExecutingError = regexp.MustCompile(`^template: (.*):([1-9][0-9]*):([1-9][0-9]*): (executing .*)`)
ret.reTemplateExecutingErrorMsg = regexp.MustCompile(`^executing "(.*)" at <(.*)>: `)
ret.mailSubjectSplit = regexp.MustCompile(`(?m)^-{3,}\s*$`)
// Match text that is between back ticks.
ret.codeMatcher = regexp.MustCompile("`([^`]+)`")
return ret
})
func (p *templateErrorPrettier) handleGenericTemplateError(err error) string {
groups := reGenericTemplateError.FindStringSubmatch(err.Error())
groups := globalVars().reGenericTemplateError.FindStringSubmatch(err.Error())
if len(groups) != 4 {
return ""
}
@@ -92,10 +114,8 @@ func (p *templateErrorPrettier) handleGenericTemplateError(err error) string {
return p.makeDetailedError(message, tmplName, lineStr, "", "")
}
var reFuncNotDefinedError = regexp.MustCompile(`^template: (.*):([0-9]+): (function "(.*)" not defined)`)
func (p *templateErrorPrettier) handleFuncNotDefinedError(err error) string {
groups := reFuncNotDefinedError.FindStringSubmatch(err.Error())
groups := globalVars().reFuncNotDefinedError.FindStringSubmatch(err.Error())
if len(groups) != 5 {
return ""
}
@@ -104,10 +124,8 @@ func (p *templateErrorPrettier) handleFuncNotDefinedError(err error) string {
return p.makeDetailedError(message, tmplName, lineStr, "", funcName)
}
var reUnexpectedOperandError = regexp.MustCompile(`^template: (.*):([0-9]+): (unexpected "(.*)" in operand)`)
func (p *templateErrorPrettier) handleUnexpectedOperandError(err error) string {
groups := reUnexpectedOperandError.FindStringSubmatch(err.Error())
groups := globalVars().reUnexpectedOperandError.FindStringSubmatch(err.Error())
if len(groups) != 5 {
return ""
}
@@ -116,10 +134,8 @@ func (p *templateErrorPrettier) handleUnexpectedOperandError(err error) string {
return p.makeDetailedError(message, tmplName, lineStr, "", unexpected)
}
var reExpectedEndError = regexp.MustCompile(`^template: (.*):([0-9]+): (expected end; found (.*))`)
func (p *templateErrorPrettier) handleExpectedEndError(err error) string {
groups := reExpectedEndError.FindStringSubmatch(err.Error())
groups := globalVars().reExpectedEndError.FindStringSubmatch(err.Error())
if len(groups) != 5 {
return ""
}
@@ -127,16 +143,11 @@ func (p *templateErrorPrettier) handleExpectedEndError(err error) string {
return p.makeDetailedError(message, tmplName, lineStr, "", unexpected)
}
var (
reTemplateExecutingError = regexp.MustCompile(`^template: (.*):([1-9][0-9]*):([1-9][0-9]*): (executing .*)`)
reTemplateExecutingErrorMsg = regexp.MustCompile(`^executing "(.*)" at <(.*)>: `)
)
func (p *templateErrorPrettier) handleTemplateRenderingError(err error) string {
if groups := reTemplateExecutingError.FindStringSubmatch(err.Error()); len(groups) > 0 {
if groups := globalVars().reTemplateExecutingError.FindStringSubmatch(err.Error()); len(groups) > 0 {
tmplName, lineStr, posStr, msgPart := groups[1], groups[2], groups[3], groups[4]
target := ""
if groups = reTemplateExecutingErrorMsg.FindStringSubmatch(msgPart); len(groups) > 0 {
if groups = globalVars().reTemplateExecutingErrorMsg.FindStringSubmatch(msgPart); len(groups) > 0 {
target = groups[2]
}
return p.makeDetailedError(msgPart, tmplName, lineStr, posStr, target)
+1 -4
View File
@@ -8,7 +8,6 @@ import (
"html/template"
"io"
"net/url"
"regexp"
"slices"
"strings"
"sync"
@@ -104,8 +103,6 @@ func mailBodyFuncMap() template.FuncMap {
}
}
var mailSubjectSplit = regexp.MustCompile(`(?m)^-{3,}\s*$`)
func newMailRenderer() (*MailRender, error) {
subjectTemplates := texttmpl.New("")
subjectTemplates.Funcs(mailSubjectTextFuncMap())
@@ -155,7 +152,7 @@ func newMailRenderer() (*MailRender, error) {
}
var subjectContent []byte
bodyContent := content
loc := mailSubjectSplit.FindIndex(content)
loc := globalVars().mailSubjectSplit.FindIndex(content)
if loc != nil {
subjectContent, bodyContent = content[0:loc[0]], content[loc[1]:]
}
+1 -5
View File
@@ -9,7 +9,6 @@ import (
"html/template"
"math"
"net/url"
"regexp"
"slices"
"strings"
@@ -78,12 +77,9 @@ func (ut *RenderUtils) RenderCommitBody(msg string, repo *repo.Repository) templ
return renderedMessage
}
// Match text that is between back ticks.
var codeMatcher = regexp.MustCompile("`([^`]+)`")
// renderCodeBlock renders "`…`" as highlighted "<code>" block, intended for issue and PR titles
func renderCodeBlock(htmlEscapedTextToRender template.HTML) template.HTML {
htmlWithCodeTags := codeMatcher.ReplaceAllString(string(htmlEscapedTextToRender), `<code class="inline-code-block">$1</code>`) // replace with HTML <code> tags
htmlWithCodeTags := globalVars().codeMatcher.ReplaceAllString(string(htmlEscapedTextToRender), `<code class="inline-code-block">$1</code>`) // replace with HTML <code> tags
return template.HTML(htmlWithCodeTags)
}