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 /routers | |
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 'routers')
-rw-r--r-- | routers/api/v1/repo/commits.go | 10 | ||||
-rw-r--r-- | routers/api/v1/repo/pull.go | 25 | ||||
-rw-r--r-- | routers/web/repo/blame.go | 17 | ||||
-rw-r--r-- | routers/web/repo/commit.go | 17 | ||||
-rw-r--r-- | routers/web/repo/compare.go | 14 | ||||
-rw-r--r-- | routers/web/repo/pull.go | 20 | ||||
-rw-r--r-- | routers/web/repo/wiki.go | 5 |
7 files changed, 32 insertions, 76 deletions
diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index e4bea4dee7..9950a7d456 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/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 3ae7b2683e..9be6228bfd 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -1211,7 +1211,7 @@ func GetPullRequestCommits(ctx *context.APIContext) { listOptions := utils.GetListOptions(ctx) - totalNumberOfCommits := commits.Len() + totalNumberOfCommits := len(commits) totalNumberOfPages := int(math.Ceil(float64(totalNumberOfCommits) / float64(listOptions.PageSize))) userCache := make(map[string]*models.User) @@ -1222,29 +1222,14 @@ func GetPullRequestCommits(ctx *context.APIContext) { end = totalNumberOfCommits } - apiCommits := make([]*api.Commit, end-start) - - i := 0 - addedCommitsCount := 0 - for commitPointer := commits.Front(); commitPointer != nil; commitPointer = commitPointer.Next() { - if i < start { - i++ - continue - } - if i >= end { - break - } - - commit := commitPointer.Value.(*git.Commit) - - // Create json struct - apiCommits[addedCommitsCount], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache) - addedCommitsCount++ + apiCommits := make([]*api.Commit, 0, end-start) + for i := start; i < end; i++ { + apiCommit, err := convert.ToCommit(ctx.Repo.Repository, commits[i], userCache) if err != nil { ctx.ServerError("toCommit", err) return } - i++ + apiCommits = append(apiCommits, apiCommit) } ctx.SetLinkHeader(int(totalNumberOfCommits), listOptions.PageSize) diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go index 4ade9e9a93..c2da8e9cdc 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" @@ -138,15 +137,15 @@ 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) // 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,22 +189,18 @@ func processBlameParts(ctx *context.Context, blameParts []git.BlamePart) (map[st } } - commits.PushBack(commit) - - commitNames[commit.ID.String()] = models.UserCommit{} + commits = append(commits, commit) } // 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 } 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) diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index fcc17a40ab..e1d93a2435 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -72,10 +72,7 @@ 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.ConvertFromGitCommit(commits, ctx.Repo.Repository) ctx.Data["Username"] = ctx.Repo.Owner.Name ctx.Data["Reponame"] = ctx.Repo.Repository.Name @@ -193,10 +190,8 @@ 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.ConvertFromGitCommit(commits, ctx.Repo.Repository) ctx.Data["Keyword"] = query if all { @@ -204,7 +199,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 +233,7 @@ 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.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 45d0624ca2..511a74cdd5 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -551,14 +551,12 @@ 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.ConvertFromGitCommit(compareInfo.Commits, 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 5c61e0d5fa..ccdd670e6a 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 } @@ -543,7 +542,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 } @@ -559,7 +558,6 @@ func ViewPullCommits(ctx *context.Context) { } pull := issue.PullRequest - var commits *list.List var prInfo *git.CompareInfo if pull.HasMerged { prInfo = PrepareMergedViewPullInfo(ctx, issue) @@ -576,12 +574,10 @@ 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) + + commits := models.ConvertFromGitCommit(prInfo.Commits, ctx.Repo.Repository) ctx.Data["Commits"] = commits - ctx.Data["CommitCount"] = commits.Len() + 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 7226039e21..2057229bf9 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -312,10 +312,7 @@ 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.ConvertFromGitCommit(commitsHistory, ctx.Repo.Repository) pager := context.NewPagination(int(commitsCount), setting.Git.CommitsRangeSize, page, 5) pager.SetDefaultParams(ctx) |