初始提交: Gitea 项目代码

This commit is contained in:
root
2026-05-30 22:47:36 +08:00
commit f288f76350
6116 changed files with 776822 additions and 0 deletions
+243
View File
@@ -0,0 +1,243 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package glob
import (
"errors"
"fmt"
"regexp"
"slices"
"strings"
"gitea.dev/modules/util"
)
// Reference: https://github.com/gobwas/glob/blob/master/glob.go
type Glob interface {
Match(string) bool
}
type globCompiler struct {
regexpQuestion bool
regexpPlus bool
superWildcardRight bool
supportNegative bool
separators []rune
nonSeparatorChars string
globPattern []rune
regexpPattern string
regexp *regexp.Regexp
builder *strings.Builder
pos int
negativeFlip bool
}
// compileChars compiles character class patterns like [abc] or [!abc]
func (g *globCompiler) compileChars() error {
g.builder.WriteByte('[')
if g.pos < len(g.globPattern) && g.globPattern[g.pos] == '!' {
g.pos++
g.builder.WriteByte('^')
}
for g.pos < len(g.globPattern) {
c := g.globPattern[g.pos]
g.pos++
if c == ']' {
g.builder.WriteByte(']')
return nil
}
if c == '\\' {
if g.pos >= len(g.globPattern) {
return errors.New("unterminated character class escape")
}
g.builder.WriteByte('\\')
g.builder.WriteRune(g.globPattern[g.pos])
g.pos++
} else {
g.builder.WriteRune(c)
}
}
return errors.New("unterminated character class")
}
// compile compiles the glob pattern into a regular expression
func (g *globCompiler) compile(subPattern bool) error {
for g.pos < len(g.globPattern) {
c := g.globPattern[g.pos]
g.pos++
if subPattern && c == '}' {
g.builder.WriteByte(')')
return nil
}
switch c {
case '*':
if g.pos < len(g.globPattern) && g.globPattern[g.pos] == '*' {
var matchRightSep bool
if g.superWildcardRight {
// check "**/" pattern, then the wildcards should also match the right separator
// e.g.: "**/docs" should match "docs"
var rightRune rune
if g.pos+1 < len(g.globPattern) {
rightRune = g.globPattern[g.pos+1]
}
if slices.Contains(g.separators, rightRune) {
matchRightSep = g.pos-2 < 0 || g.globPattern[g.pos-2] == rightRune
}
}
if matchRightSep {
g.pos += 2
} else {
g.pos++
}
g.builder.WriteString(".*") // match any sequence of characters
} else {
g.builder.WriteString(g.nonSeparatorChars)
g.builder.WriteByte('*') // match any sequence of non-separator characters
}
case '?':
if g.regexpQuestion {
g.builder.WriteByte('?')
} else {
g.builder.WriteString(g.nonSeparatorChars) // match any single non-separator character
}
case '+':
if g.regexpPlus {
g.builder.WriteByte('+')
} else {
g.builder.WriteByte('\\')
g.builder.WriteRune(c)
}
case '[':
if err := g.compileChars(); err != nil {
return err
}
case '{':
g.builder.WriteByte('(')
if err := g.compile(true); err != nil {
return err
}
case ',':
if subPattern {
g.builder.WriteByte('|')
} else {
g.builder.WriteByte(',')
}
case '\\':
if g.pos >= len(g.globPattern) {
return errors.New("no character to escape")
}
g.builder.WriteByte('\\')
g.builder.WriteRune(g.globPattern[g.pos])
g.pos++
case '.', '^', '$', '(', ')', '|':
g.builder.WriteByte('\\')
g.builder.WriteRune(c) // escape regexp special characters
default:
g.builder.WriteRune(c)
}
}
return nil
}
func initGlobCompiler(g *globCompiler, pattern string, separators []rune) (Glob, error) {
g.globPattern = []rune(pattern)
g.separators = separators
g.builder = new(strings.Builder)
// Escape separators for use in character class
escapedSeparators := regexp.QuoteMeta(string(separators))
if escapedSeparators != "" {
g.nonSeparatorChars = "[^" + escapedSeparators + "]"
} else {
g.nonSeparatorChars = "."
}
if g.supportNegative && len(g.globPattern) > 0 && g.globPattern[0] == '!' {
g.negativeFlip = true
g.pos++
}
g.builder.WriteByte('^')
if err := g.compile(false); err != nil {
return nil, err
}
g.builder.WriteByte('$')
g.regexpPattern = g.builder.String()
g.builder = nil
regex, err := regexp.Compile(g.regexpPattern)
if err != nil {
return nil, fmt.Errorf("failed to compile regexp: %w", err)
}
g.regexp = regex
return g, nil
}
func (g *globCompiler) Match(s string) bool {
ret := g.regexp.MatchString(s)
if g.negativeFlip {
ret = !ret
}
return ret
}
func Compile(pattern string, separators ...rune) (Glob, error) {
return initGlobCompiler(&globCompiler{}, pattern, separators)
}
func CompileWorkflow(pattern string) (Glob, error) {
return initGlobCompiler(&globCompiler{
regexpQuestion: true,
regexpPlus: true,
superWildcardRight: true,
supportNegative: true,
}, pattern, []rune{'/'})
}
func MustCompile(pattern string, separators ...rune) Glob {
g, err := Compile(pattern, separators...)
if err != nil {
panic(err)
}
return g
}
func IsSpecialByte(c byte) bool {
return c == '*' || c == '?' || c == '\\' || c == '[' || c == ']' || c == '{' || c == '}'
}
// QuoteMeta returns a string that quotes all glob pattern meta characters
// inside the argument text; For example, QuoteMeta(`{foo*}`) returns `\[foo\*\]`.
// Reference: https://github.com/gobwas/glob/blob/master/glob.go
func QuoteMeta(s string) string {
pos := 0
for pos < len(s) && !IsSpecialByte(s[pos]) {
pos++
}
if pos == len(s) {
return s
}
b := make([]byte, pos+2*(len(s)-pos))
copy(b, s[0:pos])
to := pos
for ; pos < len(s); pos++ {
if IsSpecialByte(s[pos]) {
b[to] = '\\'
to++
}
b[to] = s[pos]
to++
}
return util.UnsafeBytesToString(b[0:to])
}
+208
View File
@@ -0,0 +1,208 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// Copyright (c) 2016 Sergey Kamardin
// SPDX-License-Identifier: MIT
//
//nolint:revive // the code is from gobwas/glob
package glob
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Reference: https://github.com/gobwas/glob/blob/master/glob_test.go
const (
pattern_all = "[a-z][!a-x]*cat*[h][!b]*eyes*"
regexp_all = `^[a-z][^a-x].*cat.*[h][^b].*eyes.*$`
fixture_all_match = "my cat has very bright eyes"
fixture_all_mismatch = "my dog has very bright eyes"
pattern_plain = "google.com"
regexp_plain = `^google\.com$`
fixture_plain_match = "google.com"
fixture_plain_mismatch = "gobwas.com"
pattern_multiple = "https://*.google.*"
regexp_multiple = `^https:\/\/.*\.google\..*$`
fixture_multiple_match = "https://account.google.com"
fixture_multiple_mismatch = "https://google.com"
pattern_alternatives = "{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}"
regexp_alternatives = `^(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)$`
fixture_alternatives_match = "http://yahoo.com"
fixture_alternatives_mismatch = "http://google.com"
pattern_alternatives_suffix = "{https://*gobwas.com,http://exclude.gobwas.com}"
regexp_alternatives_suffix = `^(https:\/\/.*gobwas\.com|http://exclude.gobwas.com)$`
fixture_alternatives_suffix_first_match = "https://safe.gobwas.com"
fixture_alternatives_suffix_first_mismatch = "http://safe.gobwas.com"
fixture_alternatives_suffix_second = "http://exclude.gobwas.com"
pattern_prefix = "abc*"
regexp_prefix = `^abc.*$`
pattern_suffix = "*def"
regexp_suffix = `^.*def$`
pattern_prefix_suffix = "ab*ef"
regexp_prefix_suffix = `^ab.*ef$`
fixture_prefix_suffix_match = "abcdef"
fixture_prefix_suffix_mismatch = "af"
pattern_alternatives_combine_lite = "{abc*def,abc?def,abc[zte]def}"
regexp_alternatives_combine_lite = `^(abc.*def|abc.def|abc[zte]def)$`
fixture_alternatives_combine_lite = "abczdef"
pattern_alternatives_combine_hard = "{abc*[a-c]def,abc?[d-g]def,abc[zte]?def}"
regexp_alternatives_combine_hard = `^(abc.*[a-c]def|abc.[d-g]def|abc[zte].def)$`
fixture_alternatives_combine_hard = "abczqdef"
)
type test struct {
pattern, match string
should bool
delimiters []rune
}
func glob(s bool, p, m string, d ...rune) test {
return test{p, m, s, d}
}
func TestGlob(t *testing.T) {
for _, test := range []test{
glob(true, "* ?at * eyes", "my cat has very bright eyes"),
glob(true, "", ""),
glob(false, "", "b"),
glob(true, "*ä", "åä"),
glob(true, "abc", "abc"),
glob(true, "a*c", "abc"),
glob(true, "a*c", "a12345c"),
glob(true, "a?c", "a1c"),
glob(true, "a.b", "a.b", '.'),
glob(true, "a.*", "a.b", '.'),
glob(true, "a.**", "a.b.c", '.'),
glob(true, "a.?.c", "a.b.c", '.'),
glob(true, "a.?.?", "a.b.c", '.'),
glob(true, "?at", "cat"),
glob(true, "?at", "fat"),
glob(true, "*", "abc"),
glob(true, `\*`, "*"),
glob(true, "**", "a.b.c", '.'),
glob(false, "?at", "at"),
glob(false, "?at", "fat", 'f'),
glob(false, "a.*", "a.b.c", '.'),
glob(false, "a.?.c", "a.bb.c", '.'),
glob(false, "*", "a.b.c", '.'),
glob(true, "*test", "this is a test"),
glob(true, "this*", "this is a test"),
glob(true, "*is *", "this is a test"),
glob(true, "*is*a*", "this is a test"),
glob(true, "**test**", "this is a test"),
glob(true, "**is**a***test*", "this is a test"),
glob(false, "*is", "this is a test"),
glob(false, "*no*", "this is a test"),
glob(true, "[!a]*", "this is a test3"),
glob(true, "*abc", "abcabc"),
glob(true, "**abc", "abcabc"),
glob(true, "???", "abc"),
glob(true, "?*?", "abc"),
glob(true, "?*?", "ac"),
glob(false, "sta", "stagnation"),
glob(true, "sta*", "stagnation"),
glob(false, "sta?", "stagnation"),
glob(false, "sta?n", "stagnation"),
glob(true, "{abc,def}ghi", "defghi"),
glob(true, "{abc,abcd}a", "abcda"),
glob(true, "{a,ab}{bc,f}", "abc"),
glob(true, "{*,**}{a,b}", "ab"),
glob(false, "{*,**}{a,b}", "ac"),
glob(true, "/{rate,[a-z][a-z][a-z]}*", "/rate"),
glob(true, "/{rate,[0-9][0-9][0-9]}*", "/rate"),
glob(true, "/{rate,[a-z][a-z][a-z]}*", "/usd"),
glob(true, "{*.google.*,*.yandex.*}", "www.google.com", '.'),
glob(true, "{*.google.*,*.yandex.*}", "www.yandex.com", '.'),
glob(false, "{*.google.*,*.yandex.*}", "yandex.com", '.'),
glob(false, "{*.google.*,*.yandex.*}", "google.com", '.'),
glob(true, "{*.google.*,yandex.*}", "www.google.com", '.'),
glob(true, "{*.google.*,yandex.*}", "yandex.com", '.'),
glob(false, "{*.google.*,yandex.*}", "www.yandex.com", '.'),
glob(false, "{*.google.*,yandex.*}", "google.com", '.'),
glob(true, "*//{,*.}example.com", "https://www.example.com"),
glob(true, "*//{,*.}example.com", "http://example.com"),
glob(false, "*//{,*.}example.com", "http://example.com.net"),
glob(true, pattern_all, fixture_all_match),
glob(false, pattern_all, fixture_all_mismatch),
glob(true, pattern_plain, fixture_plain_match),
glob(false, pattern_plain, fixture_plain_mismatch),
glob(true, pattern_multiple, fixture_multiple_match),
glob(false, pattern_multiple, fixture_multiple_mismatch),
glob(true, pattern_alternatives, fixture_alternatives_match),
glob(false, pattern_alternatives, fixture_alternatives_mismatch),
glob(true, pattern_alternatives_suffix, fixture_alternatives_suffix_first_match),
glob(false, pattern_alternatives_suffix, fixture_alternatives_suffix_first_mismatch),
glob(true, pattern_alternatives_suffix, fixture_alternatives_suffix_second),
glob(true, pattern_alternatives_combine_hard, fixture_alternatives_combine_hard),
glob(true, pattern_alternatives_combine_lite, fixture_alternatives_combine_lite),
glob(true, pattern_prefix, fixture_prefix_suffix_match),
glob(false, pattern_prefix, fixture_prefix_suffix_mismatch),
glob(true, pattern_suffix, fixture_prefix_suffix_match),
glob(false, pattern_suffix, fixture_prefix_suffix_mismatch),
glob(true, pattern_prefix_suffix, fixture_prefix_suffix_match),
glob(false, pattern_prefix_suffix, fixture_prefix_suffix_mismatch),
} {
g, err := Compile(test.pattern, test.delimiters...)
require.NoError(t, err)
result := g.Match(test.match)
assert.Equal(t, test.should, result, "pattern %q matching %q should be %v but got %v, compiled=%s", test.pattern, test.match, test.should, result, g.(*globCompiler).regexpPattern)
}
}
func TestQuoteMeta(t *testing.T) {
for id, test := range []struct {
in, out string
}{
{
in: `[foo*]`,
out: `\[foo\*\]`,
},
{
in: `{foo*}`,
out: `\{foo\*\}`,
},
{
in: `*?\[]{}`,
out: `\*\?\\\[\]\{\}`,
},
{
in: `some text and *?\[]{}`,
out: `some text and \*\?\\\[\]\{\}`,
},
} {
act := QuoteMeta(test.in)
assert.Equal(t, test.out, act, "QuoteMeta(%q)", test.in)
_, err := Compile(act)
assert.NoError(t, err, "#%d _, err := Compile(QuoteMeta(%q) = %q); err = %q", id, test.in, act, err)
}
}