From 9cf7f3e46f1be812a96eba9b456be3d5140c92a8 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 6 Mar 2016 14:44:22 -0500 Subject: [PATCH] Remove duplicated of code --- README.md | 2 +- gogs.go | 2 +- routers/repo/release.go | 101 +++++++++++++++++++--------------------- templates/.VERSION | 2 +- 4 files changed, 51 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 513f97952e..a746ac9722 100644 --- a/README.md +++ b/README.md @@ -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 | |:-------------:|:-------:|:-------:| diff --git a/gogs.go b/gogs.go index 114d90d152..b6c95a20fd 100644 --- a/gogs.go +++ b/gogs.go @@ -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()) diff --git a/routers/repo/release.go b/routers/repo/release.go index 92db5e806a..4073fde82e 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -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 diff --git a/templates/.VERSION b/templates/.VERSION index cdd381d124..faec99ba69 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.8.60.0306 \ No newline at end of file +0.8.61.0306 \ No newline at end of file