From 7d07b58114199f682a9caa059f239e24c820dc41 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 11 Apr 2014 13:24:19 -0400 Subject: [PATCH 1/3] UPDATE README --- CONTRIBUTING.md | 2 +- README.md | 2 +- README_ZH.md | 2 +- models/models.go | 2 +- models/user.go | 2 ++ templates/base/navbar.tmpl | 12 ++++++------ templates/user/dashboard.tmpl | 2 +- 7 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 17a3ebe68f..cfc6c14f21 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,7 +2,7 @@ > Thanks [drone](https://github.com/drone/drone) because this guidelines sheet is forked from its [CONTRIBUTING.md](https://github.com/drone/drone/blob/master/CONTRIBUTING.md). -**This document is pre^3 release, we're not ready for receiving contribution until v0.5.0 release.** +**This document is pre^2 release, we're not ready for receiving contribution until v0.5.0 release.** Want to hack on Gogs? Awesome! Here are instructions to get you started. They are probably not perfect, please let us know if anything feels wrong or incomplete. diff --git a/README.md b/README.md index d30e81356a..37a2b9e2f0 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ More importantly, Gogs only needs one binary to setup your own project hosting o Make sure you install [Prerequirements](https://github.com/gogits/gogs/wiki/Prerequirements) first. -There are two ways to install Gogs: +There are 3 ways to install Gogs: - [Install from binary](https://github.com/gogits/gogs/wiki/Install-from-binary): **STRONGLY RECOMMENDED** - [Install from source](https://github.com/gogits/gogs/wiki/Install-from-source) diff --git a/README_ZH.md b/README_ZH.md index 43303cdf5c..b16e7a58f7 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -37,7 +37,7 @@ Gogs 完全使用 Go 语言来实现对 Git 数据的操作,实现 **零** 依 在安装 Gogs 之前,您需要先安装 [基本环境](https://github.com/gogits/gogs/wiki/Prerequirements)。 -然后,您可以通过以下两种方式来安装 Gogs: +然后,您可以通过以下 3 种方式来安装 Gogs: - [二进制安装](https://github.com/gogits/gogs/wiki/Install-from-binary): **强烈推荐** - [源码安装](https://github.com/gogits/gogs/wiki/Install-from-source) diff --git a/models/models.go b/models/models.go index ee96207d10..b380d0e0f2 100644 --- a/models/models.go +++ b/models/models.go @@ -32,7 +32,7 @@ var ( func init() { tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch), - new(Action), new(Access), new(Issue), new(Comment), new(Oauth2)) + new(Action), new(Access), new(Issue), new(Comment), new(Oauth2), new(Follow)) } func LoadModelsConfig() { diff --git a/models/user.go b/models/user.go index b2fddd0a1d..5274970fa0 100644 --- a/models/user.go +++ b/models/user.go @@ -294,6 +294,8 @@ func DeleteUser(user *User) error { return err } + // Delete oauth2. + // Delete all feeds. if _, err = orm.Delete(&Action{UserId: user.Id}); err != nil { return err diff --git a/templates/base/navbar.tmpl b/templates/base/navbar.tmpl index 8d6ca47e9c..e74fd3160c 100644 --- a/templates/base/navbar.tmpl +++ b/templates/base/navbar.tmpl @@ -4,19 +4,19 @@ Dashboard Help{{if .IsSigned}} - {{if .Repository}}{{end}} + user-avatar @@ -29,7 +29,7 @@ diff --git a/templates/user/dashboard.tmpl b/templates/user/dashboard.tmpl index e2d7a5093f..efa78d8807 100644 --- a/templates/user/dashboard.tmpl +++ b/templates/user/dashboard.tmpl @@ -35,7 +35,7 @@ From 47aa53bd369014b0788f18a605e7347801f6c31d Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 11 Apr 2014 19:44:13 -0400 Subject: [PATCH 2/3] Add search commits --- models/git.go | 61 +++++++++++++++++++++++++++++++++ models/repo.go | 12 +++---- routers/api/v1/repositories.go | 32 +++++++++++++++++ routers/repo/commit.go | 40 ++++++++++++++++++++- routers/repo/repo.go | 4 +++ templates/repo/commits.tmpl | 12 +++---- templates/repo/single_bare.tmpl | 2 +- web.go | 2 ++ 8 files changed, 151 insertions(+), 14 deletions(-) create mode 100644 routers/api/v1/repositories.go diff --git a/models/git.go b/models/git.go index 68e139056a..af7915482a 100644 --- a/models/git.go +++ b/models/git.go @@ -6,7 +6,9 @@ package models import ( "bufio" + "bytes" "container/list" + "errors" "fmt" "io" "os" @@ -409,3 +411,62 @@ func GetDiff(repoPath, commitid string) (*Diff, error) { defer rd.Close() return ParsePatch(rd) } + +const prettyLogFormat = `--pretty=format:%H%n%an <%ae> %at%n%s` + +func parsePrettyFormatLog(logByts []byte) (*list.List, error) { + l := list.New() + buf := bytes.NewBuffer(logByts) + if buf.Len() == 0 { + return l, nil + } + + idx := 0 + var commit *git.Commit + + for { + line, err := buf.ReadString('\n') + if err != nil && err != io.EOF { + return nil, err + } + line = strings.TrimSpace(line) + // fmt.Println(line) + + var parseErr error + switch idx { + case 0: // SHA1. + commit = &git.Commit{} + commit.Oid, parseErr = git.NewOidFromString(line) + case 1: // Signature. + commit.Author, parseErr = git.NewSignatureFromCommitline([]byte(line + " ")) + case 2: // Commit message. + commit.CommitMessage = line + l.PushBack(commit) + idx = -1 + } + + if parseErr != nil { + return nil, parseErr + } + + idx++ + + if err == io.EOF { + break + } + } + + return l, nil +} + +// SearchCommits searches commits in given branch and keyword of repository. +func SearchCommits(repoPath, branch, keyword string) (*list.List, error) { + stdout, stderr, err := com.ExecCmdDirBytes(repoPath, "git", "log", branch, "-100", + "-i", "--grep="+keyword, prettyLogFormat) + if err != nil { + return nil, err + } else if len(stderr) > 0 { + return nil, errors.New(string(stderr)) + } + return parsePrettyFormatLog(stdout) +} diff --git a/models/repo.go b/models/repo.go index 91dc710281..ce8665cc63 100644 --- a/models/repo.go +++ b/models/repo.go @@ -192,12 +192,6 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv return nil, err } - c := exec.Command("git", "update-server-info") - c.Dir = repoPath - if err = c.Run(); err != nil { - log.Error("repo.CreateRepository(exec update-server-info): %v", err) - } - if err = NewRepoAction(user, repo); err != nil { log.Error("repo.CreateRepository(NewRepoAction): %v", err) } @@ -210,6 +204,12 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv return nil, err } + c := exec.Command("git", "update-server-info") + c.Dir = repoPath + if err = c.Run(); err != nil { + log.Error("repo.CreateRepository(exec update-server-info): %v", err) + } + return repo, nil } diff --git a/routers/api/v1/repositories.go b/routers/api/v1/repositories.go new file mode 100644 index 0000000000..4d05c1a77a --- /dev/null +++ b/routers/api/v1/repositories.go @@ -0,0 +1,32 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package v1 + +import ( + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/middleware" +) + +func SearchCommits(ctx *middleware.Context) { + userName := ctx.Query("username") + repoName := ctx.Query("reponame") + branch := ctx.Query("branch") + keyword := ctx.Query("q") + if len(keyword) == 0 { + ctx.Render.JSON(404, nil) + return + } + + commits, err := models.SearchCommits(models.RepoPath(userName, repoName), branch, keyword) + if err != nil { + ctx.Render.JSON(200, map[string]interface{}{"ok": false}) + return + } + + ctx.Render.JSON(200, map[string]interface{}{ + "ok": true, + "commits": commits, + }) +} diff --git a/routers/repo/commit.go b/routers/repo/commit.go index d29c40e67e..5e4cc63f11 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -22,7 +22,7 @@ func Commits(ctx *middleware.Context, params martini.Params) { brs, err := models.GetBranches(userName, repoName) if err != nil { - ctx.Handle(200, "repo.Commits", err) + ctx.Handle(500, "repo.Commits", err) return } else if len(brs) == 0 { ctx.Handle(404, "repo.Commits", nil) @@ -90,3 +90,41 @@ func Diff(ctx *middleware.Context, params martini.Params) { ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId) ctx.HTML(200, "repo/diff") } + +func SearchCommits(ctx *middleware.Context, params martini.Params) { + keyword := ctx.Query("q") + if len(keyword) == 0 { + ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName) + return + } + + userName := params["username"] + repoName := params["reponame"] + branchName := params["branchname"] + + brs, err := models.GetBranches(userName, repoName) + if err != nil { + ctx.Handle(500, "repo.SearchCommits(GetBranches)", err) + return + } else if len(brs) == 0 { + ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil) + return + } + + var commits *list.List + if !models.IsBranchExist(userName, repoName, branchName) { + ctx.Handle(404, "repo.SearchCommits(IsBranchExist)", err) + return + } else if commits, err = models.SearchCommits(models.RepoPath(userName, repoName), branchName, keyword); err != nil { + ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err) + return + } + + ctx.Data["Keyword"] = keyword + ctx.Data["Username"] = userName + ctx.Data["Reponame"] = repoName + ctx.Data["CommitCount"] = commits.Len() + ctx.Data["Commits"] = commits + ctx.Data["IsRepoToolbarCommits"] = true + ctx.HTML(200, "repo/commits") +} diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 1ae4a3740a..3859b43e87 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -425,3 +425,7 @@ func Action(ctx *middleware.Context, params martini.Params) { "ok": true, }) } + +func Import(ctx *middleware.Context, params martini.Params) { + ctx.ResponseWriter.Write([]byte("not done yet")) +} diff --git a/templates/repo/commits.tmpl b/templates/repo/commits.tmpl index 842a2a6d8f..092d48688d 100644 --- a/templates/repo/commits.tmpl +++ b/templates/repo/commits.tmpl @@ -6,11 +6,11 @@
- @@ -20,7 +20,7 @@ Author - Commit + SHA1 Message Date @@ -31,10 +31,10 @@ {{$r := List .Commits}} {{range $r}} - {{.Committer.Name}} + {{.Author.Name}} {{SubStr .Id.String 0 10}} {{.Message}} - {{TimeSince .Committer.When}} + {{TimeSince .Author.When}} {{end}} diff --git a/templates/repo/single_bare.tmpl b/templates/repo/single_bare.tmpl index 3f63915352..7d7016e5c5 100644 --- a/templates/repo/single_bare.tmpl +++ b/templates/repo/single_bare.tmpl @@ -16,7 +16,7 @@ - + diff --git a/web.go b/web.go index b2be73d677..ecc824d391 100644 --- a/web.go +++ b/web.go @@ -164,6 +164,7 @@ func runWeb(*cli.Context) { r.Post("/issues/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost) r.Post("/issues/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue) r.Post("/comment/:action", repo.Comment) + r.Post("/import", repo.Import) }, reqSignIn, middleware.RepoAssignment(true)) m.Group("/:username/:reponame", func(r martini.Router) { @@ -180,6 +181,7 @@ func runWeb(*cli.Context) { r.Get("/src/:branchname/**", repo.Single) r.Get("/raw/:branchname/**", repo.SingleDownload) r.Get("/commits/:branchname", repo.Commits) + r.Get("/commits/:branchname/search", repo.SearchCommits) r.Get("/commit/:branchname", repo.Diff) r.Get("/commit/:branchname/**", repo.Diff) }, ignSignIn, middleware.RepoAssignment(true, true)) From d6dac160dfcac068b31bda9316ddc3d4919e3288 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 11 Apr 2014 20:23:34 -0400 Subject: [PATCH 3/3] 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 @@
- + {{if not .IsSearchPage}}{{end}}
{{template "base/footer" .}}