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/git | |
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/git')
-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 |
5 files changed, 31 insertions, 36 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 |