-
Notifications
You must be signed in to change notification settings - Fork 9
/
split-on-maintainer
executable file
·360 lines (328 loc) · 10.3 KB
/
split-on-maintainer
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python3
# Copyright 2020 Kees Cook <[email protected]>
# License: GPLv2+
#
# Split a single large patch into separate per-maintainer patches based on
# the MAINTAINERS entries.
#
# How to send the results: (Note that the "to" argument is intentionally a
# space to have git use the "To:" from the patches. Additional CCs can be
# also added if needed.)
#
# for i in 0*.patch; do git send-email --transfer-encoding=8bit --8bit-encoding=UTF-8 --from='Kees Cook <[email protected]>' --to=' ' --cc='...' $i; done
#
import sys, re, fnmatch, subprocess, operator, tempfile, argparse
opts = argparse.ArgumentParser(description='Split single patch by maintainer')
opts.add_argument('patches', metavar='PATCH', nargs=1, help='Patch to split')
opts.add_argument('--build-log', metavar='LOG', help='Compiler output for warning extraction')
args = opts.parse_args()
chunks = dict()
files = []
who = 'unknown author'
date = ''
text = ''
sob = ''
subject = ''
# TODO: this doesn't actually deal well with multiple files, so don't
# (see nargs=1 above).
for arg in args.patches:
path = None
body = False
in_sob = False
trailer = False
diff = False
for line in open(arg):
# diff --git a/net/decnet/dn_dev.c b/net/decnet/dn_dev.c
# index b2c26b081134..41f803e35da3 100644
# --- a/net/decnet/dn_dev.c
# +++ b/net/decnet/dn_dev.c
if line.startswith('diff '):
diff = True
path = '/'.join(line.split(' ').pop().strip().split('/')[1:])
files.append(path)
chunks.setdefault(path, '')
if not diff and not trailer:
if not body:
if line == '\n':
body = True
continue
if line.startswith('Author:') or line.startswith('From:'):
who = line.split(':', 1)[1].strip()
continue
if line.startswith('Date:'):
date = line.split(':', 1)[1].strip()
continue
if line.startswith('Subject:'):
subject = line.split(':', 1)[1].strip()
if subject.startswith('[PATCH] '):
subject = subject[8:]
if subject.startswith('treewide: '):
subject = subject[10:]
continue
continue
if line.startswith('[1]') or '-by: ' in line:
in_sob = True
if line == "---\n":
trailer = True
continue
if in_sob:
sob += line.rstrip() + "\n"
else:
text += line.rstrip() + "\n"
if path == None:
continue
chunks[path] += line
# Parse a build log to look for matching warnings...
logs = dict()
if args.build_log:
filepath = None
for line in open(args.build_log):
# drivers/tty/n_tty.c: In function ‘__process_echoes’:
# drivers/tty/n_tty.c:657:18: warning: statement will never be executed [-Wswitch-unreachable]
# 1657 | unsigned int num_chars, num_bs;
# | ^~~~~~~~~
if '|' not in line:
if ':' not in line:
raise ValueError("unparseable build log line: %s" % (line.rstrip()))
filepath = line.split(':', 1)[0]
if filepath == None:
raise ValueError('Unable to find filename in build log: %s' % (args.build_log))
logs.setdefault(filepath, '')
logs[filepath] += line
# Now parse MAINTAINERS to find how to split up the chunks...
parsing = False
patterns = dict()
email = dict()
output = dict()
contains = dict()
areas = []
area = None
for line in open('MAINTAINERS'):
if not parsing:
# Start parsing once we see all-capitals (and/or numbers)
if re.match(r'[A-Z0-9]{2}', line):
parsing = True
else:
continue
if line.startswith('\n'):
area = None
continue
if area == None:
area = line.strip()
areas.append(area)
output.setdefault(area, '')
contains.setdefault(area, [])
patterns.setdefault(area, {'re':[], 'exclude':[], 'content':[]})
email.setdefault(area, {'maint':[], 'cc':[]})
continue
try:
mark, rest = line.strip().split(':', 1)
except:
print(line.strip())
raise
rest = rest.strip()
if mark in ['M', 'P', 'L', 'R']:
# Ignore unemailable Person lines.
if mark == 'P':
if not '@' in rest:
continue
mark = 'M'
if '(' in rest:
rest, note = rest.split('(',1)
rest = rest.strip()
# Skip subscribers-only mailing lists.
if 'subscribers-only' in note:
continue
if mark == 'M':
email[area]['maint'].append(rest)
else:
email[area]['cc'].append(rest)
elif mark in ['F', 'X']:
pattern = rest
# Handle the "catch all" super-globs
if pattern == '*/':
continue
if pattern == '*':
pattern = '.*'
else:
# Otherwise convert glob to simple regex
pattern = pattern.replace('.', '\.')
pattern = pattern.replace('*', '[^/]+')
pattern = pattern.replace('?', '.')
if mark == 'F':
kind = 're'
else:
kind = 'exclude'
pair = (rest, re.compile(pattern))
patterns[area]['re'].append(pair)
elif mark in ['N']:
pair = (rest, re.compile(rest))
patterns[area]['re'].append(pair)
elif mark in ['K']:
patterns[area]['content'].append(rest)
elif mark in ['S']:
if '(' in rest:
rest, note = rest.split('(', 1)
rest = rest.strip()
if rest in ['Supported', 'Maintained', 'Odd Fixes', 'Odd fixes', 'Buried alive in reporters']:
continue
elif rest in ['Orphan', 'Obsolete', 'Orphan / Obsolete']:
# Ignore orphan or obsolete areas
area = None
parsing = False
continue
else:
raise ValueError("Unknown 'S)tatus' for area '%s': %s" % (area, rest))
def maintained(area, path):
debug = False
#if area == 'THE REST':
# debug = True
match = ''
for pattern, matcher in patterns[area]['exclude']:
if debug:
print("%s -> %s" % (pattern, matcher))
if matcher.match(path):
return ''
for pattern, matcher in patterns[area]['re']:
if debug:
print("%s -> %s" % (pattern, matcher))
if matcher.match(path):
if len(pattern) > len(match):
match = pattern
return match
def get_prefix(area, paths):
# "--follow" is very slow, but sometime needed:
#ret = subprocess.run(['git', 'log', '-n', '64', '--no-merges', '--oneline', '--follow', '--'] + paths,
ret = subprocess.run(['git', 'log', '-n', '64', '--no-merges', '--oneline', '--'] + paths,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, check=False)
commits = str(ret.stdout, 'utf-8').strip().splitlines()
prefixes = dict()
for commit in commits:
title = commit.split(' ', 1)[1].strip()
# Drop old-style []s
if '[PATCH]' in title:
title = title.replace('[PATCH]','').strip()
if '[' in title:
prefix = title.partition(']')[0].replace('[','').replace(']','')
title = title.replace('[%s]' % (prefix), '').strip()
if not title.startswith(':'):
prefix += ': '
title = prefix.lower() + title
if title.startswith('Revert "'):
continue
if ':' in title:
prefix = title.rpartition(':')[0]
# Ignore useless prefixes.
if not prefix in ['net', 'treewide']:
prefixes.setdefault(prefix, 0)
prefixes[prefix] += 1
best = 0
prefix = None
likely = sorted(prefixes.items(), key=operator.itemgetter(1), reverse=True)
# Drop pointless prefixes
if 'treewide' in likely:
likely.remove('treewide')
count = len(likely)
if len(likely):
return likely[0][0]
return area
def get_ccs(diff_str, author):
patch = tempfile.NamedTemporaryFile(mode='w', prefix='get_ccs-', suffix='.patch', encoding='utf-8')
patch.write(diff_str)
patch.flush()
ccs = subprocess.run(["./scripts/get_maintainer.pl", "--email",
"--git-min-percent", "15",
"--git-since", '3-years-ago',
"--no-rolestats", patch.name],
stdout=subprocess.PIPE,
encoding='utf8').stdout.splitlines()
if author in ccs:
ccs.remove(author)
return ccs
for path in files:
longest = ''
hit = None
sticky = None
for area in areas:
match = maintained(area, path)
if len(match) > len(longest):
longest = match
hit = area
if len(longest) == 0:
raise ValueError("Catch-all didn't catch all!? %s" % (path))
output[hit] += chunks[path]
contains[hit].append(path)
counter = 0
for area in output:
if len(output[area]) == 0:
continue
#print("\n".join(contains[area]))
print("%s ..." % area)
for path in contains[area]:
print("\t%s" % path)
prefix = get_prefix(area, contains[area])
# Make sure this goes somewhere
if len(email[area]['maint']) == 0:
email[area]['maint'].append('[email protected]')
else:
email[area]['cc'].append('[email protected]')
maintainer_ccs = email[area]['cc']
# There are some unwritten rules about top-level maintainers...
overrides = []
for path in contains[area]:
if path.startswith('drivers/char/') or \
path.startswith('drivers/misc/') or \
path.startswith('drivers/usb/'):
overrides.append('Greg Kroah-Hartman <[email protected]>')
if '[email protected]' in maintainer_ccs:
overrides.append('Andrew Morton <[email protected]>')
tos = overrides
tos.extend(x for x in email[area]['maint'] if x not in overrides)
# Perform proper "get_maintainer.pl" expansion...
ccs = [x for x in get_ccs(output[area], who) if x not in tos]
ccs.extend(x for x in maintainer_ccs if x not in tos and x not in ccs)
# More unwritten rules for wireless...
if '[email protected],' in ccs:
if '[email protected]' not in ccs:
ccs.append('[email protected]')
if not prefix.startswith('wifi: '):
prefix = "wifi: %s" % (prefix)
counter += 1
fname = "%s %s" % (prefix, subject)
fname = re.sub(r'[^a-zA-Z0-9]+', '-', fname)
fname = "%04d-%s.patch" % (counter, fname)
out = open(fname, "w")
print("\t\t%s" % fname)
print("From auto-maintainer-split", file=out)
print("From: %s" % (who), file=out)
print("Date: %s" % (date), file=out)
print("To: %s" % (", ".join(tos)), file=out)
print("Cc: %s" % (", ".join(ccs)), file=out)
if subject != '':
# Explicit subject
combined = "[PATCH] %s: %s" % (prefix, subject)
print("Subject: %s" % (combined), file=out)
print("", file=out)
print(text.strip(), file=out)
else:
# Body contains the subject
combined = text.strip()
if not combined.startswith('[PATCH] '):
combined = "[PATCH] %s: %s" % (prefix, combined)
print("Subject: %s" % (combined), file=out)
print("\t\t\tSubject: %s" % (combined))
# Emit any log lines
if args.build_log:
print("", file=out)
for path in contains[area]:
print(logs[path], file=out)
tag_ccs = tos
tag_ccs.extend(x for x in ccs if x not in tos and x != "[email protected]")
print("Cc: %s" % ("\nCc: ".join(tag_ccs)), file=out)
print(sob.strip(), file=out)
print("---", file=out)
print(subprocess.run(["diffstat", "-p1"], stdout=subprocess.PIPE,
input=output[area], encoding='utf8').stdout, file=out)
print(output[area], file=out)
out.close()