]> source.dussan.org Git - gitea.git/commitdiff
Pages in commits list page
authorUnknown <joe2010xtmf@163.com>
Sat, 12 Apr 2014 00:23:34 +0000 (20:23 -0400)
committerUnknown <joe2010xtmf@163.com>
Sat, 12 Apr 2014 00:23:34 +0000 (20:23 -0400)
README.md
README_ZH.md
gogs.go
models/git.go
routers/repo/commit.go
templates/repo/commits.tmpl

index 37a2b9e2f0e70874b4add2005501769604ba6185..dfc2fff5e6035fd13a8ebeec06dd3753a88d4672 100644 (file)
--- 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.
 
index b16e7a58f7233db1820421a2cbc24a986009cd54..e9aa74adb6939f131f33f5bcbeb641a96e5ae7c8 100644 (file)
@@ -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 d0b0c6ca049581a4f38cab28f9d9df90abd0bd78..8c96ec90fdfa1ed4290881ff96925cb035cf523e 100644 (file)
--- 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
index af7915482a3fba2b9e27c26c74ff21c9f617efeb..f20e663b1b1c9128e8e4be9ea25d501d7c31f90f 100644 (file)
@@ -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()
+}
index 5e4cc63f11bd48efc34ca41177ec01e75aa9d95d..e6f6d7ed8991757a714d43f18e89782d0635ccaa 100644 (file)
@@ -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")
 }
index 092d48688dc9599e2895f854987ad4d7f50f3e84..68b140358942f8744a2a3b8586d2ca82ba2fb479 100644 (file)
                 </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" .}}