diff options
Diffstat (limited to 'modules/context/repo.go')
-rw-r--r-- | modules/context/repo.go | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go index c33396c0f9..3aaf1ce64a 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -13,7 +13,9 @@ import ( "code.gitea.io/git" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/setting" + "github.com/Unknwon/com" "gopkg.in/editorconfig/editorconfig-core-go.v1" "gopkg.in/macaron.v1" @@ -100,6 +102,21 @@ func (r *Repository) CanUseTimetracker(issue *models.Issue, user *models.User) b r.IsWriter() || issue.IsPoster(user.ID) || issue.AssigneeID == user.ID) } +// GetCommitsCount returns cached commit count for current view +func (r *Repository) GetCommitsCount() (int64, error) { + var contextName string + if r.IsViewBranch { + contextName = r.BranchName + } else if r.IsViewTag { + contextName = r.TagName + } else { + contextName = r.CommitID + } + return cache.GetInt64(r.Repository.GetCommitsCountCacheKey(contextName, r.IsViewBranch || r.IsViewTag), func() (int64, error) { + return r.Commit.CommitsCount() + }) +} + // GetEditorconfig returns the .editorconfig definition if found in the // HEAD of the default repo branch. func (r *Repository) GetEditorconfig() (*editorconfig.Editorconfig, error) { @@ -535,9 +552,9 @@ func RepoRef() macaron.Handler { ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch() - ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount() + ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount() if err != nil { - ctx.Handle(500, "CommitsCount", err) + ctx.Handle(500, "GetCommitsCount", err) return } ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount |