scripts/get_maintainer.pl: support both "P:/M:" and integrated "M:" lines
[pandora-kernel.git] / scripts / get_maintainer.pl
1 #!/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
13 use strict;
14
15 my $P = $0;
16 my $V = '0.16';
17
18 use Getopt::Long qw(:config no_auto_abbrev);
19
20 my $lk_path = "./";
21 my $email = 1;
22 my $email_usename = 1;
23 my $email_maintainer = 1;
24 my $email_list = 1;
25 my $email_subscriber_list = 0;
26 my $email_git = 1;
27 my $email_git_penguin_chiefs = 0;
28 my $email_git_min_signatures = 1;
29 my $email_git_max_maintainers = 5;
30 my $email_git_since = "1-year-ago";
31 my $output_multiline = 1;
32 my $output_separator = ", ";
33 my $scm = 0;
34 my $web = 0;
35 my $subsystem = 0;
36 my $status = 0;
37 my $from_filename = 0;
38 my $version = 0;
39 my $help = 0;
40
41 my $exit = 0;
42
43 my @penguin_chief = ();
44 push(@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
48 my @penguin_chief_names = ();
49 foreach 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 }
56 my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
57
58 # rfc822 email address - preloaded methods go here.
59 my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
60 my $rfc822_char = '[\\000-\\177]';
61
62 if (!GetOptions(
63                 'email!' => \$email,
64                 'git!' => \$email_git,
65                 'git-chief-penguins!' => \$email_git_penguin_chiefs,
66                 'git-min-signatures=i' => \$email_git_min_signatures,
67                 'git-max-maintainers=i' => \$email_git_max_maintainers,
68                 'git-since=s' => \$email_git_since,
69                 'm!' => \$email_maintainer,
70                 'n!' => \$email_usename,
71                 'l!' => \$email_list,
72                 's!' => \$email_subscriber_list,
73                 'multiline!' => \$output_multiline,
74                 'separator=s' => \$output_separator,
75                 'subsystem!' => \$subsystem,
76                 'status!' => \$status,
77                 'scm!' => \$scm,
78                 'web!' => \$web,
79                 'f|file' => \$from_filename,
80                 'v|version' => \$version,
81                 'h|help' => \$help,
82                 )) {
83     usage();
84     die "$P: invalid argument\n";
85 }
86
87 if ($help != 0) {
88     usage();
89     exit 0;
90 }
91
92 if ($version != 0) {
93     print("${P} ${V}\n");
94     exit 0;
95 }
96
97 if ($#ARGV < 0) {
98     usage();
99     die "$P: argument missing: patchfile or -f file please\n";
100 }
101
102 my $selections = $email + $scm + $status + $subsystem + $web;
103 if ($selections == 0) {
104     usage();
105     die "$P:  Missing required option: email, scm, status, subsystem or web\n";
106 }
107
108 if ($email && ($email_maintainer + $email_list + $email_subscriber_list
109                + $email_git + $email_git_penguin_chiefs) == 0) {
110     usage();
111     die "$P: Please select at least 1 email option\n";
112 }
113
114 if (!top_of_kernel_tree($lk_path)) {
115     die "$P: The current directory does not appear to be "
116         . "a linux kernel source tree.\n";
117 }
118
119 ## Read MAINTAINERS for type/value pairs
120
121 my @typevalue = ();
122 open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
123 while (<MAINT>) {
124     my $line = $_;
125
126     if ($line =~ m/^(\C):\s*(.*)/) {
127         my $type = $1;
128         my $value = $2;
129
130         ##Filename pattern matching
131         if ($type eq "F" || $type eq "X") {
132             $value =~ s@\.@\\\.@g;       ##Convert . to \.
133             $value =~ s/\*/\.\*/g;       ##Convert * to .*
134             $value =~ s/\?/\./g;         ##Convert ? to .
135         }
136         push(@typevalue, "$type:$value");
137     } elsif (!/^(\s)*$/) {
138         $line =~ s/\n$//g;
139         push(@typevalue, $line);
140     }
141 }
142 close(MAINT);
143
144 ## use the filenames on the command line or find the filenames in the patchfiles
145
146 my @files = ();
147
148 foreach my $file (@ARGV) {
149     next if ((-d $file));
150     if (!(-f $file)) {
151         die "$P: file '${file}' not found\n";
152     }
153     if ($from_filename) {
154         push(@files, $file);
155     } else {
156         my $file_cnt = @files;
157         open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
158         while (<PATCH>) {
159             if (m/^\+\+\+\s+(\S+)/) {
160                 my $filename = $1;
161                 $filename =~ s@^[^/]*/@@;
162                 $filename =~ s@\n@@;
163                 push(@files, $filename);
164             }
165         }
166         close(PATCH);
167         if ($file_cnt == @files) {
168             die "$P: file '${file}' doesn't appear to be a patch.  "
169                 . "Add -f to options?\n";
170         }
171         @files = sort_and_uniq(@files);
172     }
173 }
174
175 my @email_to = ();
176 my @list_to = ();
177 my @scm = ();
178 my @web = ();
179 my @subsystem = ();
180 my @status = ();
181
182 # Find responsible parties
183
184 foreach my $file (@files) {
185
186 #Do not match excluded file patterns
187
188     my $exclude = 0;
189     foreach my $line (@typevalue) {
190         if ($line =~ m/^(\C):\s*(.*)/) {
191             my $type = $1;
192             my $value = $2;
193             if ($type eq 'X') {
194                 if (file_match_pattern($file, $value)) {
195                     $exclude = 1;
196                 }
197             }
198         }
199     }
200
201     if (!$exclude) {
202         my $tvi = 0;
203         foreach my $line (@typevalue) {
204             if ($line =~ m/^(\C):\s*(.*)/) {
205                 my $type = $1;
206                 my $value = $2;
207                 if ($type eq 'F') {
208                     if (file_match_pattern($file, $value)) {
209                         add_categories($tvi);
210                     }
211                 }
212             }
213             $tvi++;
214         }
215     }
216
217     if ($email && $email_git) {
218         recent_git_signoffs($file);
219     }
220
221 }
222
223 if ($email) {
224     foreach my $chief (@penguin_chief) {
225         if ($chief =~ m/^(.*):(.*)/) {
226             my $email_address;
227             if ($email_usename) {
228                 $email_address = format_email($1, $2);
229             } else {
230                 $email_address = $2;
231             }
232             if ($email_git_penguin_chiefs) {
233                 push(@email_to, $email_address);
234             } else {
235                 @email_to = grep(!/${email_address}/, @email_to);
236             }
237         }
238     }
239 }
240
241 if ($email || $email_list) {
242     my @to = ();
243     if ($email) {
244         @to = (@to, @email_to);
245     }
246     if ($email_list) {
247         @to = (@to, @list_to);
248     }
249     output(uniq(@to));
250 }
251
252 if ($scm) {
253     @scm = sort_and_uniq(@scm);
254     output(@scm);
255 }
256
257 if ($status) {
258     @status = sort_and_uniq(@status);
259     output(@status);
260 }
261
262 if ($subsystem) {
263     @subsystem = sort_and_uniq(@subsystem);
264     output(@subsystem);
265 }
266
267 if ($web) {
268     @web = sort_and_uniq(@web);
269     output(@web);
270 }
271
272 exit($exit);
273
274 sub file_match_pattern {
275     my ($file, $pattern) = @_;
276     if (substr($pattern, -1) eq "/") {
277         if ($file =~ m@^$pattern@) {
278             return 1;
279         }
280     } else {
281         if ($file =~ m@^$pattern@) {
282             my $s1 = ($file =~ tr@/@@);
283             my $s2 = ($pattern =~ tr@/@@);
284             if ($s1 == $s2) {
285                 return 1;
286             }
287         }
288     }
289     return 0;
290 }
291
292 sub usage {
293     print <<EOT;
294 usage: $P [options] patchfile
295        $P [options] -f file
296 version: $V
297
298 MAINTAINER field selection options:
299   --email => print email address(es) if any
300     --git => include recent git \*-by: signers
301     --git-chief-penguins => include ${penguin_chiefs}
302     --git-min-signatures => number of signatures required (default: 1)
303     --git-max-maintainers => maximum maintainers to add (default: 5)
304     --git-since => git history to use (default: 1-year-ago)
305     --m => include maintainer(s) if any
306     --n => include name 'Full Name <addr\@domain.tld>'
307     --l => include list(s) if any
308     --s => include subscriber only list(s) if any
309   --scm => print SCM tree(s) if any
310   --status => print status if any
311   --subsystem => print subsystem name if any
312   --web => print website(s) if any
313
314 Output type options:
315   --separator [, ] => separator for multiple entries on 1 line
316   --multiline => print 1 entry per line
317
318 Default options:
319   [--email --git --m --n --l --multiline]
320
321 Other options:
322   --version => show version
323   --help => show this help information
324
325 EOT
326 }
327
328 sub top_of_kernel_tree {
329         my ($lk_path) = @_;
330
331         if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
332             $lk_path .= "/";
333         }
334         if (   (-f "${lk_path}COPYING")
335             && (-f "${lk_path}CREDITS")
336             && (-f "${lk_path}Kbuild")
337             && (-f "${lk_path}MAINTAINERS")
338             && (-f "${lk_path}Makefile")
339             && (-f "${lk_path}README")
340             && (-d "${lk_path}Documentation")
341             && (-d "${lk_path}arch")
342             && (-d "${lk_path}include")
343             && (-d "${lk_path}drivers")
344             && (-d "${lk_path}fs")
345             && (-d "${lk_path}init")
346             && (-d "${lk_path}ipc")
347             && (-d "${lk_path}kernel")
348             && (-d "${lk_path}lib")
349             && (-d "${lk_path}scripts")) {
350                 return 1;
351         }
352         return 0;
353 }
354
355 sub format_email {
356     my ($name, $email) = @_;
357
358     $name =~ s/^\s+|\s+$//g;
359     $name =~ s/^\"|\"$//g;
360     $email =~ s/^\s+|\s+$//g;
361
362     my $formatted_email = "";
363
364     if ($name =~ /[^a-z0-9 \.\-]/i) {    ##has "must quote" chars
365         $name =~ s/(?<!\\)"/\\"/g;       ##escape quotes
366         $formatted_email = "\"${name}\"\ \<${email}\>";
367     } else {
368         $formatted_email = "${name} \<${email}\>";
369     }
370     return $formatted_email;
371 }
372
373 sub add_categories {
374     my ($index) = @_;
375
376     $index = $index - 1;
377     while ($index >= 0) {
378         my $tv = $typevalue[$index];
379         if ($tv =~ m/^(\C):\s*(.*)/) {
380             my $ptype = $1;
381             my $pvalue = $2;
382             if ($ptype eq "L") {
383                 my $list_address = $pvalue;
384                 my $list_additional = "";
385                 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
386                     $list_address = $1;
387                     $list_additional = $2;
388                 }
389                 if ($list_additional =~ m/subscribers-only/) {
390                     if ($email_subscriber_list) {
391                         push(@list_to, $list_address);
392                     }
393                 } else {
394                     if ($email_list) {
395                         push(@list_to, $list_address);
396                     }
397                 }
398             } elsif ($ptype eq "M") {
399                 my $p_used = 0;
400                 if ($index >= 0) {
401                     my $tv = $typevalue[$index - 1];
402                     if ($tv =~ m/^(\C):\s*(.*)/) {
403                         if ($1 eq "P") {
404                             if ($email_usename) {
405                                 push_email_address(format_email($2, $pvalue));
406                                 $p_used = 1;
407                             }
408                         }
409                     }
410                 }
411                 if (!$p_used) {
412                     push_email_addresses($pvalue);
413                 }
414             } elsif ($ptype eq "T") {
415                 push(@scm, $pvalue);
416             } elsif ($ptype eq "W") {
417                 push(@web, $pvalue);
418             } elsif ($ptype eq "S") {
419                 push(@status, $pvalue);
420             }
421
422             $index--;
423         } else {
424             push(@subsystem,$tv);
425             $index = -1;
426         }
427     }
428 }
429
430 sub push_email_address {
431     my ($email_address) = @_;
432
433     my $email_name = "";
434     if ($email_address =~ m/([^<]+)<(.*\@.*)>$/) {
435         $email_name = $1;
436         $email_address = $2;
437     }
438
439     if ($email_usename && $email_name) {
440         push(@email_to, format_email($email_name, $email_address));
441     } else {
442         push(@email_to, $email_address);
443     }
444 }
445
446 sub push_email_addresses {
447     my ($address) = @_;
448
449     my @address_list = ();
450
451     if (rfc822_valid($address)) {
452         push_email_address($address);
453     } elsif (@address_list = rfc822_validlist($address)) {
454         my $array_count = shift(@address_list);
455         while (my $entry = shift(@address_list)) {
456             push_email_address($entry);
457         }
458     } else {
459         warn("Invalid MAINTAINERS address: '" . $address . "'\n");
460     }
461 }
462
463 sub which {
464     my ($bin) = @_;
465
466     foreach my $path (split(/:/, $ENV{PATH})) {
467         if (-e "$path/$bin") {
468             return "$path/$bin";
469         }
470     }
471
472     return "";
473 }
474
475 sub recent_git_signoffs {
476     my ($file) = @_;
477
478     my $sign_offs = "";
479     my $cmd = "";
480     my $output = "";
481     my $count = 0;
482     my @lines = ();
483
484     if (which("git") eq "") {
485         warn("$P: git not found.  Add --nogit to options?\n");
486         return;
487     }
488     if (!(-d ".git")) {
489         warn("$P: .git directory not found.  Use a git repository for better results.\n");
490         warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
491         return;
492     }
493
494     $cmd = "git log --since=${email_git_since} -- ${file}";
495     $cmd .= " | grep -Ei \"^[-_         a-z]+by:.*\\\@.*\$\"";
496     if (!$email_git_penguin_chiefs) {
497         $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
498     }
499     $cmd .= " | cut -f2- -d\":\"";
500     $cmd .= " | sort | uniq -c | sort -rn";
501
502     $output = `${cmd}`;
503     $output =~ s/^\s*//gm;
504
505     @lines = split("\n", $output);
506     foreach my $line (@lines) {
507         if ($line =~ m/([0-9]+)\s+(.*)/) {
508             my $sign_offs = $1;
509             $line = $2;
510             $count++;
511             if ($sign_offs < $email_git_min_signatures ||
512                 $count > $email_git_max_maintainers) {
513                 last;
514             }
515         } else {
516             die("$P: Unexpected git output: ${line}\n");
517         }
518         if ($line =~ m/(.+)<(.+)>/) {
519             my $git_name = $1;
520             my $git_addr = $2;
521             if ($email_usename) {
522                 push(@email_to, format_email($git_name, $git_addr));
523             } else {
524                 push(@email_to, $git_addr);
525             }
526         } elsif ($line =~ m/<(.+)>/) {
527             my $git_addr = $1;
528             push(@email_to, $git_addr);
529         } else {
530             push(@email_to, $line);
531         }
532     }
533 }
534
535 sub uniq {
536     my @parms = @_;
537
538     my %saw;
539     @parms = grep(!$saw{$_}++, @parms);
540     return @parms;
541 }
542
543 sub sort_and_uniq {
544     my @parms = @_;
545
546     my %saw;
547     @parms = sort @parms;
548     @parms = grep(!$saw{$_}++, @parms);
549     return @parms;
550 }
551
552 sub output {
553     my @parms = @_;
554
555     if ($output_multiline) {
556         foreach my $line (@parms) {
557             print("${line}\n");
558         }
559     } else {
560         print(join($output_separator, @parms));
561         print("\n");
562     }
563 }
564
565 my $rfc822re;
566
567 sub make_rfc822re {
568 #   Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
569 #   comment.  We must allow for rfc822_lwsp (or comments) after each of these.
570 #   This regexp will only work on addresses which have had comments stripped
571 #   and replaced with rfc822_lwsp.
572
573     my $specials = '()<>@,;:\\\\".\\[\\]';
574     my $controls = '\\000-\\037\\177';
575
576     my $dtext = "[^\\[\\]\\r\\\\]";
577     my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
578
579     my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
580
581 #   Use zero-width assertion to spot the limit of an atom.  A simple
582 #   $rfc822_lwsp* causes the regexp engine to hang occasionally.
583     my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
584     my $word = "(?:$atom|$quoted_string)";
585     my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
586
587     my $sub_domain = "(?:$atom|$domain_literal)";
588     my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
589
590     my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
591
592     my $phrase = "$word*";
593     my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
594     my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
595     my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
596
597     my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
598     my $address = "(?:$mailbox|$group)";
599
600     return "$rfc822_lwsp*$address";
601 }
602
603 sub rfc822_strip_comments {
604     my $s = shift;
605 #   Recursively remove comments, and replace with a single space.  The simpler
606 #   regexps in the Email Addressing FAQ are imperfect - they will miss escaped
607 #   chars in atoms, for example.
608
609     while ($s =~ s/^((?:[^"\\]|\\.)*
610                     (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
611                     \((?:[^()\\]|\\.)*\)/$1 /osx) {}
612     return $s;
613 }
614
615 #   valid: returns true if the parameter is an RFC822 valid address
616 #
617 sub rfc822_valid ($) {
618     my $s = rfc822_strip_comments(shift);
619
620     if (!$rfc822re) {
621         $rfc822re = make_rfc822re();
622     }
623
624     return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
625 }
626
627 #   validlist: In scalar context, returns true if the parameter is an RFC822
628 #              valid list of addresses.
629 #
630 #              In list context, returns an empty list on failure (an invalid
631 #              address was found); otherwise a list whose first element is the
632 #              number of addresses found and whose remaining elements are the
633 #              addresses.  This is needed to disambiguate failure (invalid)
634 #              from success with no addresses found, because an empty string is
635 #              a valid list.
636
637 sub rfc822_validlist ($) {
638     my $s = rfc822_strip_comments(shift);
639
640     if (!$rfc822re) {
641         $rfc822re = make_rfc822re();
642     }
643     # * null list items are valid according to the RFC
644     # * the '1' business is to aid in distinguishing failure from no results
645
646     my @r;
647     if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
648         $s =~ m/^$rfc822_char*$/) {
649         while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
650             push @r, $1;
651         }
652         return wantarray ? (scalar(@r), @r) : 1;
653     }
654     else {
655         return wantarray ? () : 0;
656     }
657 }