summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorUnknown <joe2010xtmf@163.com>2014-04-11 19:44:13 -0400
committerUnknown <joe2010xtmf@163.com>2014-04-11 19:44:13 -0400
commit47aa53bd369014b0788f18a605e7347801f6c31d (patch)
treed24e083ed102c457854e00e13e3e767d71a272c1 /models
parent7d07b58114199f682a9caa059f239e24c820dc41 (diff)
downloadgitea-47aa53bd369014b0788f18a605e7347801f6c31d.tar.gz
gitea-47aa53bd369014b0788f18a605e7347801f6c31d.zip
Add search commits
Diffstat (limited to 'models')
-rw-r--r--models/git.go61
-rw-r--r--models/repo.go12
2 files changed, 67 insertions, 6 deletions
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
}