From 04d489dbdd9906c51bf836203eb5d1af061902e1 Mon Sep 17 00:00:00 2001 From: Giteabot Date: Wed, 15 Mar 2023 08:13:27 -0400 Subject: [PATCH] Make branches list page operations remember current page (#23420) (#23459) Backport #23420 by @wxiaoguang Close #23411 Always pass "page" query parameter to backend, and make backend respect it. The `ctx.FormInt("limit")` is never used, so removed. Co-authored-by: wxiaoguang Co-authored-by: Jason Song Co-authored-by: Lunny Xiao Co-authored-by: KN4CK3R --- modules/context/pagination.go | 7 ++++--- routers/web/repo/branch.go | 17 +++++++---------- templates/repo/branch/list.tmpl | 8 ++++---- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/modules/context/pagination.go b/modules/context/pagination.go index 617b472f0d..8cd3c1d5a4 100644 --- a/modules/context/pagination.go +++ b/modules/context/pagination.go @@ -19,10 +19,11 @@ type Pagination struct { urlParams []string } -// NewPagination creates a new instance of the Pagination struct -func NewPagination(total, page, issueNum, numPages int) *Pagination { +// NewPagination creates a new instance of the Pagination struct. +// "pagingNum" is "page size" or "limit", "current" is "page" +func NewPagination(total, pagingNum, current, numPages int) *Pagination { p := &Pagination{} - p.Paginater = paginator.New(total, page, issueNum, numPages) + p.Paginater = paginator.New(total, pagingNum, current, numPages) return p } diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index 6085fd8693..bc7f87451a 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "net/http" + "net/url" "strings" "code.gitea.io/gitea/models" @@ -66,21 +67,17 @@ func Branches(ctx *context.Context) { if page <= 1 { page = 1 } + pageSize := setting.Git.BranchesRangeSize - limit := ctx.FormInt("limit") - if limit <= 0 || limit > setting.Git.BranchesRangeSize { - limit = setting.Git.BranchesRangeSize - } - - skip := (page - 1) * limit - log.Debug("Branches: skip: %d limit: %d", skip, limit) - defaultBranchBranch, branches, branchesCount := loadBranches(ctx, skip, limit) + skip := (page - 1) * pageSize + log.Debug("Branches: skip: %d limit: %d", skip, pageSize) + defaultBranchBranch, branches, branchesCount := loadBranches(ctx, skip, pageSize) if ctx.Written() { return } ctx.Data["Branches"] = branches ctx.Data["DefaultBranchBranch"] = defaultBranchBranch - pager := context.NewPagination(branchesCount, setting.Git.BranchesRangeSize, page, 5) + pager := context.NewPagination(branchesCount, pageSize, page, 5) pager.SetDefaultParams(ctx) ctx.Data["Page"] = pager @@ -166,7 +163,7 @@ func RestoreBranchPost(ctx *context.Context) { func redirect(ctx *context.Context) { ctx.JSON(http.StatusOK, map[string]interface{}{ - "redirect": ctx.Repo.RepoLink + "/branches", + "redirect": ctx.Repo.RepoLink + "/branches?page=" + url.QueryEscape(ctx.FormString("page")), }) } diff --git a/templates/repo/branch/list.tmpl b/templates/repo/branch/list.tmpl index 3702410df5..acb73f71d0 100644 --- a/templates/repo/branch/list.tmpl +++ b/templates/repo/branch/list.tmpl @@ -81,9 +81,9 @@ {{if not .LatestPullRequest}} {{if .IsIncluded}} - + {{svg "octicon-git-pull-request"}} {{$.locale.Tr "repo.branch.included"}} - + {{else if and (not .IsDeleted) $.AllowsPulls (gt .CommitsAhead 0)}} @@ -123,13 +123,13 @@ {{end}} {{if and $.IsWriter (not $.IsMirror) (not $.Repository.IsArchived) (not .IsProtected)}} {{if .IsDeleted}} - {{else}} - {{end}}