From 47aa53bd369014b0788f18a605e7347801f6c31d Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 11 Apr 2014 19:44:13 -0400 Subject: [PATCH] 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 68e139056ac6..af7915482a3f 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 91dc710281df..ce8665cc63d6 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 000000000000..4d05c1a77a5c --- /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 d29c40e67e0e..5e4cc63f11bd 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 1ae4a3740aa6..3859b43e879d 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 842a2a6d8f46..092d48688dc9 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 3f6391535255..7d7016e5c5f0 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 b2be73d677f8..ecc824d3919e 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))