mirror of
https://github.com/go-gitea/gitea.git
synced 2024-09-05 23:34:19 -04:00
Add search commits
This commit is contained in:
parent
7d07b58114
commit
47aa53bd36
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
32
routers/api/v1/repositories.go
Normal file
32
routers/api/v1/repositories.go
Normal file
@ -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,
|
||||
})
|
||||
}
|
@ -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")
|
||||
}
|
||||
|
@ -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"))
|
||||
}
|
||||
|
@ -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>
|
||||
@ -31,10 +31,10 @@
|
||||
{{$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>
|
||||
|
@ -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>
|
||||
|
2
web.go
2
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))
|
||||
|
Loading…
Reference in New Issue
Block a user