2 # (c) 2007, Joe Perches <joe@perches.com>
3 # created from checkpatch.pl
5 # Print selected MAINTAINERS information for
6 # the files modified in a patch or for a file
8 # usage: perl scripts/get_maintainers.pl [OPTIONS] <patch>
9 # perl scripts/get_maintainers.pl [OPTIONS] -f <file>
11 # Licensed under the terms of the GNU GPL License version 2
18 use Getopt::Long qw(:config no_auto_abbrev);
22 my $email_usename = 1;
23 my $email_maintainer = 1;
25 my $email_subscriber_list = 0;
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 = ", ";
37 my $from_filename = 0;
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");
48 my @penguin_chief_names = ();
49 foreach my $chief (@penguin_chief) {
50 if ($chief =~ m/^(.*):(.*)/) {
53 push(@penguin_chief_names, $chief_name);
56 my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
58 # rfc822 email address - preloaded methods go here.
59 my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
60 my $rfc822_char = '[\\000-\\377]';
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,
72 's!' => \$email_subscriber_list,
73 'multiline!' => \$output_multiline,
74 'separator=s' => \$output_separator,
75 'subsystem!' => \$subsystem,
76 'status!' => \$status,
79 'f|file' => \$from_filename,
80 'v|version' => \$version,
84 die "$P: invalid argument\n";
99 die "$P: argument missing: patchfile or -f file please\n";
102 my $selections = $email + $scm + $status + $subsystem + $web;
103 if ($selections == 0) {
105 die "$P: Missing required option: email, scm, status, subsystem or web\n";
108 if ($email && ($email_maintainer + $email_list + $email_subscriber_list
109 + $email_git + $email_git_penguin_chiefs) == 0) {
111 die "$P: Please select at least 1 email option\n";
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";
119 ## Read MAINTAINERS for type/value pairs
122 open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
126 if ($line =~ m/^(\C):\s*(.*)/) {
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 ##if pattern is a directory and it lacks a trailing slash, add one
137 $value =~ s@([^/])$@$1/@;
140 push(@typevalue, "$type:$value");
141 } elsif (!/^(\s)*$/) {
143 push(@typevalue, $line);
148 ## use the filenames on the command line or find the filenames in the patchfiles
152 foreach my $file (@ARGV) {
153 ##if $file is a directory and it lacks a trailing slash, add one
155 $file =~ s@([^/])$@$1/@;
156 } elsif (!(-f $file)) {
157 die "$P: file '${file}' not found\n";
159 if ($from_filename) {
162 my $file_cnt = @files;
163 open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
165 if (m/^\+\+\+\s+(\S+)/) {
167 $filename =~ s@^[^/]*/@@;
169 push(@files, $filename);
173 if ($file_cnt == @files) {
174 warn "$P: file '${file}' doesn't appear to be a patch. "
175 . "Add -f to options?\n";
177 @files = sort_and_uniq(@files);
188 # Find responsible parties
190 foreach my $file (@files) {
192 #Do not match excluded file patterns
195 foreach my $line (@typevalue) {
196 if ($line =~ m/^(\C):\s*(.*)/) {
200 if (file_match_pattern($file, $value)) {
209 foreach my $line (@typevalue) {
210 if ($line =~ m/^(\C):\s*(.*)/) {
214 if (file_match_pattern($file, $value)) {
215 add_categories($tvi);
223 if ($email && $email_git) {
224 recent_git_signoffs($file);
230 foreach my $chief (@penguin_chief) {
231 if ($chief =~ m/^(.*):(.*)/) {
233 if ($email_usename) {
234 $email_address = format_email($1, $2);
238 if ($email_git_penguin_chiefs) {
239 push(@email_to, $email_address);
241 @email_to = grep(!/${email_address}/, @email_to);
247 if ($email || $email_list) {
250 @to = (@to, @email_to);
253 @to = (@to, @list_to);
259 @scm = sort_and_uniq(@scm);
264 @status = sort_and_uniq(@status);
269 @subsystem = sort_and_uniq(@subsystem);
274 @web = sort_and_uniq(@web);
280 sub file_match_pattern {
281 my ($file, $pattern) = @_;
282 if (substr($pattern, -1) eq "/") {
283 if ($file =~ m@^$pattern@) {
287 if ($file =~ m@^$pattern@) {
288 my $s1 = ($file =~ tr@/@@);
289 my $s2 = ($pattern =~ tr@/@@);
300 usage: $P [options] patchfile
301 $P [options] -f file|directory
304 MAINTAINER field selection options:
305 --email => print email address(es) if any
306 --git => include recent git \*-by: signers
307 --git-chief-penguins => include ${penguin_chiefs}
308 --git-min-signatures => number of signatures required (default: 1)
309 --git-max-maintainers => maximum maintainers to add (default: 5)
310 --git-since => git history to use (default: 1-year-ago)
311 --m => include maintainer(s) if any
312 --n => include name 'Full Name <addr\@domain.tld>'
313 --l => include list(s) if any
314 --s => include subscriber only list(s) if any
315 --scm => print SCM tree(s) if any
316 --status => print status if any
317 --subsystem => print subsystem name if any
318 --web => print website(s) if any
321 --separator [, ] => separator for multiple entries on 1 line
322 --multiline => print 1 entry per line
325 [--email --git --m --n --l --multiline]
328 --version => show version
329 --help => show this help information
332 Using "-f directory" may give unexpected results:
334 Used with "--git", git signators for _all_ files in and below
335 directory are examined as git recurses directories.
336 Any specified X: (exclude) pattern matches are _not_ ignored.
337 Used with "--nogit", directory is used as a pattern match,
338 no individual file within the directory or subdirectory
343 sub top_of_kernel_tree {
346 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
349 if ( (-f "${lk_path}COPYING")
350 && (-f "${lk_path}CREDITS")
351 && (-f "${lk_path}Kbuild")
352 && (-f "${lk_path}MAINTAINERS")
353 && (-f "${lk_path}Makefile")
354 && (-f "${lk_path}README")
355 && (-d "${lk_path}Documentation")
356 && (-d "${lk_path}arch")
357 && (-d "${lk_path}include")
358 && (-d "${lk_path}drivers")
359 && (-d "${lk_path}fs")
360 && (-d "${lk_path}init")
361 && (-d "${lk_path}ipc")
362 && (-d "${lk_path}kernel")
363 && (-d "${lk_path}lib")
364 && (-d "${lk_path}scripts")) {
371 my ($name, $email) = @_;
373 $name =~ s/^\s+|\s+$//g;
374 $name =~ s/^\"|\"$//g;
375 $email =~ s/^\s+|\s+$//g;
377 my $formatted_email = "";
379 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
380 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
381 $formatted_email = "\"${name}\"\ \<${email}\>";
383 $formatted_email = "${name} \<${email}\>";
385 return $formatted_email;
392 while ($index >= 0) {
393 my $tv = $typevalue[$index];
394 if ($tv =~ m/^(\C):\s*(.*)/) {
398 my $list_address = $pvalue;
399 my $list_additional = "";
400 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
402 $list_additional = $2;
404 if ($list_additional =~ m/subscribers-only/) {
405 if ($email_subscriber_list) {
406 push(@list_to, $list_address);
410 push(@list_to, $list_address);
413 } elsif ($ptype eq "M") {
416 my $tv = $typevalue[$index - 1];
417 if ($tv =~ m/^(\C):\s*(.*)/) {
419 if ($email_usename) {
420 push_email_address(format_email($2, $pvalue));
427 push_email_addresses($pvalue);
429 } elsif ($ptype eq "T") {
431 } elsif ($ptype eq "W") {
433 } elsif ($ptype eq "S") {
434 push(@status, $pvalue);
439 push(@subsystem,$tv);
445 sub push_email_address {
446 my ($email_address) = @_;
449 if ($email_address =~ m/([^<]+)<(.*\@.*)>$/) {
454 if ($email_maintainer) {
455 if ($email_usename && $email_name) {
456 push(@email_to, format_email($email_name, $email_address));
458 push(@email_to, $email_address);
463 sub push_email_addresses {
466 my @address_list = ();
468 if (rfc822_valid($address)) {
469 push_email_address($address);
470 } elsif (@address_list = rfc822_validlist($address)) {
471 my $array_count = shift(@address_list);
472 while (my $entry = shift(@address_list)) {
473 push_email_address($entry);
476 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
483 foreach my $path (split(/:/, $ENV{PATH})) {
484 if (-e "$path/$bin") {
492 sub recent_git_signoffs {
501 if (which("git") eq "") {
502 warn("$P: git not found. Add --nogit to options?\n");
506 warn("$P: .git directory not found. Use a git repository for better results.\n");
507 warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
511 $cmd = "git log --since=${email_git_since} -- ${file}";
512 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
513 if (!$email_git_penguin_chiefs) {
514 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
516 $cmd .= " | cut -f2- -d\":\"";
517 $cmd .= " | sort | uniq -c | sort -rn";
520 $output =~ s/^\s*//gm;
522 @lines = split("\n", $output);
523 foreach my $line (@lines) {
524 if ($line =~ m/([0-9]+)\s+(.*)/) {
528 if ($sign_offs < $email_git_min_signatures ||
529 $count > $email_git_max_maintainers) {
533 die("$P: Unexpected git output: ${line}\n");
535 if ($line =~ m/(.+)<(.+)>/) {
538 if ($email_usename) {
539 push(@email_to, format_email($git_name, $git_addr));
541 push(@email_to, $git_addr);
543 } elsif ($line =~ m/<(.+)>/) {
545 push(@email_to, $git_addr);
547 push(@email_to, $line);
556 @parms = grep(!$saw{$_}++, @parms);
564 @parms = sort @parms;
565 @parms = grep(!$saw{$_}++, @parms);
572 if ($output_multiline) {
573 foreach my $line (@parms) {
577 print(join($output_separator, @parms));
585 # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
586 # comment. We must allow for rfc822_lwsp (or comments) after each of these.
587 # This regexp will only work on addresses which have had comments stripped
588 # and replaced with rfc822_lwsp.
590 my $specials = '()<>@,;:\\\\".\\[\\]';
591 my $controls = '\\000-\\037\\177';
593 my $dtext = "[^\\[\\]\\r\\\\]";
594 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
596 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
598 # Use zero-width assertion to spot the limit of an atom. A simple
599 # $rfc822_lwsp* causes the regexp engine to hang occasionally.
600 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
601 my $word = "(?:$atom|$quoted_string)";
602 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
604 my $sub_domain = "(?:$atom|$domain_literal)";
605 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
607 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
609 my $phrase = "$word*";
610 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
611 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
612 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
614 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
615 my $address = "(?:$mailbox|$group)";
617 return "$rfc822_lwsp*$address";
620 sub rfc822_strip_comments {
622 # Recursively remove comments, and replace with a single space. The simpler
623 # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
624 # chars in atoms, for example.
626 while ($s =~ s/^((?:[^"\\]|\\.)*
627 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
628 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
632 # valid: returns true if the parameter is an RFC822 valid address
634 sub rfc822_valid ($) {
635 my $s = rfc822_strip_comments(shift);
638 $rfc822re = make_rfc822re();
641 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
644 # validlist: In scalar context, returns true if the parameter is an RFC822
645 # valid list of addresses.
647 # In list context, returns an empty list on failure (an invalid
648 # address was found); otherwise a list whose first element is the
649 # number of addresses found and whose remaining elements are the
650 # addresses. This is needed to disambiguate failure (invalid)
651 # from success with no addresses found, because an empty string is
654 sub rfc822_validlist ($) {
655 my $s = rfc822_strip_comments(shift);
658 $rfc822re = make_rfc822re();
660 # * null list items are valid according to the RFC
661 # * the '1' business is to aid in distinguishing failure from no results
664 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
665 $s =~ m/^$rfc822_char*$/) {
666 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
669 return wantarray ? (scalar(@r), @r) : 1;
672 return wantarray ? () : 0;