diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2017-01-06 09:51:15 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-06 09:51:15 +0800 |
commit | 61306fa737f693c3b325d9a8da047ba0b939537e (patch) | |
tree | 91d85ffddf960a7cb64343ed47277027f9227110 /routers/repo/release.go | |
parent | 79d527195d98d74867a067ce93a4dace2b86d2bb (diff) | |
download | gitea-61306fa737f693c3b325d9a8da047ba0b939537e.tar.gz gitea-61306fa737f693c3b325d9a8da047ba0b939537e.zip |
Make releases faster than before and resolved #490 (#588)
* make releases faster than before and resolved #490
* fix comment
Diffstat (limited to 'routers/repo/release.go')
-rw-r--r-- | routers/repo/release.go | 102 |
1 files changed, 46 insertions, 56 deletions
diff --git a/routers/repo/release.go b/routers/repo/release.go index 371ea3b597..c2e5aabf8f 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -5,8 +5,10 @@ package repo import ( + "errors" "fmt" + "code.gitea.io/git" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/base" @@ -54,34 +56,55 @@ func Releases(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.release.releases") ctx.Data["PageIsReleaseList"] = true - rawTags, err := ctx.Repo.GitRepo.GetTags() + page := ctx.QueryInt("page") + if page <= 1 { + page = 1 + } + limit := ctx.QueryInt("limit") + if limit <= 0 { + limit = 10 + } + + rawTags, err := ctx.Repo.GitRepo.GetTagInfos(git.TagOption{}) if err != nil { ctx.Handle(500, "GetTags", err) return } - page := ctx.QueryInt("page") - if page <= 1 { - page = 1 + if len(rawTags) <= (page-1)*limit { + ctx.Handle(500, "Releases", errors.New("no more pages")) + return + } + + var tags []*git.Tag + if page*limit > len(rawTags) { + tags = rawTags[(page-1)*limit:] + } else { + tags = rawTags[(page-1)*limit : page*limit] + } + + var tagNames []string + for _, t := range tags { + tagNames = append(tagNames, t.Name) } - releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, page, 10) + + releases, err := models.GetReleasesByRepoIDAndNames(ctx.Repo.Repository.ID, tagNames) if err != nil { - ctx.Handle(500, "GetReleasesByRepoID", err) + ctx.Handle(500, "GetReleasesByRepoIDAndNames", err) return } // Temproray cache commits count of used branches to speed up. countCache := make(map[string]int64) - var cacheUsers = make(map[int64]*models.User) var ok bool - tags := make([]*models.Release, len(rawTags)) - for i, rawTag := range rawTags { - for j, r := range releases { - if r == nil || (r.IsDraft && !ctx.Repo.IsOwner()) { + releaseTags := make([]*models.Release, len(tags)) + for i, rawTag := range tags { + for _, r := range releases { + if r.IsDraft && !ctx.Repo.IsOwner() { continue } - if r.TagName == rawTag { + if r.TagName == rawTag.Name { if r.Publisher, ok = cacheUsers[r.PublisherID]; !ok { r.Publisher, err = models.GetUserByID(r.PublisherID) if err != nil { @@ -101,64 +124,31 @@ func Releases(ctx *context.Context) { } r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()) - tags[i] = r - releases[j] = nil // Mark as used. + releaseTags[i] = r break } } - if tags[i] == nil { - commit, err := ctx.Repo.GitRepo.GetTagCommit(rawTag) - if err != nil { - ctx.Handle(500, "GetTagCommit", err) - return - } - - tags[i] = &models.Release{ - Title: rawTag, - TagName: rawTag, - Sha1: commit.ID.String(), + if releaseTags[i] == nil { + releaseTags[i] = &models.Release{ + Title: rawTag.Name, + TagName: rawTag.Name, + Sha1: rawTag.Object.String(), + Note: rawTag.Message, } - tags[i].NumCommits, err = commit.CommitsCount() + releaseTags[i].NumCommits, err = git.CommitsCount(ctx.Repo.GitRepo.Path, rawTag.Object.String()) if err != nil { ctx.Handle(500, "CommitsCount", err) return } - tags[i].NumCommitsBehind = ctx.Repo.CommitsCount - tags[i].NumCommits + releaseTags[i].NumCommitsBehind = ctx.Repo.CommitsCount - releaseTags[i].NumCommits } } - for _, r := range releases { - if r == nil { - continue - } - - if r.Publisher, ok = cacheUsers[r.PublisherID]; !ok { - 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 - } - } - cacheUsers[r.PublisherID] = r.Publisher - } - - 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) - } - pager := paginater.New(ctx.Repo.Repository.NumTags, 10, page, 5) + pager := paginater.New(ctx.Repo.Repository.NumTags, limit, page, 5) ctx.Data["Page"] = pager - models.SortReleases(tags) - ctx.Data["Releases"] = tags + ctx.Data["Releases"] = releaseTags ctx.HTML(200, tplReleases) } |