Skip to content

Commit 64e6bdb

Browse files
committed
Let AppendRepoUnitList and Range handle nil
1 parent 5a28e43 commit 64e6bdb

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

models/repo_permission.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func getUserRepoPermission(e Engine, repo *Repository, user *User) (perm Permiss
269269
for t := range perm.UnitsMode {
270270
repo.Units.Range(func(_ int, u *RepoUnit) bool {
271271
if u.Type == t {
272-
perm.Units.Append(u)
272+
perm.Units = AppendRepoUnitList(perm.Units, u)
273273
}
274274
return true
275275
})

models/repo_unit_list.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@ func NewRepoUnitList(us []*RepoUnit) *RepoUnitList {
1515
}
1616
}
1717

18+
// AppendRepoUnitList appends one or more elements to the list
19+
func AppendRepoUnitList(l *RepoUnitList, us ...*RepoUnit) *RepoUnitList {
20+
if l != nil {
21+
l.Lock()
22+
defer l.Unlock()
23+
l.list = append(l.list, us...)
24+
return l
25+
}
26+
return NewRepoUnitList(us)
27+
}
28+
1829
// Load reads i-th element from the list
1930
func (l *RepoUnitList) Load(i int) *RepoUnit {
2031
l.RLock()
2132
defer l.RUnlock()
2233
return l.list[i]
2334
}
2435

25-
// Append appends a element to the list
26-
func (l *RepoUnitList) Append(u *RepoUnit) {
27-
l.Lock()
28-
defer l.Unlock()
29-
l.list = append(l.list, u)
30-
}
31-
3236
// Len returns the length of the list
3337
func (l *RepoUnitList) Len() int {
3438
l.RLock()
@@ -38,6 +42,9 @@ func (l *RepoUnitList) Len() int {
3842

3943
// Range iterates through the elements of the list like sync.Map.Range.
4044
func (l *RepoUnitList) Range(f func(i int, u *RepoUnit) bool) {
45+
if l == nil {
46+
return
47+
}
4148

4249
l.RLock()
4350
for i, v := range l.list {

0 commit comments

Comments
 (0)