-
Notifications
You must be signed in to change notification settings - Fork 7
/
parse.go
178 lines (162 loc) · 3.52 KB
/
parse.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
package porcelain
import (
"bufio"
"io"
"log"
"strconv"
"strings"
)
func consumeNext(s *bufio.Scanner) string {
if s.Scan() {
return s.Text()
}
return ""
}
func (pi *PorcInfo) ParsePorcInfo(r io.Reader) error {
log.Println("parsing git output")
var err error
var s = bufio.NewScanner(r)
for s.Scan() {
if len(s.Text()) < 1 {
continue
}
// NOTE we only return the last error, probably should fail early?
err = pi.ParseLine(s.Text())
if err != nil {
log.Printf("error parsing %q: %v", s.Text(), err)
}
}
return err
}
func (pi *PorcInfo) ParseLine(line string) error {
s := bufio.NewScanner(strings.NewReader(line))
// switch to a word based scanner
s.Split(bufio.ScanWords)
var err error
for s.Scan() {
switch s.Text() {
case "#":
err = pi.parseBranchInfo(s)
if err != nil {
log.Printf("error parsing branch: %v", err)
}
case "1":
err = pi.parseTrackedFile(s)
if err != nil {
log.Printf("error parsing tracked file: %v", err)
}
case "2":
err = pi.parseRenamedFile(s)
if err != nil {
log.Printf("error parsing renamed file: %v", err)
}
case "u":
pi.unmerged++
case "?":
pi.untracked++
}
}
return nil
}
func (pi *PorcInfo) parseBranchInfo(s *bufio.Scanner) (err error) {
// uses the word based scanner from ParseLine
for s.Scan() {
switch s.Text() {
case "branch.oid":
pi.commit = consumeNext(s)
case "branch.head":
pi.branch = consumeNext(s)
case "branch.upstream":
pi.upstream = consumeNext(s)
case "branch.ab":
err = pi.parseAheadBehind(s)
if err != nil {
log.Printf("error parsing branch.ab: %v", err)
}
}
}
return err
}
func (pi *PorcInfo) parseAheadBehind(s *bufio.Scanner) error {
// uses the word based scanner from ParseLine
for s.Scan() {
i, err := strconv.Atoi(s.Text()[1:])
if err != nil {
return err
}
switch s.Text()[:1] {
case "+":
pi.ahead = i
case "-":
pi.behind = i
}
}
return nil
}
// parseTrackedFile parses the porcelain v2 output for tracked entries
// doc: https://git-scm.com/docs/git-status#_changed_tracked_entries
//
func (pi *PorcInfo) parseTrackedFile(s *bufio.Scanner) error {
// uses the word based scanner from ParseLine
var index int
var err error
for s.Scan() {
switch index {
case 0: // xy
err = pi.parseXY(s.Text())
if err != nil {
log.Printf("error parsing XY: %q: %v", s.Text(), err)
}
default:
continue
// case 1: // sub
// if s.Text() != "N..." {
// log.Println("is submodule!!!")
// }
// case 2: // mH - octal file mode in HEAD
// log.Println(index, s.Text())
// case 3: // mI - octal file mode in index
// log.Println(index, s.Text())
// case 4: // mW - octal file mode in worktree
// log.Println(index, s.Text())
// case 5: // hH - object name in HEAD
// log.Println(index, s.Text())
// case 6: // hI - object name in index
// log.Println(index, s.Text())
// case 7: // path
// log.Println(index, s.Text())
}
index++
}
return nil
}
func (pi *PorcInfo) parseXY(xy string) error {
switch xy[:1] { // parse staged
case "M":
pi.Staged.modified++
case "A":
pi.Staged.added++
case "D":
pi.Staged.deleted++
case "R":
pi.Staged.renamed++
case "C":
pi.Staged.copied++
}
switch xy[1:] { // parse unstaged
case "M":
pi.Unstaged.modified++
case "A":
pi.Unstaged.added++
case "D":
pi.Unstaged.deleted++
case "R":
pi.Unstaged.renamed++
case "C":
pi.Unstaged.copied++
}
return nil
}
func (pi *PorcInfo) parseRenamedFile(s *bufio.Scanner) error {
return pi.parseTrackedFile(s)
}