blob: f302ad3759f35d661899f6e204b7c0cd9445e74a [file] [log] [blame]
Joe Perchescb7301c2009-04-07 20:40:12 -07001#!/usr/bin/perl -w
2# (c) 2007, Joe Perches <joe@perches.com>
3# created from checkpatch.pl
4#
5# Print selected MAINTAINERS information for
6# the files modified in a patch or for a file
7#
8# usage: perl scripts/get_maintainers.pl [OPTIONS] <patch>
9# perl scripts/get_maintainers.pl [OPTIONS] -f <file>
10#
11# Licensed under the terms of the GNU GPL License version 2
12
13use strict;
14
15my $P = $0;
Joe Perches290603c12009-06-16 15:33:58 -070016my $V = '0.16';
Joe Perchescb7301c2009-04-07 20:40:12 -070017
18use Getopt::Long qw(:config no_auto_abbrev);
19
20my $lk_path = "./";
21my $email = 1;
22my $email_usename = 1;
23my $email_maintainer = 1;
24my $email_list = 1;
25my $email_subscriber_list = 0;
26my $email_git = 1;
27my $email_git_penguin_chiefs = 0;
28my $email_git_min_signatures = 1;
29my $email_git_max_maintainers = 5;
30my $email_git_since = "1-year-ago";
31my $output_multiline = 1;
32my $output_separator = ", ";
33my $scm = 0;
34my $web = 0;
35my $subsystem = 0;
36my $status = 0;
Joe Perches4a7fdb52009-04-10 12:28:57 -070037my $from_filename = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070038my $version = 0;
39my $help = 0;
40
41my $exit = 0;
42
43my @penguin_chief = ();
44push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
45#Andrew wants in on most everything - 2009/01/14
46#push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
47
48my @penguin_chief_names = ();
49foreach my $chief (@penguin_chief) {
50 if ($chief =~ m/^(.*):(.*)/) {
51 my $chief_name = $1;
52 my $chief_addr = $2;
53 push(@penguin_chief_names, $chief_name);
54 }
55}
56my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
57
58if (!GetOptions(
59 'email!' => \$email,
60 'git!' => \$email_git,
61 'git-chief-penguins!' => \$email_git_penguin_chiefs,
62 'git-min-signatures=i' => \$email_git_min_signatures,
63 'git-max-maintainers=i' => \$email_git_max_maintainers,
64 'git-since=s' => \$email_git_since,
65 'm!' => \$email_maintainer,
66 'n!' => \$email_usename,
67 'l!' => \$email_list,
68 's!' => \$email_subscriber_list,
69 'multiline!' => \$output_multiline,
70 'separator=s' => \$output_separator,
71 'subsystem!' => \$subsystem,
72 'status!' => \$status,
73 'scm!' => \$scm,
74 'web!' => \$web,
Joe Perches4a7fdb52009-04-10 12:28:57 -070075 'f|file' => \$from_filename,
Joe Perchescb7301c2009-04-07 20:40:12 -070076 'v|version' => \$version,
77 'h|help' => \$help,
78 )) {
79 usage();
80 die "$P: invalid argument\n";
81}
82
83if ($help != 0) {
84 usage();
85 exit 0;
86}
87
88if ($version != 0) {
89 print("${P} ${V}\n");
90 exit 0;
91}
92
Joe Perchescb7301c2009-04-07 20:40:12 -070093if ($#ARGV < 0) {
94 usage();
95 die "$P: argument missing: patchfile or -f file please\n";
96}
97
98my $selections = $email + $scm + $status + $subsystem + $web;
99if ($selections == 0) {
100 usage();
101 die "$P: Missing required option: email, scm, status, subsystem or web\n";
102}
103
104if ($email && ($email_maintainer + $email_list + $email_subscriber_list
105 + $email_git + $email_git_penguin_chiefs) == 0) {
106 usage();
107 die "$P: Please select at least 1 email option\n";
108}
109
110if (!top_of_kernel_tree($lk_path)) {
111 die "$P: The current directory does not appear to be "
112 . "a linux kernel source tree.\n";
113}
114
115## Read MAINTAINERS for type/value pairs
116
117my @typevalue = ();
118open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
119while (<MAINT>) {
120 my $line = $_;
121
122 if ($line =~ m/^(\C):\s*(.*)/) {
123 my $type = $1;
124 my $value = $2;
125
126 ##Filename pattern matching
127 if ($type eq "F" || $type eq "X") {
128 $value =~ s@\.@\\\.@g; ##Convert . to \.
129 $value =~ s/\*/\.\*/g; ##Convert * to .*
130 $value =~ s/\?/\./g; ##Convert ? to .
131 }
132 push(@typevalue, "$type:$value");
133 } elsif (!/^(\s)*$/) {
134 $line =~ s/\n$//g;
135 push(@typevalue, $line);
136 }
137}
138close(MAINT);
139
Joe Perches4a7fdb52009-04-10 12:28:57 -0700140## use the filenames on the command line or find the filenames in the patchfiles
Joe Perchescb7301c2009-04-07 20:40:12 -0700141
142my @files = ();
143
Joe Perches4a7fdb52009-04-10 12:28:57 -0700144foreach my $file (@ARGV) {
145 next if ((-d $file));
146 if (!(-f $file)) {
147 die "$P: file '${file}' not found\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700148 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700149 if ($from_filename) {
150 push(@files, $file);
151 } else {
152 my $file_cnt = @files;
153 open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
154 while (<PATCH>) {
155 if (m/^\+\+\+\s+(\S+)/) {
156 my $filename = $1;
157 $filename =~ s@^[^/]*/@@;
158 $filename =~ s@\n@@;
159 push(@files, $filename);
160 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700161 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700162 close(PATCH);
163 if ($file_cnt == @files) {
164 die "$P: file '${file}' doesn't appear to be a patch. "
165 . "Add -f to options?\n";
166 }
167 @files = sort_and_uniq(@files);
Joe Perchescb7301c2009-04-07 20:40:12 -0700168 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700169}
170
171my @email_to = ();
Joe Perches290603c12009-06-16 15:33:58 -0700172my @list_to = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700173my @scm = ();
174my @web = ();
175my @subsystem = ();
176my @status = ();
177
178# Find responsible parties
179
180foreach my $file (@files) {
181
182#Do not match excluded file patterns
183
184 my $exclude = 0;
185 foreach my $line (@typevalue) {
Joe Perches290603c12009-06-16 15:33:58 -0700186 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700187 my $type = $1;
188 my $value = $2;
189 if ($type eq 'X') {
190 if (file_match_pattern($file, $value)) {
191 $exclude = 1;
192 }
193 }
194 }
195 }
196
197 if (!$exclude) {
198 my $tvi = 0;
199 foreach my $line (@typevalue) {
Joe Perches290603c12009-06-16 15:33:58 -0700200 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700201 my $type = $1;
202 my $value = $2;
203 if ($type eq 'F') {
204 if (file_match_pattern($file, $value)) {
205 add_categories($tvi);
206 }
207 }
208 }
209 $tvi++;
210 }
211 }
212
Joe Perches4a7fdb52009-04-10 12:28:57 -0700213 if ($email && $email_git) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700214 recent_git_signoffs($file);
215 }
216
217}
218
219if ($email_git_penguin_chiefs) {
220 foreach my $chief (@penguin_chief) {
221 if ($chief =~ m/^(.*):(.*)/) {
222 my $chief_name = $1;
223 my $chief_addr = $2;
224 if ($email_usename) {
225 push(@email_to, format_email($chief_name, $chief_addr));
226 } else {
227 push(@email_to, $chief_addr);
228 }
229 }
230 }
231}
232
Joe Perches290603c12009-06-16 15:33:58 -0700233if ($email || $email_list) {
234 my @to = ();
235 if ($email) {
236 @to = (@to, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700237 }
Joe Perches290603c12009-06-16 15:33:58 -0700238 if ($email_list) {
239 if (@list_to == 0) {
240 push(@list_to, "linux-kernel\@vger.kernel.org");
241 @to = (@to, @list_to);
242 }
243 }
244 output(uniq(@to));
Joe Perchescb7301c2009-04-07 20:40:12 -0700245}
246
247if ($scm) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700248 @scm = sort_and_uniq(@scm);
Joe Perchescb7301c2009-04-07 20:40:12 -0700249 output(@scm);
250}
251
252if ($status) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700253 @status = sort_and_uniq(@status);
Joe Perchescb7301c2009-04-07 20:40:12 -0700254 output(@status);
255}
256
257if ($subsystem) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700258 @subsystem = sort_and_uniq(@subsystem);
Joe Perchescb7301c2009-04-07 20:40:12 -0700259 output(@subsystem);
260}
261
262if ($web) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700263 @web = sort_and_uniq(@web);
Joe Perchescb7301c2009-04-07 20:40:12 -0700264 output(@web);
265}
266
267exit($exit);
268
269sub file_match_pattern {
270 my ($file, $pattern) = @_;
271 if (substr($pattern, -1) eq "/") {
272 if ($file =~ m@^$pattern@) {
273 return 1;
274 }
275 } else {
276 if ($file =~ m@^$pattern@) {
277 my $s1 = ($file =~ tr@/@@);
278 my $s2 = ($pattern =~ tr@/@@);
279 if ($s1 == $s2) {
280 return 1;
281 }
282 }
283 }
284 return 0;
285}
286
287sub usage {
288 print <<EOT;
289usage: $P [options] patchfile
290 $P [options] -f file
291version: $V
292
293MAINTAINER field selection options:
294 --email => print email address(es) if any
295 --git => include recent git \*-by: signers
296 --git-chief-penguins => include ${penguin_chiefs}
297 --git-min-signatures => number of signatures required (default: 1)
298 --git-max-maintainers => maximum maintainers to add (default: 5)
299 --git-since => git history to use (default: 1-year-ago)
300 --m => include maintainer(s) if any
301 --n => include name 'Full Name <addr\@domain.tld>'
302 --l => include list(s) if any
303 --s => include subscriber only list(s) if any
304 --scm => print SCM tree(s) if any
305 --status => print status if any
306 --subsystem => print subsystem name if any
307 --web => print website(s) if any
308
309Output type options:
310 --separator [, ] => separator for multiple entries on 1 line
311 --multiline => print 1 entry per line
312
313Default options:
Joe Perches290603c12009-06-16 15:33:58 -0700314 [--email --git --m --n --l --multiline]
Joe Perchescb7301c2009-04-07 20:40:12 -0700315
316Other options:
317 --version -> show version
318 --help => show this help information
319
320EOT
321}
322
323sub top_of_kernel_tree {
324 my ($lk_path) = @_;
325
326 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
327 $lk_path .= "/";
328 }
329 if ( (-f "${lk_path}COPYING")
330 && (-f "${lk_path}CREDITS")
331 && (-f "${lk_path}Kbuild")
332 && (-f "${lk_path}MAINTAINERS")
333 && (-f "${lk_path}Makefile")
334 && (-f "${lk_path}README")
335 && (-d "${lk_path}Documentation")
336 && (-d "${lk_path}arch")
337 && (-d "${lk_path}include")
338 && (-d "${lk_path}drivers")
339 && (-d "${lk_path}fs")
340 && (-d "${lk_path}init")
341 && (-d "${lk_path}ipc")
342 && (-d "${lk_path}kernel")
343 && (-d "${lk_path}lib")
344 && (-d "${lk_path}scripts")) {
345 return 1;
346 }
347 return 0;
348}
349
350sub format_email {
351 my ($name, $email) = @_;
352
353 $name =~ s/^\s+|\s+$//g;
354 $email =~ s/^\s+|\s+$//g;
355
356 my $formatted_email = "";
357
358 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
359 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
360 $formatted_email = "\"${name}\"\ \<${email}\>";
361 } else {
362 $formatted_email = "${name} \<${email}\>";
363 }
364 return $formatted_email;
365}
366
367sub add_categories {
368 my ($index) = @_;
369
370 $index = $index - 1;
371 while ($index >= 0) {
372 my $tv = $typevalue[$index];
Joe Perches290603c12009-06-16 15:33:58 -0700373 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700374 my $ptype = $1;
375 my $pvalue = $2;
376 if ($ptype eq "L") {
Joe Perches290603c12009-06-16 15:33:58 -0700377 my $list_address = $pvalue;
378 my $list_additional = "";
379 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
380 $list_address = $1;
381 $list_additional = $2;
382 }
Joe Perchesbdf7c682009-06-16 15:33:59 -0700383 if ($list_additional =~ m/subscribers-only/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700384 if ($email_subscriber_list) {
Joe Perches290603c12009-06-16 15:33:58 -0700385 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700386 }
387 } else {
388 if ($email_list) {
Joe Perches290603c12009-06-16 15:33:58 -0700389 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700390 }
391 }
392 } elsif ($ptype eq "M") {
393 if ($email_maintainer) {
394 if ($index >= 0) {
395 my $tv = $typevalue[$index - 1];
Joe Perches290603c12009-06-16 15:33:58 -0700396 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700397 if ($1 eq "P" && $email_usename) {
398 push(@email_to, format_email($2, $pvalue));
399 } else {
400 push(@email_to, $pvalue);
401 }
402 }
403 } else {
404 push(@email_to, $pvalue);
405 }
406 }
407 } elsif ($ptype eq "T") {
408 push(@scm, $pvalue);
409 } elsif ($ptype eq "W") {
410 push(@web, $pvalue);
411 } elsif ($ptype eq "S") {
412 push(@status, $pvalue);
413 }
414
415 $index--;
416 } else {
417 push(@subsystem,$tv);
418 $index = -1;
419 }
420 }
421}
422
423sub which {
424 my ($bin) = @_;
425
426 foreach my $path (split /:/, $ENV{PATH}) {
427 if (-e "$path/$bin") {
428 return "$path/$bin";
429 }
430 }
431
432 return "";
433}
434
435sub recent_git_signoffs {
436 my ($file) = @_;
437
438 my $sign_offs = "";
439 my $cmd = "";
440 my $output = "";
441 my $count = 0;
442 my @lines = ();
443
444 if (which("git") eq "") {
445 die("$P: git not found. Add --nogit to options?\n");
446 }
447
448 $cmd = "git log --since=${email_git_since} -- ${file}";
Joe Perches4a7fdb52009-04-10 12:28:57 -0700449 $cmd .= " | grep -Pi \"^[-_ a-z]+by:.*\\\@\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700450 if (!$email_git_penguin_chiefs) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700451 $cmd .= " | grep -Pv \"${penguin_chiefs}\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700452 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700453 $cmd .= " | cut -f2- -d\":\"";
454 $cmd .= " | sed -e \"s/^\\s+//g\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700455 $cmd .= " | sort | uniq -c | sort -rn";
456
457 $output = `${cmd}`;
458 $output =~ s/^\s*//gm;
459
460 @lines = split("\n", $output);
461 foreach my $line (@lines) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700462 if ($line =~ m/([0-9]+)\s+(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700463 my $sign_offs = $1;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700464 $line = $2;
Joe Perchescb7301c2009-04-07 20:40:12 -0700465 $count++;
466 if ($sign_offs < $email_git_min_signatures ||
467 $count > $email_git_max_maintainers) {
468 last;
469 }
470 } else {
471 die("$P: Unexpected git output: ${line}\n");
472 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700473 if ($line =~ m/(.+)<(.+)>/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700474 my $git_name = $1;
475 my $git_addr = $2;
476 $git_name =~ tr/^\"//;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700477 $git_name =~ tr/^\\s*//;
Joe Perchescb7301c2009-04-07 20:40:12 -0700478 $git_name =~ tr/\"$//;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700479 $git_name =~ tr/\\s*$//;
Joe Perchescb7301c2009-04-07 20:40:12 -0700480 if ($email_usename) {
481 push(@email_to, format_email($git_name, $git_addr));
482 } else {
483 push(@email_to, $git_addr);
484 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700485 } elsif ($line =~ m/<(.+)>/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700486 my $git_addr = $1;
487 push(@email_to, $git_addr);
488 } else {
489 push(@email_to, $line);
490 }
491 }
492 return $output;
493}
494
495sub uniq {
496 my @parms = @_;
497
498 my %saw;
499 @parms = grep(!$saw{$_}++, @parms);
500 return @parms;
501}
502
503sub sort_and_uniq {
504 my @parms = @_;
505
506 my %saw;
507 @parms = sort @parms;
508 @parms = grep(!$saw{$_}++, @parms);
509 return @parms;
510}
511
512sub output {
513 my @parms = @_;
514
515 if ($output_multiline) {
516 foreach my $line (@parms) {
517 print("${line}\n");
518 }
519 } else {
520 print(join($output_separator, @parms));
521 print("\n");
522 }
523}