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)
##### Current version: 0.8.60
##### Current version: 0.8.61
| Web | UI | Preview |
|:-------------:|:-------:|:-------:|

View file

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

View file

@ -5,6 +5,8 @@
package repo
import (
"fmt"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base"
@ -18,6 +20,29 @@ const (
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) {
ctx.Data["Title"] = ctx.Tr("repo.release.releases")
ctx.Data["PageIsReleaseList"] = true
@ -28,7 +53,7 @@ func Releases(ctx *middleware.Context) {
return
}
rels, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID)
releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID)
if err != nil {
ctx.Handle(500, "GetReleasesByRepoID", err)
return
@ -39,44 +64,29 @@ func Releases(ctx *middleware.Context) {
tags := make([]*models.Release, len(rawTags))
for i, rawTag := range rawTags {
for j, rel := range rels {
if rel == nil || (rel.IsDraft && !ctx.Repo.IsOwner()) {
for j, r := range releases {
if r == nil || (r.IsDraft && !ctx.Repo.IsOwner()) {
continue
}
if rel.TagName == rawTag {
rel.Publisher, err = models.GetUserByID(rel.PublisherID)
if r.TagName == rawTag {
r.Publisher, err = models.GetUserByID(r.PublisherID)
if err != nil {
if models.IsErrUserNotExist(err) {
rel.Publisher = models.NewFakeUser()
r.Publisher = models.NewFakeUser()
} else {
ctx.Handle(500, "GetUserByID", err)
return
}
}
// FIXME: duplicated code.
// Get corresponding target if it's not the current branch.
if ctx.Repo.BranchName != rel.Target {
// Get count if not exists.
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
if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
ctx.Handle(500, "calReleaseNumCommitsBehind", err)
return
}
rel.Note = markdown.RenderString(rel.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
tags[i] = rel
rels[j] = nil // Mark as used.
r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
tags[i] = r
releases[j] = nil // Mark as used.
break
}
}
@ -103,43 +113,28 @@ func Releases(ctx *middleware.Context) {
}
}
for _, rel := range rels {
if rel == nil {
for _, r := range releases {
if r == nil {
continue
}
rel.Publisher, err = models.GetUserByID(rel.PublisherID)
r.Publisher, err = models.GetUserByID(r.PublisherID)
if err != nil {
if models.IsErrUserNotExist(err) {
rel.Publisher = models.NewFakeUser()
r.Publisher = models.NewFakeUser()
} else {
ctx.Handle(500, "GetUserByID", err)
return
}
}
// FIXME: duplicated code.
// Get corresponding target if it's not the current branch.
if ctx.Repo.BranchName != rel.Target {
// Get count if not exists.
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
if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
ctx.Handle(500, "calReleaseNumCommitsBehind", err)
return
}
rel.Note = markdown.RenderString(rel.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
tags = append(tags, rel)
r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
tags = append(tags, r)
}
models.SortReleases(tags)
ctx.Data["Releases"] = tags

View file

@ -1 +1 @@
0.8.60.0306
0.8.61.0306