Skip to content

Commit 03b6dea

Browse files
committed
Merge branch 'master' into followup-from-16562-prepare-for-16567
2 parents 1e4f5e1 + d9ef43a commit 03b6dea

33 files changed

+212
-310
lines changed

models/commit.go

Lines changed: 20 additions & 0 deletions
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

Lines changed: 7 additions & 13 deletions
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

Lines changed: 6 additions & 12 deletions
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

Lines changed: 29 additions & 54 deletions
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/migrations/migrations.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ var migrations = []Migration{
327327
NewMigration("Drop unneeded webhook related columns", dropWebhookColumns),
328328
// v188 -> v189
329329
NewMigration("Add key is verified to gpg key", addKeyIsVerified),
330+
331+
// Gitea 1.15.0 ends at v189
332+
330333
// v189 -> v190
331334
NewMigration("Unwrap ldap.Sources", unwrapLDAPSourceCfg),
332335
// v190 -> v191
@@ -840,7 +843,7 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
840843
}
841844
cols += "`" + strings.ToLower(col) + "`"
842845
}
843-
sql := fmt.Sprintf("SELECT Name FROM SYS.DEFAULT_CONSTRAINTS WHERE PARENT_OBJECT_ID = OBJECT_ID('%[1]s') AND PARENT_COLUMN_ID IN (SELECT column_id FROM sys.columns WHERE lower(NAME) IN (%[2]s) AND object_id = OBJECT_ID('%[1]s'))",
846+
sql := fmt.Sprintf("SELECT Name FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID('%[1]s') AND parent_column_id IN (SELECT column_id FROM sys.columns WHERE LOWER(name) IN (%[2]s) AND object_id = OBJECT_ID('%[1]s'))",
844847
tableName, strings.ReplaceAll(cols, "`", "'"))
845848
constraints := make([]string, 0)
846849
if err := sess.SQL(sql).Find(&constraints); err != nil {
@@ -851,17 +854,14 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
851854
return fmt.Errorf("Drop table `%s` default constraint `%s`: %v", tableName, constraint, err)
852855
}
853856
}
854-
sql = fmt.Sprintf("SELECT DISTINCT Name FROM SYS.INDEXES INNER JOIN SYS.INDEX_COLUMNS ON INDEXES.INDEX_ID = INDEX_COLUMNS.INDEX_ID AND INDEXES.OBJECT_ID = INDEX_COLUMNS.OBJECT_ID WHERE INDEXES.OBJECT_ID = OBJECT_ID('%[1]s') AND INDEX_COLUMNS.COLUMN_ID IN (SELECT column_id FROM sys.columns WHERE lower(NAME) IN (%[2]s) AND object_id = OBJECT_ID('%[1]s'))",
857+
sql = fmt.Sprintf("SELECT DISTINCT Name FROM sys.indexes INNER JOIN sys.index_columns ON indexes.index_id = index_columns.index_id AND indexes.object_id = index_columns.object_id WHERE indexes.object_id = OBJECT_ID('%[1]s') AND index_columns.column_id IN (SELECT column_id FROM sys.columns WHERE LOWER(name) IN (%[2]s) AND object_id = OBJECT_ID('%[1]s'))",
855858
tableName, strings.ReplaceAll(cols, "`", "'"))
856859
constraints = make([]string, 0)
857860
if err := sess.SQL(sql).Find(&constraints); err != nil {
858861
return fmt.Errorf("Find constraints: %v", err)
859862
}
860863
for _, constraint := range constraints {
861-
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP CONSTRAINT IF EXISTS `%s`", tableName, constraint)); err != nil {
862-
return fmt.Errorf("Drop table `%s` index constraint `%s`: %v", tableName, constraint, err)
863-
}
864-
if _, err := sess.Exec(fmt.Sprintf("DROP INDEX IF EXISTS `%[2]s` ON `%[1]s`", tableName, constraint)); err != nil {
864+
if _, err := sess.Exec(fmt.Sprintf("DROP INDEX `%[2]s` ON `%[1]s`", tableName, constraint)); err != nil {
865865
return fmt.Errorf("Drop index `%[2]s` on `%[1]s`: %v", tableName, constraint, err)
866866
}
867867
}

models/pull_sign.go

Lines changed: 1 addition & 2 deletions
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

Lines changed: 6 additions & 13 deletions
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
}

0 commit comments

Comments
 (0)