mirror of
https://github.com/go-gitea/gitea.git
synced 2024-11-24 23:56:36 +08:00
Optimize branch protection rule loading (#32280)
before if it was nonglob each load would try to glob it and the check that is not glob ... now we only do that once and no future loading will trigger it --- *Sponsored by Kithara Software GmbH*
This commit is contained in:
parent
b7fb20e73e
commit
5d43801b72
|
@ -84,14 +84,20 @@ func IsRuleNameSpecial(ruleName string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (protectBranch *ProtectedBranch) loadGlob() {
|
func (protectBranch *ProtectedBranch) loadGlob() {
|
||||||
if protectBranch.globRule == nil {
|
if protectBranch.isPlainName || protectBranch.globRule != nil {
|
||||||
var err error
|
return
|
||||||
protectBranch.globRule, err = glob.Compile(protectBranch.RuleName, '/')
|
}
|
||||||
if err != nil {
|
// detect if it is not glob
|
||||||
log.Warn("Invalid glob rule for ProtectedBranch[%d]: %s %v", protectBranch.ID, protectBranch.RuleName, err)
|
if !IsRuleNameSpecial(protectBranch.RuleName) {
|
||||||
protectBranch.globRule = glob.MustCompile(glob.QuoteMeta(protectBranch.RuleName), '/')
|
protectBranch.isPlainName = true
|
||||||
}
|
return
|
||||||
protectBranch.isPlainName = !IsRuleNameSpecial(protectBranch.RuleName)
|
}
|
||||||
|
// now we load the glob
|
||||||
|
var err error
|
||||||
|
protectBranch.globRule, err = glob.Compile(protectBranch.RuleName, '/')
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("Invalid glob rule for ProtectedBranch[%d]: %s %v", protectBranch.ID, protectBranch.RuleName, err)
|
||||||
|
protectBranch.globRule = glob.MustCompile(glob.QuoteMeta(protectBranch.RuleName), '/')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,3 +74,32 @@ func TestBranchRuleMatchPriority(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBranchRuleSort(t *testing.T) {
|
||||||
|
in := []*ProtectedBranch{{
|
||||||
|
RuleName: "b",
|
||||||
|
CreatedUnix: 1,
|
||||||
|
}, {
|
||||||
|
RuleName: "b/*",
|
||||||
|
CreatedUnix: 3,
|
||||||
|
}, {
|
||||||
|
RuleName: "a/*",
|
||||||
|
CreatedUnix: 2,
|
||||||
|
}, {
|
||||||
|
RuleName: "c",
|
||||||
|
CreatedUnix: 0,
|
||||||
|
}, {
|
||||||
|
RuleName: "a",
|
||||||
|
CreatedUnix: 4,
|
||||||
|
}}
|
||||||
|
expect := []string{"c", "b", "a", "a/*", "b/*"}
|
||||||
|
|
||||||
|
pbr := ProtectedBranchRules(in)
|
||||||
|
pbr.sort()
|
||||||
|
|
||||||
|
var got []string
|
||||||
|
for i := range pbr {
|
||||||
|
got = append(got, pbr[i].RuleName)
|
||||||
|
}
|
||||||
|
assert.Equal(t, expect, got)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user