Skip to content

Commit d9ef43a

Browse files
KN4CK3R6543
andauthored
Replace list.List with slices (#16311)
* Replaced list with slice. * Fixed usage of pointer to temporary variable. * Replaced LIFO list with slice. * Lint * Removed type check. * Removed duplicated code. * Lint * Fixed merge. Co-authored-by: 6543 <[email protected]>
1 parent 23d438f commit d9ef43a

29 files changed

+185
-304
lines changed

models/commit.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2021 Gitea. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package models
6+
7+
import (
8+
"code.gitea.io/gitea/modules/git"
9+
)
10+
11+
// ConvertFromGitCommit converts git commits into SignCommitWithStatuses
12+
func ConvertFromGitCommit(commits []*git.Commit, repo *Repository) []*SignCommitWithStatuses {
13+
return ParseCommitsWithStatus(
14+
ParseCommitsWithSignature(
15+
ValidateCommitsWithEmails(commits),
16+
repo,
17+
),
18+
repo,
19+
)
20+
}

models/commit_status.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package models
66

77
import (
8-
"container/list"
98
"crypto/sha1"
109
"fmt"
1110
"strings"
@@ -257,16 +256,12 @@ type SignCommitWithStatuses struct {
257256
}
258257

259258
// ParseCommitsWithStatus checks commits latest statuses and calculates its worst status state
260-
func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List {
261-
var (
262-
newCommits = list.New()
263-
e = oldCommits.Front()
264-
)
265-
266-
for e != nil {
267-
c := e.Value.(SignCommit)
268-
commit := SignCommitWithStatuses{
269-
SignCommit: &c,
259+
func ParseCommitsWithStatus(oldCommits []*SignCommit, repo *Repository) []*SignCommitWithStatuses {
260+
newCommits := make([]*SignCommitWithStatuses, 0, len(oldCommits))
261+
262+
for _, c := range oldCommits {
263+
commit := &SignCommitWithStatuses{
264+
SignCommit: c,
270265
}
271266
statuses, err := GetLatestCommitStatus(repo.ID, commit.ID.String(), ListOptions{})
272267
if err != nil {
@@ -276,8 +271,7 @@ func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List
276271
commit.Status = CalcCommitStatus(statuses)
277272
}
278273

279-
newCommits.PushBack(commit)
280-
e = e.Next()
274+
newCommits = append(newCommits, commit)
281275
}
282276
return newCommits
283277
}

models/gpg_key_commit_verification.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package models
66

77
import (
8-
"container/list"
98
"fmt"
109
"hash"
1110
"strings"
@@ -68,24 +67,19 @@ const (
6867
)
6968

7069
// ParseCommitsWithSignature checks if signaute of commits are corresponding to users gpg keys.
71-
func ParseCommitsWithSignature(oldCommits *list.List, repository *Repository) *list.List {
72-
var (
73-
newCommits = list.New()
74-
e = oldCommits.Front()
75-
)
70+
func ParseCommitsWithSignature(oldCommits []*UserCommit, repository *Repository) []*SignCommit {
71+
newCommits := make([]*SignCommit, 0, len(oldCommits))
7672
keyMap := map[string]bool{}
7773

78-
for e != nil {
79-
c := e.Value.(UserCommit)
80-
signCommit := SignCommit{
81-
UserCommit: &c,
74+
for _, c := range oldCommits {
75+
signCommit := &SignCommit{
76+
UserCommit: c,
8277
Verification: ParseCommitWithSignature(c.Commit),
8378
}
8479

8580
_ = CalculateTrustStatus(signCommit.Verification, repository, &keyMap)
8681

87-
newCommits.PushBack(signCommit)
88-
e = e.Next()
82+
newCommits = append(newCommits, signCommit)
8983
}
9084
return newCommits
9185
}

models/issue_comment.go

+29-54
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package models
88

99
import (
10-
"container/list"
1110
"fmt"
1211
"regexp"
1312
"strconv"
@@ -191,11 +190,11 @@ type Comment struct {
191190
RefIssue *Issue `xorm:"-"`
192191
RefComment *Comment `xorm:"-"`
193192

194-
Commits *list.List `xorm:"-"`
195-
OldCommit string `xorm:"-"`
196-
NewCommit string `xorm:"-"`
197-
CommitsNum int64 `xorm:"-"`
198-
IsForcePush bool `xorm:"-"`
193+
Commits []*SignCommitWithStatuses `xorm:"-"`
194+
OldCommit string `xorm:"-"`
195+
NewCommit string `xorm:"-"`
196+
CommitsNum int64 `xorm:"-"`
197+
IsForcePush bool `xorm:"-"`
199198
}
200199

201200
// PushActionContent is content of push pull comment
@@ -675,13 +674,8 @@ func (c *Comment) LoadPushCommits() (err error) {
675674
}
676675
defer gitRepo.Close()
677676

678-
c.Commits = gitRepo.GetCommitsFromIDs(data.CommitIDs)
679-
c.CommitsNum = int64(c.Commits.Len())
680-
if c.CommitsNum > 0 {
681-
c.Commits = ValidateCommitsWithEmails(c.Commits)
682-
c.Commits = ParseCommitsWithSignature(c.Commits, c.Issue.Repo)
683-
c.Commits = ParseCommitsWithStatus(c.Commits, c.Issue.Repo)
684-
}
677+
c.Commits = ConvertFromGitCommit(gitRepo.GetCommitsFromIDs(data.CommitIDs), c.Issue.Repo)
678+
c.CommitsNum = int64(len(c.Commits))
685679
}
686680

687681
return err
@@ -1293,21 +1287,17 @@ func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch
12931287
return nil, false, err
12941288
}
12951289

1296-
var (
1297-
commits *list.List
1298-
commitChecks map[string]commitBranchCheckItem
1299-
)
1300-
commits, err = newCommit.CommitsBeforeUntil(oldCommitID)
1290+
commits, err := newCommit.CommitsBeforeUntil(oldCommitID)
13011291
if err != nil {
13021292
return nil, false, err
13031293
}
13041294

1305-
commitIDs = make([]string, 0, commits.Len())
1306-
commitChecks = make(map[string]commitBranchCheckItem)
1295+
commitIDs = make([]string, 0, len(commits))
1296+
commitChecks := make(map[string]*commitBranchCheckItem)
13071297

1308-
for e := commits.Front(); e != nil; e = e.Next() {
1309-
commitChecks[e.Value.(*git.Commit).ID.String()] = commitBranchCheckItem{
1310-
Commit: e.Value.(*git.Commit),
1298+
for _, commit := range commits {
1299+
commitChecks[commit.ID.String()] = &commitBranchCheckItem{
1300+
Commit: commit,
13111301
Checked: false,
13121302
}
13131303
}
@@ -1316,8 +1306,8 @@ func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch
13161306
return
13171307
}
13181308

1319-
for e := commits.Back(); e != nil; e = e.Prev() {
1320-
commitID := e.Value.(*git.Commit).ID.String()
1309+
for i := len(commits) - 1; i >= 0; i-- {
1310+
commitID := commits[i].ID.String()
13211311
if item, ok := commitChecks[commitID]; ok && item.Checked {
13221312
commitIDs = append(commitIDs, commitID)
13231313
}
@@ -1331,64 +1321,49 @@ type commitBranchCheckItem struct {
13311321
Checked bool
13321322
}
13331323

1334-
func commitBranchCheck(gitRepo *git.Repository, startCommit *git.Commit, endCommitID, baseBranch string, commitList map[string]commitBranchCheckItem) (err error) {
1335-
var (
1336-
item commitBranchCheckItem
1337-
ok bool
1338-
listItem *list.Element
1339-
tmp string
1340-
)
1341-
1324+
func commitBranchCheck(gitRepo *git.Repository, startCommit *git.Commit, endCommitID, baseBranch string, commitList map[string]*commitBranchCheckItem) error {
13421325
if startCommit.ID.String() == endCommitID {
1343-
return
1326+
return nil
13441327
}
13451328

1346-
checkStack := list.New()
1347-
checkStack.PushBack(startCommit.ID.String())
1348-
listItem = checkStack.Back()
1329+
checkStack := make([]string, 0, 10)
1330+
checkStack = append(checkStack, startCommit.ID.String())
13491331

1350-
for listItem != nil {
1351-
tmp = listItem.Value.(string)
1352-
checkStack.Remove(listItem)
1332+
for len(checkStack) > 0 {
1333+
commitID := checkStack[0]
1334+
checkStack = checkStack[1:]
13531335

1354-
if item, ok = commitList[tmp]; !ok {
1355-
listItem = checkStack.Back()
1336+
item, ok := commitList[commitID]
1337+
if !ok {
13561338
continue
13571339
}
13581340

13591341
if item.Commit.ID.String() == endCommitID {
1360-
listItem = checkStack.Back()
13611342
continue
13621343
}
13631344

1364-
if err = item.Commit.LoadBranchName(); err != nil {
1365-
return
1345+
if err := item.Commit.LoadBranchName(); err != nil {
1346+
return err
13661347
}
13671348

13681349
if item.Commit.Branch == baseBranch {
1369-
listItem = checkStack.Back()
13701350
continue
13711351
}
13721352

13731353
if item.Checked {
1374-
listItem = checkStack.Back()
13751354
continue
13761355
}
13771356

13781357
item.Checked = true
1379-
commitList[tmp] = item
13801358

13811359
parentNum := item.Commit.ParentCount()
13821360
for i := 0; i < parentNum; i++ {
1383-
var parentCommit *git.Commit
1384-
parentCommit, err = item.Commit.Parent(i)
1361+
parentCommit, err := item.Commit.Parent(i)
13851362
if err != nil {
1386-
return
1363+
return err
13871364
}
1388-
checkStack.PushBack(parentCommit.ID.String())
1365+
checkStack = append(checkStack, parentCommit.ID.String())
13891366
}
1390-
1391-
listItem = checkStack.Back()
13921367
}
13931368
return nil
13941369
}

models/pull_sign.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ Loop:
118118
if err != nil {
119119
return false, "", nil, err
120120
}
121-
for e := commitList.Front(); e != nil; e = e.Next() {
122-
commit = e.Value.(*git.Commit)
121+
for _, commit := range commitList {
123122
verification := ParseCommitWithSignature(commit)
124123
if !verification.Verified {
125124
return false, "", nil, &ErrWontSign{commitsSigned}

models/user.go

+6-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package models
77

88
import (
9-
"container/list"
109
"context"
1110
"crypto/sha256"
1211
"crypto/subtle"
@@ -1509,32 +1508,26 @@ func ValidateCommitWithEmail(c *git.Commit) *User {
15091508
}
15101509

15111510
// ValidateCommitsWithEmails checks if authors' e-mails of commits are corresponding to users.
1512-
func ValidateCommitsWithEmails(oldCommits *list.List) *list.List {
1511+
func ValidateCommitsWithEmails(oldCommits []*git.Commit) []*UserCommit {
15131512
var (
1514-
u *User
1515-
emails = map[string]*User{}
1516-
newCommits = list.New()
1517-
e = oldCommits.Front()
1513+
emails = make(map[string]*User)
1514+
newCommits = make([]*UserCommit, 0, len(oldCommits))
15181515
)
1519-
for e != nil {
1520-
c := e.Value.(*git.Commit)
1521-
1516+
for _, c := range oldCommits {
1517+
var u *User
15221518
if c.Author != nil {
15231519
if v, ok := emails[c.Author.Email]; !ok {
15241520
u, _ = GetUserByEmail(c.Author.Email)
15251521
emails[c.Author.Email] = u
15261522
} else {
15271523
u = v
15281524
}
1529-
} else {
1530-
u = nil
15311525
}
15321526

1533-
newCommits.PushBack(UserCommit{
1527+
newCommits = append(newCommits, &UserCommit{
15341528
User: u,
15351529
Commit: c,
15361530
})
1537-
e = e.Next()
15381531
}
15391532
return newCommits
15401533
}

modules/git/commit.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package git
88
import (
99
"bufio"
1010
"bytes"
11-
"container/list"
1211
"errors"
1312
"fmt"
1413
"io"
@@ -187,12 +186,12 @@ func (c *Commit) CommitsCount() (int64, error) {
187186
}
188187

189188
// CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize
190-
func (c *Commit) CommitsByRange(page, pageSize int) (*list.List, error) {
189+
func (c *Commit) CommitsByRange(page, pageSize int) ([]*Commit, error) {
191190
return c.repo.commitsByRange(c.ID, page, pageSize)
192191
}
193192

194193
// CommitsBefore returns all the commits before current revision
195-
func (c *Commit) CommitsBefore() (*list.List, error) {
194+
func (c *Commit) CommitsBefore() ([]*Commit, error) {
196195
return c.repo.getCommitsBefore(c.ID)
197196
}
198197

@@ -228,12 +227,12 @@ func (c *Commit) HasPreviousCommit(commitHash SHA1) (bool, error) {
228227
}
229228

230229
// CommitsBeforeLimit returns num commits before current revision
231-
func (c *Commit) CommitsBeforeLimit(num int) (*list.List, error) {
230+
func (c *Commit) CommitsBeforeLimit(num int) ([]*Commit, error) {
232231
return c.repo.getCommitsBeforeLimit(c.ID, num)
233232
}
234233

235234
// CommitsBeforeUntil returns the commits between commitID to current revision
236-
func (c *Commit) CommitsBeforeUntil(commitID string) (*list.List, error) {
235+
func (c *Commit) CommitsBeforeUntil(commitID string) ([]*Commit, error) {
237236
endCommit, err := c.repo.GetCommit(commitID)
238237
if err != nil {
239238
return nil, err
@@ -281,7 +280,7 @@ func NewSearchCommitsOptions(searchString string, forAllRefs bool) SearchCommits
281280
}
282281

283282
// SearchCommits returns the commits match the keyword before current revision
284-
func (c *Commit) SearchCommits(opts SearchCommitsOptions) (*list.List, error) {
283+
func (c *Commit) SearchCommits(opts SearchCommitsOptions) ([]*Commit, error) {
285284
return c.repo.searchCommits(c.ID, opts)
286285
}
287286

modules/git/repo.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package git
77

88
import (
99
"bytes"
10-
"container/list"
1110
"context"
1211
"fmt"
1312
"os"
@@ -33,10 +32,10 @@ func (repo *Repository) GetAllCommitsCount() (int64, error) {
3332
return AllCommitsCount(repo.Path, false)
3433
}
3534

36-
func (repo *Repository) parsePrettyFormatLogToList(logs []byte) (*list.List, error) {
37-
l := list.New()
35+
func (repo *Repository) parsePrettyFormatLogToList(logs []byte) ([]*Commit, error) {
36+
var commits []*Commit
3837
if len(logs) == 0 {
39-
return l, nil
38+
return commits, nil
4039
}
4140

4241
parts := bytes.Split(logs, []byte{'\n'})
@@ -46,10 +45,10 @@ func (repo *Repository) parsePrettyFormatLogToList(logs []byte) (*list.List, err
4645
if err != nil {
4746
return nil, err
4847
}
49-
l.PushBack(commit)
48+
commits = append(commits, commit)
5049
}
5150

52-
return l, nil
51+
return commits, nil
5352
}
5453

5554
// IsRepoURLAccessible checks if given repository URL is accessible.

0 commit comments

Comments
 (0)