From 065bbddab91791666ef1d0e10765f58595094a1e Mon Sep 17 00:00:00 2001 From: jaqra <48099350+jaqra@users.noreply.github.com> Date: Thu, 7 Nov 2019 21:09:51 +0300 Subject: [PATCH] Fix count for commit graph last page (#8843) * Fix count for commit graph last page * Remove used once variable * Move func to model * capitalize method name * fix error message --- modules/git/commit.go | 10 ++++++++++ modules/git/repo.go | 5 +++++ routers/repo/commit.go | 8 +++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index 45b943e79e..ce55dd55f6 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -248,6 +248,16 @@ func CommitChanges(repoPath string, opts CommitChangesOptions) error { return err } +// AllCommitsCount returns count of all commits in repository +func AllCommitsCount(repoPath string) (int64, error) { + stdout, err := NewCommand("rev-list", "--all", "--count").RunInDir(repoPath) + if err != nil { + return 0, err + } + + return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64) +} + func commitsCount(repoPath, revision, relpath string) (int64, error) { cmd := NewCommand("rev-list", "--count") cmd.AddArguments(revision) diff --git a/modules/git/repo.go b/modules/git/repo.go index e1d75ca4aa..4c6690b913 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -46,6 +46,11 @@ type GPGSettings struct { const prettyLogFormat = `--pretty=format:%H` +// GetAllCommitsCount returns count of all commits in repository +func (repo *Repository) GetAllCommitsCount() (int64, error) { + return AllCommitsCount(repo.Path) +} + func (repo *Repository) parsePrettyFormatLogToList(logs []byte) (*list.List, error) { l := list.New() if len(logs) == 0 { diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 550e4c3a9c..f067729ca9 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -91,6 +91,12 @@ func Graph(ctx *context.Context) { return } + allCommitsCount, err := ctx.Repo.GitRepo.GetAllCommitsCount() + if err != nil { + ctx.ServerError("GetAllCommitsCount", err) + return + } + page := ctx.QueryInt("page") graph, err := models.GetCommitGraph(ctx.Repo.GitRepo, page) @@ -105,7 +111,7 @@ func Graph(ctx *context.Context) { ctx.Data["CommitCount"] = commitsCount ctx.Data["Branch"] = ctx.Repo.BranchName ctx.Data["RequireGitGraph"] = true - ctx.Data["Page"] = context.NewPagination(int(commitsCount), setting.UI.GraphMaxCommitNum, page, 5) + ctx.Data["Page"] = context.NewPagination(int(allCommitsCount), setting.UI.GraphMaxCommitNum, page, 5) ctx.HTML(200, tplGraph) }