diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2021-08-09 20:08:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-09 14:08:51 -0400 |
commit | d9ef43a7126ab83af563c1c9b54cdf0092327b2a (patch) | |
tree | fabc9a9d24d42bb0de6dd9557c9e07e95bd5722b /modules | |
parent | 23d438f56524a7c3fc185df66d6d95f797a80eee (diff) | |
download | gitea-d9ef43a7126ab83af563c1c9b54cdf0092327b2a.tar.gz gitea-d9ef43a7126ab83af563c1c9b54cdf0092327b2a.zip |
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>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/git/commit.go | 11 | ||||
-rw-r--r-- | modules/git/repo.go | 11 | ||||
-rw-r--r-- | modules/git/repo_commit.go | 38 | ||||
-rw-r--r-- | modules/git/repo_commit_test.go | 2 | ||||
-rw-r--r-- | modules/git/repo_compare.go | 5 | ||||
-rw-r--r-- | modules/repository/commits.go | 12 | ||||
-rw-r--r-- | modules/repository/commits_test.go | 30 | ||||
-rw-r--r-- | modules/templates/helper.go | 29 |
8 files changed, 51 insertions, 87 deletions
diff --git a/modules/git/commit.go b/modules/git/commit.go index 3ce2b03886..fe2c2b9774 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" @@ -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) } @@ -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 @@ -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) } diff --git a/modules/git/repo.go b/modules/git/repo.go index 43f329f448..4e6f90c3ef 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 16ee5b2fd6..e456f04e87 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 revision 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 revision 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 { @@ -266,7 +265,7 @@ func (repo *Repository) FilesCountBetween(startCommitID, endCommitID string) (in // CommitsBetween returns a list that contains commits between [before, last). // If before is detached (removed by reset + push) it is not included. -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 { @@ -286,7 +285,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 [before, last) -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 { @@ -306,7 +305,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 @@ -334,7 +333,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()) @@ -352,9 +351,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 @@ -364,17 +362,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) } @@ -413,13 +411,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_commit_test.go b/modules/git/repo_commit_test.go index a6c27ea4d5..5943334843 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) } } diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index 3255e68392..5d1208aab1 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 eaaf3b8b19..7f22105745 100644 --- a/modules/repository/commits.go +++ b/modules/repository/commits.go @@ -5,7 +5,6 @@ package repository import ( - "container/list" "fmt" "time" @@ -175,12 +174,11 @@ func CommitToPushCommit(commit *git.Commit) *PushCommit { } } -// ListToPushCommits transforms a list.List to PushCommits type. -func ListToPushCommits(l *list.List) *PushCommits { - var commits []*PushCommit - for e := l.Front(); e != nil; e = e.Next() { - commit := CommitToPushCommit(e.Value.(*git.Commit)) - commits = append(commits, commit) +// 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{commits, nil, "", make(map[string]string), make(map[string]*models.User)} } diff --git a/modules/repository/commits_test.go b/modules/repository/commits_test.go index 8e0d8bf90f..9f08525258 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" @@ -173,21 +172,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) if assert.Len(t, pushCommits.Commits, 2) { assert.Equal(t, "Message1", pushCommits.Commits[0].Message) assert.Equal(t, hexString1, pushCommits.Commits[0].Sha1) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 1e8f00b669..6517127ebf 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -7,7 +7,6 @@ package templates import ( "bytes" - "container/list" "errors" "fmt" "html" @@ -126,7 +125,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 "" @@ -297,18 +295,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), @@ -428,7 +414,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 "" @@ -636,20 +621,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) |