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 $email_remove_duplicates = 1;
34 my $output_multiline = 1;
35 my $output_separator = ", ";
40 my $from_filename = 0;
41 my $pattern_depth = 0;
47 my @penguin_chief = ();
48 push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
49 #Andrew wants in on most everything - 2009/01/14
50 #push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
52 my @penguin_chief_names = ();
53 foreach my $chief (@penguin_chief) {
54 if ($chief =~ m/^(.*):(.*)/) {
57 push(@penguin_chief_names, $chief_name);
60 my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
62 # rfc822 email address - preloaded methods go here.
63 my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
64 my $rfc822_char = '[\\000-\\377]';
68 'git!' => \
$email_git,
69 'git-chief-penguins!' => \
$email_git_penguin_chiefs,
70 'git-min-signatures=i' => \
$email_git_min_signatures,
71 'git-max-maintainers=i' => \
$email_git_max_maintainers,
72 'git-min-percent=i' => \
$email_git_min_percent,
73 'git-since=s' => \
$email_git_since,
74 'git-blame!' => \
$email_git_blame,
75 'remove-duplicates!' => \
$email_remove_duplicates,
76 'm!' => \
$email_maintainer,
77 'n!' => \
$email_usename,
79 's!' => \
$email_subscriber_list,
80 'multiline!' => \
$output_multiline,
81 'separator=s' => \
$output_separator,
82 'subsystem!' => \
$subsystem,
83 'status!' => \
$status,
86 'pattern-depth=i' => \
$pattern_depth,
87 'f|file' => \
$from_filename,
88 'v|version' => \
$version,
92 die "$P: invalid argument\n";
101 print("${P} ${V}\n");
107 die "$P: argument missing: patchfile or -f file please\n";
110 if ($output_separator ne ", ") {
111 $output_multiline = 0;
114 my $selections = $email + $scm + $status + $subsystem + $web;
115 if ($selections == 0) {
117 die "$P: Missing required option: email, scm, status, subsystem or web\n";
121 ($email_maintainer + $email_list + $email_subscriber_list +
122 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
124 die "$P: Please select at least 1 email option\n";
127 if (!top_of_kernel_tree
($lk_path)) {
128 die "$P: The current directory does not appear to be "
129 . "a linux kernel source tree.\n";
132 ## Read MAINTAINERS for type/value pairs
135 open(MAINT
, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
139 if ($line =~ m/^(\C):\s*(.*)/) {
143 ##Filename pattern matching
144 if ($type eq "F" || $type eq "X") {
145 $value =~ s@\
.@
\\\
.@g; ##Convert . to \.
146 $value =~ s/\*/\.\*/g; ##Convert * to .*
147 $value =~ s/\?/\./g; ##Convert ? to .
148 ##if pattern is a directory and it lacks a trailing slash, add one
150 $value =~ s@
([^/])$@$1/@
;
153 push(@typevalue, "$type:$value");
154 } elsif (!/^(\s)*$/) {
156 push(@typevalue, $line);
163 if ($email_remove_duplicates) {
164 open(MAILMAP
, "<${lk_path}.mailmap") || warn "$P: Can't open .mailmap\n";
168 next if ($line =~ m/^\s*#/);
169 next if ($line =~ m/^\s*$/);
171 my ($name, $address) = parse_email
($line);
172 $line = format_email
($name, $address);
174 next if ($line =~ m/^\s*$/);
176 if (exists($mailmap{$name})) {
177 my $obj = $mailmap{$name};
178 push(@
$obj, $address);
180 my @arr = ($address);
181 $mailmap{$name} = \
@arr;
187 ## use the filenames on the command line or find the filenames in the patchfiles
192 foreach my $file (@ARGV) {
193 ##if $file is a directory and it lacks a trailing slash, add one
195 $file =~ s@
([^/])$@$1/@
;
196 } elsif (!(-f
$file)) {
197 die "$P: file '${file}' not found\n";
199 if ($from_filename) {
202 my $file_cnt = @files;
204 open(PATCH
, "<$file") or die "$P: Can't open ${file}\n";
206 if (m/^\+\+\+\s+(\S+)/) {
208 $filename =~ s@
^[^/]*/@@
;
210 $lastfile = $filename;
211 push(@files, $filename);
212 } elsif (m/^\@\@ -(\d+),(\d+)/) {
213 if ($email_git_blame) {
214 push(@range, "$lastfile:$1:$2");
219 if ($file_cnt == @files) {
220 warn "$P: file '${file}' doesn't appear to be a patch. "
221 . "Add -f to options?\n";
223 @files = sort_and_uniq
(@files);
234 # Find responsible parties
236 foreach my $file (@files) {
238 #Do not match excluded file patterns
241 foreach my $line (@typevalue) {
242 if ($line =~ m/^(\C):\s*(.*)/) {
246 if (file_match_pattern
($file, $value)) {
257 foreach my $line (@typevalue) {
258 if ($line =~ m/^(\C):\s*(.*)/) {
262 if (file_match_pattern
($file, $value)) {
263 my $value_pd = ($value =~ tr@
/@@
);
264 my $file_pd = ($file =~ tr@
/@@
);
265 $value_pd++ if (substr($value,-1,1) ne "/");
266 if ($pattern_depth == 0 ||
267 (($file_pd - $value_pd) < $pattern_depth)) {
268 $hash{$tvi} = $value_pd;
275 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
276 add_categories
($line);
280 if ($email && $email_git) {
281 recent_git_signoffs
($file);
284 if ($email && $email_git_blame) {
285 git_assign_blame
($file);
290 foreach my $chief (@penguin_chief) {
291 if ($chief =~ m/^(.*):(.*)/) {
294 $email_address = format_email
($1, $2);
295 if ($email_git_penguin_chiefs) {
296 push(@email_to, $email_address);
298 @email_to = grep(!/${email_address}/, @email_to);
304 if ($email || $email_list) {
307 @to = (@to, @email_to);
310 @to = (@to, @list_to);
321 @status = uniq
(@status);
326 @subsystem = uniq
(@subsystem);
337 sub file_match_pattern
{
338 my ($file, $pattern) = @_;
339 if (substr($pattern, -1) eq "/") {
340 if ($file =~ m@
^$pattern@
) {
344 if ($file =~ m@
^$pattern@
) {
345 my $s1 = ($file =~ tr@
/@@
);
346 my $s2 = ($pattern =~ tr@
/@@
);
357 usage: $P [options] patchfile
358 $P [options] -f file|directory
361 MAINTAINER field selection options:
362 --email => print email address(es) if any
363 --git => include recent git \*-by: signers
364 --git-chief-penguins => include ${penguin_chiefs}
365 --git-min-signatures => number of signatures required (default: 1)
366 --git-max-maintainers => maximum maintainers to add (default: 5)
367 --git-min-percent => minimum percentage of commits required (default: 5)
368 --git-since => git history to use (default: 1-year-ago)
369 --git-blame => use git blame to find modified commits for patch or file
370 --m => include maintainer(s) if any
371 --n => include name 'Full Name <addr\@domain.tld>'
372 --l => include list(s) if any
373 --s => include subscriber only list(s) if any
374 --remove-duplicates => minimize duplicate email names/addresses
375 --scm => print SCM tree(s) if any
376 --status => print status if any
377 --subsystem => print subsystem name if any
378 --web => print website(s) if any
381 --separator [, ] => separator for multiple entries on 1 line
382 using --separator also sets --nomultiline if --separator is not [, ]
383 --multiline => print 1 entry per line
386 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
387 --version => show version
388 --help => show this help information
391 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
394 Using "-f directory" may give unexpected results:
395 Used with "--git", git signators for _all_ files in and below
396 directory are examined as git recurses directories.
397 Any specified X: (exclude) pattern matches are _not_ ignored.
398 Used with "--nogit", directory is used as a pattern match,
399 no individual file within the directory or subdirectory
401 Used with "--git-blame", does not iterate all files in directory
402 Using "--git-blame" is slow and may add old committers and authors
403 that are no longer active maintainers to the output.
407 sub top_of_kernel_tree
{
410 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
413 if ( (-f
"${lk_path}COPYING")
414 && (-f
"${lk_path}CREDITS")
415 && (-f
"${lk_path}Kbuild")
416 && (-f
"${lk_path}MAINTAINERS")
417 && (-f
"${lk_path}Makefile")
418 && (-f
"${lk_path}README")
419 && (-d
"${lk_path}Documentation")
420 && (-d
"${lk_path}arch")
421 && (-d
"${lk_path}include")
422 && (-d
"${lk_path}drivers")
423 && (-d
"${lk_path}fs")
424 && (-d
"${lk_path}init")
425 && (-d
"${lk_path}ipc")
426 && (-d
"${lk_path}kernel")
427 && (-d
"${lk_path}lib")
428 && (-d
"${lk_path}scripts")) {
435 my ($formatted_email) = @_;
440 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
443 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
445 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
449 $name =~ s/^\s+|\s+$//g;
450 $name =~ s/^\"|\"$//g;
451 $address =~ s/^\s+|\s+$//g;
453 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
454 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
458 return ($name, $address);
462 my ($name, $address) = @_;
466 $name =~ s/^\s+|\s+$//g;
467 $name =~ s/^\"|\"$//g;
468 $address =~ s/^\s+|\s+$//g;
470 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
471 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
475 if ($email_usename) {
477 $formatted_email = "$address";
479 $formatted_email = "$name <${address}>";
482 $formatted_email = $address;
485 return $formatted_email;
488 sub find_starting_index
{
493 my $tv = $typevalue[$index];
494 if (!($tv =~ m/^(\C):\s*(.*)/)) {
503 sub find_ending_index
{
506 while ($index < @typevalue) {
507 my $tv = $typevalue[$index];
508 if (!($tv =~ m/^(\C):\s*(.*)/)) {
521 my $start = find_starting_index
($index);
522 my $end = find_ending_index
($index);
524 push(@subsystem, $typevalue[$start]);
526 for ($i = $start + 1; $i < $end; $i++) {
527 my $tv = $typevalue[$i];
528 if ($tv =~ m/^(\C):\s*(.*)/) {
532 my $list_address = $pvalue;
533 my $list_additional = "";
534 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
536 $list_additional = $2;
538 if ($list_additional =~ m/subscribers-only/) {
539 if ($email_subscriber_list) {
540 push(@list_to, $list_address);
544 push(@list_to, $list_address);
547 } elsif ($ptype eq "M") {
548 my ($name, $address) = parse_email
($pvalue);
551 my $tv = $typevalue[$i - 1];
552 if ($tv =~ m/^(\C):\s*(.*)/) {
555 $pvalue = format_email
($name, $address);
560 if ($email_maintainer) {
561 push_email_addresses
($pvalue);
563 } elsif ($ptype eq "T") {
565 } elsif ($ptype eq "W") {
567 } elsif ($ptype eq "S") {
568 push(@status, $pvalue);
575 my %email_hash_address;
578 my ($name, $address) = @_;
580 return 1 if (($name eq "") && ($address eq ""));
581 return 1 if (($name ne "") && exists($email_hash_name{$name}));
582 return 1 if (($address ne "") && exists($email_hash_address{$address}));
587 sub push_email_address
{
590 my ($name, $address) = parse_email
($line);
592 if ($address eq "") {
596 if (!$email_remove_duplicates) {
597 push(@email_to, format_email
($name, $address));
598 } elsif (!email_inuse
($name, $address)) {
599 push(@email_to, format_email
($name, $address));
600 $email_hash_name{$name}++;
601 $email_hash_address{$address}++;
607 sub push_email_addresses
{
610 my @address_list = ();
612 if (rfc822_valid
($address)) {
613 push_email_address
($address);
614 } elsif (@address_list = rfc822_validlist
($address)) {
615 my $array_count = shift(@address_list);
616 while (my $entry = shift(@address_list)) {
617 push_email_address
($entry);
620 if (!push_email_address
($address)) {
621 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
629 foreach my $path (split(/:/, $ENV{PATH
})) {
630 if (-e
"$path/$bin") {
642 foreach my $line (@lines) {
643 my ($name, $address) = parse_email
($line);
644 if (!exists($hash{$name})) {
645 $hash{$name} = $address;
646 } elsif ($address ne $hash{$name}) {
647 $address = $hash{$name};
648 $line = format_email
($name, $address);
650 if (exists($mailmap{$name})) {
651 my $obj = $mailmap{$name};
652 foreach my $map_address (@
$obj) {
653 if (($map_address eq $address) &&
654 ($map_address ne $hash{$name})) {
655 $line = format_email
($name, $hash{$name});
664 sub recent_git_signoffs
{
675 if (which
("git") eq "") {
676 warn("$P: git not found. Add --nogit to options?\n");
680 warn("$P: .git directory not found. Use a git repository for better results.\n");
681 warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
685 $cmd = "git log --since=${email_git_since} -- ${file}";
688 $output =~ s/^\s*//gm;
690 @lines = split("\n", $output);
692 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
693 if (!$email_git_penguin_chiefs) {
694 @lines = grep(!/${penguin_chiefs}/i, @lines);
697 s/.*:\s*(.+)\s*/$1/ for (@lines);
699 $total_sign_offs = @lines;
701 if ($email_remove_duplicates) {
702 @lines = mailmap
(@lines);
705 @lines = sort(@lines);
708 $hash{$_}++ for @lines;
711 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
712 my $sign_offs = $hash{$line};
714 last if ($sign_offs < $email_git_min_signatures ||
715 $count > $email_git_max_maintainers ||
716 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
717 push_email_address
($line);
722 my ($cmd, @commits) = @_;
728 @lines = split("\n", $output);
729 foreach my $line (@lines) {
730 if ($line =~ m/^(\w+) /) {
737 sub git_assign_blame
{
749 foreach my $file_range_diff (@range) {
750 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
753 my $diff_length = $3;
754 next if (!("$file" eq "$diff_file"));
755 $cmd = "git blame -l -L $diff_start,+$diff_length $file";
756 @commits = save_commits
($cmd, @commits);
760 $cmd = "git blame -l $file";
761 @commits = save_commits
($cmd, @commits);
765 $total_sign_offs = 0;
766 @commits = uniq
(@commits);
767 foreach my $commit (@commits) {
768 $cmd = "git log -1 ${commit}";
771 $output =~ s/^\s*//gm;
772 @lines = split("\n", $output);
774 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
775 if (!$email_git_penguin_chiefs) {
776 @lines = grep(!/${penguin_chiefs}/i, @lines);
780 s/.*:\s*(.+)\s*/$1/ for (@lines);
782 $total_sign_offs += @lines;
784 if ($email_remove_duplicates) {
785 @lines = mailmap
(@lines);
788 $hash{$_}++ for @lines;
792 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
793 my $sign_offs = $hash{$line};
795 last if ($sign_offs < $email_git_min_signatures ||
796 $count > $email_git_max_maintainers ||
797 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
798 push_email_address
($line);
806 @parms = grep(!$saw{$_}++, @parms);
814 @parms = sort @parms;
815 @parms = grep(!$saw{$_}++, @parms);
822 if ($output_multiline) {
823 foreach my $line (@parms) {
827 print(join($output_separator, @parms));
835 # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
836 # comment. We must allow for rfc822_lwsp (or comments) after each of these.
837 # This regexp will only work on addresses which have had comments stripped
838 # and replaced with rfc822_lwsp.
840 my $specials = '()<>@,;:\\\\".\\[\\]';
841 my $controls = '\\000-\\037\\177';
843 my $dtext = "[^\\[\\]\\r\\\\]";
844 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
846 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
848 # Use zero-width assertion to spot the limit of an atom. A simple
849 # $rfc822_lwsp* causes the regexp engine to hang occasionally.
850 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
851 my $word = "(?:$atom|$quoted_string)";
852 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
854 my $sub_domain = "(?:$atom|$domain_literal)";
855 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
857 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
859 my $phrase = "$word*";
860 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
861 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
862 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
864 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
865 my $address = "(?:$mailbox|$group)";
867 return "$rfc822_lwsp*$address";
870 sub rfc822_strip_comments
{
872 # Recursively remove comments, and replace with a single space. The simpler
873 # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
874 # chars in atoms, for example.
876 while ($s =~ s
/^((?
:[^"\\]|\\.)*
877 (?:"(?
:[^"\\]|\\.)*"(?
:[^"\\]|\\.)*)*)
878 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
882 # valid: returns true if the parameter is an RFC822 valid address
884 sub rfc822_valid ($) {
885 my $s = rfc822_strip_comments(shift);
888 $rfc822re = make_rfc822re();
891 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
894 # validlist: In scalar context, returns true if the parameter is an RFC822
895 # valid list of addresses.
897 # In list context, returns an empty list on failure (an invalid
898 # address was found); otherwise a list whose first element is the
899 # number of addresses found and whose remaining elements are the
900 # addresses. This is needed to disambiguate failure (invalid)
901 # from success with no addresses found, because an empty string is
904 sub rfc822_validlist ($) {
905 my $s = rfc822_strip_comments(shift);
908 $rfc822re = make_rfc822re();
910 # * null list items are valid according to the RFC
911 # * the '1' business is to aid in distinguishing failure from no results
914 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
915 $s =~ m/^$rfc822_char*$/) {
916 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
919 return wantarray ? (scalar(@r), @r) : 1;
922 return wantarray ? () : 0;