]> source.dussan.org Git - gitea.git/commitdiff
Add search commits
authorUnknown <joe2010xtmf@163.com>
Fri, 11 Apr 2014 23:44:13 +0000 (19:44 -0400)
committerUnknown <joe2010xtmf@163.com>
Fri, 11 Apr 2014 23:44:13 +0000 (19:44 -0400)
models/git.go
models/repo.go
routers/api/v1/repositories.go [new file with mode: 0644]
routers/repo/commit.go
routers/repo/repo.go
templates/repo/commits.tmpl
templates/repo/single_bare.tmpl
web.go

index 68e139056ac6abc4ae7ea77bc5a77d27238629eb..af7915482a3fba2b9e27c26c74ff21c9f617efeb 100644 (file)
@@ -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)
+}
index 91dc710281dffb63c4fe661c84bb58662dd8c7e6..ce8665cc63d6b41ea9d69621ccf6c94b6dd24542 100644 (file)
@@ -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 (file)
index 0000000..4d05c1a
--- /dev/null
@@ -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,
+       })
+}
index d29c40e67e0e3cebc84293848d1a0e035d2e1dd1..5e4cc63f11bd48efc34ca41177ec01e75aa9d95d 100644 (file)
@@ -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")
+}
index 1ae4a3740aa67a370bc8af31295288fc6258b95e..3859b43e879d5321cc3f85b7d3a554701b5989af 100644 (file)
@@ -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"))
+}
index 842a2a6d8f4651d3979097e76003091cc7a96963..092d48688dc9599e2895f854987ad4d7f50f3e84 100644 (file)
@@ -6,11 +6,11 @@
     <div id="commits">
         <div class="panel panel-default commit-box info-box">
             <div class="panel-heading info-head">
-                <form class="search pull-right col-md-3" action="" method="post" id="commits-search-form">
+                <form class="search pull-right col-md-3" action="{{.RepoLink}}/commits/{{.BranchName}}/search" method="get" id="commits-search-form">
                     <div class="input-group">
-                        <input class="form-control search" type="search" placeholder="search commit" name="q"/>
+                        <input class="form-control search" type="search" placeholder="search commit" name="q" value="{{.Keyword}}" />
                         <div class="input-group-btn">
-                            <button type="button" class="btn btn-default">Find</button>
+                            <button type="submit" class="btn btn-default">Find</button>
                         </div>
                     </div>
                 </form>
@@ -20,7 +20,7 @@
                 <thead>
                     <tr>
                         <th class="author">Author</th>
-                        <th class="sha">Commit</th>
+                        <th class="sha">SHA1</th>
                         <th class="message">Message</th>
                         <th class="date">Date</th>
                     </tr>
                 {{$r := List .Commits}}
                 {{range $r}}
                 <tr>
-                    <td class="author"><img class="avatar" src="{{AvatarLink .Committer.Email}}" alt=""/><a href="/user/{{.Committer.Name}}">{{.Committer.Name}}</a></td>
+                    <td class="author"><img class="avatar" src="{{AvatarLink .Author.Email}}" alt=""/><a href="/user/{{.Author.Name}}">{{.Author.Name}}</a></td>
                     <td class="sha"><a class="label label-success" href="/{{$username}}/{{$reponame}}/commit/{{.Id}} ">{{SubStr .Id.String 0 10}} </a></td>
                     <td class="message">{{.Message}} </td>
-                    <td class="date">{{TimeSince .Committer.When}}</td>
+                    <td class="date">{{TimeSince .Author.When}}</td>
                 </tr>
                 {{end}}
                 </tbody>
index 3f63915352558f74a3d9f6838399faeb26f271b1..7d7016e5c5f06866f11948744c1a226575305c5a 100644 (file)
@@ -16,7 +16,7 @@
                         <span class="input-group-btn">
                             <button class="btn btn-default" type="button">URL</button>
                         </span>
-                        <input name="passwd" type="password" class="form-control" placeholder="Type existing repository address" required="required">
+                        <input name="import_url" class="form-control" placeholder="Type existing repository address" required="required">
                         <span class="input-group-btn">
                             <button type="submit" class="btn btn-default" type="button">Clone</button>
                         </span>
diff --git a/web.go b/web.go
index b2be73d677f829540693011146d4a56e92167df3..ecc824d3919eab995385ea250f17c2e341b0375f 100644 (file)
--- 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))