diff --git a/README.md b/README.md index 37a2b9e2f..dfc2fff5e 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 b16e7a58f..e9aa74adb 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 d0b0c6ca0..8c96ec90f 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 af7915482..f20e663b1 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 5e4cc63f1..e6f6d7ed8 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 092d48688..68b140358 100644 --- a/templates/repo/commits.tmpl +++ b/templates/repo/commits.tmpl @@ -40,15 +40,10 @@ - + {{if not .IsSearchPage}}{{end}} {{template "base/footer" .}}