diff --git a/models/git.go b/models/git.go index 68e139056..af7915482 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 91dc71028..ce8665cc6 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 000000000..4d05c1a77 --- /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 d29c40e67..5e4cc63f1 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 1ae4a3740..3859b43e8 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 842a2a6d8..092d48688 100644 --- a/templates/repo/commits.tmpl +++ b/templates/repo/commits.tmpl @@ -6,11 +6,11 @@