-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
117 lines (101 loc) · 2.47 KB
/
scanner.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
package main
import (
"regexp"
"strings"
)
type Match struct {
line string
filename string
commit string
author string
}
func (match *Match) ArrayWithDescription(description string) []string {
return []string{
description,
match.filename,
match.commit,
match.author,
match.line,
}
}
type Scanner struct {
description string
re *regexp.Regexp
matches []Match
currAuthor string
currFilename string
currCommit string
}
var reFilename *regexp.Regexp = regexp.MustCompile("[+-]{3} (a/(.*?$)|b/(.*?$))")
var reCommit *regexp.Regexp = regexp.MustCompile("^commit ([a-f0-9]{40})")
var reAuthor *regexp.Regexp = regexp.MustCompile("Author: (.*?) <")
func NewScanner(description string, pattern string) *Scanner {
scanner := Scanner{description: description}
scanner.re = regexp.MustCompile(pattern)
return &scanner
}
func (me *Scanner) ScanLine(line string) bool {
var ok bool
var filename, commit, author string
if ok, filename = getFilename(line); ok {
me.currFilename = filename
return false
}
if ok, commit = getCommit(line); ok {
me.currCommit = commit
return false
}
if ok, author = getAuthor(line); ok {
me.currAuthor = author
return false
}
match := me.re.FindString(line)
if len(match) > 0 {
trimmed := strings.Trim(line, "\n")
match := Match{
line: trimmed,
author: me.currAuthor,
commit: me.currCommit,
filename: me.currFilename,
}
me.matches = append(me.matches, match)
return true
}
return false
}
func (me *Scanner) Records() [][]string {
res := [][]string{}
for _, match := range me.matches {
res = append(res, match.ArrayWithDescription(me.description))
}
return res
}
func getFilename(line string) (bool, string) {
// We can do it this way because if the filename changes
// we will care what it changed to more than what it
// changed from so the b/filename will overwrite unless
// the file was deleted in which case it will be /dev/null
// Otherwise, the filenames will be the same
matches := reFilename.FindStringSubmatch(line)
if len(matches) < 4 {
return false, ""
}
if len(matches[2]) > 0 {
return true, matches[2]
}
return true, matches[3]
}
func getCommit(line string) (bool, string) {
matches := reCommit.FindStringSubmatch(line)
if len(matches) > 1 {
return true, matches[1]
}
return false, ""
}
func getAuthor(line string) (bool, string) {
matches := reAuthor.FindStringSubmatch(line)
if len(matches) > 1 {
return true, matches[1]
}
return false, ""
}