diff options
author | 6543 <6543@obermui.de> | 2024-10-29 15:43:47 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-29 15:43:47 +0100 |
commit | 5d43801b72790ce5862aefdc4520edb06bb4cbba (patch) | |
tree | 1e9500720e7f763fdfd69dcad7d6f9fbde0622dc | |
parent | b7fb20e73e63b8edc9b90c52073e248bef428fcc (diff) | |
download | gitea-5d43801b72790ce5862aefdc4520edb06bb4cbba.tar.gz gitea-5d43801b72790ce5862aefdc4520edb06bb4cbba.zip |
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*
-rw-r--r-- | models/git/protected_branch.go | 22 | ||||
-rw-r--r-- | models/git/protected_branch_list_test.go (renamed from models/git/protected_banch_list_test.go) | 29 |
2 files changed, 43 insertions, 8 deletions
diff --git a/models/git/protected_branch.go b/models/git/protected_branch.go index 0033a42d4e..37d933a982 100644 --- a/models/git/protected_branch.go +++ b/models/git/protected_branch.go @@ -84,14 +84,20 @@ func IsRuleNameSpecial(ruleName string) bool { } func (protectBranch *ProtectedBranch) loadGlob() { - if protectBranch.globRule == nil { - 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), '/') - } - protectBranch.isPlainName = !IsRuleNameSpecial(protectBranch.RuleName) + if protectBranch.isPlainName || protectBranch.globRule != nil { + return + } + // detect if it is not glob + if !IsRuleNameSpecial(protectBranch.RuleName) { + protectBranch.isPlainName = true + return + } + // 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), '/') } } diff --git a/models/git/protected_banch_list_test.go b/models/git/protected_branch_list_test.go index 4bb3136d58..94a48f37e6 100644 --- a/models/git/protected_banch_list_test.go +++ b/models/git/protected_branch_list_test.go @@ -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) +} |