scripts/get_maintainer.pl: optionally ignore non-maintainer signatures
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / scripts / get_maintainer.pl
blobf66018d1d5eea6a0674ffd5ea680b2b171acf1f5
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_maintainer.pl [OPTIONS] <patch>
9 # perl scripts/get_maintainer.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.23';
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_penguin_chiefs = 0;
27 my $email_git = 1;
28 my $email_git_all_signature_types = 1;
29 my $email_git_blame = 0;
30 my $email_git_min_signatures = 1;
31 my $email_git_max_maintainers = 5;
32 my $email_git_min_percent = 5;
33 my $email_git_since = "1-year-ago";
34 my $email_hg_since = "-365";
35 my $email_remove_duplicates = 1;
36 my $output_multiline = 1;
37 my $output_separator = ", ";
38 my $output_roles = 0;
39 my $output_rolestats = 0;
40 my $scm = 0;
41 my $web = 0;
42 my $subsystem = 0;
43 my $status = 0;
44 my $keywords = 1;
45 my $sections = 0;
46 my $file_emails = 0;
47 my $from_filename = 0;
48 my $pattern_depth = 0;
49 my $version = 0;
50 my $help = 0;
52 my $exit = 0;
54 my @penguin_chief = ();
55 push(@penguin_chief, "Linus Torvalds:torvalds\@linux-foundation.org");
56 #Andrew wants in on most everything - 2009/01/14
57 #push(@penguin_chief, "Andrew Morton:akpm\@linux-foundation.org");
59 my @penguin_chief_names = ();
60 foreach my $chief (@penguin_chief) {
61 if ($chief =~ m/^(.*):(.*)/) {
62 my $chief_name = $1;
63 my $chief_addr = $2;
64 push(@penguin_chief_names, $chief_name);
67 my $penguin_chiefs = "\(" . join("|", @penguin_chief_names) . "\)";
69 # Signature types of people who are either
70 # a) responsible for the code in question, or
71 # b) familiar enough with it to give relevant feedback
72 my @signature_tags = ();
73 push(@signature_tags, "Signed-off-by:");
74 push(@signature_tags, "Reviewed-by:");
75 push(@signature_tags, "Acked-by:");
76 my $signaturePattern = "\(" . join("|", @signature_tags) . "\)";
78 # rfc822 email address - preloaded methods go here.
79 my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
80 my $rfc822_char = '[\\000-\\377]';
82 # VCS command support: class-like functions and strings
84 my %VCS_cmds;
86 my %VCS_cmds_git = (
87 "execute_cmd" => \&git_execute_cmd,
88 "available" => '(which("git") ne "") && (-d ".git")',
89 "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file",
90 "find_commit_signers_cmd" => "git log --no-color -1 \$commit",
91 "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
92 "blame_file_cmd" => "git blame -l \$file",
93 "commit_pattern" => "^commit [0-9a-f]{40,40}",
94 "blame_commit_pattern" => "^([0-9a-f]+) "
97 my %VCS_cmds_hg = (
98 "execute_cmd" => \&hg_execute_cmd,
99 "available" => '(which("hg") ne "") && (-d ".hg")',
100 "find_signers_cmd" =>
101 "hg log --date=\$email_hg_since" .
102 " --template='commit {node}\\n{desc}\\n' -- \$file",
103 "find_commit_signers_cmd" => "hg log --template='{desc}\\n' -r \$commit",
104 "blame_range_cmd" => "", # not supported
105 "blame_file_cmd" => "hg blame -c \$file",
106 "commit_pattern" => "^commit [0-9a-f]{40,40}",
107 "blame_commit_pattern" => "^([0-9a-f]+):"
110 if (!GetOptions(
111 'email!' => \$email,
112 'git!' => \$email_git,
113 'git-all-signature-types!' => \$email_git_all_signature_types,
114 'git-blame!' => \$email_git_blame,
115 'git-chief-penguins!' => \$email_git_penguin_chiefs,
116 'git-min-signatures=i' => \$email_git_min_signatures,
117 'git-max-maintainers=i' => \$email_git_max_maintainers,
118 'git-min-percent=i' => \$email_git_min_percent,
119 'git-since=s' => \$email_git_since,
120 'hg-since=s' => \$email_hg_since,
121 'remove-duplicates!' => \$email_remove_duplicates,
122 'm!' => \$email_maintainer,
123 'n!' => \$email_usename,
124 'l!' => \$email_list,
125 's!' => \$email_subscriber_list,
126 'multiline!' => \$output_multiline,
127 'roles!' => \$output_roles,
128 'rolestats!' => \$output_rolestats,
129 'separator=s' => \$output_separator,
130 'subsystem!' => \$subsystem,
131 'status!' => \$status,
132 'scm!' => \$scm,
133 'web!' => \$web,
134 'pattern-depth=i' => \$pattern_depth,
135 'k|keywords!' => \$keywords,
136 'sections!' => \$sections,
137 'fe|file-emails!' => \$file_emails,
138 'f|file' => \$from_filename,
139 'v|version' => \$version,
140 'h|help|usage' => \$help,
141 )) {
142 die "$P: invalid argument - use --help if necessary\n";
145 if ($help != 0) {
146 usage();
147 exit 0;
150 if ($version != 0) {
151 print("${P} ${V}\n");
152 exit 0;
155 if (-t STDIN && !@ARGV) {
156 # We're talking to a terminal, but have no command line arguments.
157 die "$P: missing patchfile or -f file - use --help if necessary\n";
160 if ($output_separator ne ", ") {
161 $output_multiline = 0;
164 if ($output_rolestats) {
165 $output_roles = 1;
168 if ($sections) {
169 $email = 0;
170 $email_list = 0;
171 $scm = 0;
172 $status = 0;
173 $subsystem = 0;
174 $web = 0;
175 $keywords = 0;
176 } else {
177 my $selections = $email + $scm + $status + $subsystem + $web;
178 if ($selections == 0) {
179 die "$P: Missing required option: email, scm, status, subsystem or web\n";
183 if ($email &&
184 ($email_maintainer + $email_list + $email_subscriber_list +
185 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
186 die "$P: Please select at least 1 email option\n";
189 if (!top_of_kernel_tree($lk_path)) {
190 die "$P: The current directory does not appear to be "
191 . "a linux kernel source tree.\n";
194 if ($email_git_all_signature_types) {
195 $signaturePattern = "(.+?)[Bb][Yy]:";
198 ## Read MAINTAINERS for type/value pairs
200 my @typevalue = ();
201 my %keyword_hash;
203 open (my $maint, '<', "${lk_path}MAINTAINERS")
204 or die "$P: Can't open MAINTAINERS: $!\n";
205 while (<$maint>) {
206 my $line = $_;
208 if ($line =~ m/^(\C):\s*(.*)/) {
209 my $type = $1;
210 my $value = $2;
212 ##Filename pattern matching
213 if ($type eq "F" || $type eq "X") {
214 $value =~ s@\.@\\\.@g; ##Convert . to \.
215 $value =~ s/\*/\.\*/g; ##Convert * to .*
216 $value =~ s/\?/\./g; ##Convert ? to .
217 ##if pattern is a directory and it lacks a trailing slash, add one
218 if ((-d $value)) {
219 $value =~ s@([^/])$@$1/@;
221 } elsif ($type eq "K") {
222 $keyword_hash{@typevalue} = $value;
224 push(@typevalue, "$type:$value");
225 } elsif (!/^(\s)*$/) {
226 $line =~ s/\n$//g;
227 push(@typevalue, $line);
230 close($maint);
232 my %mailmap;
234 if ($email_remove_duplicates) {
235 open(my $mailmap, '<', "${lk_path}.mailmap")
236 or warn "$P: Can't open .mailmap: $!\n";
237 while (<$mailmap>) {
238 my $line = $_;
240 next if ($line =~ m/^\s*#/);
241 next if ($line =~ m/^\s*$/);
243 my ($name, $address) = parse_email($line);
244 $line = format_email($name, $address, $email_usename);
246 next if ($line =~ m/^\s*$/);
248 if (exists($mailmap{$name})) {
249 my $obj = $mailmap{$name};
250 push(@$obj, $address);
251 } else {
252 my @arr = ($address);
253 $mailmap{$name} = \@arr;
256 close($mailmap);
259 ## use the filenames on the command line or find the filenames in the patchfiles
261 my @files = ();
262 my @range = ();
263 my @keyword_tvi = ();
264 my @file_emails = ();
266 if (!@ARGV) {
267 push(@ARGV, "&STDIN");
270 foreach my $file (@ARGV) {
271 if ($file ne "&STDIN") {
272 ##if $file is a directory and it lacks a trailing slash, add one
273 if ((-d $file)) {
274 $file =~ s@([^/])$@$1/@;
275 } elsif (!(-f $file)) {
276 die "$P: file '${file}' not found\n";
279 if ($from_filename) {
280 push(@files, $file);
281 if (-f $file && ($keywords || $file_emails)) {
282 open(my $f, '<', $file)
283 or die "$P: Can't open $file: $!\n";
284 my $text = do { local($/) ; <$f> };
285 close($f);
286 if ($keywords) {
287 foreach my $line (keys %keyword_hash) {
288 if ($text =~ m/$keyword_hash{$line}/x) {
289 push(@keyword_tvi, $line);
293 if ($file_emails) {
294 my @poss_addr = $text =~ m$[A-Za--ÿ\"\' \,\.\+-]*\s*[\,]*\s*[\(\<\{]{0,1}[A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+\.[A-Za-z0-9]+[\)\>\}]{0,1}$g;
295 push(@file_emails, clean_file_emails(@poss_addr));
298 } else {
299 my $file_cnt = @files;
300 my $lastfile;
302 open(my $patch, "< $file")
303 or die "$P: Can't open $file: $!\n";
304 while (<$patch>) {
305 my $patch_line = $_;
306 if (m/^\+\+\+\s+(\S+)/) {
307 my $filename = $1;
308 $filename =~ s@^[^/]*/@@;
309 $filename =~ s@\n@@;
310 $lastfile = $filename;
311 push(@files, $filename);
312 } elsif (m/^\@\@ -(\d+),(\d+)/) {
313 if ($email_git_blame) {
314 push(@range, "$lastfile:$1:$2");
316 } elsif ($keywords) {
317 foreach my $line (keys %keyword_hash) {
318 if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
319 push(@keyword_tvi, $line);
324 close($patch);
326 if ($file_cnt == @files) {
327 warn "$P: file '${file}' doesn't appear to be a patch. "
328 . "Add -f to options?\n";
330 @files = sort_and_uniq(@files);
334 @file_emails = uniq(@file_emails);
336 my @email_to = ();
337 my @list_to = ();
338 my @scm = ();
339 my @web = ();
340 my @subsystem = ();
341 my @status = ();
343 # Find responsible parties
345 foreach my $file (@files) {
347 my %hash;
348 my $tvi = find_first_section();
349 while ($tvi < @typevalue) {
350 my $start = find_starting_index($tvi);
351 my $end = find_ending_index($tvi);
352 my $exclude = 0;
353 my $i;
355 #Do not match excluded file patterns
357 for ($i = $start; $i < $end; $i++) {
358 my $line = $typevalue[$i];
359 if ($line =~ m/^(\C):\s*(.*)/) {
360 my $type = $1;
361 my $value = $2;
362 if ($type eq 'X') {
363 if (file_match_pattern($file, $value)) {
364 $exclude = 1;
365 last;
371 if (!$exclude) {
372 for ($i = $start; $i < $end; $i++) {
373 my $line = $typevalue[$i];
374 if ($line =~ m/^(\C):\s*(.*)/) {
375 my $type = $1;
376 my $value = $2;
377 if ($type eq 'F') {
378 if (file_match_pattern($file, $value)) {
379 my $value_pd = ($value =~ tr@/@@);
380 my $file_pd = ($file =~ tr@/@@);
381 $value_pd++ if (substr($value,-1,1) ne "/");
382 if ($pattern_depth == 0 ||
383 (($file_pd - $value_pd) < $pattern_depth)) {
384 $hash{$tvi} = $value_pd;
392 $tvi = $end + 1;
395 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
396 add_categories($line);
397 if ($sections) {
398 my $i;
399 my $start = find_starting_index($line);
400 my $end = find_ending_index($line);
401 for ($i = $start; $i < $end; $i++) {
402 my $line = $typevalue[$i];
403 if ($line =~ /^[FX]:/) { ##Restore file patterns
404 $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
405 $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
406 $line =~ s/\\\./\./g; ##Convert \. to .
407 $line =~ s/\.\*/\*/g; ##Convert .* to *
409 $line =~ s/^([A-Z]):/$1:\t/g;
410 print("$line\n");
412 print("\n");
416 if ($email && $email_git) {
417 vcs_file_signoffs($file);
420 if ($email && $email_git_blame) {
421 vcs_file_blame($file);
425 if ($keywords) {
426 @keyword_tvi = sort_and_uniq(@keyword_tvi);
427 foreach my $line (@keyword_tvi) {
428 add_categories($line);
432 if ($email) {
433 foreach my $chief (@penguin_chief) {
434 if ($chief =~ m/^(.*):(.*)/) {
435 my $email_address;
437 $email_address = format_email($1, $2, $email_usename);
438 if ($email_git_penguin_chiefs) {
439 push(@email_to, [$email_address, 'chief penguin']);
440 } else {
441 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
446 foreach my $email (@file_emails) {
447 my ($name, $address) = parse_email($email);
449 my $tmp_email = format_email($name, $address, $email_usename);
450 push_email_address($tmp_email, '');
451 add_role($tmp_email, 'in file');
455 if ($email || $email_list) {
456 my @to = ();
457 if ($email) {
458 @to = (@to, @email_to);
460 if ($email_list) {
461 @to = (@to, @list_to);
463 output(merge_email(@to));
466 if ($scm) {
467 @scm = uniq(@scm);
468 output(@scm);
471 if ($status) {
472 @status = uniq(@status);
473 output(@status);
476 if ($subsystem) {
477 @subsystem = uniq(@subsystem);
478 output(@subsystem);
481 if ($web) {
482 @web = uniq(@web);
483 output(@web);
486 exit($exit);
488 sub file_match_pattern {
489 my ($file, $pattern) = @_;
490 if (substr($pattern, -1) eq "/") {
491 if ($file =~ m@^$pattern@) {
492 return 1;
494 } else {
495 if ($file =~ m@^$pattern@) {
496 my $s1 = ($file =~ tr@/@@);
497 my $s2 = ($pattern =~ tr@/@@);
498 if ($s1 == $s2) {
499 return 1;
503 return 0;
506 sub usage {
507 print <<EOT;
508 usage: $P [options] patchfile
509 $P [options] -f file|directory
510 version: $V
512 MAINTAINER field selection options:
513 --email => print email address(es) if any
514 --git => include recent git \*-by: signers
515 --git-all-signature-types => include signers regardless of signature type
516 or use only ${signaturePattern} signers (default: $email_git_all_signature_types)
517 --git-chief-penguins => include ${penguin_chiefs}
518 --git-min-signatures => number of signatures required (default: $email_git_min_signatures)
519 --git-max-maintainers => maximum maintainers to add (default: $email_git_max_maintainers)
520 --git-min-percent => minimum percentage of commits required (default: $email_git_min_percent)
521 --git-blame => use git blame to find modified commits for patch or file
522 --git-since => git history to use (default: $email_git_since)
523 --hg-since => hg history to use (default: $email_hg_since)
524 --m => include maintainer(s) if any
525 --n => include name 'Full Name <addr\@domain.tld>'
526 --l => include list(s) if any
527 --s => include subscriber only list(s) if any
528 --remove-duplicates => minimize duplicate email names/addresses
529 --roles => show roles (status:subsystem, git-signer, list, etc...)
530 --rolestats => show roles and statistics (commits/total_commits, %)
531 --file-emails => add email addresses found in -f file (default: 0 (off))
532 --scm => print SCM tree(s) if any
533 --status => print status if any
534 --subsystem => print subsystem name if any
535 --web => print website(s) if any
537 Output type options:
538 --separator [, ] => separator for multiple entries on 1 line
539 using --separator also sets --nomultiline if --separator is not [, ]
540 --multiline => print 1 entry per line
542 Other options:
543 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
544 --keywords => scan patch for keywords (default: 1 (on))
545 --sections => print the entire subsystem sections with pattern matches
546 --version => show version
547 --help => show this help information
549 Default options:
550 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
552 Notes:
553 Using "-f directory" may give unexpected results:
554 Used with "--git", git signators for _all_ files in and below
555 directory are examined as git recurses directories.
556 Any specified X: (exclude) pattern matches are _not_ ignored.
557 Used with "--nogit", directory is used as a pattern match,
558 no individual file within the directory or subdirectory
559 is matched.
560 Used with "--git-blame", does not iterate all files in directory
561 Using "--git-blame" is slow and may add old committers and authors
562 that are no longer active maintainers to the output.
563 Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
564 other automated tools that expect only ["name"] <email address>
565 may not work because of additional output after <email address>.
566 Using "--rolestats" and "--git-blame" shows the #/total=% commits,
567 not the percentage of the entire file authored. # of commits is
568 not a good measure of amount of code authored. 1 major commit may
569 contain a thousand lines, 5 trivial commits may modify a single line.
570 If git is not installed, but mercurial (hg) is installed and an .hg
571 repository exists, the following options apply to mercurial:
572 --git,
573 --git-min-signatures, --git-max-maintainers, --git-min-percent, and
574 --git-blame
575 Use --hg-since not --git-since to control date selection
579 sub top_of_kernel_tree {
580 my ($lk_path) = @_;
582 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
583 $lk_path .= "/";
585 if ( (-f "${lk_path}COPYING")
586 && (-f "${lk_path}CREDITS")
587 && (-f "${lk_path}Kbuild")
588 && (-f "${lk_path}MAINTAINERS")
589 && (-f "${lk_path}Makefile")
590 && (-f "${lk_path}README")
591 && (-d "${lk_path}Documentation")
592 && (-d "${lk_path}arch")
593 && (-d "${lk_path}include")
594 && (-d "${lk_path}drivers")
595 && (-d "${lk_path}fs")
596 && (-d "${lk_path}init")
597 && (-d "${lk_path}ipc")
598 && (-d "${lk_path}kernel")
599 && (-d "${lk_path}lib")
600 && (-d "${lk_path}scripts")) {
601 return 1;
603 return 0;
606 sub parse_email {
607 my ($formatted_email) = @_;
609 my $name = "";
610 my $address = "";
612 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
613 $name = $1;
614 $address = $2;
615 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
616 $address = $1;
617 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
618 $address = $1;
621 $name =~ s/^\s+|\s+$//g;
622 $name =~ s/^\"|\"$//g;
623 $address =~ s/^\s+|\s+$//g;
625 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
626 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
627 $name = "\"$name\"";
630 return ($name, $address);
633 sub format_email {
634 my ($name, $address, $usename) = @_;
636 my $formatted_email;
638 $name =~ s/^\s+|\s+$//g;
639 $name =~ s/^\"|\"$//g;
640 $address =~ s/^\s+|\s+$//g;
642 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
643 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
644 $name = "\"$name\"";
647 if ($usename) {
648 if ("$name" eq "") {
649 $formatted_email = "$address";
650 } else {
651 $formatted_email = "$name <$address>";
653 } else {
654 $formatted_email = $address;
657 return $formatted_email;
660 sub find_first_section {
661 my $index = 0;
663 while ($index < @typevalue) {
664 my $tv = $typevalue[$index];
665 if (($tv =~ m/^(\C):\s*(.*)/)) {
666 last;
668 $index++;
671 return $index;
674 sub find_starting_index {
675 my ($index) = @_;
677 while ($index > 0) {
678 my $tv = $typevalue[$index];
679 if (!($tv =~ m/^(\C):\s*(.*)/)) {
680 last;
682 $index--;
685 return $index;
688 sub find_ending_index {
689 my ($index) = @_;
691 while ($index < @typevalue) {
692 my $tv = $typevalue[$index];
693 if (!($tv =~ m/^(\C):\s*(.*)/)) {
694 last;
696 $index++;
699 return $index;
702 sub get_maintainer_role {
703 my ($index) = @_;
705 my $i;
706 my $start = find_starting_index($index);
707 my $end = find_ending_index($index);
709 my $role;
710 my $subsystem = $typevalue[$start];
711 if (length($subsystem) > 20) {
712 $subsystem = substr($subsystem, 0, 17);
713 $subsystem =~ s/\s*$//;
714 $subsystem = $subsystem . "...";
717 for ($i = $start + 1; $i < $end; $i++) {
718 my $tv = $typevalue[$i];
719 if ($tv =~ m/^(\C):\s*(.*)/) {
720 my $ptype = $1;
721 my $pvalue = $2;
722 if ($ptype eq "S") {
723 $role = $pvalue;
728 $role = lc($role);
729 if ($role eq "supported") {
730 $role = "supporter";
731 } elsif ($role eq "maintained") {
732 $role = "maintainer";
733 } elsif ($role eq "odd fixes") {
734 $role = "odd fixer";
735 } elsif ($role eq "orphan") {
736 $role = "orphan minder";
737 } elsif ($role eq "obsolete") {
738 $role = "obsolete minder";
739 } elsif ($role eq "buried alive in reporters") {
740 $role = "chief penguin";
743 return $role . ":" . $subsystem;
746 sub get_list_role {
747 my ($index) = @_;
749 my $i;
750 my $start = find_starting_index($index);
751 my $end = find_ending_index($index);
753 my $subsystem = $typevalue[$start];
754 if (length($subsystem) > 20) {
755 $subsystem = substr($subsystem, 0, 17);
756 $subsystem =~ s/\s*$//;
757 $subsystem = $subsystem . "...";
760 if ($subsystem eq "THE REST") {
761 $subsystem = "";
764 return $subsystem;
767 sub add_categories {
768 my ($index) = @_;
770 my $i;
771 my $start = find_starting_index($index);
772 my $end = find_ending_index($index);
774 push(@subsystem, $typevalue[$start]);
776 for ($i = $start + 1; $i < $end; $i++) {
777 my $tv = $typevalue[$i];
778 if ($tv =~ m/^(\C):\s*(.*)/) {
779 my $ptype = $1;
780 my $pvalue = $2;
781 if ($ptype eq "L") {
782 my $list_address = $pvalue;
783 my $list_additional = "";
784 my $list_role = get_list_role($i);
786 if ($list_role ne "") {
787 $list_role = ":" . $list_role;
789 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
790 $list_address = $1;
791 $list_additional = $2;
793 if ($list_additional =~ m/subscribers-only/) {
794 if ($email_subscriber_list) {
795 push(@list_to, [$list_address, "subscriber list${list_role}"]);
797 } else {
798 if ($email_list) {
799 push(@list_to, [$list_address, "open list${list_role}"]);
802 } elsif ($ptype eq "M") {
803 my ($name, $address) = parse_email($pvalue);
804 if ($name eq "") {
805 if ($i > 0) {
806 my $tv = $typevalue[$i - 1];
807 if ($tv =~ m/^(\C):\s*(.*)/) {
808 if ($1 eq "P") {
809 $name = $2;
810 $pvalue = format_email($name, $address, $email_usename);
815 if ($email_maintainer) {
816 my $role = get_maintainer_role($i);
817 push_email_addresses($pvalue, $role);
819 } elsif ($ptype eq "T") {
820 push(@scm, $pvalue);
821 } elsif ($ptype eq "W") {
822 push(@web, $pvalue);
823 } elsif ($ptype eq "S") {
824 push(@status, $pvalue);
830 my %email_hash_name;
831 my %email_hash_address;
833 sub email_inuse {
834 my ($name, $address) = @_;
836 return 1 if (($name eq "") && ($address eq ""));
837 return 1 if (($name ne "") && exists($email_hash_name{$name}));
838 return 1 if (($address ne "") && exists($email_hash_address{$address}));
840 return 0;
843 sub push_email_address {
844 my ($line, $role) = @_;
846 my ($name, $address) = parse_email($line);
848 if ($address eq "") {
849 return 0;
852 if (!$email_remove_duplicates) {
853 push(@email_to, [format_email($name, $address, $email_usename), $role]);
854 } elsif (!email_inuse($name, $address)) {
855 push(@email_to, [format_email($name, $address, $email_usename), $role]);
856 $email_hash_name{$name}++;
857 $email_hash_address{$address}++;
860 return 1;
863 sub push_email_addresses {
864 my ($address, $role) = @_;
866 my @address_list = ();
868 if (rfc822_valid($address)) {
869 push_email_address($address, $role);
870 } elsif (@address_list = rfc822_validlist($address)) {
871 my $array_count = shift(@address_list);
872 while (my $entry = shift(@address_list)) {
873 push_email_address($entry, $role);
875 } else {
876 if (!push_email_address($address, $role)) {
877 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
882 sub add_role {
883 my ($line, $role) = @_;
885 my ($name, $address) = parse_email($line);
886 my $email = format_email($name, $address, $email_usename);
888 foreach my $entry (@email_to) {
889 if ($email_remove_duplicates) {
890 my ($entry_name, $entry_address) = parse_email($entry->[0]);
891 if (($name eq $entry_name || $address eq $entry_address)
892 && ($role eq "" || !($entry->[1] =~ m/$role/))
894 if ($entry->[1] eq "") {
895 $entry->[1] = "$role";
896 } else {
897 $entry->[1] = "$entry->[1],$role";
900 } else {
901 if ($email eq $entry->[0]
902 && ($role eq "" || !($entry->[1] =~ m/$role/))
904 if ($entry->[1] eq "") {
905 $entry->[1] = "$role";
906 } else {
907 $entry->[1] = "$entry->[1],$role";
914 sub which {
915 my ($bin) = @_;
917 foreach my $path (split(/:/, $ENV{PATH})) {
918 if (-e "$path/$bin") {
919 return "$path/$bin";
923 return "";
926 sub mailmap {
927 my (@lines) = @_;
928 my %hash;
930 foreach my $line (@lines) {
931 my ($name, $address) = parse_email($line);
932 if (!exists($hash{$name})) {
933 $hash{$name} = $address;
934 } elsif ($address ne $hash{$name}) {
935 $address = $hash{$name};
936 $line = format_email($name, $address, $email_usename);
938 if (exists($mailmap{$name})) {
939 my $obj = $mailmap{$name};
940 foreach my $map_address (@$obj) {
941 if (($map_address eq $address) &&
942 ($map_address ne $hash{$name})) {
943 $line = format_email($name, $hash{$name}, $email_usename);
949 return @lines;
952 sub git_execute_cmd {
953 my ($cmd) = @_;
954 my @lines = ();
956 my $output = `$cmd`;
957 $output =~ s/^\s*//gm;
958 @lines = split("\n", $output);
960 return @lines;
963 sub hg_execute_cmd {
964 my ($cmd) = @_;
965 my @lines = ();
967 my $output = `$cmd`;
968 @lines = split("\n", $output);
970 return @lines;
973 sub vcs_find_signers {
974 my ($cmd) = @_;
975 my @lines = ();
976 my $commits;
978 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
980 my $pattern = $VCS_cmds{"commit_pattern"};
982 $commits = grep(/$pattern/, @lines); # of commits
984 @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines);
985 if (!$email_git_penguin_chiefs) {
986 @lines = grep(!/${penguin_chiefs}/i, @lines);
988 # cut -f2- -d":"
989 s/.*:\s*(.+)\s*/$1/ for (@lines);
991 ## Reformat email addresses (with names) to avoid badly written signatures
993 foreach my $line (@lines) {
994 my ($name, $address) = parse_email($line);
995 $line = format_email($name, $address, 1);
998 return ($commits, @lines);
1001 sub vcs_save_commits {
1002 my ($cmd) = @_;
1003 my @lines = ();
1004 my @commits = ();
1006 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1008 foreach my $line (@lines) {
1009 if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
1010 push(@commits, $1);
1014 return @commits;
1017 sub vcs_blame {
1018 my ($file) = @_;
1019 my $cmd;
1020 my @commits = ();
1022 return @commits if (!(-f $file));
1024 if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
1025 my @all_commits = ();
1027 $cmd = $VCS_cmds{"blame_file_cmd"};
1028 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1029 @all_commits = vcs_save_commits($cmd);
1031 foreach my $file_range_diff (@range) {
1032 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1033 my $diff_file = $1;
1034 my $diff_start = $2;
1035 my $diff_length = $3;
1036 next if ("$file" ne "$diff_file");
1037 for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
1038 push(@commits, $all_commits[$i]);
1041 } elsif (@range) {
1042 foreach my $file_range_diff (@range) {
1043 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1044 my $diff_file = $1;
1045 my $diff_start = $2;
1046 my $diff_length = $3;
1047 next if ("$file" ne "$diff_file");
1048 $cmd = $VCS_cmds{"blame_range_cmd"};
1049 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1050 push(@commits, vcs_save_commits($cmd));
1052 } else {
1053 $cmd = $VCS_cmds{"blame_file_cmd"};
1054 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1055 @commits = vcs_save_commits($cmd);
1058 return @commits;
1061 my $printed_novcs = 0;
1062 sub vcs_exists {
1063 %VCS_cmds = %VCS_cmds_git;
1064 return 1 if eval $VCS_cmds{"available"};
1065 %VCS_cmds = %VCS_cmds_hg;
1066 return 1 if eval $VCS_cmds{"available"};
1067 %VCS_cmds = ();
1068 if (!$printed_novcs) {
1069 warn("$P: No supported VCS found. Add --nogit to options?\n");
1070 warn("Using a git repository produces better results.\n");
1071 warn("Try Linus Torvalds' latest git repository using:\n");
1072 warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
1073 $printed_novcs = 1;
1075 return 0;
1078 sub vcs_assign {
1079 my ($role, $divisor, @lines) = @_;
1081 my %hash;
1082 my $count = 0;
1084 return if (@lines <= 0);
1086 if ($divisor <= 0) {
1087 warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
1088 $divisor = 1;
1091 if ($email_remove_duplicates) {
1092 @lines = mailmap(@lines);
1095 @lines = sort(@lines);
1097 # uniq -c
1098 $hash{$_}++ for @lines;
1100 # sort -rn
1101 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
1102 my $sign_offs = $hash{$line};
1103 my $percent = $sign_offs * 100 / $divisor;
1105 $percent = 100 if ($percent > 100);
1106 $count++;
1107 last if ($sign_offs < $email_git_min_signatures ||
1108 $count > $email_git_max_maintainers ||
1109 $percent < $email_git_min_percent);
1110 push_email_address($line, '');
1111 if ($output_rolestats) {
1112 my $fmt_percent = sprintf("%.0f", $percent);
1113 add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
1114 } else {
1115 add_role($line, $role);
1120 sub vcs_file_signoffs {
1121 my ($file) = @_;
1123 my @signers = ();
1124 my $commits;
1126 return if (!vcs_exists());
1128 my $cmd = $VCS_cmds{"find_signers_cmd"};
1129 $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
1131 ($commits, @signers) = vcs_find_signers($cmd);
1132 vcs_assign("commit_signer", $commits, @signers);
1135 sub vcs_file_blame {
1136 my ($file) = @_;
1138 my @signers = ();
1139 my @commits = ();
1140 my $total_commits;
1142 return if (!vcs_exists());
1144 @commits = vcs_blame($file);
1145 @commits = uniq(@commits);
1146 $total_commits = @commits;
1148 foreach my $commit (@commits) {
1149 my $commit_count;
1150 my @commit_signers = ();
1152 my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1153 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1155 ($commit_count, @commit_signers) = vcs_find_signers($cmd);
1156 push(@signers, @commit_signers);
1159 if ($from_filename) {
1160 vcs_assign("commits", $total_commits, @signers);
1161 } else {
1162 vcs_assign("modified commits", $total_commits, @signers);
1166 sub uniq {
1167 my (@parms) = @_;
1169 my %saw;
1170 @parms = grep(!$saw{$_}++, @parms);
1171 return @parms;
1174 sub sort_and_uniq {
1175 my (@parms) = @_;
1177 my %saw;
1178 @parms = sort @parms;
1179 @parms = grep(!$saw{$_}++, @parms);
1180 return @parms;
1183 sub clean_file_emails {
1184 my (@file_emails) = @_;
1185 my @fmt_emails = ();
1187 foreach my $email (@file_emails) {
1188 $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
1189 my ($name, $address) = parse_email($email);
1190 if ($name eq '"[,\.]"') {
1191 $name = "";
1194 my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
1195 if (@nw > 2) {
1196 my $first = $nw[@nw - 3];
1197 my $middle = $nw[@nw - 2];
1198 my $last = $nw[@nw - 1];
1200 if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
1201 (length($first) == 2 && substr($first, -1) eq ".")) ||
1202 (length($middle) == 1 ||
1203 (length($middle) == 2 && substr($middle, -1) eq "."))) {
1204 $name = "$first $middle $last";
1205 } else {
1206 $name = "$middle $last";
1210 if (substr($name, -1) =~ /[,\.]/) {
1211 $name = substr($name, 0, length($name) - 1);
1212 } elsif (substr($name, -2) =~ /[,\.]"/) {
1213 $name = substr($name, 0, length($name) - 2) . '"';
1216 if (substr($name, 0, 1) =~ /[,\.]/) {
1217 $name = substr($name, 1, length($name) - 1);
1218 } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
1219 $name = '"' . substr($name, 2, length($name) - 2);
1222 my $fmt_email = format_email($name, $address, $email_usename);
1223 push(@fmt_emails, $fmt_email);
1225 return @fmt_emails;
1228 sub merge_email {
1229 my @lines;
1230 my %saw;
1232 for (@_) {
1233 my ($address, $role) = @$_;
1234 if (!$saw{$address}) {
1235 if ($output_roles) {
1236 push(@lines, "$address ($role)");
1237 } else {
1238 push(@lines, $address);
1240 $saw{$address} = 1;
1244 return @lines;
1247 sub output {
1248 my (@parms) = @_;
1250 if ($output_multiline) {
1251 foreach my $line (@parms) {
1252 print("${line}\n");
1254 } else {
1255 print(join($output_separator, @parms));
1256 print("\n");
1260 my $rfc822re;
1262 sub make_rfc822re {
1263 # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
1264 # comment. We must allow for rfc822_lwsp (or comments) after each of these.
1265 # This regexp will only work on addresses which have had comments stripped
1266 # and replaced with rfc822_lwsp.
1268 my $specials = '()<>@,;:\\\\".\\[\\]';
1269 my $controls = '\\000-\\037\\177';
1271 my $dtext = "[^\\[\\]\\r\\\\]";
1272 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1274 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1276 # Use zero-width assertion to spot the limit of an atom. A simple
1277 # $rfc822_lwsp* causes the regexp engine to hang occasionally.
1278 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
1279 my $word = "(?:$atom|$quoted_string)";
1280 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
1282 my $sub_domain = "(?:$atom|$domain_literal)";
1283 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1285 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1287 my $phrase = "$word*";
1288 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
1289 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
1290 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
1292 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1293 my $address = "(?:$mailbox|$group)";
1295 return "$rfc822_lwsp*$address";
1298 sub rfc822_strip_comments {
1299 my $s = shift;
1300 # Recursively remove comments, and replace with a single space. The simpler
1301 # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
1302 # chars in atoms, for example.
1304 while ($s =~ s/^((?:[^"\\]|\\.)*
1305 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1306 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1307 return $s;
1310 # valid: returns true if the parameter is an RFC822 valid address
1312 sub rfc822_valid {
1313 my $s = rfc822_strip_comments(shift);
1315 if (!$rfc822re) {
1316 $rfc822re = make_rfc822re();
1319 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1322 # validlist: In scalar context, returns true if the parameter is an RFC822
1323 # valid list of addresses.
1325 # In list context, returns an empty list on failure (an invalid
1326 # address was found); otherwise a list whose first element is the
1327 # number of addresses found and whose remaining elements are the
1328 # addresses. This is needed to disambiguate failure (invalid)
1329 # from success with no addresses found, because an empty string is
1330 # a valid list.
1332 sub rfc822_validlist {
1333 my $s = rfc822_strip_comments(shift);
1335 if (!$rfc822re) {
1336 $rfc822re = make_rfc822re();
1338 # * null list items are valid according to the RFC
1339 # * the '1' business is to aid in distinguishing failure from no results
1341 my @r;
1342 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
1343 $s =~ m/^$rfc822_char*$/) {
1344 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
1345 push(@r, $1);
1347 return wantarray ? (scalar(@r), @r) : 1;
1349 return wantarray ? () : 0;