From 07801cbf094b55bd848865176dd0eafe99b37ad7 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Fri, 17 Feb 2017 17:41:55 -0500 Subject: [PATCH] repo/release: add pagination (#2164) --- cmd/web.go | 2 +- gogs.go | 2 +- routers/repo/release.go | 73 +++++++-------- templates/.VERSION | 2 +- templates/repo/release/list.tmpl | 8 ++ vendor/github.com/gogits/git-module/git.go | 2 +- .../github.com/gogits/git-module/repo_tag.go | 90 +++++++++++++++++-- vendor/vendor.json | 6 +- 8 files changed, 132 insertions(+), 53 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index 37c497e5b..0d18e34ec 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -95,7 +95,7 @@ func checkVersion() { {"github.com/go-macaron/toolbox", toolbox.Version, "0.1.0"}, {"gopkg.in/ini.v1", ini.Version, "1.8.4"}, {"gopkg.in/macaron.v1", macaron.Version, "1.1.7"}, - {"github.com/gogits/git-module", git.Version, "0.4.9"}, + {"github.com/gogits/git-module", git.Version, "0.4.10"}, {"github.com/gogits/go-gogs-client", gogs.Version, "0.12.1"}, } for _, c := range checkers { diff --git a/gogs.go b/gogs.go index 5d4d32256..be68464b7 100644 --- a/gogs.go +++ b/gogs.go @@ -16,7 +16,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.9.154.0217" +const APP_VER = "0.9.155.0217" func init() { setting.AppVer = APP_VER diff --git a/routers/repo/release.go b/routers/repo/release.go index f74fc7b6c..8df329691 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -53,7 +53,7 @@ func Releases(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.release.releases") ctx.Data["PageIsReleaseList"] = true - rawTags, err := ctx.Repo.GitRepo.GetTags() + tagsResult, err := ctx.Repo.GitRepo.GetTagsAfter(ctx.Query("after"), 10) if err != nil { ctx.Handle(500, fmt.Sprintf("GetTags '%s'", ctx.Repo.Repository.RepoPath()), err) return @@ -68,33 +68,39 @@ func Releases(ctx *context.Context) { // Temproray cache commits count of used branches to speed up. countCache := make(map[string]int64) - tags := make([]*models.Release, len(rawTags)) - for i, rawTag := range rawTags { + drafts := make([]*models.Release, 0, 1) + tags := make([]*models.Release, len(tagsResult.Tags)) + for i, rawTag := range tagsResult.Tags { for j, r := range releases { if r == nil || (r.IsDraft && !ctx.Repo.IsOwner()) { continue } - if r.TagName == rawTag { - r.Publisher, err = models.GetUserByID(r.PublisherID) - if err != nil { - if models.IsErrUserNotExist(err) { - r.Publisher = models.NewGhostUser() - } else { - ctx.Handle(500, "GetUserByID", err) - return - } - } + releases[j] = nil // Mark as used. - if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil { - ctx.Handle(500, "calReleaseNumCommitsBehind", err) + r.Publisher, err = models.GetUserByID(r.PublisherID) + if err != nil { + if models.IsErrUserNotExist(err) { + r.Publisher = models.NewGhostUser() + } else { + ctx.Handle(500, "GetUserByID", err) return } + } - r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()) + if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil { + ctx.Handle(500, "calReleaseNumCommitsBehind", err) + return + } + + r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()) + if r.TagName == rawTag { tags[i] = r - releases[j] = nil // Mark as used. break } + + if r.IsDraft { + drafts = append(drafts, r) + } } if tags[i] == nil { @@ -119,31 +125,18 @@ func Releases(ctx *context.Context) { } } - for _, r := range releases { - if r == nil { - continue - } - - r.Publisher, err = models.GetUserByID(r.PublisherID) - if err != nil { - if models.IsErrUserNotExist(err) { - r.Publisher = models.NewGhostUser() - } else { - ctx.Handle(500, "GetUserByID", err) - return - } - } - - if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil { - ctx.Handle(500, "calReleaseNumCommitsBehind", err) - return - } - - r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()) - tags = append(tags, r) - } models.SortReleases(tags) + if len(drafts) > 0 && tagsResult.HasLatest { + tags = append(drafts, tags...) + } + ctx.Data["Releases"] = tags + ctx.Data["HasPrevious"] = !tagsResult.HasLatest + ctx.Data["ReachEnd"] = tagsResult.ReachEnd + ctx.Data["PreviousAfter"] = tagsResult.PreviousAfter + if len(tags) > 0 { + ctx.Data["NextAfter"] = tags[len(tags)-1].TagName + } ctx.HTML(200, RELEASES) } diff --git a/templates/.VERSION b/templates/.VERSION index 859e5fac8..8633bb346 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.9.154.0217 \ No newline at end of file +0.9.155.0217 \ No newline at end of file diff --git a/templates/repo/release/list.tmpl b/templates/repo/release/list.tmpl index b9b1b7c2f..e8cb0d8aa 100644 --- a/templates/repo/release/list.tmpl +++ b/templates/repo/release/list.tmpl @@ -75,6 +75,14 @@ {{end}} +
+ + {{$.i18n.Tr "repo.issues.previous"}} + + + {{$.i18n.Tr "repo.issues.next"}} + +
{{template "base/footer" .}} diff --git a/vendor/github.com/gogits/git-module/git.go b/vendor/github.com/gogits/git-module/git.go index 9c95d45ef..a01f4e229 100644 --- a/vendor/github.com/gogits/git-module/git.go +++ b/vendor/github.com/gogits/git-module/git.go @@ -10,7 +10,7 @@ import ( "time" ) -const _VERSION = "0.4.9" +const _VERSION = "0.4.10" func Version() string { return _VERSION diff --git a/vendor/github.com/gogits/git-module/repo_tag.go b/vendor/github.com/gogits/git-module/repo_tag.go index 1ad8cdec5..b7e96bdb8 100644 --- a/vendor/github.com/gogits/git-module/repo_tag.go +++ b/vendor/github.com/gogits/git-module/repo_tag.go @@ -5,6 +5,7 @@ package git import ( + "fmt" "strings" "github.com/mcuadros/go-version" @@ -110,7 +111,7 @@ func (repo *Repository) GetTags() ([]string, error) { version.Sort(tags) // Reverse order - for i := 0; i < len(tags) / 2; i++ { + for i := 0; i < len(tags)/2; i++ { j := len(tags) - i - 1 tags[i], tags[j] = tags[j], tags[i] } @@ -119,13 +120,90 @@ func (repo *Repository) GetTags() ([]string, error) { return tags, nil } +type TagsResult struct { + // Indicates whether results include the latest tag. + HasLatest bool + // If results do not include the latest tag, a indicator 'after' to go back. + PreviousAfter string + // Indicates whether results include the oldest tag. + ReachEnd bool + // List of returned tags. + Tags []string +} + +// GetTagsAfter returns list of tags 'after' (exlusive) given tag. +func (repo *Repository) GetTagsAfter(after string, limit int) (*TagsResult, error) { + allTags, err := repo.GetTags() + if err != nil { + return nil, fmt.Errorf("GetTags: %v", err) + } + + if limit < 0 { + limit = 0 + } + + numAllTags := len(allTags) + if len(after) == 0 && limit == 0 { + return &TagsResult{ + HasLatest: true, + ReachEnd: true, + Tags: allTags, + }, nil + } else if len(after) == 0 && limit > 0 { + endIdx := limit + if limit >= numAllTags { + endIdx = numAllTags + } + return &TagsResult{ + HasLatest: true, + ReachEnd: limit >= numAllTags, + Tags: allTags[:endIdx], + }, nil + } + + previousAfter := "" + hasMatch := false + tags := make([]string, 0, len(allTags)) + for i := range allTags { + if hasMatch { + tags = allTags[i:] + break + } + if allTags[i] == after { + hasMatch = true + if limit > 0 && i-limit > 0 { + previousAfter = allTags[i-limit] + } + continue + } + } + + if !hasMatch { + tags = allTags + } + + // If all tags after match is equal to the limit, it reaches the oldest tag as well. + if limit == 0 || len(tags) <= limit { + return &TagsResult{ + HasLatest: !hasMatch, + PreviousAfter: previousAfter, + ReachEnd: true, + Tags: tags, + }, nil + } + return &TagsResult{ + HasLatest: !hasMatch, + PreviousAfter: previousAfter, + Tags: tags[:limit], + }, nil +} + // DeleteTag deletes a tag from the repository func (repo *Repository) DeleteTag(name string) error { - cmd := NewCommand("tag", "-d") + cmd := NewCommand("tag", "-d") - cmd.AddArguments(name) - _, err := cmd.RunInDir(repo.Path) + cmd.AddArguments(name) + _, err := cmd.RunInDir(repo.Path) - return err + return err } - diff --git a/vendor/vendor.json b/vendor/vendor.json index 950f806e9..b34308003 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -159,10 +159,10 @@ "revisionTime": "2016-08-10T03:50:02Z" }, { - "checksumSHA1": "RSr3IvGo5PFxAP3ybtDcEojTWPI=", + "checksumSHA1": "5SLknh130FbmnSNWkf6LtVFqdMI=", "path": "github.com/gogits/git-module", - "revision": "41f3ca26f6b202c82d022a1062f7b7ea6339924b", - "revisionTime": "2017-02-16T16:46:07Z" + "revision": "7c2ab580a5b25e8b045139a44635258ceef64ace", + "revisionTime": "2017-02-17T22:39:06Z" }, { "checksumSHA1": "xvG+RgJODQqlmdAkHUQK2TyLR88=",