Skip to content

Commit

Permalink
make releases faster than before and resolved go-gitea#490
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Jan 5, 2017
1 parent eb9ce39 commit b9bfdc0
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 64 deletions.
17 changes: 13 additions & 4 deletions models/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (

// Release represents a release of repository.
type Release struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"index unique(n)"`
Repo *Repository `xorm:"-"`
PublisherID int64
Publisher *User `xorm:"-"`
TagName string
Publisher *User `xorm:"-"`
TagName string `xorm:"index unique(n)"`
LowerTagName string
Target string
Title string
Expand Down Expand Up @@ -213,6 +213,15 @@ func GetReleasesByRepoID(repoID int64, page, pageSize int) (rels []*Release, err
return rels, err
}

// GetReleasesByRepoIDAndNamesreturns a list of releases of repository accroding repoID and tagNames.
func GetReleasesByRepoIDAndNames(repoID int64, tagNames []string) (rels []*Release, err error) {
err = x.
Desc("created_unix").
In("tag_name", tagNames).
Find(&rels, Release{RepoID: repoID})
return rels, err
}

type releaseSorter struct {
rels []*Release
}
Expand Down
102 changes: 46 additions & 56 deletions routers/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions vendor/code.gitea.io/git/MAINTAINERS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions vendor/code.gitea.io/git/repo_tag.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion vendor/code.gitea.io/git/tag.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"ignore": "test",
"package": [
{
"checksumSHA1": "mIaKLz6373W+jDLjgE/Yzt/exeo=",
"checksumSHA1": "zK/6EifSPy/O5Vbx7CMWfnLHExI=",
"path": "code.gitea.io/git",
"revision": "3d0fa331865619d2f3a7a0fcf23670a389310954",
"revisionTime": "2016-12-28T14:57:51Z"
"revision": "a3ee12b97af51eec1b7aa0525f6a39c97520817d",
"revisionTime": "2017-01-05T02:48:44Z"
},
{
"checksumSHA1": "BKj0haFTDebzdC2nACpoGzp3s8A=",
Expand Down

0 comments on commit b9bfdc0

Please sign in to comment.