-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgitlog.go
More file actions
174 lines (153 loc) · 4.08 KB
/
Copy pathgitlog.go
File metadata and controls
174 lines (153 loc) · 4.08 KB
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
package gitlog
import (
"bufio"
"context"
"fmt"
"io"
"io/ioutil"
"os/exec"
"strconv"
"strings"
"time"
)
// Commit represents a parsed commit from git log
type Commit struct {
SHA string
Author Event
Committer Event
Stats map[string]Stat
}
// Event represents the who and when of a commit event
type Event struct {
Name string
Email string
When time.Time
}
// Stat holds the diff stat of a file
type Stat struct {
Additions int
Deletions int
}
// Result is a list of commits
type Result []*Commit
func parseLog(reader io.Reader) (Result, error) {
scanner := bufio.NewScanner(reader)
res := make(Result, 0)
// line prefixes for the `fuller` formatted output
const (
commit = "commit "
author = "Author: "
authorDate = "AuthorDate: "
committer = "Commit: "
commitDate = "CommitDate: "
)
var currentCommit *Commit
for scanner.Scan() {
line := scanner.Text()
switch {
case strings.HasPrefix(line, commit):
if currentCommit != nil { // if we're seeing a new commit but already have a current commit, we've finished a commit
res = append(res, currentCommit)
}
currentCommit = &Commit{
SHA: strings.TrimPrefix(line, commit),
Stats: make(map[string]Stat),
}
case strings.HasPrefix(line, author):
s := strings.TrimPrefix(line, author)
spl := strings.Split(s, " ")
email := strings.Trim(spl[len(spl)-1], "<>")
name := strings.Join(spl[:len(spl)-1], " ")
currentCommit.Author.Email = strings.Trim(email, "<>")
currentCommit.Author.Name = strings.TrimSpace(name)
case strings.HasPrefix(line, authorDate):
authorDateString := strings.TrimPrefix(line, authorDate)
aD, err := time.Parse(time.RFC3339, authorDateString)
if err != nil {
return nil, err
}
currentCommit.Author.When = aD
case strings.HasPrefix(line, committer):
s := strings.TrimPrefix(line, committer)
spl := strings.Split(s, " ")
email := strings.Trim(spl[len(spl)-1], "<>")
name := strings.Join(spl[:len(spl)-1], " ")
currentCommit.Committer.Email = strings.Trim(email, "<>")
currentCommit.Committer.Name = strings.TrimSpace(name)
case strings.HasPrefix(line, commitDate):
commitDateString := strings.TrimPrefix(line, commitDate)
cD, err := time.Parse(time.RFC3339, commitDateString)
if err != nil {
return nil, err
}
currentCommit.Committer.When = cD
case strings.HasPrefix(line, " "): // ignore commit message lines
case strings.TrimSpace(line) == "": // ignore empty lines
default:
s := strings.Split(line, "\t")
var additions int
var deletions int
var err error
if s[0] != "-" {
additions, err = strconv.Atoi(s[0])
if err != nil {
return nil, err
}
}
if s[1] != "-" {
deletions, err = strconv.Atoi(s[1])
if err != nil {
return nil, err
}
}
currentCommit.Stats[s[2]] = Stat{
Additions: additions,
Deletions: deletions,
}
}
}
if currentCommit != nil {
res = append(res, currentCommit)
}
return res, nil
}
// Exec runs the git log command
func Exec(ctx context.Context, repoPath string, filePattern string, additionalFlags []string) (Result, error) {
gitPath, err := exec.LookPath("git")
if err != nil {
return nil, fmt.Errorf("could not find git: %w", err)
}
args := []string{"log"}
// TODO we should allow a way for the caller to specify additional flags (in a way that doesn't conflict with these)
args = append(args, "--numstat", "--format=fuller", "--no-merges", "--no-decorate", "--date=iso8601-strict", "-w")
args = append(args, additionalFlags...)
if filePattern != "" {
args = append(args, filePattern)
}
cmd := exec.CommandContext(ctx, gitPath, args...)
cmd.Dir = repoPath
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}
res, err := parseLog(stdout)
if err != nil {
return nil, err
}
errs, err := ioutil.ReadAll(stderr)
if err != nil {
return nil, err
}
if err := cmd.Wait(); err != nil {
fmt.Println(string(errs))
return nil, err
}
return res, nil
}