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_min_percent = 5;
31 my $email_git_since = "1-year-ago";
32 my $output_multiline = 1;
33 my $output_separator = ", ";
38 my $from_filename = 0;
44 my @penguin_chief = ();
45 push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
46 #Andrew wants in on most everything - 2009/01/14
47 #push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
49 my @penguin_chief_names = ();
50 foreach my $chief (@penguin_chief) {
51 if ($chief =~ m/^(.*):(.*)/) {
54 push(@penguin_chief_names, $chief_name);
57 my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
59 # rfc822 email address - preloaded methods go here.
60 my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
61 my $rfc822_char = '[\\000-\\377]';
65 'git!' => \
$email_git,
66 'git-chief-penguins!' => \
$email_git_penguin_chiefs,
67 'git-min-signatures=i' => \
$email_git_min_signatures,
68 'git-max-maintainers=i' => \
$email_git_max_maintainers,
69 'git-min-percent=i' => \
$email_git_min_percent,
70 'git-since=s' => \
$email_git_since,
71 'm!' => \
$email_maintainer,
72 'n!' => \
$email_usename,
74 's!' => \
$email_subscriber_list,
75 'multiline!' => \
$output_multiline,
76 'separator=s' => \
$output_separator,
77 'subsystem!' => \
$subsystem,
78 'status!' => \
$status,
81 'f|file' => \
$from_filename,
82 'v|version' => \
$version,
86 die "$P: invalid argument\n";
101 die "$P: argument missing: patchfile or -f file please\n";
104 my $selections = $email + $scm + $status + $subsystem + $web;
105 if ($selections == 0) {
107 die "$P: Missing required option: email, scm, status, subsystem or web\n";
110 if ($email && ($email_maintainer + $email_list + $email_subscriber_list
111 + $email_git + $email_git_penguin_chiefs) == 0) {
113 die "$P: Please select at least 1 email option\n";
116 if (!top_of_kernel_tree
($lk_path)) {
117 die "$P: The current directory does not appear to be "
118 . "a linux kernel source tree.\n";
121 ## Read MAINTAINERS for type/value pairs
124 open(MAINT
, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
128 if ($line =~ m/^(\C):\s*(.*)/) {
132 ##Filename pattern matching
133 if ($type eq "F" || $type eq "X") {
134 $value =~ s@\
.@
\\\
.@g; ##Convert . to \.
135 $value =~ s/\*/\.\*/g; ##Convert * to .*
136 $value =~ s/\?/\./g; ##Convert ? to .
137 ##if pattern is a directory and it lacks a trailing slash, add one
139 $value =~ s@
([^/])$@$1/@
;
142 push(@typevalue, "$type:$value");
143 } elsif (!/^(\s)*$/) {
145 push(@typevalue, $line);
150 ## use the filenames on the command line or find the filenames in the patchfiles
154 foreach my $file (@ARGV) {
155 ##if $file is a directory and it lacks a trailing slash, add one
157 $file =~ s@
([^/])$@$1/@
;
158 } elsif (!(-f
$file)) {
159 die "$P: file '${file}' not found\n";
161 if ($from_filename) {
164 my $file_cnt = @files;
165 open(PATCH
, "<$file") or die "$P: Can't open ${file}\n";
167 if (m/^\+\+\+\s+(\S+)/) {
169 $filename =~ s@
^[^/]*/@@
;
171 push(@files, $filename);
175 if ($file_cnt == @files) {
176 warn "$P: file '${file}' doesn't appear to be a patch. "
177 . "Add -f to options?\n";
179 @files = sort_and_uniq
(@files);
190 # Find responsible parties
192 foreach my $file (@files) {
194 #Do not match excluded file patterns
197 foreach my $line (@typevalue) {
198 if ($line =~ m/^(\C):\s*(.*)/) {
202 if (file_match_pattern
($file, $value)) {
211 foreach my $line (@typevalue) {
212 if ($line =~ m/^(\C):\s*(.*)/) {
216 if (file_match_pattern
($file, $value)) {
217 add_categories
($tvi);
225 if ($email && $email_git) {
226 recent_git_signoffs
($file);
232 foreach my $chief (@penguin_chief) {
233 if ($chief =~ m/^(.*):(.*)/) {
235 if ($email_usename) {
236 $email_address = format_email
($1, $2);
240 if ($email_git_penguin_chiefs) {
241 push(@email_to, $email_address);
243 @email_to = grep(!/${email_address}/, @email_to);
249 if ($email || $email_list) {
252 @to = (@to, @email_to);
255 @to = (@to, @list_to);
261 @scm = sort_and_uniq
(@scm);
266 @status = sort_and_uniq
(@status);
271 @subsystem = sort_and_uniq
(@subsystem);
276 @web = sort_and_uniq
(@web);
282 sub file_match_pattern
{
283 my ($file, $pattern) = @_;
284 if (substr($pattern, -1) eq "/") {
285 if ($file =~ m@
^$pattern@
) {
289 if ($file =~ m@
^$pattern@
) {
290 my $s1 = ($file =~ tr@
/@@
);
291 my $s2 = ($pattern =~ tr@
/@@
);
302 usage: $P [options] patchfile
303 $P [options] -f file|directory
306 MAINTAINER field selection options:
307 --email => print email address(es) if any
308 --git => include recent git \*-by: signers
309 --git-chief-penguins => include ${penguin_chiefs}
310 --git-min-signatures => number of signatures required (default: 1)
311 --git-max-maintainers => maximum maintainers to add (default: 5)
312 --git-min-percent => minimum percentage of commits required (default: 5)
313 --git-since => git history to use (default: 1-year-ago)
314 --m => include maintainer(s) if any
315 --n => include name 'Full Name <addr\@domain.tld>'
316 --l => include list(s) if any
317 --s => include subscriber only list(s) if any
318 --scm => print SCM tree(s) if any
319 --status => print status if any
320 --subsystem => print subsystem name if any
321 --web => print website(s) if any
324 --separator [, ] => separator for multiple entries on 1 line
325 --multiline => print 1 entry per line
328 [--email --git --m --n --l --multiline]
331 --version => show version
332 --help => show this help information
335 Using "-f directory" may give unexpected results:
337 Used with "--git", git signators for _all_ files in and below
338 directory are examined as git recurses directories.
339 Any specified X: (exclude) pattern matches are _not_ ignored.
340 Used with "--nogit", directory is used as a pattern match,
341 no individual file within the directory or subdirectory
346 sub top_of_kernel_tree
{
349 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
352 if ( (-f
"${lk_path}COPYING")
353 && (-f
"${lk_path}CREDITS")
354 && (-f
"${lk_path}Kbuild")
355 && (-f
"${lk_path}MAINTAINERS")
356 && (-f
"${lk_path}Makefile")
357 && (-f
"${lk_path}README")
358 && (-d
"${lk_path}Documentation")
359 && (-d
"${lk_path}arch")
360 && (-d
"${lk_path}include")
361 && (-d
"${lk_path}drivers")
362 && (-d
"${lk_path}fs")
363 && (-d
"${lk_path}init")
364 && (-d
"${lk_path}ipc")
365 && (-d
"${lk_path}kernel")
366 && (-d
"${lk_path}lib")
367 && (-d
"${lk_path}scripts")) {
374 my ($name, $email) = @_;
376 $name =~ s/^\s+|\s+$//g;
377 $name =~ s/^\"|\"$//g;
378 $email =~ s/^\s+|\s+$//g;
380 my $formatted_email = "";
382 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
383 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
384 $formatted_email = "\"${name}\"\ \<${email}\>";
386 $formatted_email = "${name} \<${email}\>";
388 return $formatted_email;
395 while ($index >= 0) {
396 my $tv = $typevalue[$index];
397 if ($tv =~ m/^(\C):\s*(.*)/) {
401 my $list_address = $pvalue;
402 my $list_additional = "";
403 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
405 $list_additional = $2;
407 if ($list_additional =~ m/subscribers-only/) {
408 if ($email_subscriber_list) {
409 push(@list_to, $list_address);
413 push(@list_to, $list_address);
416 } elsif ($ptype eq "M") {
419 my $tv = $typevalue[$index - 1];
420 if ($tv =~ m/^(\C):\s*(.*)/) {
422 if ($email_usename) {
423 push_email_address
(format_email
($2, $pvalue));
430 push_email_addresses
($pvalue);
432 } elsif ($ptype eq "T") {
434 } elsif ($ptype eq "W") {
436 } elsif ($ptype eq "S") {
437 push(@status, $pvalue);
442 push(@subsystem,$tv);
448 sub push_email_address
{
449 my ($email_address) = @_;
452 if ($email_address =~ m/([^<]+)<(.*\@.*)>$/) {
457 if ($email_maintainer) {
458 if ($email_usename && $email_name) {
459 push(@email_to, format_email
($email_name, $email_address));
461 push(@email_to, $email_address);
466 sub push_email_addresses
{
469 my @address_list = ();
471 if (rfc822_valid
($address)) {
472 push_email_address
($address);
473 } elsif (@address_list = rfc822_validlist
($address)) {
474 my $array_count = shift(@address_list);
475 while (my $entry = shift(@address_list)) {
476 push_email_address
($entry);
479 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
486 foreach my $path (split(/:/, $ENV{PATH
})) {
487 if (-e
"$path/$bin") {
495 sub recent_git_signoffs
{
505 if (which
("git") eq "") {
506 warn("$P: git not found. Add --nogit to options?\n");
510 warn("$P: .git directory not found. Use a git repository for better results.\n");
511 warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
515 $cmd = "git log --since=${email_git_since} -- ${file}";
516 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
517 if (!$email_git_penguin_chiefs) {
518 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
520 $cmd .= " | cut -f2- -d\":\"";
521 $cmd .= " | sort | uniq -c | sort -rn";
524 $output =~ s/^\s*//gm;
526 @lines = split("\n", $output);
528 $total_sign_offs = 0;
529 foreach my $line (@lines) {
530 if ($line =~ m/([0-9]+)\s+(.*)/) {
531 $total_sign_offs += $1;
533 die("$P: Unexpected git output: ${line}\n");
537 foreach my $line (@lines) {
538 if ($line =~ m/([0-9]+)\s+(.*)/) {
542 if ($sign_offs < $email_git_min_signatures ||
543 $count > $email_git_max_maintainers ||
544 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) {
548 if ($line =~ m/(.+)<(.+)>/) {
551 if ($email_usename) {
552 push(@email_to, format_email
($git_name, $git_addr));
554 push(@email_to, $git_addr);
556 } elsif ($line =~ m/<(.+)>/) {
558 push(@email_to, $git_addr);
560 push(@email_to, $line);
569 @parms = grep(!$saw{$_}++, @parms);
577 @parms = sort @parms;
578 @parms = grep(!$saw{$_}++, @parms);
585 if ($output_multiline) {
586 foreach my $line (@parms) {
590 print(join($output_separator, @parms));
598 # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
599 # comment. We must allow for rfc822_lwsp (or comments) after each of these.
600 # This regexp will only work on addresses which have had comments stripped
601 # and replaced with rfc822_lwsp.
603 my $specials = '()<>@,;:\\\\".\\[\\]';
604 my $controls = '\\000-\\037\\177';
606 my $dtext = "[^\\[\\]\\r\\\\]";
607 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
609 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
611 # Use zero-width assertion to spot the limit of an atom. A simple
612 # $rfc822_lwsp* causes the regexp engine to hang occasionally.
613 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
614 my $word = "(?:$atom|$quoted_string)";
615 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
617 my $sub_domain = "(?:$atom|$domain_literal)";
618 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
620 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
622 my $phrase = "$word*";
623 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
624 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
625 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
627 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
628 my $address = "(?:$mailbox|$group)";
630 return "$rfc822_lwsp*$address";
633 sub rfc822_strip_comments
{
635 # Recursively remove comments, and replace with a single space. The simpler
636 # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
637 # chars in atoms, for example.
639 while ($s =~ s
/^((?
:[^"\\]|\\.)*
640 (?:"(?
:[^"\\]|\\.)*"(?
:[^"\\]|\\.)*)*)
641 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
645 # valid: returns true if the parameter is an RFC822 valid address
647 sub rfc822_valid ($) {
648 my $s = rfc822_strip_comments(shift);
651 $rfc822re = make_rfc822re();
654 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
657 # validlist: In scalar context, returns true if the parameter is an RFC822
658 # valid list of addresses.
660 # In list context, returns an empty list on failure (an invalid
661 # address was found); otherwise a list whose first element is the
662 # number of addresses found and whose remaining elements are the
663 # addresses. This is needed to disambiguate failure (invalid)
664 # from success with no addresses found, because an empty string is
667 sub rfc822_validlist ($) {
668 my $s = rfc822_strip_comments(shift);
671 $rfc822re = make_rfc822re();
673 # * null list items are valid according to the RFC
674 # * the '1' business is to aid in distinguishing failure from no results
677 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
678 $s =~ m/^$rfc822_char*$/) {
679 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
682 return wantarray ? (scalar(@r), @r) : 1;
685 return wantarray ? () : 0;