-
Notifications
You must be signed in to change notification settings - Fork 0
/
bscdiff.go
201 lines (178 loc) · 4.88 KB
/
bscdiff.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// bscdiff compares bsc, issue and CVE numbers from a source changelog, to a
// target changelog. Missing numbers are then printed with their occurrence
// in the source changelog.
//
// Usage: bscdiff <source_file> <target_file>
package main
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"sort"
)
func init() {
// The syscall restriciton is only available for Linux right now via
// seccomp.
applySyscallRestrictions()
}
type searchResult struct {
line int
match []string
text string
}
var regexStrings []string
func main() {
var out io.Writer = os.Stdout // needed to redirect output for testing
// These are the regex-strings that will be used later on.
regexStrings = []string{
`bsc#\d*`,
`bnc#\d*`,
`fate#\d*`,
`U#\d*`,
`(CVE-(1999|2\d{3})-(0\d{2}[1-9]|[1-9]\d{3,}))`}
args := os.Args
if len(args) > 1 && (args[1] == "-h" || args[1] == "--help") {
fmt.Println("bscdiff compares bsc, issue and CVE numbers from a source changelog, ")
fmt.Println("to a target changelog. Missing numbers are then printed with their ")
fmt.Println("occurrence in the source changelog")
fmt.Println()
fmt.Println(fmt.Sprintf("usage: %s <source_file> <target_file>\n", args[0]))
os.Exit(0)
}
if len(args) < 3 {
fmt.Fprintf(os.Stderr, "usage: %s <source_file> <target_file>", args[0])
os.Exit(1)
}
// Check if files are actually there… and files.
for _, file := range os.Args[1:3] {
if !fileExists(file) {
fmt.Fprintf(os.Stderr, "%s does not exist!", file)
os.Exit(1)
}
}
c1 := make(chan []searchResult)
c2 := make(chan []searchResult)
var searchResults1 []searchResult
var searchResults2 []searchResult
go scanFile(args[1], c1)
go scanFile(args[2], c2)
for i := 0; i < 2; i++ {
select {
case msg1 := <-c1:
searchResults1 = msg1
case msg2 := <-c2:
searchResults2 = msg2
}
}
missingBscs := findMissingBsc(searchResults1, searchResults2)
prettyPrintMissingBscs(searchResults1, missingBscs, out)
}
// Outputs the missing BSC numbers in a useful format.
func prettyPrintMissingBscs(searchResults1 []searchResult, missingBscs []string, out io.Writer) {
sort.Strings(missingBscs)
for _, bsc := range missingBscs {
for _, searchResult := range searchResults1 {
searchPos := sort.SearchStrings(searchResult.match, bsc)
if searchPos < len(searchResult.match) && searchResult.match[searchPos] == bsc {
fmt.Fprintf(out, "%d: %s -> %s\n",
searchResult.line,
bsc,
searchResult.text)
}
}
}
}
// Returns a list of BSC numbers, that are missing from the second changelog file.
func findMissingBsc(changelog1 []searchResult, changelog2 []searchResult) []string {
bscList1 := getBscs(changelog1)
bscList2 := getBscs(changelog2)
sort.Strings(bscList1)
sort.Strings(bscList2)
var missingBscs []string
for _, bsc := range bscList1 {
searchPos := sort.SearchStrings(bscList2, bsc)
if searchPos >= len(bscList2) || (searchPos < len(bscList2) && bscList2[searchPos] != bsc) {
missingBscs = append(missingBscs, bsc)
}
}
return removeDuplicates(missingBscs)
}
// Extracts the BSC numbers from the search results and returns them as an array.
func getBscs(res []searchResult) []string {
var bsc []string
for _, v := range res {
for _, value := range v.match {
bsc = append(bsc, value)
}
}
return bsc
}
// Scans the file for bsc, CVE and issue numbers and returns the search results.
func scanFile(pathToFile string, ch chan<- []searchResult) {
var regexes []*regexp.Regexp
// creating the regexes with the regex-strings from main().
for _, regexString := range regexStrings {
var re, _ = regexp.Compile(regexString)
regexes = append(regexes, re)
}
lines, err := scanLines(pathToFile)
if err != nil {
panic(err)
}
var searchResults []searchResult
for i, line := range lines {
for _, re := range regexes {
results := re.FindAllString(line, -1)
if len(results) > 0 {
res := searchResult{
line: i + 1,
match: results,
text: line}
searchResults = append(searchResults, res)
}
}
}
ch <- searchResults
}
// Returns the given file as an array of lines.
func scanLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, nil
}
// Removes duplicates form an array.
func removeDuplicates(s []string) []string {
m := make(map[string]bool)
for _, item := range s {
if _, ok := m[item]; ok {
// duplicate item
} else {
m[item] = true
}
}
var result []string
for item := range m {
result = append(result, item)
}
return result
}
// fileExists checks if a file exists and is not a directory before we
// try using it to prevent further errors.
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}