Skip to content

Commit

Permalink
Replace list.List with slices (#16311)
Browse files Browse the repository at this point in the history
* 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 <6543@obermui.de>
  • Loading branch information
KN4CK3R and 6543 committed Aug 9, 2021
1 parent 23d438f commit d9ef43a
Show file tree
Hide file tree
Showing 29 changed files with 185 additions and 304 deletions.
20 changes: 20 additions & 0 deletions models/commit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2021 Gitea. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models

import (
"code.gitea.io/gitea/modules/git"
)

// ConvertFromGitCommit converts git commits into SignCommitWithStatuses
func ConvertFromGitCommit(commits []*git.Commit, repo *Repository) []*SignCommitWithStatuses {
return ParseCommitsWithStatus(
ParseCommitsWithSignature(
ValidateCommitsWithEmails(commits),
repo,
),
repo,
)
}
20 changes: 7 additions & 13 deletions models/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package models

import (
"container/list"
"crypto/sha1"
"fmt"
"strings"
Expand Down Expand Up @@ -257,16 +256,12 @@ type SignCommitWithStatuses struct {
}

// ParseCommitsWithStatus checks commits latest statuses and calculates its worst status state
func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List {
var (
newCommits = list.New()
e = oldCommits.Front()
)

for e != nil {
c := e.Value.(SignCommit)
commit := SignCommitWithStatuses{
SignCommit: &c,
func ParseCommitsWithStatus(oldCommits []*SignCommit, repo *Repository) []*SignCommitWithStatuses {
newCommits := make([]*SignCommitWithStatuses, 0, len(oldCommits))

for _, c := range oldCommits {
commit := &SignCommitWithStatuses{
SignCommit: c,
}
statuses, err := GetLatestCommitStatus(repo.ID, commit.ID.String(), ListOptions{})
if err != nil {
Expand All @@ -276,8 +271,7 @@ func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List
commit.Status = CalcCommitStatus(statuses)
}

newCommits.PushBack(commit)
e = e.Next()
newCommits = append(newCommits, commit)
}
return newCommits
}
Expand Down
18 changes: 6 additions & 12 deletions models/gpg_key_commit_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package models

import (
"container/list"
"fmt"
"hash"
"strings"
Expand Down Expand Up @@ -68,24 +67,19 @@ const (
)

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

for e != nil {
c := e.Value.(UserCommit)
signCommit := SignCommit{
UserCommit: &c,
for _, c := range oldCommits {
signCommit := &SignCommit{
UserCommit: c,
Verification: ParseCommitWithSignature(c.Commit),
}

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

newCommits.PushBack(signCommit)
e = e.Next()
newCommits = append(newCommits, signCommit)
}
return newCommits
}
Expand Down
83 changes: 29 additions & 54 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package models

import (
"container/list"
"fmt"
"regexp"
"strconv"
Expand Down Expand Up @@ -191,11 +190,11 @@ type Comment struct {
RefIssue *Issue `xorm:"-"`
RefComment *Comment `xorm:"-"`

Commits *list.List `xorm:"-"`
OldCommit string `xorm:"-"`
NewCommit string `xorm:"-"`
CommitsNum int64 `xorm:"-"`
IsForcePush bool `xorm:"-"`
Commits []*SignCommitWithStatuses `xorm:"-"`
OldCommit string `xorm:"-"`
NewCommit string `xorm:"-"`
CommitsNum int64 `xorm:"-"`
IsForcePush bool `xorm:"-"`
}

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

c.Commits = gitRepo.GetCommitsFromIDs(data.CommitIDs)
c.CommitsNum = int64(c.Commits.Len())
if c.CommitsNum > 0 {
c.Commits = ValidateCommitsWithEmails(c.Commits)
c.Commits = ParseCommitsWithSignature(c.Commits, c.Issue.Repo)
c.Commits = ParseCommitsWithStatus(c.Commits, c.Issue.Repo)
}
c.Commits = ConvertFromGitCommit(gitRepo.GetCommitsFromIDs(data.CommitIDs), c.Issue.Repo)
c.CommitsNum = int64(len(c.Commits))
}

return err
Expand Down Expand Up @@ -1293,21 +1287,17 @@ func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch
return nil, false, err
}

var (
commits *list.List
commitChecks map[string]commitBranchCheckItem
)
commits, err = newCommit.CommitsBeforeUntil(oldCommitID)
commits, err := newCommit.CommitsBeforeUntil(oldCommitID)
if err != nil {
return nil, false, err
}

commitIDs = make([]string, 0, commits.Len())
commitChecks = make(map[string]commitBranchCheckItem)
commitIDs = make([]string, 0, len(commits))
commitChecks := make(map[string]*commitBranchCheckItem)

for e := commits.Front(); e != nil; e = e.Next() {
commitChecks[e.Value.(*git.Commit).ID.String()] = commitBranchCheckItem{
Commit: e.Value.(*git.Commit),
for _, commit := range commits {
commitChecks[commit.ID.String()] = &commitBranchCheckItem{
Commit: commit,
Checked: false,
}
}
Expand All @@ -1316,8 +1306,8 @@ func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch
return
}

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

func commitBranchCheck(gitRepo *git.Repository, startCommit *git.Commit, endCommitID, baseBranch string, commitList map[string]commitBranchCheckItem) (err error) {
var (
item commitBranchCheckItem
ok bool
listItem *list.Element
tmp string
)

func commitBranchCheck(gitRepo *git.Repository, startCommit *git.Commit, endCommitID, baseBranch string, commitList map[string]*commitBranchCheckItem) error {
if startCommit.ID.String() == endCommitID {
return
return nil
}

checkStack := list.New()
checkStack.PushBack(startCommit.ID.String())
listItem = checkStack.Back()
checkStack := make([]string, 0, 10)
checkStack = append(checkStack, startCommit.ID.String())

for listItem != nil {
tmp = listItem.Value.(string)
checkStack.Remove(listItem)
for len(checkStack) > 0 {
commitID := checkStack[0]
checkStack = checkStack[1:]

if item, ok = commitList[tmp]; !ok {
listItem = checkStack.Back()
item, ok := commitList[commitID]
if !ok {
continue
}

if item.Commit.ID.String() == endCommitID {
listItem = checkStack.Back()
continue
}

if err = item.Commit.LoadBranchName(); err != nil {
return
if err := item.Commit.LoadBranchName(); err != nil {
return err
}

if item.Commit.Branch == baseBranch {
listItem = checkStack.Back()
continue
}

if item.Checked {
listItem = checkStack.Back()
continue
}

item.Checked = true
commitList[tmp] = item

parentNum := item.Commit.ParentCount()
for i := 0; i < parentNum; i++ {
var parentCommit *git.Commit
parentCommit, err = item.Commit.Parent(i)
parentCommit, err := item.Commit.Parent(i)
if err != nil {
return
return err
}
checkStack.PushBack(parentCommit.ID.String())
checkStack = append(checkStack, parentCommit.ID.String())
}

listItem = checkStack.Back()
}
return nil
}
3 changes: 1 addition & 2 deletions models/pull_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ Loop:
if err != nil {
return false, "", nil, err
}
for e := commitList.Front(); e != nil; e = e.Next() {
commit = e.Value.(*git.Commit)
for _, commit := range commitList {
verification := ParseCommitWithSignature(commit)
if !verification.Verified {
return false, "", nil, &ErrWontSign{commitsSigned}
Expand Down
19 changes: 6 additions & 13 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package models

import (
"container/list"
"context"
"crypto/sha256"
"crypto/subtle"
Expand Down Expand Up @@ -1509,32 +1508,26 @@ func ValidateCommitWithEmail(c *git.Commit) *User {
}

// ValidateCommitsWithEmails checks if authors' e-mails of commits are corresponding to users.
func ValidateCommitsWithEmails(oldCommits *list.List) *list.List {
func ValidateCommitsWithEmails(oldCommits []*git.Commit) []*UserCommit {
var (
u *User
emails = map[string]*User{}
newCommits = list.New()
e = oldCommits.Front()
emails = make(map[string]*User)
newCommits = make([]*UserCommit, 0, len(oldCommits))
)
for e != nil {
c := e.Value.(*git.Commit)

for _, c := range oldCommits {
var u *User
if c.Author != nil {
if v, ok := emails[c.Author.Email]; !ok {
u, _ = GetUserByEmail(c.Author.Email)
emails[c.Author.Email] = u
} else {
u = v
}
} else {
u = nil
}

newCommits.PushBack(UserCommit{
newCommits = append(newCommits, &UserCommit{
User: u,
Commit: c,
})
e = e.Next()
}
return newCommits
}
Expand Down
11 changes: 5 additions & 6 deletions modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package git
import (
"bufio"
"bytes"
"container/list"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -187,12 +186,12 @@ func (c *Commit) CommitsCount() (int64, error) {
}

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

// CommitsBefore returns all the commits before current revision
func (c *Commit) CommitsBefore() (*list.List, error) {
func (c *Commit) CommitsBefore() ([]*Commit, error) {
return c.repo.getCommitsBefore(c.ID)
}

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

// CommitsBeforeLimit returns num commits before current revision
func (c *Commit) CommitsBeforeLimit(num int) (*list.List, error) {
func (c *Commit) CommitsBeforeLimit(num int) ([]*Commit, error) {
return c.repo.getCommitsBeforeLimit(c.ID, num)
}

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

// SearchCommits returns the commits match the keyword before current revision
func (c *Commit) SearchCommits(opts SearchCommitsOptions) (*list.List, error) {
func (c *Commit) SearchCommits(opts SearchCommitsOptions) ([]*Commit, error) {
return c.repo.searchCommits(c.ID, opts)
}

Expand Down
11 changes: 5 additions & 6 deletions modules/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package git

import (
"bytes"
"container/list"
"context"
"fmt"
"os"
Expand All @@ -33,10 +32,10 @@ func (repo *Repository) GetAllCommitsCount() (int64, error) {
return AllCommitsCount(repo.Path, false)
}

func (repo *Repository) parsePrettyFormatLogToList(logs []byte) (*list.List, error) {
l := list.New()
func (repo *Repository) parsePrettyFormatLogToList(logs []byte) ([]*Commit, error) {
var commits []*Commit
if len(logs) == 0 {
return l, nil
return commits, nil
}

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

return l, nil
return commits, nil
}

// IsRepoURLAccessible checks if given repository URL is accessible.
Expand Down

0 comments on commit d9ef43a

Please sign in to comment.