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 $email_git_blame = 0;
33 my $output_multiline = 1;
34 my $output_separator = ", ";
39 my $from_filename = 0;
40 my $pattern_depth = 0;
46 my @penguin_chief = ();
47 push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
48 #Andrew wants in on most everything - 2009/01/14
49 #push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
51 my @penguin_chief_names = ();
52 foreach my $chief (@penguin_chief) {
53 if ($chief =~ m/^(.*):(.*)/) {
56 push(@penguin_chief_names, $chief_name);
59 my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
61 # rfc822 email address - preloaded methods go here.
62 my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
63 my $rfc822_char = '[\\000-\\377]';
67 'git!' => \$email_git,
68 'git-chief-penguins!' => \$email_git_penguin_chiefs,
69 'git-min-signatures=i' => \$email_git_min_signatures,
70 'git-max-maintainers=i' => \$email_git_max_maintainers,
71 'git-min-percent=i' => \$email_git_min_percent,
72 'git-since=s' => \$email_git_since,
73 'git-blame!' => \$email_git_blame,
74 'm!' => \$email_maintainer,
75 'n!' => \$email_usename,
77 's!' => \$email_subscriber_list,
78 'multiline!' => \$output_multiline,
79 'separator=s' => \$output_separator,
80 'subsystem!' => \$subsystem,
81 'status!' => \$status,
84 'pattern-depth=i' => \$pattern_depth,
85 'f|file' => \$from_filename,
86 'v|version' => \$version,
90 die "$P: invalid argument\n";
105 die "$P: argument missing: patchfile or -f file please\n";
108 my $selections = $email + $scm + $status + $subsystem + $web;
109 if ($selections == 0) {
111 die "$P: Missing required option: email, scm, status, subsystem or web\n";
115 ($email_maintainer + $email_list + $email_subscriber_list +
116 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
118 die "$P: Please select at least 1 email option\n";
121 if (!top_of_kernel_tree($lk_path)) {
122 die "$P: The current directory does not appear to be "
123 . "a linux kernel source tree.\n";
126 ## Read MAINTAINERS for type/value pairs
129 open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
133 if ($line =~ m/^(\C):\s*(.*)/) {
137 ##Filename pattern matching
138 if ($type eq "F" || $type eq "X") {
139 $value =~ s@\.@\\\.@g; ##Convert . to \.
140 $value =~ s/\*/\.\*/g; ##Convert * to .*
141 $value =~ s/\?/\./g; ##Convert ? to .
142 ##if pattern is a directory and it lacks a trailing slash, add one
144 $value =~ s@([^/])$@$1/@;
147 push(@typevalue, "$type:$value");
148 } elsif (!/^(\s)*$/) {
150 push(@typevalue, $line);
155 ## use the filenames on the command line or find the filenames in the patchfiles
160 foreach my $file (@ARGV) {
161 ##if $file is a directory and it lacks a trailing slash, add one
163 $file =~ s@([^/])$@$1/@;
164 } elsif (!(-f $file)) {
165 die "$P: file '${file}' not found\n";
167 if ($from_filename) {
170 my $file_cnt = @files;
172 open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
174 if (m/^\+\+\+\s+(\S+)/) {
176 $filename =~ s@^[^/]*/@@;
178 $lastfile = $filename;
179 push(@files, $filename);
180 } elsif (m/^\@\@ -(\d+),(\d+)/) {
181 if ($email_git_blame) {
182 push(@range, "$lastfile:$1:$2");
187 if ($file_cnt == @files) {
188 warn "$P: file '${file}' doesn't appear to be a patch. "
189 . "Add -f to options?\n";
191 @files = sort_and_uniq(@files);
202 # Find responsible parties
204 foreach my $file (@files) {
206 #Do not match excluded file patterns
209 foreach my $line (@typevalue) {
210 if ($line =~ m/^(\C):\s*(.*)/) {
214 if (file_match_pattern($file, $value)) {
225 foreach my $line (@typevalue) {
226 if ($line =~ m/^(\C):\s*(.*)/) {
230 if (file_match_pattern($file, $value)) {
231 my $value_pd = ($value =~ tr@/@@);
232 my $file_pd = ($file =~ tr@/@@);
233 $value_pd++ if (substr($value,-1,1) ne "/");
234 if ($pattern_depth == 0 ||
235 (($file_pd - $value_pd) < $pattern_depth)) {
236 $hash{$tvi} = $value_pd;
243 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
244 add_categories($line);
248 if ($email && $email_git) {
249 recent_git_signoffs($file);
252 if ($email && $email_git_blame) {
253 git_assign_blame($file);
258 foreach my $chief (@penguin_chief) {
259 if ($chief =~ m/^(.*):(.*)/) {
262 $email_address = format_email($1, $2);
263 if ($email_git_penguin_chiefs) {
264 push(@email_to, $email_address);
266 @email_to = grep(!/${email_address}/, @email_to);
272 if ($email || $email_list) {
275 @to = (@to, @email_to);
278 @to = (@to, @list_to);
284 @scm = sort_and_uniq(@scm);
289 @status = sort_and_uniq(@status);
294 @subsystem = sort_and_uniq(@subsystem);
299 @web = sort_and_uniq(@web);
305 sub file_match_pattern {
306 my ($file, $pattern) = @_;
307 if (substr($pattern, -1) eq "/") {
308 if ($file =~ m@^$pattern@) {
312 if ($file =~ m@^$pattern@) {
313 my $s1 = ($file =~ tr@/@@);
314 my $s2 = ($pattern =~ tr@/@@);
325 usage: $P [options] patchfile
326 $P [options] -f file|directory
329 MAINTAINER field selection options:
330 --email => print email address(es) if any
331 --git => include recent git \*-by: signers
332 --git-chief-penguins => include ${penguin_chiefs}
333 --git-min-signatures => number of signatures required (default: 1)
334 --git-max-maintainers => maximum maintainers to add (default: 5)
335 --git-min-percent => minimum percentage of commits required (default: 5)
336 --git-since => git history to use (default: 1-year-ago)
337 --git-blame => use git blame to find modified commits for patch or file
338 --m => include maintainer(s) if any
339 --n => include name 'Full Name <addr\@domain.tld>'
340 --l => include list(s) if any
341 --s => include subscriber only list(s) if any
342 --scm => print SCM tree(s) if any
343 --status => print status if any
344 --subsystem => print subsystem name if any
345 --web => print website(s) if any
348 --separator [, ] => separator for multiple entries on 1 line
349 --multiline => print 1 entry per line
352 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
353 --version => show version
354 --help => show this help information
357 [--email --git --m --n --l --multiline --pattern-depth=0]
360 Using "-f directory" may give unexpected results:
361 Used with "--git", git signators for _all_ files in and below
362 directory are examined as git recurses directories.
363 Any specified X: (exclude) pattern matches are _not_ ignored.
364 Used with "--nogit", directory is used as a pattern match,
365 no individual file within the directory or subdirectory
367 Used with "--git-blame", does not iterate all files in directory
368 Using "--git-blame" is slow and may add old committers and authors
369 that are no longer active maintainers to the output.
373 sub top_of_kernel_tree {
376 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
379 if ( (-f "${lk_path}COPYING")
380 && (-f "${lk_path}CREDITS")
381 && (-f "${lk_path}Kbuild")
382 && (-f "${lk_path}MAINTAINERS")
383 && (-f "${lk_path}Makefile")
384 && (-f "${lk_path}README")
385 && (-d "${lk_path}Documentation")
386 && (-d "${lk_path}arch")
387 && (-d "${lk_path}include")
388 && (-d "${lk_path}drivers")
389 && (-d "${lk_path}fs")
390 && (-d "${lk_path}init")
391 && (-d "${lk_path}ipc")
392 && (-d "${lk_path}kernel")
393 && (-d "${lk_path}lib")
394 && (-d "${lk_path}scripts")) {
401 my ($formatted_email) = @_;
406 if ($formatted_email =~ /^([^<]+)<(.*\@.*)>$/) {
409 } elsif ($formatted_email =~ /^<(.*\@.*)>$/) {
411 } elsif ($formatted_email =~ /^(.*\@.*)$/) {
415 $name =~ s/^\s+|\s+$//g;
416 $name =~ s/^\"|\"$//g;
417 $address =~ s/^\s+|\s+$//g;
419 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
420 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
424 return ($name, $address);
428 my ($name, $address) = @_;
432 $name =~ s/^\s+|\s+$//g;
433 $name =~ s/^\"|\"$//g;
434 $address =~ s/^\s+|\s+$//g;
436 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
437 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
441 if ($email_usename) {
443 $formatted_email = "$address";
445 $formatted_email = "$name <${address}>";
448 $formatted_email = $address;
451 return $formatted_email;
458 while ($index >= 0) {
459 my $tv = $typevalue[$index];
460 if ($tv =~ m/^(\C):\s*(.*)/) {
464 my $list_address = $pvalue;
465 my $list_additional = "";
466 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
468 $list_additional = $2;
470 if ($list_additional =~ m/subscribers-only/) {
471 if ($email_subscriber_list) {
472 push(@list_to, $list_address);
476 push(@list_to, $list_address);
479 } elsif ($ptype eq "M") {
480 my ($name, $address) = parse_email($pvalue);
483 my $tv = $typevalue[$index - 1];
484 if ($tv =~ m/^(\C):\s*(.*)/) {
491 if ($email_maintainer) {
492 push_email_addresses($pvalue);
494 } elsif ($ptype eq "T") {
496 } elsif ($ptype eq "W") {
498 } elsif ($ptype eq "S") {
499 push(@status, $pvalue);
504 push(@subsystem,$tv);
510 sub email_address_inuse {
511 my ($test_address) = @_;
513 foreach my $line (@email_to) {
514 my ($name, $address) = parse_email($line);
516 return 1 if ($address eq $test_address);
521 sub push_email_address {
524 my ($name, $address) = parse_email($line);
526 if (!email_address_inuse($address)) {
527 push(@email_to, format_email($name, $address));
531 sub push_email_addresses {
534 my @address_list = ();
536 if (rfc822_valid($address)) {
537 push_email_address($address);
538 } elsif (@address_list = rfc822_validlist($address)) {
539 my $array_count = shift(@address_list);
540 while (my $entry = shift(@address_list)) {
541 push_email_address($entry);
544 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
551 foreach my $path (split(/:/, $ENV{PATH})) {
552 if (-e "$path/$bin") {
560 sub recent_git_signoffs {
571 if (which("git") eq "") {
572 warn("$P: git not found. Add --nogit to options?\n");
576 warn("$P: .git directory not found. Use a git repository for better results.\n");
577 warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
581 $cmd = "git log --since=${email_git_since} -- ${file}";
584 $output =~ s/^\s*//gm;
586 @lines = split("\n", $output);
588 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
589 if (!$email_git_penguin_chiefs) {
590 @lines = grep(!/${penguin_chiefs}/i, @lines);
593 s/.*:\s*(.+)\s*/$1/ for (@lines);
595 @lines = mailmap(@lines);
597 $total_sign_offs = @lines;
598 @lines = sort(@lines);
600 foreach my $line (@lines) {
605 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
606 push(@lines,"$hash{$line} $line");
609 foreach my $line (@lines) {
610 if ($line =~ m/([0-9]+)\s+(.*)/) {
614 if ($sign_offs < $email_git_min_signatures ||
615 $count > $email_git_max_maintainers ||
616 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) {
619 push_email_address($line);
625 my ($cmd, @commits) = @_;
631 @lines = split("\n", $output);
632 foreach my $line (@lines) {
633 if ($line =~ m/^(\w+) /) {
640 sub git_assign_blame {
652 foreach my $file_range_diff (@range) {
653 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
656 my $diff_length = $3;
657 next if (!("$file" eq "$diff_file"));
658 $cmd = "git blame -l -L $diff_start,+$diff_length $file\n";
659 @commits = save_commits($cmd, @commits);
663 $cmd = "git blame -l $file\n";
664 @commits = save_commits($cmd, @commits);
668 $total_sign_offs = 0;
669 @commits = uniq(@commits);
670 foreach my $commit (@commits) {
671 $cmd = "git log -1 ${commit}";
674 $output =~ s/^\s*//gm;
675 @lines = split("\n", $output);
677 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
678 if (!$email_git_penguin_chiefs) {
679 @lines = grep(!/${penguin_chiefs}/i, @lines);
682 s/.*:\s*(.+)\s*/$1/ for (@lines);
684 $hash{$_}++ for @lines;
685 $total_sign_offs += @lines;
689 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
690 my $sign_offs = $hash{$line};
692 last if ($sign_offs < $email_git_min_signatures ||
693 $count > $email_git_max_maintainers ||
694 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
695 push_email_address($line);
703 @parms = grep(!$saw{$_}++, @parms);
711 @parms = sort @parms;
712 @parms = grep(!$saw{$_}++, @parms);
719 if ($output_multiline) {
720 foreach my $line (@parms) {
724 print(join($output_separator, @parms));
732 # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
733 # comment. We must allow for rfc822_lwsp (or comments) after each of these.
734 # This regexp will only work on addresses which have had comments stripped
735 # and replaced with rfc822_lwsp.
737 my $specials = '()<>@,;:\\\\".\\[\\]';
738 my $controls = '\\000-\\037\\177';
740 my $dtext = "[^\\[\\]\\r\\\\]";
741 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
743 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
745 # Use zero-width assertion to spot the limit of an atom. A simple
746 # $rfc822_lwsp* causes the regexp engine to hang occasionally.
747 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
748 my $word = "(?:$atom|$quoted_string)";
749 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
751 my $sub_domain = "(?:$atom|$domain_literal)";
752 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
754 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
756 my $phrase = "$word*";
757 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
758 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
759 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
761 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
762 my $address = "(?:$mailbox|$group)";
764 return "$rfc822_lwsp*$address";
767 sub rfc822_strip_comments {
769 # Recursively remove comments, and replace with a single space. The simpler
770 # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
771 # chars in atoms, for example.
773 while ($s =~ s/^((?:[^"\\]|\\.)*
774 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
775 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
779 # valid: returns true if the parameter is an RFC822 valid address
781 sub rfc822_valid ($) {
782 my $s = rfc822_strip_comments(shift);
785 $rfc822re = make_rfc822re();
788 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
791 # validlist: In scalar context, returns true if the parameter is an RFC822
792 # valid list of addresses.
794 # In list context, returns an empty list on failure (an invalid
795 # address was found); otherwise a list whose first element is the
796 # number of addresses found and whose remaining elements are the
797 # addresses. This is needed to disambiguate failure (invalid)
798 # from success with no addresses found, because an empty string is
801 sub rfc822_validlist ($) {
802 my $s = rfc822_strip_comments(shift);
805 $rfc822re = make_rfc822re();
807 # * null list items are valid according to the RFC
808 # * the '1' business is to aid in distinguishing failure from no results
811 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
812 $s =~ m/^$rfc822_char*$/) {
813 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
816 return wantarray ? (scalar(@r), @r) : 1;
819 return wantarray ? () : 0;