From 99f5c8c1d64fb0388bde8d1a27276f2c65dc7418 Mon Sep 17 00:00:00 2001
From: KN4CK3R
Date: Tue, 29 Jun 2021 14:36:00 +0000
Subject: [PATCH 1/8] Replaced list with slice.
---
models/commit_status.go | 16 ++-----
models/gpg_key.go | 14 ++----
models/issue_comment.go | 47 +++++++++----------
models/pull_sign.go | 3 +-
models/user.go | 19 +++-----
modules/git/commit.go | 11 ++---
modules/git/repo.go | 11 ++---
modules/git/repo_commit.go | 38 +++++++--------
modules/git/repo_compare.go | 5 +-
modules/repository/commits.go | 16 ++-----
modules/repository/commits_test.go | 30 ++++++------
modules/templates/helper.go | 17 -------
routers/api/v1/repo/commits.go | 10 +---
routers/web/repo/blame.go | 9 ++--
routers/web/repo/commit.go | 35 +++++++++-----
routers/web/repo/compare.go | 20 ++++----
routers/web/repo/pull.go | 26 +++++-----
routers/web/repo/wiki.go | 8 ++--
services/mirror/mirror_pull.go | 2 +-
services/pull/pull.go | 32 ++++++-------
services/repository/push.go | 5 +-
templates/mail/issue/default.tmpl | 7 ++-
templates/repo/commits_list.tmpl | 3 +-
templates/repo/commits_list_small.tmpl | 3 +-
.../repo/issue/view_content/comments.tmpl | 2 +-
templates/user/dashboard/feeds.tmpl | 20 ++++----
26 files changed, 177 insertions(+), 232 deletions(-)
diff --git a/models/commit_status.go b/models/commit_status.go
index 1105c3b173156..49719f4205d53 100644
--- a/models/commit_status.go
+++ b/models/commit_status.go
@@ -5,7 +5,6 @@
package models
import (
- "container/list"
"crypto/sha1"
"fmt"
"strings"
@@ -257,14 +256,10 @@ 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)
+func ParseCommitsWithStatus(oldCommits []SignCommit, repo *Repository) []SignCommitWithStatuses {
+ newCommits := make([]SignCommitWithStatuses, 0, len(oldCommits))
+
+ for _, c := range oldCommits {
commit := SignCommitWithStatuses{
SignCommit: &c,
}
@@ -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
}
diff --git a/models/gpg_key.go b/models/gpg_key.go
index 9530eacb0a701..4dff0e5979075 100644
--- a/models/gpg_key.go
+++ b/models/gpg_key.go
@@ -6,7 +6,6 @@ package models
import (
"bytes"
- "container/list"
"crypto"
"encoding/base64"
"fmt"
@@ -830,15 +829,11 @@ func verifyWithGPGSettings(gpgSettings *git.GPGSettings, sig *packet.Signature,
}
// 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)
+ for _, c := range oldCommits {
signCommit := SignCommit{
UserCommit: &c,
Verification: ParseCommitWithSignature(c.Commit),
@@ -846,8 +841,7 @@ func ParseCommitsWithSignature(oldCommits *list.List, repository *Repository) *l
_ = CalculateTrustStatus(signCommit.Verification, repository, &keyMap)
- newCommits.PushBack(signCommit)
- e = e.Next()
+ newCommits = append(newCommits, signCommit)
}
return newCommits
}
diff --git a/models/issue_comment.go b/models/issue_comment.go
index 1b98b248b1fcc..9ce7655747ed2 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -191,11 +191,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
@@ -676,13 +676,16 @@ 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 = ParseCommitsWithStatus(
+ ParseCommitsWithSignature(
+ ValidateCommitsWithEmails(
+ gitRepo.GetCommitsFromIDs(data.CommitIDs),
+ ),
+ c.Issue.Repo,
+ ),
+ c.Issue.Repo,
+ )
+ c.CommitsNum = int64(len(c.Commits))
}
return err
@@ -1295,21 +1298,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,
}
}
@@ -1318,8 +1317,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)
}
diff --git a/models/pull_sign.go b/models/pull_sign.go
index 10a6522ebef73..e7cf4ab666ee8 100644
--- a/models/pull_sign.go
+++ b/models/pull_sign.go
@@ -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}
diff --git a/models/user.go b/models/user.go
index 47d24aefd6aa4..029b808fc34d4 100644
--- a/models/user.go
+++ b/models/user.go
@@ -6,7 +6,6 @@
package models
import (
- "container/list"
"context"
"crypto/sha256"
"crypto/subtle"
@@ -1503,16 +1502,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)
@@ -1520,15 +1516,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
}
diff --git a/modules/git/commit.go b/modules/git/commit.go
index f4d6075fe2d22..23226644354ba 100644
--- a/modules/git/commit.go
+++ b/modules/git/commit.go
@@ -8,7 +8,6 @@ package git
import (
"bufio"
"bytes"
- "container/list"
"errors"
"fmt"
"io"
@@ -185,12 +184,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)
}
@@ -226,12 +225,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
@@ -279,7 +278,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)
}
diff --git a/modules/git/repo.go b/modules/git/repo.go
index 43f329f4487d6..4e6f90c3efa5e 100644
--- a/modules/git/repo.go
+++ b/modules/git/repo.go
@@ -7,7 +7,6 @@ package git
import (
"bytes"
- "container/list"
"context"
"fmt"
"os"
@@ -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'})
@@ -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.
diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go
index 815aa141e532f..25b4955e71eff 100644
--- a/modules/git/repo_commit.go
+++ b/modules/git/repo_commit.go
@@ -7,7 +7,6 @@ package git
import (
"bytes"
- "container/list"
"io"
"io/ioutil"
"strconv"
@@ -84,10 +83,10 @@ func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
if err != nil {
return nil, err
}
- return commits.Front().Value.(*Commit), nil
+ return commits[0], nil
}
-func (repo *Repository) commitsByRange(id SHA1, page, pageSize int) (*list.List, error) {
+func (repo *Repository) commitsByRange(id SHA1, page, pageSize int) ([]*Commit, error) {
stdout, err := NewCommand("log", id.String(), "--skip="+strconv.Itoa((page-1)*pageSize),
"--max-count="+strconv.Itoa(pageSize), prettyLogFormat).RunInDirBytes(repo.Path)
@@ -97,7 +96,7 @@ func (repo *Repository) commitsByRange(id SHA1, page, pageSize int) (*list.List,
return repo.parsePrettyFormatLogToList(stdout)
}
-func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) (*list.List, error) {
+func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Commit, error) {
// create new git log command with limit of 100 commis
cmd := NewCommand("log", id.String(), "-100", prettyLogFormat)
// ignore case
@@ -201,7 +200,7 @@ func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
}
// CommitsByFileAndRange return the commits according revison file and the page
-func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (*list.List, error) {
+func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) ([]*Commit, error) {
skip := (page - 1) * setting.Git.CommitsRangeSize
stdoutReader, stdoutWriter := io.Pipe()
@@ -226,7 +225,7 @@ func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (
_, err := io.CopyN(ioutil.Discard, stdoutReader, int64(skip*41))
if err != nil {
if err == io.EOF {
- return list.New(), nil
+ return []*Commit{}, nil
}
_ = stdoutReader.CloseWithError(err)
return nil, err
@@ -241,7 +240,7 @@ func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (
}
// CommitsByFileAndRangeNoFollow return the commits according revison file and the page
-func (repo *Repository) CommitsByFileAndRangeNoFollow(revision, file string, page int) (*list.List, error) {
+func (repo *Repository) CommitsByFileAndRangeNoFollow(revision, file string, page int) ([]*Commit, error) {
stdout, err := NewCommand("log", revision, "--skip="+strconv.Itoa((page-1)*50),
"--max-count="+strconv.Itoa(setting.Git.CommitsRangeSize), prettyLogFormat, "--", file).RunInDirBytes(repo.Path)
if err != nil {
@@ -265,7 +264,7 @@ func (repo *Repository) FilesCountBetween(startCommitID, endCommitID string) (in
}
// CommitsBetween returns a list that contains commits between [last, before).
-func (repo *Repository) CommitsBetween(last *Commit, before *Commit) (*list.List, error) {
+func (repo *Repository) CommitsBetween(last *Commit, before *Commit) ([]*Commit, error) {
var stdout []byte
var err error
if before == nil {
@@ -285,7 +284,7 @@ func (repo *Repository) CommitsBetween(last *Commit, before *Commit) (*list.List
}
// CommitsBetweenLimit returns a list that contains at most limit commits skipping the first skip commits between [last, before)
-func (repo *Repository) CommitsBetweenLimit(last *Commit, before *Commit, limit, skip int) (*list.List, error) {
+func (repo *Repository) CommitsBetweenLimit(last *Commit, before *Commit, limit, skip int) ([]*Commit, error) {
var stdout []byte
var err error
if before == nil {
@@ -305,7 +304,7 @@ func (repo *Repository) CommitsBetweenLimit(last *Commit, before *Commit, limit,
}
// CommitsBetweenIDs return commits between twoe commits
-func (repo *Repository) CommitsBetweenIDs(last, before string) (*list.List, error) {
+func (repo *Repository) CommitsBetweenIDs(last, before string) ([]*Commit, error) {
lastCommit, err := repo.GetCommit(last)
if err != nil {
return nil, err
@@ -333,7 +332,7 @@ func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) {
}
// commitsBefore the limit is depth, not total number of returned commits.
-func (repo *Repository) commitsBefore(id SHA1, limit int) (*list.List, error) {
+func (repo *Repository) commitsBefore(id SHA1, limit int) ([]*Commit, error) {
cmd := NewCommand("log")
if limit > 0 {
cmd.AddArguments("-"+strconv.Itoa(limit), prettyLogFormat, id.String())
@@ -351,9 +350,8 @@ func (repo *Repository) commitsBefore(id SHA1, limit int) (*list.List, error) {
return nil, err
}
- commits := list.New()
- for logEntry := formattedLog.Front(); logEntry != nil; logEntry = logEntry.Next() {
- commit := logEntry.Value.(*Commit)
+ commits := make([]*Commit, 0, len(formattedLog))
+ for _, commit := range formattedLog {
branches, err := repo.getBranches(commit, 2)
if err != nil {
return nil, err
@@ -363,17 +361,17 @@ func (repo *Repository) commitsBefore(id SHA1, limit int) (*list.List, error) {
break
}
- commits.PushBack(commit)
+ commits = append(commits, commit)
}
return commits, nil
}
-func (repo *Repository) getCommitsBefore(id SHA1) (*list.List, error) {
+func (repo *Repository) getCommitsBefore(id SHA1) ([]*Commit, error) {
return repo.commitsBefore(id, 0)
}
-func (repo *Repository) getCommitsBeforeLimit(id SHA1, num int) (*list.List, error) {
+func (repo *Repository) getCommitsBeforeLimit(id SHA1, num int) ([]*Commit, error) {
return repo.commitsBefore(id, num)
}
@@ -412,13 +410,13 @@ func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error)
}
// GetCommitsFromIDs get commits from commit IDs
-func (repo *Repository) GetCommitsFromIDs(commitIDs []string) (commits *list.List) {
- commits = list.New()
+func (repo *Repository) GetCommitsFromIDs(commitIDs []string) []*Commit {
+ commits := make([]*Commit, 0, len(commitIDs))
for _, commitID := range commitIDs {
commit, err := repo.GetCommit(commitID)
if err == nil && commit != nil {
- commits.PushBack(commit)
+ commits = append(commits, commit)
}
}
diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go
index 3255e68392a2d..5d1208aab18fe 100644
--- a/modules/git/repo_compare.go
+++ b/modules/git/repo_compare.go
@@ -7,7 +7,6 @@ package git
import (
"bytes"
- "container/list"
"fmt"
"io"
"regexp"
@@ -23,7 +22,7 @@ type CompareInfo struct {
MergeBase string
BaseCommitID string
HeadCommitID string
- Commits *list.List
+ Commits []*Commit
NumFiles int
}
@@ -90,7 +89,7 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string)
return nil, fmt.Errorf("parsePrettyFormatLogToList: %v", err)
}
} else {
- compareInfo.Commits = list.New()
+ compareInfo.Commits = []*Commit{}
compareInfo.MergeBase, err = GetFullCommitID(repo.Path, remoteBranch)
if err != nil {
compareInfo.MergeBase = remoteBranch
diff --git a/modules/repository/commits.go b/modules/repository/commits.go
index 6b67c2c262428..79ee3d5071dd1 100644
--- a/modules/repository/commits.go
+++ b/modules/repository/commits.go
@@ -5,7 +5,6 @@
package repository
import (
- "container/list"
"fmt"
"time"
@@ -154,16 +153,11 @@ func CommitToPushCommit(commit *git.Commit) *PushCommit {
}
}
-// ListToPushCommits transforms a list.List to PushCommits type.
-func ListToPushCommits(l *list.List) *PushCommits {
- var commits []*PushCommit
- var actEmail string
- for e := l.Front(); e != nil; e = e.Next() {
- commit := e.Value.(*git.Commit)
- if actEmail == "" {
- actEmail = commit.Committer.Email
- }
+// GitToPushCommits transforms a list of git.Commits to PushCommits type.
+func GitToPushCommits(gitCommits []*git.Commit) *PushCommits {
+ commits := make([]*PushCommit, 0, len(gitCommits))
+ for _, commit := range gitCommits {
commits = append(commits, CommitToPushCommit(commit))
}
- return &PushCommits{l.Len(), commits, "", make(map[string]string), make(map[string]*models.User)}
+ return &PushCommits{len(commits), commits, "", make(map[string]string), make(map[string]*models.User)}
}
diff --git a/modules/repository/commits_test.go b/modules/repository/commits_test.go
index a5b28ce933ed0..3d3a093f4e4c5 100644
--- a/modules/repository/commits_test.go
+++ b/modules/repository/commits_test.go
@@ -5,7 +5,6 @@
package repository
import (
- "container/list"
"crypto/md5"
"fmt"
"testing"
@@ -162,21 +161,22 @@ func TestListToPushCommits(t *testing.T) {
hash2, err := git.NewIDFromString(hexString2)
assert.NoError(t, err)
- l := list.New()
- l.PushBack(&git.Commit{
- ID: hash1,
- Author: sig,
- Committer: sig,
- CommitMessage: "Message1",
- })
- l.PushBack(&git.Commit{
- ID: hash2,
- Author: sig,
- Committer: sig,
- CommitMessage: "Message2",
- })
+ l := []*git.Commit{
+ {
+ ID: hash1,
+ Author: sig,
+ Committer: sig,
+ CommitMessage: "Message1",
+ },
+ {
+ ID: hash2,
+ Author: sig,
+ Committer: sig,
+ CommitMessage: "Message2",
+ },
+ }
- pushCommits := ListToPushCommits(l)
+ pushCommits := GitToPushCommits(l)
assert.Equal(t, 2, pushCommits.Len)
if assert.Len(t, pushCommits.Commits, 2) {
assert.Equal(t, "Message1", pushCommits.Commits[0].Message)
diff --git a/modules/templates/helper.go b/modules/templates/helper.go
index 83359a6ef234b..6334750937282 100644
--- a/modules/templates/helper.go
+++ b/modules/templates/helper.go
@@ -7,7 +7,6 @@ package templates
import (
"bytes"
- "container/list"
"encoding/json"
"errors"
"fmt"
@@ -125,7 +124,6 @@ func NewFuncMap() []template.FuncMap {
},
"SizeFmt": base.FileSize,
"CountFmt": base.FormatNumberSI,
- "List": List,
"SubStr": func(str string, start, length int) string {
if len(str) == 0 {
return ""
@@ -424,7 +422,6 @@ func NewTextFuncMap() []texttmpl.FuncMap {
"DateFmtShort": func(t time.Time) string {
return t.Format("Jan 02, 2006")
},
- "List": List,
"SubStr": func(str string, start, length int) string {
if len(str) == 0 {
return ""
@@ -632,20 +629,6 @@ func JSEscape(raw string) string {
return template.JSEscapeString(raw)
}
-// List traversings the list
-func List(l *list.List) chan interface{} {
- e := l.Front()
- c := make(chan interface{})
- go func() {
- for e != nil {
- c <- e.Value
- e = e.Next()
- }
- close(c)
- }()
- return c
-}
-
// Sha1 returns sha1 sum of string
func Sha1(str string) string {
return base.EncodeSha1(str)
diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go
index 9a0fd1d0b6f17..ed19d4499317b 100644
--- a/routers/api/v1/repo/commits.go
+++ b/routers/api/v1/repo/commits.go
@@ -190,20 +190,14 @@ func GetAllCommits(ctx *context.APIContext) {
userCache := make(map[string]*models.User)
- apiCommits := make([]*api.Commit, commits.Len())
-
- i := 0
- for commitPointer := commits.Front(); commitPointer != nil; commitPointer = commitPointer.Next() {
- commit := commitPointer.Value.(*git.Commit)
-
+ apiCommits := make([]*api.Commit, len(commits))
+ for i, commit := range commits {
// Create json struct
apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache)
if err != nil {
ctx.Error(http.StatusInternalServerError, "toCommit", err)
return
}
-
- i++
}
// kept for backwards compatibility
diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go
index 4ade9e9a93a5a..7add7dbb9300f 100644
--- a/routers/web/repo/blame.go
+++ b/routers/web/repo/blame.go
@@ -5,7 +5,6 @@
package repo
import (
- "container/list"
"fmt"
"html"
gotemplate "html/template"
@@ -146,7 +145,7 @@ func processBlameParts(ctx *context.Context, blameParts []git.BlamePart) (map[st
previousCommits := make(map[string]string)
// and as blameParts can reference the same commits multiple
// times, we cache the lookup work locally
- commits := list.New()
+ commits := make([]*git.Commit, 0, len(blameParts))
commitCache := map[string]*git.Commit{}
commitCache[ctx.Repo.Commit.ID.String()] = ctx.Repo.Commit
@@ -190,15 +189,13 @@ func processBlameParts(ctx *context.Context, blameParts []git.BlamePart) (map[st
}
}
- commits.PushBack(commit)
+ commits = append(commits, commit)
commitNames[commit.ID.String()] = models.UserCommit{}
}
// populate commit email addresses to later look up avatars.
- commits = models.ValidateCommitsWithEmails(commits)
- for e := commits.Front(); e != nil; e = e.Next() {
- c := e.Value.(models.UserCommit)
+ for _, c := range models.ValidateCommitsWithEmails(commits) {
commitNames[c.ID.String()] = c
}
diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go
index 45ef22f498be2..f231b9b675b8b 100644
--- a/routers/web/repo/commit.go
+++ b/routers/web/repo/commit.go
@@ -72,10 +72,13 @@ func Commits(ctx *context.Context) {
ctx.ServerError("CommitsByRange", err)
return
}
- commits = models.ValidateCommitsWithEmails(commits)
- commits = models.ParseCommitsWithSignature(commits, ctx.Repo.Repository)
- commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
- ctx.Data["Commits"] = commits
+ ctx.Data["Commits"] = models.ParseCommitsWithStatus(
+ models.ParseCommitsWithSignature(
+ models.ValidateCommitsWithEmails(commits),
+ ctx.Repo.Repository,
+ ),
+ ctx.Repo.Repository,
+ )
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
@@ -193,10 +196,14 @@ func SearchCommits(ctx *context.Context) {
ctx.ServerError("SearchCommits", err)
return
}
- commits = models.ValidateCommitsWithEmails(commits)
- commits = models.ParseCommitsWithSignature(commits, ctx.Repo.Repository)
- commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
- ctx.Data["Commits"] = commits
+ ctx.Data["CommitCount"] = len(commits)
+ ctx.Data["Commits"] = models.ParseCommitsWithStatus(
+ models.ParseCommitsWithSignature(
+ models.ValidateCommitsWithEmails(commits),
+ ctx.Repo.Repository,
+ ),
+ ctx.Repo.Repository,
+ )
ctx.Data["Keyword"] = query
if all {
@@ -204,7 +211,6 @@ func SearchCommits(ctx *context.Context) {
}
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
- ctx.Data["CommitCount"] = commits.Len()
ctx.Data["Branch"] = ctx.Repo.BranchName
ctx.HTML(http.StatusOK, tplCommits)
}
@@ -239,10 +245,13 @@ func FileHistory(ctx *context.Context) {
ctx.ServerError("CommitsByFileAndRange", err)
return
}
- commits = models.ValidateCommitsWithEmails(commits)
- commits = models.ParseCommitsWithSignature(commits, ctx.Repo.Repository)
- commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
- ctx.Data["Commits"] = commits
+ ctx.Data["Commits"] = models.ParseCommitsWithStatus(
+ models.ParseCommitsWithSignature(
+ models.ValidateCommitsWithEmails(commits),
+ ctx.Repo.Repository,
+ ),
+ ctx.Repo.Repository,
+ )
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go
index fddfc4a63a897..de35ecb0e4f72 100644
--- a/routers/web/repo/compare.go
+++ b/routers/web/repo/compare.go
@@ -550,14 +550,18 @@ func PrepareCompareDiff(
return false
}
- compareInfo.Commits = models.ValidateCommitsWithEmails(compareInfo.Commits)
- compareInfo.Commits = models.ParseCommitsWithSignature(compareInfo.Commits, headRepo)
- compareInfo.Commits = models.ParseCommitsWithStatus(compareInfo.Commits, headRepo)
- ctx.Data["Commits"] = compareInfo.Commits
- ctx.Data["CommitCount"] = compareInfo.Commits.Len()
-
- if compareInfo.Commits.Len() == 1 {
- c := compareInfo.Commits.Front().Value.(models.SignCommitWithStatuses)
+ commits := models.ParseCommitsWithStatus(
+ models.ParseCommitsWithSignature(
+ models.ValidateCommitsWithEmails(compareInfo.Commits),
+ headRepo,
+ ),
+ headRepo,
+ )
+ ctx.Data["Commits"] = commits
+ ctx.Data["CommitCount"] = len(commits)
+
+ if len(commits) == 1 {
+ c := commits[0]
title = strings.TrimSpace(c.UserCommit.Summary())
body := strings.Split(strings.TrimSpace(c.UserCommit.Message()), "\n")
diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go
index e5554e9664444..aca596d95bd54 100644
--- a/routers/web/repo/pull.go
+++ b/routers/web/repo/pull.go
@@ -7,7 +7,6 @@
package repo
import (
- "container/list"
"crypto/subtle"
"errors"
"fmt"
@@ -327,11 +326,11 @@ func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) *git.C
ctx.ServerError("GetCompareInfo", err)
return nil
}
- ctx.Data["NumCommits"] = compareInfo.Commits.Len()
+ ctx.Data["NumCommits"] = len(compareInfo.Commits)
ctx.Data["NumFiles"] = compareInfo.NumFiles
- if compareInfo.Commits.Len() != 0 {
- sha := compareInfo.Commits.Front().Value.(*git.Commit).ID.String()
+ if len(compareInfo.Commits) != 0 {
+ sha := compareInfo.Commits[0].ID.String()
commitStatuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, sha, models.ListOptions{})
if err != nil {
ctx.ServerError("GetLatestCommitStatus", err)
@@ -411,7 +410,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare
return nil
}
- ctx.Data["NumCommits"] = compareInfo.Commits.Len()
+ ctx.Data["NumCommits"] = len(compareInfo.Commits)
ctx.Data["NumFiles"] = compareInfo.NumFiles
return compareInfo
}
@@ -531,7 +530,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare
ctx.Data["ConflictedFiles"] = pull.ConflictedFiles
}
- ctx.Data["NumCommits"] = compareInfo.Commits.Len()
+ ctx.Data["NumCommits"] = len(compareInfo.Commits)
ctx.Data["NumFiles"] = compareInfo.NumFiles
return compareInfo
}
@@ -547,7 +546,6 @@ func ViewPullCommits(ctx *context.Context) {
}
pull := issue.PullRequest
- var commits *list.List
var prInfo *git.CompareInfo
if pull.HasMerged {
prInfo = PrepareMergedViewPullInfo(ctx, issue)
@@ -564,12 +562,14 @@ func ViewPullCommits(ctx *context.Context) {
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
- commits = prInfo.Commits
- commits = models.ValidateCommitsWithEmails(commits)
- commits = models.ParseCommitsWithSignature(commits, ctx.Repo.Repository)
- commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
- ctx.Data["Commits"] = commits
- ctx.Data["CommitCount"] = commits.Len()
+ ctx.Data["Commits"] = models.ParseCommitsWithStatus(
+ models.ParseCommitsWithSignature(
+ models.ValidateCommitsWithEmails(prInfo.Commits),
+ ctx.Repo.Repository,
+ ),
+ ctx.Repo.Repository,
+ )
+ ctx.Data["CommitCount"] = len(prInfo.Commits)
getBranchData(ctx, issue)
ctx.HTML(http.StatusOK, tplPullCommits)
diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go
index 5271fe9b4ad99..c683f1c6e58e5 100644
--- a/routers/web/repo/wiki.go
+++ b/routers/web/repo/wiki.go
@@ -312,10 +312,10 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
ctx.ServerError("CommitsByFileAndRangeNoFollow", err)
return nil, nil
}
- commitsHistory = models.ValidateCommitsWithEmails(commitsHistory)
- commitsHistory = models.ParseCommitsWithSignature(commitsHistory, ctx.Repo.Repository)
-
- ctx.Data["Commits"] = commitsHistory
+ ctx.Data["Commits"] = models.ParseCommitsWithSignature(
+ models.ValidateCommitsWithEmails(commitsHistory),
+ ctx.Repo.Repository,
+ )
pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5)
pager.SetDefaultParams(ctx)
diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go
index a16724b36fefa..89b5df4638069 100644
--- a/services/mirror/mirror_pull.go
+++ b/services/mirror/mirror_pull.go
@@ -354,7 +354,7 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
continue
}
- theCommits := repo_module.ListToPushCommits(commits)
+ theCommits := repo_module.GitToPushCommits(commits)
if len(theCommits.Commits) > setting.UI.FeedMaxCommitNum {
theCommits.Commits = theCommits.Commits[:setting.UI.FeedMaxCommitNum]
}
diff --git a/services/pull/pull.go b/services/pull/pull.go
index db216ddbf4556..39eb99a770ac5 100644
--- a/services/pull/pull.go
+++ b/services/pull/pull.go
@@ -79,11 +79,11 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6
return err
}
- if compareInfo.Commits.Len() > 0 {
+ if len(compareInfo.Commits) > 0 {
data := models.PushActionContent{IsForcePush: false}
- data.CommitIDs = make([]string, 0, compareInfo.Commits.Len())
- for e := compareInfo.Commits.Back(); e != nil; e = e.Prev() {
- data.CommitIDs = append(data.CommitIDs, e.Value.(*git.Commit).ID.String())
+ data.CommitIDs = make([]string, 0, len(compareInfo.Commits))
+ for i := len(compareInfo.Commits) - 1; i >= 0; i-- {
+ data.CommitIDs = append(data.CommitIDs, compareInfo.Commits[i].ID.String())
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
@@ -573,7 +573,7 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
limit := setting.Repository.PullRequest.DefaultMergeMessageCommitsLimit
- list, err := gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, 0)
+ commits, err := gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, 0)
if err != nil {
log.Error("Unable to get commits between: %s %s Error: %v", pr.HeadBranch, pr.MergeBase, err)
return ""
@@ -582,7 +582,7 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
posterSig := pr.Issue.Poster.NewGitSig().String()
authorsMap := map[string]bool{}
- authors := make([]string, 0, list.Len())
+ authors := make([]string, 0, len(commits))
stringBuilder := strings.Builder{}
if !setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages {
@@ -597,15 +597,16 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
}
// commits list is in reverse chronological order
- element := list.Back()
- for element != nil {
- commit := element.Value.(*git.Commit)
+ first := true
+ for i := len(commits) - 1; i >= 0; i-- {
+ commit := commits[i]
if setting.Repository.PullRequest.PopulateSquashCommentWithCommitMessages {
maxSize := setting.Repository.PullRequest.DefaultMergeMessageSize
if maxSize < 0 || stringBuilder.Len() < maxSize {
var toWrite []byte
- if element == list.Back() {
+ if first {
+ first = false
toWrite = []byte(strings.TrimPrefix(commit.CommitMessage, pr.Issue.Title))
} else {
toWrite = []byte(commit.CommitMessage)
@@ -631,7 +632,6 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
authors = append(authors, authorString)
authorsMap[authorString] = true
}
- element = element.Prev()
}
// Consider collecting the remaining authors
@@ -639,25 +639,21 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string {
skip := limit
limit = 30
for {
- list, err := gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, skip)
+ commits, err := gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, skip)
if err != nil {
log.Error("Unable to get commits between: %s %s Error: %v", pr.HeadBranch, pr.MergeBase, err)
return ""
}
- if list.Len() == 0 {
+ if len(commits) == 0 {
break
}
- element := list.Front()
- for element != nil {
- commit := element.Value.(*git.Commit)
-
+ for _, commit := range commits {
authorString := commit.Author.String()
if !authorsMap[authorString] && authorString != posterSig {
authors = append(authors, authorString)
authorsMap[authorString] = true
}
- element = element.Next()
}
skip += limit
}
diff --git a/services/repository/push.go b/services/repository/push.go
index dcb3bc779fc98..9d071704ad219 100644
--- a/services/repository/push.go
+++ b/services/repository/push.go
@@ -5,7 +5,6 @@
package repository
import (
- "container/list"
"fmt"
"time"
@@ -148,7 +147,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
refName := opts.RefName()
// Push new branch.
- var l *list.List
+ var l []*git.Commit
if opts.IsNewRef() {
if repo.IsEmpty { // Change default branch and empty status only if pushed ref is non-empty branch.
repo.DefaultBranch = refName
@@ -192,7 +191,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
}
}
- commits = repo_module.ListToPushCommits(l)
+ commits = repo_module.GitToPushCommits(l)
if err := repofiles.UpdateIssuesCommit(pusher, repo, commits.Commits, refName); err != nil {
log.Error("updateIssuesCommit: %v", err)
diff --git a/templates/mail/issue/default.tmpl b/templates/mail/issue/default.tmpl
index f7257f6e6b5ce..b60d0401c8922 100644
--- a/templates/mail/issue/default.tmpl
+++ b/templates/mail/issue/default.tmpl
@@ -30,10 +30,10 @@
{{.i18n.Tr "mail.issue.action.force_push" .Doer.Name .Comment.Issue.PullRequest.HeadBranch $oldCommitLink $newCommitLink | Str2html}}
{{else}}
- {{if eq .Comment.Commits.Len 1}}
+ {{if eq (len .Comment.Commits) 1}}
{{.i18n.Tr "mail.issue.action.push_1" .Doer.Name .Comment.Issue.PullRequest.HeadBranch | Str2html}}
{{else}}
- {{.i18n.Tr "mail.issue.action.push_n" .Doer.Name .Comment.Issue.PullRequest.HeadBranch .Comment.Commits.Len | Str2html}}
+ {{.i18n.Tr "mail.issue.action.push_n" .Doer.Name .Comment.Issue.PullRequest.HeadBranch (len .Comment.Commits) | Str2html}}
{{end}}
{{end}}
@@ -73,9 +73,8 @@
{{end -}}
{{if eq .ActionName "push"}}
- {{ $r:= List .Comment.Commits}}
- {{range $r}}
+ {{range .Comment.Commits}}
-
{{ShortSha .ID.String}}
diff --git a/templates/repo/commits_list.tmpl b/templates/repo/commits_list.tmpl
index 66138e21389db..8376da1f9b7bd 100644
--- a/templates/repo/commits_list.tmpl
+++ b/templates/repo/commits_list.tmpl
@@ -9,8 +9,7 @@
- {{ $r:= List .Commits}}
- {{range $r}}
+ {{range .Commits}}
{{$userName := .Author.Name}}
diff --git a/templates/repo/commits_list_small.tmpl b/templates/repo/commits_list_small.tmpl
index acab040a45040..71d33d8531073 100644
--- a/templates/repo/commits_list_small.tmpl
+++ b/templates/repo/commits_list_small.tmpl
@@ -1,7 +1,6 @@
-{{ $r:= List .comment.Commits}}
{{ $index := 0}}
-{{range $r}}
+{{range .comment.Commits}}
{{ $tag := printf "%s-%d" $.comment.HashTag $index }}
{{ $index = Add $index 1}}
diff --git a/templates/repo/issue/view_content/comments.tmpl b/templates/repo/issue/view_content/comments.tmpl
index dcc0401c99962..7bc90b0434b70 100644
--- a/templates/repo/issue/view_content/comments.tmpl
+++ b/templates/repo/issue/view_content/comments.tmpl
@@ -699,7 +699,7 @@
{{ if .IsForcePush }}
{{$.i18n.Tr "repo.issues.force_push_codes" $.Issue.PullRequest.HeadBranch (ShortSha .OldCommit) ($.Issue.Repo.CommitLink .OldCommit) (ShortSha .NewCommit) ($.Issue.Repo.CommitLink .NewCommit) $createdStr | Safe}}
{{else}}
- {{$.i18n.Tr (TrN $.i18n.Lang .Commits.Len "repo.issues.push_commit_1" "repo.issues.push_commits_n") .Commits.Len $createdStr | Safe}}
+ {{$.i18n.Tr (TrN $.i18n.Lang (len .Commits) "repo.issues.push_commit_1" "repo.issues.push_commits_n") (len .Commits) $createdStr | Safe}}
{{end}}
diff --git a/templates/user/dashboard/feeds.tmpl b/templates/user/dashboard/feeds.tmpl
index 057a4a76256e0..6d6f0b745c6ed 100644
--- a/templates/user/dashboard/feeds.tmpl
+++ b/templates/user/dashboard/feeds.tmpl
@@ -89,17 +89,15 @@
{{ $push := ActionContent2Commits .}}
{{ $repoLink := .GetRepoLink}}
- {{if $push.Commits}}
- {{range $push.Commits}}
- {{ $commitLink := printf "%s/commit/%s" $repoLink .Sha1}}
- -
- {{avatarHTML ($push.AvatarLink .AuthorEmail) 16 "mr-2" .AuthorName}}
- {{ShortSha .Sha1}}
-
- {{RenderCommitMessage .Message $repoLink $.ComposeMetas}}
-
-
- {{end}}
+ {{range $push.Commits}}
+ {{ $commitLink := printf "%s/commit/%s" $repoLink .Sha1}}
+ -
+ {{avatarHTML ($push.AvatarLink .AuthorEmail) 16 "mr-2" .AuthorName}}
+ {{ShortSha .Sha1}}
+
+ {{RenderCommitMessage .Message $repoLink $.ComposeMetas}}
+
+
{{end}}
{{if and (gt $push.Len 1) $push.CompareURL}}- {{$.i18n.Tr "action.compare_commits" $push.Len}} ยป
{{end}}
From f236707dd4b8517ab61ae9681b58cdb4f08d396e Mon Sep 17 00:00:00 2001
From: KN4CK3R
Date: Wed, 30 Jun 2021 07:33:00 +0200
Subject: [PATCH 2/8] Fixed usage of pointer to temporary variable.
---
models/commit_status.go | 8 ++++----
models/gpg_key.go | 8 ++++----
models/issue_comment.go | 10 +++++-----
models/user.go | 6 +++---
routers/web/repo/blame.go | 8 +++-----
5 files changed, 19 insertions(+), 21 deletions(-)
diff --git a/models/commit_status.go b/models/commit_status.go
index 49719f4205d53..c27582024280f 100644
--- a/models/commit_status.go
+++ b/models/commit_status.go
@@ -256,12 +256,12 @@ type SignCommitWithStatuses struct {
}
// ParseCommitsWithStatus checks commits latest statuses and calculates its worst status state
-func ParseCommitsWithStatus(oldCommits []SignCommit, repo *Repository) []SignCommitWithStatuses {
- newCommits := make([]SignCommitWithStatuses, 0, len(oldCommits))
+func ParseCommitsWithStatus(oldCommits []*SignCommit, repo *Repository) []*SignCommitWithStatuses {
+ newCommits := make([]*SignCommitWithStatuses, 0, len(oldCommits))
for _, c := range oldCommits {
- commit := SignCommitWithStatuses{
- SignCommit: &c,
+ commit := &SignCommitWithStatuses{
+ SignCommit: c,
}
statuses, err := GetLatestCommitStatus(repo.ID, commit.ID.String(), ListOptions{})
if err != nil {
diff --git a/models/gpg_key.go b/models/gpg_key.go
index 4dff0e5979075..84e504d30131d 100644
--- a/models/gpg_key.go
+++ b/models/gpg_key.go
@@ -829,13 +829,13 @@ func verifyWithGPGSettings(gpgSettings *git.GPGSettings, sig *packet.Signature,
}
// ParseCommitsWithSignature checks if signaute of commits are corresponding to users gpg keys.
-func ParseCommitsWithSignature(oldCommits []UserCommit, repository *Repository) []SignCommit {
- newCommits := make([]SignCommit, 0, len(oldCommits))
+func ParseCommitsWithSignature(oldCommits []*UserCommit, repository *Repository) []*SignCommit {
+ newCommits := make([]*SignCommit, 0, len(oldCommits))
keyMap := map[string]bool{}
for _, c := range oldCommits {
- signCommit := SignCommit{
- UserCommit: &c,
+ signCommit := &SignCommit{
+ UserCommit: c,
Verification: ParseCommitWithSignature(c.Commit),
}
diff --git a/models/issue_comment.go b/models/issue_comment.go
index 9ce7655747ed2..d1ecb542559a0 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -191,11 +191,11 @@ type Comment struct {
RefIssue *Issue `xorm:"-"`
RefComment *Comment `xorm:"-"`
- Commits []SignCommitWithStatuses `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
diff --git a/models/user.go b/models/user.go
index 029b808fc34d4..bf25861f7dc6f 100644
--- a/models/user.go
+++ b/models/user.go
@@ -1502,10 +1502,10 @@ func ValidateCommitWithEmail(c *git.Commit) *User {
}
// ValidateCommitsWithEmails checks if authors' e-mails of commits are corresponding to users.
-func ValidateCommitsWithEmails(oldCommits []*git.Commit) []UserCommit {
+func ValidateCommitsWithEmails(oldCommits []*git.Commit) []*UserCommit {
var (
emails = make(map[string]*User)
- newCommits = make([]UserCommit, 0, len(oldCommits))
+ newCommits = make([]*UserCommit, 0, len(oldCommits))
)
for _, c := range oldCommits {
var u *User
@@ -1518,7 +1518,7 @@ func ValidateCommitsWithEmails(oldCommits []*git.Commit) []UserCommit {
}
}
- newCommits = append(newCommits, UserCommit{
+ newCommits = append(newCommits, &UserCommit{
User: u,
Commit: c,
})
diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go
index 7add7dbb9300f..c2da8e9cdca53 100644
--- a/routers/web/repo/blame.go
+++ b/routers/web/repo/blame.go
@@ -137,9 +137,9 @@ func RefBlame(ctx *context.Context) {
ctx.HTML(http.StatusOK, tplBlame)
}
-func processBlameParts(ctx *context.Context, blameParts []git.BlamePart) (map[string]models.UserCommit, map[string]string) {
+func processBlameParts(ctx *context.Context, blameParts []git.BlamePart) (map[string]*models.UserCommit, map[string]string) {
// store commit data by SHA to look up avatar info etc
- commitNames := make(map[string]models.UserCommit)
+ commitNames := make(map[string]*models.UserCommit)
// previousCommits contains links from SHA to parent SHA,
// if parent also contains the current TreePath.
previousCommits := make(map[string]string)
@@ -190,8 +190,6 @@ func processBlameParts(ctx *context.Context, blameParts []git.BlamePart) (map[st
}
commits = append(commits, commit)
-
- commitNames[commit.ID.String()] = models.UserCommit{}
}
// populate commit email addresses to later look up avatars.
@@ -202,7 +200,7 @@ func processBlameParts(ctx *context.Context, blameParts []git.BlamePart) (map[st
return commitNames, previousCommits
}
-func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames map[string]models.UserCommit, previousCommits map[string]string) {
+func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames map[string]*models.UserCommit, previousCommits map[string]string) {
repoLink := ctx.Repo.RepoLink
var lines = make([]string, 0)
From d226063150d507751628182371698410091f3d64 Mon Sep 17 00:00:00 2001
From: KN4CK3R
Date: Wed, 30 Jun 2021 07:38:00 +0200
Subject: [PATCH 3/8] Replaced LIFO list with slice.
---
models/issue_comment.go | 50 ++++++++++++++---------------------------
1 file changed, 17 insertions(+), 33 deletions(-)
diff --git a/models/issue_comment.go b/models/issue_comment.go
index d1ecb542559a0..636d6a7119dfa 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -7,7 +7,6 @@
package models
import (
- "container/list"
"fmt"
"regexp"
"strconv"
@@ -1265,7 +1264,7 @@ func CreatePushPullComment(pusher *User, pr *PullRequest, oldCommitID, newCommit
return
}
-// getCommitsFromRepo get commit IDs from repo in betwern oldCommitID and newCommitID
+// getCommitsFromRepo get commit IDs from repo in between oldCommitID and newCommitID
// isForcePush will be true if oldCommit isn't on the branch
// Commit on baseBranch will skip
func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) {
@@ -1304,10 +1303,10 @@ func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch
}
commitIDs = make([]string, 0, len(commits))
- commitChecks := make(map[string]commitBranchCheckItem)
+ commitChecks := make(map[string]*commitBranchCheckItem)
for _, commit := range commits {
- commitChecks[commit.ID.String()] = commitBranchCheckItem{
+ commitChecks[commit.ID.String()] = &commitBranchCheckItem{
Commit: commit,
Checked: false,
}
@@ -1332,64 +1331,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
}
From 5390964f5d9bafc93ec619345baed76bcf79931b Mon Sep 17 00:00:00 2001
From: KN4CK3R
Date: Wed, 30 Jun 2021 11:11:02 +0000
Subject: [PATCH 4/8] Lint
---
models/issue_comment.go | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/models/issue_comment.go b/models/issue_comment.go
index 636d6a7119dfa..1aa3ddfeb7307 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -1340,10 +1340,10 @@ func commitBranchCheck(gitRepo *git.Repository, startCommit *git.Commit, endComm
checkStack = append(checkStack, startCommit.ID.String())
for len(checkStack) > 0 {
- commitId := checkStack[0]
+ commitID := checkStack[0]
checkStack = checkStack[1:]
- item, ok := commitList[commitId]
+ item, ok := commitList[commitID]
if !ok {
continue
}
From 5ad2fd588ce7721baf34b01f46a7d5ec42d5023c Mon Sep 17 00:00:00 2001
From: KN4CK3R
Date: Wed, 30 Jun 2021 16:38:00 +0200
Subject: [PATCH 5/8] Removed type check.
---
modules/templates/helper.go | 12 ------------
routers/web/repo/wiki.go | 7 +++++--
templates/repo/commits_list.tmpl | 4 +---
templates/repo/commits_list_small.tmpl | 4 +---
4 files changed, 7 insertions(+), 20 deletions(-)
diff --git a/modules/templates/helper.go b/modules/templates/helper.go
index b7c081a6a9209..6898cda1e820e 100644
--- a/modules/templates/helper.go
+++ b/modules/templates/helper.go
@@ -297,18 +297,6 @@ func NewFuncMap() []template.FuncMap {
},
"CommentMustAsDiff": gitdiff.CommentMustAsDiff,
"MirrorRemoteAddress": mirrorRemoteAddress,
- "CommitType": func(commit interface{}) string {
- switch commit.(type) {
- case models.SignCommitWithStatuses:
- return "SignCommitWithStatuses"
- case models.SignCommit:
- return "SignCommit"
- case models.UserCommit:
- return "UserCommit"
- default:
- return ""
- }
- },
"NotificationSettings": func() map[string]interface{} {
return map[string]interface{}{
"MinTimeout": int(setting.UI.Notification.MinTimeout / time.Millisecond),
diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go
index c683f1c6e58e5..2197d5cd04f59 100644
--- a/routers/web/repo/wiki.go
+++ b/routers/web/repo/wiki.go
@@ -312,8 +312,11 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
ctx.ServerError("CommitsByFileAndRangeNoFollow", err)
return nil, nil
}
- ctx.Data["Commits"] = models.ParseCommitsWithSignature(
- models.ValidateCommitsWithEmails(commitsHistory),
+ ctx.Data["Commits"] = models.ParseCommitsWithStatus(
+ models.ParseCommitsWithSignature(
+ models.ValidateCommitsWithEmails(commitsHistory),
+ ctx.Repo.Repository,
+ ),
ctx.Repo.Repository,
)
diff --git a/templates/repo/commits_list.tmpl b/templates/repo/commits_list.tmpl
index 8376da1f9b7bd..5282430ec796f 100644
--- a/templates/repo/commits_list.tmpl
+++ b/templates/repo/commits_list.tmpl
@@ -68,9 +68,7 @@
{{if IsMultilineCommitMessage .Message}}
{{end}}
- {{if eq (CommitType .) "SignCommitWithStatuses"}}
- {{template "repo/commit_statuses" dict "Status" .Status "Statuses" .Statuses "root" $}}
- {{end}}
+ {{template "repo/commit_statuses" dict "Status" .Status "Statuses" .Statuses "root" $}}
{{if IsMultilineCommitMessage .Message}}
{{RenderCommitBody .Message $.RepoLink $.Repository.ComposeMetas}}
{{end}}
diff --git a/templates/repo/commits_list_small.tmpl b/templates/repo/commits_list_small.tmpl
index 71d33d8531073..bdbee816cc580 100644
--- a/templates/repo/commits_list_small.tmpl
+++ b/templates/repo/commits_list_small.tmpl
@@ -14,9 +14,7 @@
{{end}}
- {{if eq (CommitType .) "SignCommitWithStatuses"}}
- {{template "repo/commit_statuses" dict "Status" .Status "Statuses" .Statuses "root" $.root}}
- {{end}}
+ {{template "repo/commit_statuses" dict "Status" .Status "Statuses" .Statuses "root" $.root}}
{{$class := "ui sha label"}}
{{if .Signature}}
{{$class = (printf "%s%s" $class " isSigned")}}
From bfb61b8e34924ace8fc15178290fe9021b594762 Mon Sep 17 00:00:00 2001
From: KN4CK3R
Date: Wed, 30 Jun 2021 14:54:23 +0000
Subject: [PATCH 6/8] Removed duplicated code.
---
models/commit.go | 20 ++++++++++++++++++++
models/issue_comment.go | 10 +---------
routers/web/repo/commit.go | 24 +++---------------------
routers/web/repo/compare.go | 8 +-------
routers/web/repo/pull.go | 12 ++++--------
routers/web/repo/wiki.go | 8 +-------
6 files changed, 30 insertions(+), 52 deletions(-)
create mode 100644 models/commit.go
diff --git a/models/commit.go b/models/commit.go
new file mode 100644
index 0000000000000..474825820afab
--- /dev/null
+++ b/models/commit.go
@@ -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,
+ )
+}
diff --git a/models/issue_comment.go b/models/issue_comment.go
index 1aa3ddfeb7307..67cfcd465219f 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -675,15 +675,7 @@ func (c *Comment) LoadPushCommits() (err error) {
}
defer gitRepo.Close()
- c.Commits = ParseCommitsWithStatus(
- ParseCommitsWithSignature(
- ValidateCommitsWithEmails(
- gitRepo.GetCommitsFromIDs(data.CommitIDs),
- ),
- c.Issue.Repo,
- ),
- c.Issue.Repo,
- )
+ c.Commits = ConvertFromGitCommit(gitRepo.GetCommitsFromIDs(data.CommitIDs), c.Issue.Repo)
c.CommitsNum = int64(len(c.Commits))
}
diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go
index f231b9b675b8b..77c1d237ef3b8 100644
--- a/routers/web/repo/commit.go
+++ b/routers/web/repo/commit.go
@@ -72,13 +72,7 @@ func Commits(ctx *context.Context) {
ctx.ServerError("CommitsByRange", err)
return
}
- ctx.Data["Commits"] = models.ParseCommitsWithStatus(
- models.ParseCommitsWithSignature(
- models.ValidateCommitsWithEmails(commits),
- ctx.Repo.Repository,
- ),
- ctx.Repo.Repository,
- )
+ ctx.Data["Commits"] = models.ConvertFromGitCommit(commits, ctx.Repo.Repository)
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
@@ -197,13 +191,7 @@ func SearchCommits(ctx *context.Context) {
return
}
ctx.Data["CommitCount"] = len(commits)
- ctx.Data["Commits"] = models.ParseCommitsWithStatus(
- models.ParseCommitsWithSignature(
- models.ValidateCommitsWithEmails(commits),
- ctx.Repo.Repository,
- ),
- ctx.Repo.Repository,
- )
+ ctx.Data["Commits"] = models.ConvertFromGitCommit(commits, ctx.Repo.Repository)
ctx.Data["Keyword"] = query
if all {
@@ -245,13 +233,7 @@ func FileHistory(ctx *context.Context) {
ctx.ServerError("CommitsByFileAndRange", err)
return
}
- ctx.Data["Commits"] = models.ParseCommitsWithStatus(
- models.ParseCommitsWithSignature(
- models.ValidateCommitsWithEmails(commits),
- ctx.Repo.Repository,
- ),
- ctx.Repo.Repository,
- )
+ ctx.Data["Commits"] = models.ConvertFromGitCommit(commits, ctx.Repo.Repository)
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go
index de35ecb0e4f72..35737787c8e29 100644
--- a/routers/web/repo/compare.go
+++ b/routers/web/repo/compare.go
@@ -550,13 +550,7 @@ func PrepareCompareDiff(
return false
}
- commits := models.ParseCommitsWithStatus(
- models.ParseCommitsWithSignature(
- models.ValidateCommitsWithEmails(compareInfo.Commits),
- headRepo,
- ),
- headRepo,
- )
+ commits := models.ConvertFromGitCommit(compareInfo.Commits, headRepo)
ctx.Data["Commits"] = commits
ctx.Data["CommitCount"] = len(commits)
diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go
index aca596d95bd54..bd52ae15b7057 100644
--- a/routers/web/repo/pull.go
+++ b/routers/web/repo/pull.go
@@ -562,14 +562,10 @@ func ViewPullCommits(ctx *context.Context) {
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
- ctx.Data["Commits"] = models.ParseCommitsWithStatus(
- models.ParseCommitsWithSignature(
- models.ValidateCommitsWithEmails(prInfo.Commits),
- ctx.Repo.Repository,
- ),
- ctx.Repo.Repository,
- )
- ctx.Data["CommitCount"] = len(prInfo.Commits)
+
+ commits := models.ConvertFromGitCommit(prInfo.Commits, ctx.Repo.Repository)
+ ctx.Data["Commits"] = commits
+ ctx.Data["CommitCount"] = len(commits)
getBranchData(ctx, issue)
ctx.HTML(http.StatusOK, tplPullCommits)
diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go
index 2197d5cd04f59..22bed019ae98d 100644
--- a/routers/web/repo/wiki.go
+++ b/routers/web/repo/wiki.go
@@ -312,13 +312,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
ctx.ServerError("CommitsByFileAndRangeNoFollow", err)
return nil, nil
}
- ctx.Data["Commits"] = models.ParseCommitsWithStatus(
- models.ParseCommitsWithSignature(
- models.ValidateCommitsWithEmails(commitsHistory),
- ctx.Repo.Repository,
- ),
- ctx.Repo.Repository,
- )
+ ctx.Data["Commits"] = models.ConvertFromGitCommit(commitsHistory, ctx.Repo.Repository)
pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5)
pager.SetDefaultParams(ctx)
From 1e0158f3c96115132c4d148bab9e2cdbe6939597 Mon Sep 17 00:00:00 2001
From: KN4CK3R
Date: Tue, 20 Jul 2021 23:00:39 +0000
Subject: [PATCH 7/8] Lint
---
routers/api/v1/repo/pull.go | 3 ---
1 file changed, 3 deletions(-)
diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go
index 81ce068798713..4f305904b7f2c 100644
--- a/routers/api/v1/repo/pull.go
+++ b/routers/api/v1/repo/pull.go
@@ -1224,9 +1224,6 @@ func GetPullRequestCommits(ctx *context.APIContext) {
apiCommits := make([]*api.Commit, 0, end-start)
for i := start; i < end; i++ {
- commit := commits[i]
-
- // Create json struct
apiCommit, err := convert.ToCommit(ctx.Repo.Repository, commits[i], userCache)
if err != nil {
ctx.ServerError("toCommit", err)
From d3a6598bbba5a3702d8568499a5090217d02cc8b Mon Sep 17 00:00:00 2001
From: KN4CK3R
Date: Thu, 22 Jul 2021 15:51:38 +0000
Subject: [PATCH 8/8] Fixed merge.
---
modules/git/repo_commit_test.go | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/modules/git/repo_commit_test.go b/modules/git/repo_commit_test.go
index a6c27ea4d55bd..594333484369b 100644
--- a/modules/git/repo_commit_test.go
+++ b/modules/git/repo_commit_test.go
@@ -97,6 +97,6 @@ func TestRepository_CommitsBetweenIDs(t *testing.T) {
for i, c := range cases {
commits, err := bareRepo1.CommitsBetweenIDs(c.NewID, c.OldID)
assert.NoError(t, err)
- assert.Equal(t, c.ExpectedCommits, commits.Len(), "case %d", i)
+ assert.Equal(t, c.ExpectedCommits, len(commits), "case %d", i)
}
}
|