-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
119 lines (103 loc) · 2.68 KB
/
git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"bytes"
"fmt"
"github.com/VictorLowther/go-git/git"
"regexp"
"strings"
)
var (
repo *git.Repo
)
type author struct {
Name string
Email string
}
func (a *author) String() string {
return fmt.Sprintf("%s <%s>", a.Name, a.Email)
}
type pageRevision struct {
Title string
Object string
Description string
Author author
Timestamp string
}
type searchResult struct {
Title string
Content string
}
func title(file string) string {
return strings.Replace(file, "."+conf.FileExtension, "", -1)
}
func gitExec(command string, args ...string) (*bytes.Buffer, error) {
res, out, stderr := repo.Git(command, args...)
runErr := res.Run()
if runErr != nil {
return out, runErr
} else if stderr.Len() > 0 {
return out, fmt.Errorf(stderr.String())
}
return out, nil
}
func gitShow(file string, revision string) (*bytes.Buffer, error) {
return gitExec("show", fmt.Sprintf("%s:%s", revision, file))
}
func gitAdd(file string) (*bytes.Buffer, error) {
return gitExec("add", file)
}
func gitCommit(message string, author author) (*bytes.Buffer, error) {
if author.String() == "" {
return gitExec("commit", "-m", message)
}
return gitExec("commit", "-m", message, "--author", author.String())
}
func gitLog(file string) ([]pageRevision, error) {
var revisions []pageRevision
out, err := gitExec("log", "--pretty=format:%h %an <%ae> %ad %s", "--date=relative", file)
if err != nil {
return revisions, err
}
var data []byte
for err == nil {
data, err = out.ReadBytes('\n')
revision := parseGitLog(string(data))
if revision.Object == "" {
continue
}
revision.Title = title(file)
revisions = append(revisions, revision)
}
return revisions, nil
}
func parseGitLog(log string) pageRevision {
re := regexp.MustCompile(`(.{0,7}) (.+) (<.+>) (\d+ \w+ ago) (.*)`)
matches := re.FindStringSubmatch(log)
if len(matches) == 6 {
return pageRevision{Object: matches[1], Author: author{Name: matches[2], Email: matches[3]}, Timestamp: matches[4], Description: matches[5]}
}
return pageRevision{}
}
func gitGrep(keyword string) ([]searchResult, error) {
var results []searchResult
out, err := gitExec("grep", "--ignore-case", keyword)
if err != nil {
return results, err
}
results = parseGitGrep(out)
return results, nil
}
func parseGitGrep(output *bytes.Buffer) []searchResult {
var err error
var bytes []byte
results := make([]searchResult, 0)
re := regexp.MustCompile(`(.+)\.` + conf.FileExtension + `:(.*)`)
for err == nil {
bytes, err = output.ReadBytes('\n')
matches := re.FindStringSubmatch(string(bytes))
if len(matches) == 3 {
results = append(results, searchResult{Title: matches[1], Content: matches[2]})
}
}
return results
}