-
Notifications
You must be signed in to change notification settings - Fork 3
/
gitscan.go
221 lines (202 loc) · 5.12 KB
/
gitscan.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os/exec"
"strconv"
"strings"
"time"
)
type commit struct {
alignment
User *author `json:"user"`
hasEndingPeriod, isMerge bool
date time.Time
Message string `json:"message"`
}
type author struct {
Commits int `json:"commits"`
alignment
accumulator alignment
Name string `json:"name"`
email string
}
type gitOption func([]string) []string
var doNothing gitOption = func(s []string) []string { return s }
func optionNoMerges(none bool) gitOption {
if none {
return func(s []string) []string { return append(s, "--no-merges") }
}
return doNothing
}
func optionAuthorPattern(author string) gitOption {
if author != "" {
return func(s []string) []string { return append(s, "--author", author) }
}
return doNothing
}
func optionMaxCommits(n int) gitOption {
return func(s []string) []string { return append(s, "-n", strconv.Itoa(n)) }
}
func optionBranch(b string) gitOption {
if b == "" {
b = "--all"
}
return func(s []string) []string { return append([]string{b}, s...) }
}
// Stats return author alignment in human readable format (with newlines)
func (a author) Stats() string {
return fmt.Sprintf("Author %v is %v\nCommits: %d\nAccumulated:%0.1g\n",
a.Name, a.alignment.Format(), a.Commits, a.accumulator)
}
// ScanCWD Scans .git in current working directory using git
// command. Scans up to maxCommit messages.
func ScanCWD(opts ...gitOption) ([]commit, []author, error) {
var args []string
for i := range opts {
args = opts[i](args)
}
args = append([]string{"log"}, args...)
cmd := exec.Command("git", args...)
reader, writer := io.Pipe()
cmd.Stdout = writer
cmdstderr := &strings.Builder{}
cmd.Stderr = cmdstderr
go func() {
cmd.Run()
writer.Close()
}()
commits, authors, err := GitLogScan(reader)
if err == io.EOF {
err = nil
}
errmsg := cmdstderr.String()
if err == nil && errmsg != "" {
err = errors.New(errmsg)
}
return commits, authors, err
}
// GitLogScan reads git log results and generates commits
func GitLogScan(r io.Reader) (commits []commit, authors []author, err error) {
rdr := bufio.NewReader(r)
commits = make([]commit, 0, maxCommits)
authors = make([]author, maxAuthors)
authmap := make(map[string]*author)
var c commit
var a author
var auth *author
counter := 0
eof := false
for !eof {
if counter >= maxCommits {
break
}
c, a, err = scanNextCommit(rdr)
if err == errSkipCommit {
continue
}
if err == io.EOF {
eof, err = true, nil
}
if err != nil {
break
}
auth, err = processAuthor(a, authors, authmap)
if err == errSkipCommit {
continue
}
processCommit(&c, auth)
commits = append(commits, c)
counter++
}
if err == errSkipCommit || err == io.EOF {
err = nil
}
return commits, authors[0:len(authmap)], err
}
func processAuthor(a author, authors []author, authmap map[string]*author) (*author, error) {
// if author name is blank, then skip the person
if a.Name == "" {
return nil, errSkipCommit
}
// find author in list
author, ok := authmap[a.Name]
nAuthors := len(authmap)
if !ok {
if len(authmap) == len(authors) {
return author, errSkipCommit
}
authors[nAuthors] = a
author = &authors[nAuthors]
authmap[a.Name] = author
}
return author, nil
}
func processCommit(c *commit, a *author) {
if strings.HasSuffix(c.Message, ".") {
c.hasEndingPeriod = true
c.Message = c.Message[:len(c.Message)-1]
}
// lowering caps improves verb detection
c.Message = strings.ToLower(c.Message)
c.User = a
}
// errSkipCommit tells program to ignore commit message
var errSkipCommit = errors.New("this commit will be ignored")
func scanNextCommit(rdr *bufio.Reader) (c commit, a author, err error) {
var line string
var commitLineScanned bool
for {
line, err = scanNextLine(rdr)
switch {
case !commitLineScanned && strings.HasPrefix(line, "commit"):
commitLineScanned = true
case strings.HasPrefix(line, "Author:"):
a, err = parseAuthor(line[len("Author:"):])
case strings.HasPrefix(line, "Date:"):
c.date, err = time.Parse("Mon Jan 2 15:04:05 2006 -0700", strings.TrimSpace(line[len("Date:"):]))
case strings.HasPrefix(line, "Merge:"):
c.isMerge = true
case strings.HasPrefix(line, "fatal:"):
err = errors.New(line)
default:
c.Message = appendMessage(c.Message, line)
}
if err != nil {
break
}
b, err := rdr.Peek(len("\ncommit"))
if err != nil || string(b) == "\ncommit" {
break
}
}
return c, a, err
}
func scanNextLine(rdr *bufio.Reader) (string, error) {
for {
b, err := rdr.ReadBytes('\n')
if err != nil {
return "", err
}
if len(b) == 1 { // no text on line
continue
}
return string(b[:len(b)-1]), nil
}
}
func appendMessage(msg, toAppend string) string {
if msg == "" {
return strings.TrimSpace(toAppend)
}
return msg + " " + strings.TrimSpace(toAppend)
}
func parseAuthor(s string) (author, error) {
mailstart := strings.Index(s, "<")
mailend := strings.Index(s, ">")
if mailstart < 1 || mailend < 3 {
return author{}, errors.New("bad author line:" + s)
}
return author{Name: strings.TrimSpace(s[:mailstart]), email: s[mailstart+1 : mailend]}, nil
}