diff options
Diffstat (limited to 'models/git.go')
-rw-r--r-- | models/git.go | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/models/git.go b/models/git.go index 68e13905..f20e663b 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,85 @@ 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) +} + +// GetCommitsByRange returns certain number of commits with given page of repository. +func GetCommitsByRange(repoPath, branch string, page int) (*list.List, error) { + stdout, stderr, err := com.ExecCmdDirBytes(repoPath, "git", "log", branch, + "--skip="+base.ToStr((page-1)*50), "--max-count=50", prettyLogFormat) + if err != nil { + return nil, err + } else if len(stderr) > 0 { + return nil, errors.New(string(stderr)) + } + return parsePrettyFormatLog(stdout) +} + +// GetCommitsCount returns the commits count of given branch of repository. +func GetCommitsCount(repoPath, branch string) (int, error) { + stdout, stderr, err := com.ExecCmdDir(repoPath, "git", "rev-list", "--count", branch) + if err != nil { + return 0, err + } else if len(stderr) > 0 { + return 0, errors.New(stderr) + } + return base.StrTo(strings.TrimSpace(stdout)).Int() +} |