fix: correct stdErr match in isErrBlameNotFoundOrNotEnoughLines (#39309)

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Abhay Pratap Singh
2026-09-14 14:59:18 +00:00
committed by GitHub
co-authored by wxiaoguang
parent 13033827b1
commit 85cbf477e5
6 changed files with 73 additions and 59 deletions
+22 -25
View File
@@ -5,40 +5,37 @@ package regexplru
import (
"regexp"
"gitea.dev/modules/log"
"sync"
lru "github.com/hashicorp/golang-lru/v2"
)
var lruCache *lru.Cache[string, any]
type lruItem struct {
regexp *regexp.Regexp
err error
}
func init() {
var err error
lruCache, err = lru.New[string, any](1000)
if err != nil {
log.Fatal("failed to new LRU cache, err: %v", err)
}
type RegexpCache struct {
lruCache *lru.Cache[string, *lruItem]
}
func NewCache(size int) *RegexpCache {
lruCache, _ := lru.New[string, *lruItem](size)
return &RegexpCache{lruCache: lruCache}
}
// GetCompiled works like regexp.Compile, the compiled expr or error is stored in LRU cache
func GetCompiled(expr string) (r *regexp.Regexp, err error) {
v, ok := lruCache.Get(expr)
func (regexpCache *RegexpCache) GetCompiled(expr string) (r *regexp.Regexp, err error) {
v, ok := regexpCache.lruCache.Get(expr)
if !ok {
r, err = regexp.Compile(expr)
if err != nil {
lruCache.Add(expr, err)
return nil, err
}
lruCache.Add(expr, r)
} else {
r, ok = v.(*regexp.Regexp)
if !ok {
if err, ok = v.(error); ok {
return nil, err
}
panic("impossible")
}
regexpCache.lruCache.Add(expr, &lruItem{regexp: r, err: err})
return r, err
}
return r, nil
return v.regexp, v.err
}
var (
UserCache = sync.OnceValue(func() *RegexpCache { return NewCache(1000) })
SystemCache = sync.OnceValue(func() *RegexpCache { return NewCache(1000) })
)
+5 -6
View File
@@ -10,17 +10,16 @@ import (
)
func TestRegexpLru(t *testing.T) {
r, err := GetCompiled("a")
r, err := UserCache().GetCompiled("a")
assert.NoError(t, err)
assert.True(t, r.MatchString("a"))
r, err = GetCompiled("a")
r, err = UserCache().GetCompiled("a")
assert.NoError(t, err)
assert.True(t, r.MatchString("a"))
assert.Equal(t, 1, UserCache().lruCache.Len())
assert.Equal(t, 1, lruCache.Len())
_, err = GetCompiled("(")
_, err = UserCache().GetCompiled("(")
assert.Error(t, err)
assert.Equal(t, 2, lruCache.Len())
assert.Equal(t, 2, UserCache().lruCache.Len())
}