Remove duplicated of code

This commit is contained in:
Unknwon 2016-03-06 14:44:22 -05:00
parent 13bd16af92
commit 9cf7f3e46f
4 changed files with 51 additions and 56 deletions

View file

@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true) ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)
##### Current version: 0.8.60 ##### Current version: 0.8.61
| Web | UI | Preview | | Web | UI | Preview |
|:-------------:|:-------:|:-------:| |:-------------:|:-------:|:-------:|

View file

@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.8.60.0306" const APP_VER = "0.8.61.0306"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View file

@ -5,6 +5,8 @@
package repo package repo
import ( import (
"fmt"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
@ -18,6 +20,29 @@ const (
RELEASE_NEW base.TplName = "repo/release/new" RELEASE_NEW base.TplName = "repo/release/new"
) )
// calReleaseNumCommitsBehind calculates given release has how many commits behind default branch.
func calReleaseNumCommitsBehind(repoCtx *middleware.RepoContext, release *models.Release, countCache map[string]int64) error {
// Fast return if release target is same as default branch.
if repoCtx.BranchName == release.Target {
release.NumCommitsBehind = repoCtx.CommitsCount - release.NumCommits
return nil
}
// Get count if not exists
if _, ok := countCache[release.Target]; !ok {
commit, err := repoCtx.GitRepo.GetBranchCommit(repoCtx.BranchName)
if err != nil {
return fmt.Errorf("GetBranchCommit: %v", err)
}
countCache[repoCtx.BranchName], err = commit.CommitsCount()
if err != nil {
return fmt.Errorf("CommitsCount: %v", err)
}
}
release.NumCommitsBehind = countCache[repoCtx.BranchName] - release.NumCommits
return nil
}
func Releases(ctx *middleware.Context) { func Releases(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("repo.release.releases") ctx.Data["Title"] = ctx.Tr("repo.release.releases")
ctx.Data["PageIsReleaseList"] = true ctx.Data["PageIsReleaseList"] = true
@ -28,7 +53,7 @@ func Releases(ctx *middleware.Context) {
return return
} }
rels, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID) releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID)
if err != nil { if err != nil {
ctx.Handle(500, "GetReleasesByRepoID", err) ctx.Handle(500, "GetReleasesByRepoID", err)
return return
@ -39,44 +64,29 @@ func Releases(ctx *middleware.Context) {
tags := make([]*models.Release, len(rawTags)) tags := make([]*models.Release, len(rawTags))
for i, rawTag := range rawTags { for i, rawTag := range rawTags {
for j, rel := range rels { for j, r := range releases {
if rel == nil || (rel.IsDraft && !ctx.Repo.IsOwner()) { if r == nil || (r.IsDraft && !ctx.Repo.IsOwner()) {
continue continue
} }
if rel.TagName == rawTag { if r.TagName == rawTag {
rel.Publisher, err = models.GetUserByID(rel.PublisherID) r.Publisher, err = models.GetUserByID(r.PublisherID)
if err != nil { if err != nil {
if models.IsErrUserNotExist(err) { if models.IsErrUserNotExist(err) {
rel.Publisher = models.NewFakeUser() r.Publisher = models.NewFakeUser()
} else { } else {
ctx.Handle(500, "GetUserByID", err) ctx.Handle(500, "GetUserByID", err)
return return
} }
} }
// FIXME: duplicated code.
// Get corresponding target if it's not the current branch. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
if ctx.Repo.BranchName != rel.Target { ctx.Handle(500, "calReleaseNumCommitsBehind", err)
// Get count if not exists. return
if _, ok := countCache[rel.Target]; !ok {
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.BranchName)
if err != nil {
ctx.Handle(500, "GetBranchCommit", err)
return
}
countCache[ctx.Repo.BranchName], err = commit.CommitsCount()
if err != nil {
ctx.Handle(500, "CommitsCount", err)
return
}
}
rel.NumCommitsBehind = countCache[ctx.Repo.BranchName] - rel.NumCommits
} else {
rel.NumCommitsBehind = ctx.Repo.CommitsCount - rel.NumCommits
} }
rel.Note = markdown.RenderString(rel.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()) r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
tags[i] = rel tags[i] = r
rels[j] = nil // Mark as used. releases[j] = nil // Mark as used.
break break
} }
} }
@ -103,43 +113,28 @@ func Releases(ctx *middleware.Context) {
} }
} }
for _, rel := range rels { for _, r := range releases {
if rel == nil { if r == nil {
continue continue
} }
rel.Publisher, err = models.GetUserByID(rel.PublisherID) r.Publisher, err = models.GetUserByID(r.PublisherID)
if err != nil { if err != nil {
if models.IsErrUserNotExist(err) { if models.IsErrUserNotExist(err) {
rel.Publisher = models.NewFakeUser() r.Publisher = models.NewFakeUser()
} else { } else {
ctx.Handle(500, "GetUserByID", err) ctx.Handle(500, "GetUserByID", err)
return return
} }
} }
// FIXME: duplicated code.
// Get corresponding target if it's not the current branch. if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
if ctx.Repo.BranchName != rel.Target { ctx.Handle(500, "calReleaseNumCommitsBehind", err)
// Get count if not exists. return
if _, ok := countCache[rel.Target]; !ok {
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.BranchName)
if err != nil {
ctx.Handle(500, "GetBranchCommit", err)
return
}
countCache[ctx.Repo.BranchName], err = commit.CommitsCount()
if err != nil {
ctx.Handle(500, "CommitsCount", err)
return
}
}
rel.NumCommitsBehind = countCache[ctx.Repo.BranchName] - rel.NumCommits
} else {
rel.NumCommitsBehind = ctx.Repo.CommitsCount - rel.NumCommits
} }
rel.Note = markdown.RenderString(rel.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()) r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
tags = append(tags, rel) tags = append(tags, r)
} }
models.SortReleases(tags) models.SortReleases(tags)
ctx.Data["Releases"] = tags ctx.Data["Releases"] = tags

View file

@ -1 +1 @@
0.8.60.0306 0.8.61.0306