scripts/get_maintainer.pl: add --pattern-depth
[linux-2.6/mini2440.git] / scripts / get_maintainer.pl
blob5132949500c1008cbbde23b34df056de923a1aa9
1 #!/usr/bin/perl -w
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
13 use strict;
15 my $P = $0;
16 my $V = '0.19';
18 use Getopt::Long qw(:config no_auto_abbrev);
20 my $lk_path = "./";
21 my $email = 1;
22 my $email_usename = 1;
23 my $email_maintainer = 1;
24 my $email_list = 1;
25 my $email_subscriber_list = 0;
26 my $email_git = 1;
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 = ", ";
35 my $scm = 0;
36 my $web = 0;
37 my $subsystem = 0;
38 my $status = 0;
39 my $from_filename = 0;
40 my $pattern_depth = 0;
41 my $version = 0;
42 my $help = 0;
44 my $exit = 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/^(.*):(.*)/) {
54 my $chief_name = $1;
55 my $chief_addr = $2;
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]';
65 if (!GetOptions(
66 'email!' => \$email,
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,
76 'l!' => \$email_list,
77 's!' => \$email_subscriber_list,
78 'multiline!' => \$output_multiline,
79 'separator=s' => \$output_separator,
80 'subsystem!' => \$subsystem,
81 'status!' => \$status,
82 'scm!' => \$scm,
83 'web!' => \$web,
84 'pattern-depth=i' => \$pattern_depth,
85 'f|file' => \$from_filename,
86 'v|version' => \$version,
87 'h|help' => \$help,
88 )) {
89 usage();
90 die "$P: invalid argument\n";
93 if ($help != 0) {
94 usage();
95 exit 0;
98 if ($version != 0) {
99 print("${P} ${V}\n");
100 exit 0;
103 if ($#ARGV < 0) {
104 usage();
105 die "$P: argument missing: patchfile or -f file please\n";
108 my $selections = $email + $scm + $status + $subsystem + $web;
109 if ($selections == 0) {
110 usage();
111 die "$P: Missing required option: email, scm, status, subsystem or web\n";
114 if ($email &&
115 ($email_maintainer + $email_list + $email_subscriber_list +
116 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
117 usage();
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
128 my @typevalue = ();
129 open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
130 while (<MAINT>) {
131 my $line = $_;
133 if ($line =~ m/^(\C):\s*(.*)/) {
134 my $type = $1;
135 my $value = $2;
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
143 if ((-d $value)) {
144 $value =~ s@([^/])$@$1/@;
147 push(@typevalue, "$type:$value");
148 } elsif (!/^(\s)*$/) {
149 $line =~ s/\n$//g;
150 push(@typevalue, $line);
153 close(MAINT);
155 ## use the filenames on the command line or find the filenames in the patchfiles
157 my @files = ();
158 my @range = ();
160 foreach my $file (@ARGV) {
161 ##if $file is a directory and it lacks a trailing slash, add one
162 if ((-d $file)) {
163 $file =~ s@([^/])$@$1/@;
164 } elsif (!(-f $file)) {
165 die "$P: file '${file}' not found\n";
167 if ($from_filename) {
168 push(@files, $file);
169 } else {
170 my $file_cnt = @files;
171 my $lastfile;
172 open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
173 while (<PATCH>) {
174 if (m/^\+\+\+\s+(\S+)/) {
175 my $filename = $1;
176 $filename =~ s@^[^/]*/@@;
177 $filename =~ s@\n@@;
178 $lastfile = $filename;
179 push(@files, $filename);
180 } elsif (m/^\@\@ -(\d+),(\d+)/) {
181 if ($email_git_blame) {
182 push(@range, "$lastfile:$1:$2");
186 close(PATCH);
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);
195 my @email_to = ();
196 my @list_to = ();
197 my @scm = ();
198 my @web = ();
199 my @subsystem = ();
200 my @status = ();
202 # Find responsible parties
204 foreach my $file (@files) {
206 #Do not match excluded file patterns
208 my $exclude = 0;
209 foreach my $line (@typevalue) {
210 if ($line =~ m/^(\C):\s*(.*)/) {
211 my $type = $1;
212 my $value = $2;
213 if ($type eq 'X') {
214 if (file_match_pattern($file, $value)) {
215 $exclude = 1;
216 last;
222 if (!$exclude) {
223 my $tvi = 0;
224 my %hash;
225 foreach my $line (@typevalue) {
226 if ($line =~ m/^(\C):\s*(.*)/) {
227 my $type = $1;
228 my $value = $2;
229 if ($type eq 'F') {
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;
241 $tvi++;
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);
257 if ($email) {
258 foreach my $chief (@penguin_chief) {
259 if ($chief =~ m/^(.*):(.*)/) {
260 my $email_address;
261 if ($email_usename) {
262 $email_address = format_email($1, $2);
263 } else {
264 $email_address = $2;
266 if ($email_git_penguin_chiefs) {
267 push(@email_to, $email_address);
268 } else {
269 @email_to = grep(!/${email_address}/, @email_to);
275 if ($email || $email_list) {
276 my @to = ();
277 if ($email) {
278 @to = (@to, @email_to);
280 if ($email_list) {
281 @to = (@to, @list_to);
283 output(uniq(@to));
286 if ($scm) {
287 @scm = sort_and_uniq(@scm);
288 output(@scm);
291 if ($status) {
292 @status = sort_and_uniq(@status);
293 output(@status);
296 if ($subsystem) {
297 @subsystem = sort_and_uniq(@subsystem);
298 output(@subsystem);
301 if ($web) {
302 @web = sort_and_uniq(@web);
303 output(@web);
306 exit($exit);
308 sub file_match_pattern {
309 my ($file, $pattern) = @_;
310 if (substr($pattern, -1) eq "/") {
311 if ($file =~ m@^$pattern@) {
312 return 1;
314 } else {
315 if ($file =~ m@^$pattern@) {
316 my $s1 = ($file =~ tr@/@@);
317 my $s2 = ($pattern =~ tr@/@@);
318 if ($s1 == $s2) {
319 return 1;
323 return 0;
326 sub usage {
327 print <<EOT;
328 usage: $P [options] patchfile
329 $P [options] -f file|directory
330 version: $V
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
350 Output type options:
351 --separator [, ] => separator for multiple entries on 1 line
352 --multiline => print 1 entry per line
354 Other options:
355 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
356 --version => show version
357 --help => show this help information
359 Default options:
360 [--email --git --m --n --l --multiline --pattern-depth=0]
362 Notes:
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
369 is matched.
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 {
377 my ($lk_path) = @_;
379 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
380 $lk_path .= "/";
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")) {
398 return 1;
400 return 0;
403 sub format_email {
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}\>";
415 } else {
416 $formatted_email = "${name} \<${email}\>";
418 return $formatted_email;
421 sub add_categories {
422 my ($index) = @_;
424 $index = $index - 1;
425 while ($index >= 0) {
426 my $tv = $typevalue[$index];
427 if ($tv =~ m/^(\C):\s*(.*)/) {
428 my $ptype = $1;
429 my $pvalue = $2;
430 if ($ptype eq "L") {
431 my $list_address = $pvalue;
432 my $list_additional = "";
433 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
434 $list_address = $1;
435 $list_additional = $2;
437 if ($list_additional =~ m/subscribers-only/) {
438 if ($email_subscriber_list) {
439 push(@list_to, $list_address);
441 } else {
442 if ($email_list) {
443 push(@list_to, $list_address);
446 } elsif ($ptype eq "M") {
447 my $p_used = 0;
448 if ($index >= 0) {
449 my $tv = $typevalue[$index - 1];
450 if ($tv =~ m/^(\C):\s*(.*)/) {
451 if ($1 eq "P") {
452 if ($email_usename) {
453 push_email_address(format_email($2, $pvalue));
454 $p_used = 1;
459 if (!$p_used) {
460 push_email_addresses($pvalue);
462 } elsif ($ptype eq "T") {
463 push(@scm, $pvalue);
464 } elsif ($ptype eq "W") {
465 push(@web, $pvalue);
466 } elsif ($ptype eq "S") {
467 push(@status, $pvalue);
470 $index--;
471 } else {
472 push(@subsystem,$tv);
473 $index = -1;
478 sub push_email_address {
479 my ($email_address) = @_;
481 my $email_name = "";
483 if ($email_maintainer) {
484 if ($email_address =~ m/([^<]+)<(.*\@.*)>$/) {
485 $email_name = $1;
486 $email_address = $2;
487 if ($email_usename) {
488 push(@email_to, format_email($email_name, $email_address));
489 } else {
490 push(@email_to, $email_address);
492 } elsif ($email_address =~ m/<(.+)>/) {
493 $email_address = $1;
494 push(@email_to, $email_address);
495 } else {
496 push(@email_to, $email_address);
501 sub push_email_addresses {
502 my ($address) = @_;
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);
513 } else {
514 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
518 sub which {
519 my ($bin) = @_;
521 foreach my $path (split(/:/, $ENV{PATH})) {
522 if (-e "$path/$bin") {
523 return "$path/$bin";
527 return "";
530 sub recent_git_signoffs {
531 my ($file) = @_;
533 my $sign_offs = "";
534 my $cmd = "";
535 my $output = "";
536 my $count = 0;
537 my @lines = ();
538 my $total_sign_offs;
540 if (which("git") eq "") {
541 warn("$P: git not found. Add --nogit to options?\n");
542 return;
544 if (!(-d ".git")) {
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");
547 return;
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";
558 $output = `${cmd}`;
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;
567 } else {
568 die("$P: Unexpected git output: ${line}\n");
572 foreach my $line (@lines) {
573 if ($line =~ m/([0-9]+)\s+(.*)/) {
574 my $sign_offs = $1;
575 $line = $2;
576 $count++;
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) {
580 last;
583 push_email_address($line);
587 sub save_commits {
588 my ($cmd, @commits) = @_;
589 my $output;
590 my @lines = ();
592 $output = `${cmd}`;
594 @lines = split("\n", $output);
595 foreach my $line (@lines) {
596 if ($line =~ m/^(\w+) /) {
597 push (@commits, $1);
600 return @commits;
603 sub git_assign_blame {
604 my ($file) = @_;
606 my @lines = ();
607 my @commits = ();
608 my $cmd;
609 my $output;
610 my %hash;
611 my $total_sign_offs;
612 my $count;
614 if (@range) {
615 foreach my $file_range_diff (@range) {
616 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
617 my $diff_file = $1;
618 my $diff_start = $2;
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);
624 } else {
625 if (-f $file) {
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\":\"";
641 $output = `${cmd}`;
642 $output =~ s/^\s*//gm;
643 @lines = split("\n", $output);
644 $hash{$_}++ for @lines;
645 $total_sign_offs += @lines;
648 $count = 0;
649 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
650 my $sign_offs = $hash{$line};
651 $count++;
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);
659 sub uniq {
660 my @parms = @_;
662 my %saw;
663 @parms = grep(!$saw{$_}++, @parms);
664 return @parms;
667 sub sort_and_uniq {
668 my @parms = @_;
670 my %saw;
671 @parms = sort @parms;
672 @parms = grep(!$saw{$_}++, @parms);
673 return @parms;
676 sub output {
677 my @parms = @_;
679 if ($output_multiline) {
680 foreach my $line (@parms) {
681 print("${line}\n");
683 } else {
684 print(join($output_separator, @parms));
685 print("\n");
689 my $rfc822re;
691 sub make_rfc822re {
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 {
728 my $s = shift;
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) {}
736 return $s;
739 # valid: returns true if the parameter is an RFC822 valid address
741 sub rfc822_valid ($) {
742 my $s = rfc822_strip_comments(shift);
744 if (!$rfc822re) {
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
759 # a valid list.
761 sub rfc822_validlist ($) {
762 my $s = rfc822_strip_comments(shift);
764 if (!$rfc822re) {
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
770 my @r;
771 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
772 $s =~ m/^$rfc822_char*$/) {
773 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
774 push @r, $1;
776 return wantarray ? (scalar(@r), @r) : 1;
778 else {
779 return wantarray ? () : 0;