Skip to content

Commit 065bbdd

Browse files
jaqralafriks
jaqra
authored andcommitted
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
1 parent 1f90147 commit 065bbdd

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

modules/git/commit.go

+10
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ func CommitChanges(repoPath string, opts CommitChangesOptions) error {
248248
return err
249249
}
250250

251+
// AllCommitsCount returns count of all commits in repository
252+
func AllCommitsCount(repoPath string) (int64, error) {
253+
stdout, err := NewCommand("rev-list", "--all", "--count").RunInDir(repoPath)
254+
if err != nil {
255+
return 0, err
256+
}
257+
258+
return strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
259+
}
260+
251261
func commitsCount(repoPath, revision, relpath string) (int64, error) {
252262
cmd := NewCommand("rev-list", "--count")
253263
cmd.AddArguments(revision)

modules/git/repo.go

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ type GPGSettings struct {
4646

4747
const prettyLogFormat = `--pretty=format:%H`
4848

49+
// GetAllCommitsCount returns count of all commits in repository
50+
func (repo *Repository) GetAllCommitsCount() (int64, error) {
51+
return AllCommitsCount(repo.Path)
52+
}
53+
4954
func (repo *Repository) parsePrettyFormatLogToList(logs []byte) (*list.List, error) {
5055
l := list.New()
5156
if len(logs) == 0 {

routers/repo/commit.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ func Graph(ctx *context.Context) {
9191
return
9292
}
9393

94+
allCommitsCount, err := ctx.Repo.GitRepo.GetAllCommitsCount()
95+
if err != nil {
96+
ctx.ServerError("GetAllCommitsCount", err)
97+
return
98+
}
99+
94100
page := ctx.QueryInt("page")
95101

96102
graph, err := models.GetCommitGraph(ctx.Repo.GitRepo, page)
@@ -105,7 +111,7 @@ func Graph(ctx *context.Context) {
105111
ctx.Data["CommitCount"] = commitsCount
106112
ctx.Data["Branch"] = ctx.Repo.BranchName
107113
ctx.Data["RequireGitGraph"] = true
108-
ctx.Data["Page"] = context.NewPagination(int(commitsCount), setting.UI.GraphMaxCommitNum, page, 5)
114+
ctx.Data["Page"] = context.NewPagination(int(allCommitsCount), setting.UI.GraphMaxCommitNum, page, 5)
109115
ctx.HTML(200, tplGraph)
110116
}
111117

0 commit comments

Comments
 (0)