From d6dac160dfcac068b31bda9316ddc3d4919e3288 Mon Sep 17 00:00:00 2001
From: Unknown <joe2010xtmf@163.com>
Date: Fri, 11 Apr 2014 20:23:34 -0400
Subject: [PATCH] Pages in commits list page

---
 README.md                   |  2 +-
 README_ZH.md                |  2 +-
 gogs.go                     |  2 +-
 models/git.go               | 23 +++++++++++++++++++++++
 routers/repo/commit.go      | 31 ++++++++++++++++++++++++++++---
 templates/repo/commits.tmpl | 13 ++++---------
 6 files changed, 58 insertions(+), 15 deletions(-)

diff --git a/README.md b/README.md
index 37a2b9e2f0..dfc2fff5e6 100644
--- a/README.md
+++ b/README.md
@@ -5,7 +5,7 @@ Gogs(Go Git Service) is a Self Hosted Git Service in the Go Programming Language
 
 ![Demo](http://gowalker.org/public/gogs_demo.gif)
 
-##### Current version: 0.2.5 Alpha
+##### Current version: 0.2.6 Alpha
 
 #### Due to testing purpose, data of [try.gogits.org](http://try.gogits.org) has been reset in April 6, 2014 and will reset multiple times after. Please do NOT put your important data on the site.
 
diff --git a/README_ZH.md b/README_ZH.md
index b16e7a58f7..e9aa74adb6 100644
--- a/README_ZH.md
+++ b/README_ZH.md
@@ -5,7 +5,7 @@ Gogs(Go Git Service) 是一个由 Go 语言编写的自助 Git 托管服务。
 
 ![Demo](http://gowalker.org/public/gogs_demo.gif)
 
-##### 当前版本:0.2.5 Alpha
+##### 当前版本:0.2.6 Alpha
 
 ## 开发目的
 
diff --git a/gogs.go b/gogs.go
index d0b0c6ca04..8c96ec90fd 100644
--- a/gogs.go
+++ b/gogs.go
@@ -19,7 +19,7 @@ import (
 // Test that go1.2 tag above is included in builds. main.go refers to this definition.
 const go12tag = true
 
-const APP_VER = "0.2.5.0410 Alpha"
+const APP_VER = "0.2.6.0411 Alpha"
 
 func init() {
 	base.AppVer = APP_VER
diff --git a/models/git.go b/models/git.go
index af7915482a..f20e663b1b 100644
--- a/models/git.go
+++ b/models/git.go
@@ -470,3 +470,26 @@ func SearchCommits(repoPath, branch, keyword string) (*list.List, error) {
 	}
 	return parsePrettyFormatLog(stdout)
 }
+
+// GetCommitsByRange returns certain number of commits with given page of repository.
+func GetCommitsByRange(repoPath, branch string, page int) (*list.List, error) {
+	stdout, stderr, err := com.ExecCmdDirBytes(repoPath, "git", "log", branch,
+		"--skip="+base.ToStr((page-1)*50), "--max-count=50", prettyLogFormat)
+	if err != nil {
+		return nil, err
+	} else if len(stderr) > 0 {
+		return nil, errors.New(string(stderr))
+	}
+	return parsePrettyFormatLog(stdout)
+}
+
+// GetCommitsCount returns the commits count of given branch of repository.
+func GetCommitsCount(repoPath, branch string) (int, error) {
+	stdout, stderr, err := com.ExecCmdDir(repoPath, "git", "rev-list", "--count", branch)
+	if err != nil {
+		return 0, err
+	} else if len(stderr) > 0 {
+		return 0, errors.New(stderr)
+	}
+	return base.StrTo(strings.TrimSpace(stdout)).Int()
+}
diff --git a/routers/repo/commit.go b/routers/repo/commit.go
index 5e4cc63f11..e6f6d7ed89 100644
--- a/routers/repo/commit.go
+++ b/routers/repo/commit.go
@@ -29,22 +29,46 @@ func Commits(ctx *middleware.Context, params martini.Params) {
 		return
 	}
 
+	repoPath := models.RepoPath(userName, repoName)
+	commitsCount, err := models.GetCommitsCount(repoPath, branchName)
+	if err != nil {
+		ctx.Handle(500, "repo.Commits(GetCommitsCount)", err)
+		return
+	}
+
+	// Calculate and validate page number.
+	page, _ := base.StrTo(ctx.Query("p")).Int()
+	if page < 1 {
+		page = 1
+	}
+	lastPage := page - 1
+	if lastPage < 0 {
+		lastPage = 0
+	}
+	nextPage := page + 1
+	if nextPage*50 > commitsCount {
+		nextPage = 0
+	}
+
 	var commits *list.List
 	if models.IsBranchExist(userName, repoName, branchName) {
-		commits, err = models.GetCommitsByBranch(userName, repoName, branchName)
+		// commits, err = models.GetCommitsByBranch(userName, repoName, branchName)
+		commits, err = models.GetCommitsByRange(repoPath, branchName, page)
 	} else {
 		commits, err = models.GetCommitsByCommitId(userName, repoName, branchName)
 	}
 
 	if err != nil {
-		ctx.Handle(404, "repo.Commits", err)
+		ctx.Handle(404, "repo.Commits(get commits)", err)
 		return
 	}
 
 	ctx.Data["Username"] = userName
 	ctx.Data["Reponame"] = repoName
-	ctx.Data["CommitCount"] = commits.Len()
+	ctx.Data["CommitCount"] = commitsCount
 	ctx.Data["Commits"] = commits
+	ctx.Data["LastPageNum"] = lastPage
+	ctx.Data["NextPageNum"] = nextPage
 	ctx.Data["IsRepoToolbarCommits"] = true
 	ctx.HTML(200, "repo/commits")
 }
@@ -125,6 +149,7 @@ func SearchCommits(ctx *middleware.Context, params martini.Params) {
 	ctx.Data["Reponame"] = repoName
 	ctx.Data["CommitCount"] = commits.Len()
 	ctx.Data["Commits"] = commits
+	ctx.Data["IsSearchPage"] = true
 	ctx.Data["IsRepoToolbarCommits"] = true
 	ctx.HTML(200, "repo/commits")
 }
diff --git a/templates/repo/commits.tmpl b/templates/repo/commits.tmpl
index 092d48688d..68b1403589 100644
--- a/templates/repo/commits.tmpl
+++ b/templates/repo/commits.tmpl
@@ -40,15 +40,10 @@
                 </tbody>
             </table>
         </div>
-        <ul class="pagination" id="commits-pager">
-            <li><a href="#">&laquo;</a></li>
-            <li><a href="#">1</a></li>
-            <li><a href="#">2</a></li>
-            <li><a href="#">3</a></li>
-            <li><a href="#">4</a></li>
-            <li><a href="#">5</a></li>
-            <li><a href="#">&raquo;</a></li>
-        </ul>
+        {{if not .IsSearchPage}}<ul class="pagination" id="commits-pager">
+            {{if .LastPageNum}}<li><a href="{{.RepoLink}}/commits/{{.BranchName}}?p={{.LastPageNum}}">&laquo; Newer</a></li>{{end}}
+            {{if .NextPageNum}}<li><a href="{{.RepoLink}}/commits/{{.BranchName}}?p={{.NextPageNum}}">&raquo; Older</a></li>{{end}}
+        </ul>{{end}}
     </div>
 </div>
 {{template "base/footer" .}}