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 <6543@obermui.de>
This commit is contained in:
parent
23d438f565
commit
d9ef43a712
29 changed files with 183 additions and 302 deletions
20
models/commit.go
Normal file
20
models/commit.go
Normal file
|
@ -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,
|
||||
)
|
||||
}
|
|
@ -5,7 +5,6 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
@ -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()
|
||||
)
|
||||
func ParseCommitsWithStatus(oldCommits []*SignCommit, repo *Repository) []*SignCommitWithStatuses {
|
||||
newCommits := make([]*SignCommitWithStatuses, 0, len(oldCommits))
|
||||
|
||||
for e != nil {
|
||||
c := e.Value.(SignCommit)
|
||||
commit := SignCommitWithStatuses{
|
||||
SignCommit: &c,
|
||||
for _, c := range oldCommits {
|
||||
commit := &SignCommitWithStatuses{
|
||||
SignCommit: c,
|
||||
}
|
||||
statuses, err := GetLatestCommitStatus(repo.ID, commit.ID.String(), ListOptions{})
|
||||
if err != nil {
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"hash"
|
||||
"strings"
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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}
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
|
@ -1509,16 +1508,13 @@ 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)
|
||||
|
@ -1526,15 +1522,12 @@ func ValidateCommitsWithEmails(oldCommits *list.List) *list.List {
|
|||
} else {
|
||||
u = v
|
||||
}
|
||||
} else {
|
||||
u = nil
|
||||
}
|
||||
|
||||
newCommits.PushBack(UserCommit{
|
||||
newCommits = append(newCommits, &UserCommit{
|
||||
User: u,
|
||||
Commit: c,
|
||||
})
|
||||
e = e.Next()
|
||||
}
|
||||
return newCommits
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue