forked from robfig/glock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logparser.go
164 lines (147 loc) · 3.98 KB
/
logparser.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
package main
import (
"bufio"
"io"
"regexp"
)
// diff represents a line of difference in a commit.
// the zero value represent non-matching lines in the diff.
// (this is necessary to distinguish an update from adding in one commit and
// removing in the next)
type diff struct {
importPath, revision string
added bool
}
var emptyLine = diff{}
type action int
const (
add action = iota
remove
update
)
type libraryAction struct {
action action
importPath, revision string
}
type cmdAction struct {
add bool
importPath string
}
type playbook struct {
library []libraryAction // updates to libraries in gopath
cmd []cmdAction // updates to cmds that should be built
}
const (
importPathExpr = `[\w.]+\.\w+/[\w/.-]+`
libLineExpr = `[+-](` + importPathExpr + `) (\w+)`
cmdLineExpr = `[+-]cmd (` + importPathExpr + `)`
)
var (
libLineRegex = regexp.MustCompile(libLineExpr)
cmdLineRegex = regexp.MustCompile(cmdLineExpr)
)
func readDiffLines(reader io.Reader) []diff {
// Get the list of diffs from the commit log.
var (
diffs []diff
scanner = bufio.NewScanner(reader)
)
for scanner.Scan() {
var txt = scanner.Text()
if matches := libLineRegex.FindStringSubmatch(txt); matches != nil {
diffs = append(diffs, diff{
importPath: matches[1],
revision: matches[2],
added: txt[0] == '+',
})
} else if matches := cmdLineRegex.FindStringSubmatch(txt); matches != nil {
diffs = append(diffs, diff{
importPath: matches[1],
revision: "cmd",
added: txt[0] == '+',
})
} else {
diffs = append(diffs, emptyLine)
}
}
return diffs
}
func processDiffBlock(diffs []diff) []libraryAction {
// Assume:
// - An import path appears once or twice in a block. If twice, it was updated.
var importPathActions = make(map[string]libraryAction)
for _, this := range diffs {
// Calculate the new action for this import path.
// (Potentially this updates a previously recorded action from e.g. add to update)
if existing, ok := importPathActions[this.importPath]; ok {
if existing.action == update {
panic("unexpected: found import path " + this.importPath + " > 2 times in the diff")
}
importPathActions[this.importPath] = newUpdate(existing, this)
} else {
importPathActions[this.importPath] = newAddOrRemove(this)
}
}
// Build all the library actions
var result []libraryAction
for _, action := range importPathActions {
result = append(result, action)
}
return result
}
func buildPlaybook(diffs []diff) playbook {
// Convert diffs into actions. Since they may touch the same lines over
// multiple commits, keep track of import paths that we've added commands for,
// and only add the first.
var (
book playbook
block []diff
seenImportPaths = make(map[string]struct{})
)
for _, this := range append(diffs, emptyLine) {
switch {
case this == emptyLine:
// Consume the past block of diffs. Blocks of diffs are separated by emptyLine.
// Assume:
// - Blocks are processed in reverse chronological order. If we've seen an
// import path in a previous block, ignore it entirely.
if block == nil {
continue
}
for _, libAction := range processDiffBlock(block) {
if _, ok := seenImportPaths[libAction.importPath]; ok {
continue
}
seenImportPaths[libAction.importPath] = struct{}{}
book.library = append(book.library, libAction)
}
block = nil
case this.revision == "cmd":
book.cmd = append(book.cmd, cmdAction{this.added, this.importPath})
default:
block = append(block, this)
}
}
return book
}
func newUpdate(a libraryAction, b diff) libraryAction {
if b.added {
return newCommand(update, b)
}
a.action = update
return a
}
func newAddOrRemove(d diff) libraryAction {
var action = add
if !d.added {
action = remove
}
return newCommand(action, d)
}
func newCommand(a action, d diff) libraryAction {
return libraryAction{
action: a,
importPath: d.importPath,
revision: d.revision,
}
}