forked from soypat/gitaligned
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitscan.go
145 lines (136 loc) · 3.1 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
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os/exec"
"strings"
"time"
)
type commit struct {
alignment
user *author
hasEndingPeriod bool
date time.Time
message string
}
type author struct {
commits int
alignment
accumulator alignment
name, email string
}
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(branch string) ([]commit, []author, error) {
if branch == "" {
branch = "--all"
}
cmd := exec.Command("git", "log", branch)
reader, writer := io.Pipe()
cmd.Stdout = writer
go func() {
cmd.Run()
writer.Close()
}()
commits, authors, err := GitLogScan(reader)
if err == io.EOF {
err = nil
}
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, 0, maxAuthors)
authmap := make(map[string]*author)
var b []byte
c := commit{}
counter := 0
var skipFlag bool
for {
if counter >= maxCommits {
break
}
b, err = rdr.ReadBytes('\n')
if err != nil {
break
}
if len(b) == 1 { // no text on line
continue
}
line := string(b[:len(b)-1])
switch {
case strings.HasPrefix(line, "commit"):
skipFlag = false
if c.user != nil {
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)
commits = append(commits, c)
counter++
}
c = commit{}
case strings.HasPrefix(line, "Author:"):
a, err := parseAuthor(line[len("Author:"):])
if err != nil {
break
}
if username != "" && username != a.name {
skipFlag = true
c = commit{}
continue
}
author, ok := authmap[a.name]
if !ok {
if len(authors) == maxAuthors {
skipFlag = true
continue
}
authors = append(authors, a)
author = &authors[len(authors)-1]
authmap[a.name] = author
}
c.user = author
case strings.HasPrefix(line, "Date:"):
if skipFlag {
continue
}
c.date, err = time.Parse("Mon Jan 2 15:04:05 2006 -0700", strings.TrimSpace(line[len("Date:"):]))
if err != nil {
return commits, authors, err
}
case strings.HasPrefix(line, "Merge:"):
continue
default:
if skipFlag {
continue
}
if c.message != "" {
c.message += " "
}
c.message += strings.TrimSpace(line)
}
}
if (err == nil || err == io.EOF) && c.user != nil {
commits = append(commits, c)
}
return commits, authors, err
}
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
}