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/^(.*):(.*)/) {
261 if ($email_usename) {
262 $email_address = format_email($1, $2);
266 if ($email_git_penguin_chiefs) {
267 push(@email_to, $email_address);
269 @email_to = grep(!/${email_address}/, @email_to);
275 if ($email || $email_list) {
278 @to = (@to, @email_to);
281 @to = (@to, @list_to);
287 @scm = sort_and_uniq(@scm);
292 @status = sort_and_uniq(@status);
297 @subsystem = sort_and_uniq(@subsystem);
302 @web = sort_and_uniq(@web);
308 sub file_match_pattern {
309 my ($file, $pattern) = @_;
310 if (substr($pattern, -1) eq "/") {
311 if ($file =~ m@^$pattern@) {
315 if ($file =~ m@^$pattern@) {
316 my $s1 = ($file =~ tr@/@@);
317 my $s2 = ($pattern =~ tr@/@@);
328 usage: $P [options] patchfile
329 $P [options] -f file|directory
332 MAINTAINER field selection options:
333 --email => print email address(es) if any
334 --git => include recent git \*-by: signers
335 --git-chief-penguins => include ${penguin_chiefs}
336 --git-min-signatures => number of signatures required (default: 1)
337 --git-max-maintainers => maximum maintainers to add (default: 5)
338 --git-min-percent => minimum percentage of commits required (default: 5)
339 --git-since => git history to use (default: 1-year-ago)
340 --git-blame => use git blame to find modified commits for patch or file
341 --m => include maintainer(s) if any
342 --n => include name 'Full Name <addr\@domain.tld>'
343 --l => include list(s) if any
344 --s => include subscriber only list(s) if any
345 --scm => print SCM tree(s) if any
346 --status => print status if any
347 --subsystem => print subsystem name if any
348 --web => print website(s) if any
351 --separator [, ] => separator for multiple entries on 1 line
352 --multiline => print 1 entry per line
355 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
356 --version => show version
357 --help => show this help information
360 [--email --git --m --n --l --multiline --pattern-depth=0]
363 Using "-f directory" may give unexpected results:
364 Used with "--git", git signators for _all_ files in and below
365 directory are examined as git recurses directories.
366 Any specified X: (exclude) pattern matches are _not_ ignored.
367 Used with "--nogit", directory is used as a pattern match,
368 no individual file within the directory or subdirectory
370 Used with "--git-blame", does not iterate all files in directory
371 Using "--git-blame" is slow and may add old committers and authors
372 that are no longer active maintainers to the output.
376 sub top_of_kernel_tree {
379 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
382 if ( (-f "${lk_path}COPYING")
383 && (-f "${lk_path}CREDITS")
384 && (-f "${lk_path}Kbuild")
385 && (-f "${lk_path}MAINTAINERS")
386 && (-f "${lk_path}Makefile")
387 && (-f "${lk_path}README")
388 && (-d "${lk_path}Documentation")
389 && (-d "${lk_path}arch")
390 && (-d "${lk_path}include")
391 && (-d "${lk_path}drivers")
392 && (-d "${lk_path}fs")
393 && (-d "${lk_path}init")
394 && (-d "${lk_path}ipc")
395 && (-d "${lk_path}kernel")
396 && (-d "${lk_path}lib")
397 && (-d "${lk_path}scripts")) {
404 my ($name, $email) = @_;
406 $name =~ s/^\s+|\s+$//g;
407 $name =~ s/^\"|\"$//g;
408 $email =~ s/^\s+|\s+$//g;
410 my $formatted_email = "";
412 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
413 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
414 $formatted_email = "\"${name}\"\ \<${email}\>";
416 $formatted_email = "${name} \<${email}\>";
418 return $formatted_email;
425 while ($index >= 0) {
426 my $tv = $typevalue[$index];
427 if ($tv =~ m/^(\C):\s*(.*)/) {
431 my $list_address = $pvalue;
432 my $list_additional = "";
433 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
435 $list_additional = $2;
437 if ($list_additional =~ m/subscribers-only/) {
438 if ($email_subscriber_list) {
439 push(@list_to, $list_address);
443 push(@list_to, $list_address);
446 } elsif ($ptype eq "M") {
449 my $tv = $typevalue[$index - 1];
450 if ($tv =~ m/^(\C):\s*(.*)/) {
452 if ($email_usename) {
453 push_email_address(format_email($2, $pvalue));
460 push_email_addresses($pvalue);
462 } elsif ($ptype eq "T") {
464 } elsif ($ptype eq "W") {
466 } elsif ($ptype eq "S") {
467 push(@status, $pvalue);
472 push(@subsystem,$tv);
478 sub push_email_address {
479 my ($email_address) = @_;
483 if ($email_maintainer) {
484 if ($email_address =~ m/([^<]+)<(.*\@.*)>$/) {
487 if ($email_usename) {
488 push(@email_to, format_email($email_name, $email_address));
490 push(@email_to, $email_address);
492 } elsif ($email_address =~ m/<(.+)>/) {
494 push(@email_to, $email_address);
496 push(@email_to, $email_address);
501 sub push_email_addresses {
504 my @address_list = ();
506 if (rfc822_valid($address)) {
507 push_email_address($address);
508 } elsif (@address_list = rfc822_validlist($address)) {
509 my $array_count = shift(@address_list);
510 while (my $entry = shift(@address_list)) {
511 push_email_address($entry);
514 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
521 foreach my $path (split(/:/, $ENV{PATH})) {
522 if (-e "$path/$bin") {
530 sub recent_git_signoffs {
540 if (which("git") eq "") {
541 warn("$P: git not found. Add --nogit to options?\n");
545 warn("$P: .git directory not found. Use a git repository for better results.\n");
546 warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
550 $cmd = "git log --since=${email_git_since} -- ${file}";
551 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
552 if (!$email_git_penguin_chiefs) {
553 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
555 $cmd .= " | cut -f2- -d\":\"";
556 $cmd .= " | sort | uniq -c | sort -rn";
559 $output =~ s/^\s*//gm;
561 @lines = split("\n", $output);
563 $total_sign_offs = 0;
564 foreach my $line (@lines) {
565 if ($line =~ m/([0-9]+)\s+(.*)/) {
566 $total_sign_offs += $1;
568 die("$P: Unexpected git output: ${line}\n");
572 foreach my $line (@lines) {
573 if ($line =~ m/([0-9]+)\s+(.*)/) {
577 if ($sign_offs < $email_git_min_signatures ||
578 $count > $email_git_max_maintainers ||
579 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) {
583 push_email_address($line);
588 my ($cmd, @commits) = @_;
594 @lines = split("\n", $output);
595 foreach my $line (@lines) {
596 if ($line =~ m/^(\w+) /) {
603 sub git_assign_blame {
615 foreach my $file_range_diff (@range) {
616 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
619 my $diff_length = $3;
620 next if (!("$file" eq "$diff_file"));
621 $cmd = "git blame -l -L $diff_start,+$diff_length $file\n";
622 @commits = save_commits($cmd, @commits);
626 $cmd = "git blame -l $file\n";
627 @commits = save_commits($cmd, @commits);
631 $total_sign_offs = 0;
632 @commits = uniq(@commits);
633 foreach my $commit (@commits) {
634 $cmd = "git log -1 ${commit}";
635 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
636 if (!$email_git_penguin_chiefs) {
637 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
639 $cmd .= " | cut -f2- -d\":\"";
642 $output =~ s/^\s*//gm;
643 @lines = split("\n", $output);
644 $hash{$_}++ for @lines;
645 $total_sign_offs += @lines;
649 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
650 my $sign_offs = $hash{$line};
652 last if ($sign_offs < $email_git_min_signatures ||
653 $count > $email_git_max_maintainers ||
654 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
655 push_email_address($line);
663 @parms = grep(!$saw{$_}++, @parms);
671 @parms = sort @parms;
672 @parms = grep(!$saw{$_}++, @parms);
679 if ($output_multiline) {
680 foreach my $line (@parms) {
684 print(join($output_separator, @parms));
692 # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
693 # comment. We must allow for rfc822_lwsp (or comments) after each of these.
694 # This regexp will only work on addresses which have had comments stripped
695 # and replaced with rfc822_lwsp.
697 my $specials = '()<>@,;:\\\\".\\[\\]';
698 my $controls = '\\000-\\037\\177';
700 my $dtext = "[^\\[\\]\\r\\\\]";
701 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
703 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
705 # Use zero-width assertion to spot the limit of an atom. A simple
706 # $rfc822_lwsp* causes the regexp engine to hang occasionally.
707 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
708 my $word = "(?:$atom|$quoted_string)";
709 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
711 my $sub_domain = "(?:$atom|$domain_literal)";
712 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
714 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
716 my $phrase = "$word*";
717 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
718 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
719 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
721 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
722 my $address = "(?:$mailbox|$group)";
724 return "$rfc822_lwsp*$address";
727 sub rfc822_strip_comments {
729 # Recursively remove comments, and replace with a single space. The simpler
730 # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
731 # chars in atoms, for example.
733 while ($s =~ s/^((?:[^"\\]|\\.)*
734 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
735 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
739 # valid: returns true if the parameter is an RFC822 valid address
741 sub rfc822_valid ($) {
742 my $s = rfc822_strip_comments(shift);
745 $rfc822re = make_rfc822re();
748 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
751 # validlist: In scalar context, returns true if the parameter is an RFC822
752 # valid list of addresses.
754 # In list context, returns an empty list on failure (an invalid
755 # address was found); otherwise a list whose first element is the
756 # number of addresses found and whose remaining elements are the
757 # addresses. This is needed to disambiguate failure (invalid)
758 # from success with no addresses found, because an empty string is
761 sub rfc822_validlist ($) {
762 my $s = rfc822_strip_comments(shift);
765 $rfc822re = make_rfc822re();
767 # * null list items are valid according to the RFC
768 # * the '1' business is to aid in distinguishing failure from no results
771 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
772 $s =~ m/^$rfc822_char*$/) {
773 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
776 return wantarray ? (scalar(@r), @r) : 1;
779 return wantarray ? () : 0;