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
+42 -18
View File
@@ -10,6 +10,7 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"gitea.dev/modules/regexplru"
"gitea.dev/modules/setting" "gitea.dev/modules/setting"
"gitea.dev/modules/util" "gitea.dev/modules/util"
) )
@@ -70,43 +71,66 @@ func IsErrorCanceledOrKilled(err error) bool {
return errors.Is(err, context.Canceled) || IsErrorSignalKilled(err) return errors.Is(err, context.Canceled) || IsErrorSignalKilled(err)
} }
type StderrCheck interface {
internalOnly()
}
type ( type (
StderrPrefix string StderrPrefix string
StderrWildcard string StderrRegexp string
) )
func (StderrPrefix) internalOnly() {}
func (StderrRegexp) internalOnly() {}
const ( const (
StderrNotValidObjectName StderrPrefix = "fatal: not a valid object name" StderrNotValidObjectName StderrPrefix = "fatal: not a valid object name"
StderrNotTreeObject StderrPrefix = "fatal: not a tree object" StderrNotTreeObject StderrPrefix = "fatal: not a tree object"
StderrPathSpec StderrPrefix = "fatal: pathspec" StderrPathSpec StderrPrefix = "fatal: pathspec"
StderrBadRevision StderrPrefix = "fatal: bad revision" StderrBadRevision StderrPrefix = "fatal: bad revision"
StderrNoSuchPath StderrPrefix = "fatal: no such path"
StderrNoSuchRemote1 StderrPrefix = "fatal: no such remote" // git < 2.30, exit status 128 StderrNoSuchRemote1 StderrPrefix = "fatal: no such remote" // git < 2.30, exit status 128
StderrNoSuchRemote2 StderrPrefix = "error: no such remote" // git >= 2.30. exit status 2 StderrNoSuchRemote2 StderrPrefix = "error: no such remote" // git >= 2.30. exit status 2
StderrUnknownRevisionOrPath StderrWildcard = "fatal: *: unknown revision or path not in the working tree" StderrUnknownRevisionOrPath StderrRegexp = "^fatal: .*: unknown revision or path not in the working tree"
StderrNoMergeBase StderrWildcard = "fatal: *: no merge base" StderrNoMergeBase StderrRegexp = "^fatal: .*: no merge base"
StderrFileNoEnoughLines StderrRegexp = `^fatal: file .* has only \d+ lines?`
) )
func IsStderr[T StderrPrefix | StderrWildcard](err error, check T) bool { func matchStderrCheck(stderr string, checkIntf StderrCheck) (match bool) {
switch check := any(checkIntf).(type) {
case StderrPrefix:
checkLen := len(check)
if len(stderr) >= checkLen {
// Git is lowercasing the "fatal: Not a valid object name" error message
// ref: https://lore.kernel.org/git/pull.2052.git.1771836302101.gitgitgadget@gmail.com
match = util.AsciiEqualFold(stderr[:checkLen], string(check))
}
case StderrRegexp:
re, err := regexplru.SystemCache().GetCompiled(string(check))
if err != nil {
setting.PanicInDevOrTesting("invalid stderr regexp %s", check)
} else {
match = re.MatchString(stderr)
}
default:
setting.PanicInDevOrTesting("invalid stderr type %T", checkIntf)
}
return match
}
func IsStderr(err error, checks ...StderrCheck) bool {
stderr, ok := ErrorAsStderr(err) stderr, ok := ErrorAsStderr(err)
if !ok { if !ok {
return false return false
} }
checkLen := len(check)
if len(stderr) < checkLen { for _, checkIntf := range checks {
return false if matchStderrCheck(stderr, checkIntf) {
return true
}
} }
switch any(check).(type) {
case StderrPrefix:
// Git is lowercasing the "fatal: Not a valid object name" error message
// ref: https://lore.kernel.org/git/pull.2052.git.1771836302101.gitgitgadget@gmail.com
return util.AsciiEqualFold(stderr[:checkLen], string(check))
case StderrWildcard:
prefix, remaining, _ := strings.Cut(string(check), "*")
return strings.HasPrefix(stderr, prefix) && strings.Contains(stderr, remaining)
}
setting.PanicInDevOrTesting("invalid stderr type %T", check)
return false return false
} }
+2 -1
View File
@@ -11,11 +11,12 @@ import (
func TestIsStderr(t *testing.T) { func TestIsStderr(t *testing.T) {
cases := []struct { cases := []struct {
check StderrWildcard check StderrCheck
stderr string stderr string
}{ }{
{StderrUnknownRevisionOrPath, "fatal: ambiguous argument 'origin': unknown revision or path not in the working tree...."}, {StderrUnknownRevisionOrPath, "fatal: ambiguous argument 'origin': unknown revision or path not in the working tree...."},
{StderrNoMergeBase, "fatal: origin/main..HEAD: no merge base...."}, {StderrNoMergeBase, "fatal: origin/main..HEAD: no merge base...."},
{StderrFileNoEnoughLines, "fatal: file foo/bar has only 1 line"},
} }
for _, tc := range cases { for _, tc := range cases {
assert.True(t, IsStderr(&runStdError{stderr: tc.stderr}, tc.check), "stderr: %s", tc.stderr) assert.True(t, IsStderr(&runStdError{stderr: tc.stderr}, tc.check), "stderr: %s", tc.stderr)
+1 -1
View File
@@ -123,7 +123,7 @@ func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
case IssueNameStyleAlphanumeric: case IssueNameStyleAlphanumeric:
ref = references.FindRenderizableReferenceAlphanumeric(node.Data) ref = references.FindRenderizableReferenceAlphanumeric(node.Data)
case IssueNameStyleRegexp: case IssueNameStyleRegexp:
pattern, err := regexplru.GetCompiled(ctx.RenderOptions.Metas["regexp"]) pattern, err := regexplru.UserCache().GetCompiled(ctx.RenderOptions.Metas["regexp"])
if err != nil { if err != nil {
return return
} }
+22 -25
View File
@@ -5,40 +5,37 @@ package regexplru
import ( import (
"regexp" "regexp"
"sync"
"gitea.dev/modules/log"
lru "github.com/hashicorp/golang-lru/v2" lru "github.com/hashicorp/golang-lru/v2"
) )
var lruCache *lru.Cache[string, any] type lruItem struct {
regexp *regexp.Regexp
err error
}
func init() { type RegexpCache struct {
var err error lruCache *lru.Cache[string, *lruItem]
lruCache, err = lru.New[string, any](1000) }
if err != nil {
log.Fatal("failed to new LRU cache, err: %v", err) 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 // GetCompiled works like regexp.Compile, the compiled expr or error is stored in LRU cache
func GetCompiled(expr string) (r *regexp.Regexp, err error) { func (regexpCache *RegexpCache) GetCompiled(expr string) (r *regexp.Regexp, err error) {
v, ok := lruCache.Get(expr) v, ok := regexpCache.lruCache.Get(expr)
if !ok { if !ok {
r, err = regexp.Compile(expr) r, err = regexp.Compile(expr)
if err != nil { regexpCache.lruCache.Add(expr, &lruItem{regexp: r, err: err})
lruCache.Add(expr, err) return r, 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")
}
} }
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) { func TestRegexpLru(t *testing.T) {
r, err := GetCompiled("a") r, err := UserCache().GetCompiled("a")
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, r.MatchString("a")) assert.True(t, r.MatchString("a"))
r, err = GetCompiled("a") r, err = UserCache().GetCompiled("a")
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, r.MatchString("a")) assert.True(t, r.MatchString("a"))
assert.Equal(t, 1, UserCache().lruCache.Len())
assert.Equal(t, 1, lruCache.Len()) _, err = UserCache().GetCompiled("(")
_, err = GetCompiled("(")
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, 2, lruCache.Len()) assert.Equal(t, 2, UserCache().lruCache.Len())
} }
+1 -8
View File
@@ -8,7 +8,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"strings"
"gitea.dev/models/db" "gitea.dev/models/db"
issues_model "gitea.dev/models/issues" issues_model "gitea.dev/models/issues"
@@ -25,13 +24,7 @@ import (
) )
func isErrBlameNotFoundOrNotEnoughLines(err error) bool { func isErrBlameNotFoundOrNotEnoughLines(err error) bool {
stdErr, ok := gitcmd.ErrorAsStderr(err) return gitcmd.IsStderr(err, gitcmd.StderrNoSuchPath, gitcmd.StderrFileNoEnoughLines)
if !ok {
return false
}
notFound := strings.HasPrefix(stdErr, "fatal: no such path")
notEnoughLines := strings.HasPrefix(stdErr, "fatal: file ") && strings.Contains(stdErr, " has only ") && strings.Contains(stdErr, " lines?")
return notFound || notEnoughLines
} }
// ErrDismissRequestOnClosedPR represents an error when a user tries to dismiss a review associated to a closed or merged PR. // ErrDismissRequestOnClosedPR represents an error when a user tries to dismiss a review associated to a closed or merged PR.