-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp.go
More file actions
317 lines (276 loc) · 10.1 KB
/
mp.go
File metadata and controls
317 lines (276 loc) · 10.1 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// This is a re-implementation of "maint-modify-patch" which is written in
// python.
//
package main
import (
"fmt"
"strings"
"os"
"bufio"
"github.com/jessevdk/go-flags"
"io/ioutil"
"os/user"
"path"
"encoding/json"
"regexp"
)
var args struct {
Acks []string `long:"ack" description:"An irc nickname which will be expanded to full a email address and added as an 'Acked-by:' line."`
Sobs []string `long:"sob" description:"An irc nickname which will be expanded to full a email address and added as an 'Signed-off-by:' line."`
Bugs []string `long:"bug" description:"A Launchpad bug-id which will be used to generate Buglink URL lines."`
CPs []string `long:"cp" description:"The SHA1 of a commit that will make up 'cherry-pick' lines in the signed-off-by block of the commit."`
BPs []string `long:"bp" description:"The SHA1 of a commit that will make up 'back-port' lines in the signed-off-by block of the commit."`
CVE string `long:"cve" description:"A CVE number which will be inserted into the commit body."`
ListAliases bool `long:"list-aliases" description:"Print out all the aliases found in the aliases file."`
}
var aliases map[string]interface{}
// hasString
// Does the given slice contain the specified string?
//
func hasString(slice []string, target string) bool {
var retval bool = false
for _, str := range slice {
if str == target {
retval = true
break
}
}
return retval
}
// sobBlock
//
func sobBlock(existingAcks []string, existingSobs []string, existingCps []string, existingBps []string) (string) {
var retval string
for _, ack := range args.Acks {
if !hasString(existingAcks, ack) {
retval += fmt.Sprintf("Acked-by: %s\n", aliases[ack])
}
}
for _, bp := range args.BPs {
if !hasString(existingBps, bp) {
retval += fmt.Sprintf("(backported from commit %s upstream)\n", bp)
}
}
for _, cp := range args.CPs {
if !hasString(existingCps, cp) {
retval += fmt.Sprintf("(cherry picked from commit %s)\n", cp)
}
}
for _, sob := range args.Sobs {
if !hasString(existingSobs, sob) {
retval += fmt.Sprintf("Signed-off-by: %s\n", aliases[sob])
}
}
return retval
}
// modify
// Open a single file; modify it's contents as they are copied to a temporary file;
// mv the temporary file to the origian file.
//
func modify(fid string) (error) {
var (
justCopy bool = false
looking4SOBInsertionPoint bool = false
looking4SubjectLine bool = true
looking4BuglinkInsertionPoint bool = false
subjectLine bool = false
cveInsertionPoint bool = false
buglinkInsertionPoint bool = false
sobInsertionPoint bool = false
existingCVEs []string
existingBugIds []string
existingAcks []string
existingSobs []string
existingCps []string
existingBps []string
buglinkBaseUrl string = "http://bugs.launchpad.net/bugs/"
cpRC = regexp.MustCompile("cherry picked from commit ([0-9a-zA-Z]+)")
bpRC = regexp.MustCompile("backported from commit ([0-9a-zA-Z]+) upstream")
)
// Open the input file, return the error if there is one.
//
inputFile, err := os.Open(fid)
if err != nil {
return err
}
defer inputFile.Close()
// Open the temp. file, return the error if there is one.
//
dst, err := ioutil.TempFile("./", "mp__")
if err != nil {
return err
}
defer dst.Close()
scanner := bufio.NewScanner(inputFile)
for scanner.Scan() {
line := scanner.Text()
if justCopy {
dst.WriteString(line)
dst.WriteString("\n")
continue
}
// If we are looking for the Sob insertion point then we've handled
// all the other cases and we just need to find the sob section.
//
if looking4SOBInsertionPoint {
looking4SOBInsertionPoint = true
if line == "---" {
sobInsertionPoint = true
looking4SOBInsertionPoint = false
} else {
if strings.Contains(line, "Acked-by:") {
id := strings.Replace(line, "Acked-by:", "", -1)
if !hasString(existingAcks, id) {
existingAcks = append(existingAcks, id)
}
}
if strings.Contains(line, "Signed-off-by:") {
id := strings.Replace(line, "Signed-off-by:", "", -1)
if !hasString(existingSobs, id) {
existingSobs = append(existingSobs, id)
}
}
if strings.Contains(line, "cherry picked") {
result := cpRC.FindStringSubmatch(line)
existingCps = append(existingCps, result[1])
}
if strings.Contains(line, "backported from") {
result := bpRC.FindStringSubmatch(line)
existingBps = append(existingBps, result[1])
}
}
}
if sobInsertionPoint {
dst.WriteString(sobBlock(existingAcks, existingSobs, existingCps, existingBps))
sobInsertionPoint = false
justCopy = true
}
// After the first blank line after the subject line is where we
// want to insert our CVE lines if we need to insert any.
//
if cveInsertionPoint {
cveInsertionPoint = true
if strings.Contains(line, "CVE-") {
cve := strings.Replace(line, "CVE-", "", -1)
existingCVEs = append(existingCVEs, cve)
} else {
// Add the CVE id here.
//
if args.CVE != "" {
if !hasString(existingCVEs, args.CVE) {
dst.WriteString("CVE-")
dst.WriteString(args.CVE)
dst.WriteString("\n")
dst.WriteString("\n") // One blank line after the CVE line (this assumes there is only one CVE)
}
}
cveInsertionPoint = false
looking4BuglinkInsertionPoint = true
// We don't know at this point if we are going to insert a Buglink
// so we can't write out the current line of text.
}
}
// After the first blank line after the CVE lines is where the Buglinks are to be
// inserted.
//
if looking4BuglinkInsertionPoint {
if line != "" {
looking4BuglinkInsertionPoint = false
buglinkInsertionPoint = true
}
}
if buglinkInsertionPoint {
buglinkInsertionPoint = true
// Just like the CVEs we skip past any existing BugLink lines and build a list of existing
// buglinks so we don't duplicate any.
//
if strings.Contains(line, "BugLink:") {
s := strings.Split(line, "/")
id := s[len(s)-1]
existingBugIds = append(existingBugIds, id)
} else {
if len(args.Bugs) > 0 {
for _, id := range args.Bugs {
if !hasString(existingBugIds, id) {
dst.WriteString(fmt.Sprintf("BugLink: %s%s\n", buglinkBaseUrl, id))
}
}
dst.WriteString("\n") // One blank line after the BugLink line
}
buglinkInsertionPoint = false
looking4SOBInsertionPoint = true
}
}
// Once we've found the subject line, we look for the first blank line after it.
//
if subjectLine {
if line == "" {
cveInsertionPoint = true
subjectLine = false
}
}
// All modificatins that we make are made after the subject line, therefore that's
// the first thing we look for.
//
if looking4SubjectLine {
if strings.Contains(line, "Subject:") {
subjectLine = true
looking4SubjectLine = false
}
}
dst.WriteString(line)
dst.WriteString("\n")
}
// If the scanner encountered an error, return it.
//
if err := scanner.Err(); err != nil {
return err
}
os.Rename(dst.Name(), inputFile.Name())
return nil
}
// main
// This is where the bugs start.
//
func main() {
// Load the aliases, JSON, file.
//
usr, err := user.Current()
if err != nil {
fmt.Printf(" Unable to determine the current user.\n")
os.Exit(1)
}
aliasesPath := path.Join(usr.HomeDir, ".sob-aliases")
file, e := ioutil.ReadFile(aliasesPath)
if e != nil {
fmt.Printf("File error: %v\n", e)
os.Exit(1)
}
if err := json.Unmarshal(file, &aliases); err != nil {
fmt.Printf(" File error: %v\n", err)
os.Exit(1)
}
// Basic command line arg parsing
//
fileNames, err := flags.Parse(&args)
if err != nil {
os.Exit(1)
}
if args.ListAliases {
fmt.Printf("list aliases\n")
for k, v := range aliases {
fmt.Printf("%20s : %s\n", k, v)
}
os.Exit(1)
}
if len(fileNames) == 0 {
fmt.Printf(" Error: No files were specified on the command line.\n")
os.Exit(1)
}
for _, fid := range fileNames {
err := modify(fid)
if err != nil {
fmt.Printf("%s", err)
}
}
}