f66018d1d5eea6a0674ffd5ea680b2b171acf1f5
[safe/jmp/linux-2.6] / scripts / get_maintainer.pl
1 #!/usr/bin/perl -w
2 # (c) 2007, Joe Perches <joe@perches.com>
3 #           created from checkpatch.pl
4 #
5 # Print selected MAINTAINERS information for
6 # the files modified in a patch or for a file
7 #
8 # usage: perl scripts/get_maintainer.pl [OPTIONS] <patch>
9 #        perl scripts/get_maintainer.pl [OPTIONS] -f <file>
10 #
11 # Licensed under the terms of the GNU GPL License version 2
12
13 use strict;
14
15 my $P = $0;
16 my $V = '0.23';
17
18 use Getopt::Long qw(:config no_auto_abbrev);
19
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;
51
52 my $exit = 0;
53
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");
58
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);
65     }
66 }
67 my $penguin_chiefs = "\(" . join("|", @penguin_chief_names) . "\)";
68
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) . "\)";
77
78 # rfc822 email address - preloaded methods go here.
79 my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
80 my $rfc822_char = '[\\000-\\377]';
81
82 # VCS command support: class-like functions and strings
83
84 my %VCS_cmds;
85
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]+) "
95 );
96
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]+):"
108 );
109
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";
143 }
144
145 if ($help != 0) {
146     usage();
147     exit 0;
148 }
149
150 if ($version != 0) {
151     print("${P} ${V}\n");
152     exit 0;
153 }
154
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";
158 }
159
160 if ($output_separator ne ", ") {
161     $output_multiline = 0;
162 }
163
164 if ($output_rolestats) {
165     $output_roles = 1;
166 }
167
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";
180     }
181 }
182
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";
187 }
188
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";
192 }
193
194 if ($email_git_all_signature_types) {
195     $signaturePattern = "(.+?)[Bb][Yy]:";
196 }
197
198 ## Read MAINTAINERS for type/value pairs
199
200 my @typevalue = ();
201 my %keyword_hash;
202
203 open (my $maint, '<', "${lk_path}MAINTAINERS")
204     or die "$P: Can't open MAINTAINERS: $!\n";
205 while (<$maint>) {
206     my $line = $_;
207
208     if ($line =~ m/^(\C):\s*(.*)/) {
209         my $type = $1;
210         my $value = $2;
211
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/@;
220             }
221         } elsif ($type eq "K") {
222             $keyword_hash{@typevalue} = $value;
223         }
224         push(@typevalue, "$type:$value");
225     } elsif (!/^(\s)*$/) {
226         $line =~ s/\n$//g;
227         push(@typevalue, $line);
228     }
229 }
230 close($maint);
231
232 my %mailmap;
233
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 = $_;
239
240         next if ($line =~ m/^\s*#/);
241         next if ($line =~ m/^\s*$/);
242
243         my ($name, $address) = parse_email($line);
244         $line = format_email($name, $address, $email_usename);
245
246         next if ($line =~ m/^\s*$/);
247
248         if (exists($mailmap{$name})) {
249             my $obj = $mailmap{$name};
250             push(@$obj, $address);
251         } else {
252             my @arr = ($address);
253             $mailmap{$name} = \@arr;
254         }
255     }
256     close($mailmap);
257 }
258
259 ## use the filenames on the command line or find the filenames in the patchfiles
260
261 my @files = ();
262 my @range = ();
263 my @keyword_tvi = ();
264 my @file_emails = ();
265
266 if (!@ARGV) {
267     push(@ARGV, "&STDIN");
268 }
269
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";
277         }
278     }
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);
290                     }
291                 }
292             }
293             if ($file_emails) {
294                 my @poss_addr = $text =~ m$[A-Za-zÀ-ÿ\"\' \,\.\+-]*\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));
296             }
297         }
298     } else {
299         my $file_cnt = @files;
300         my $lastfile;
301
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");
315                 }
316             } elsif ($keywords) {
317                 foreach my $line (keys %keyword_hash) {
318                     if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
319                         push(@keyword_tvi, $line);
320                     }
321                 }
322             }
323         }
324         close($patch);
325
326         if ($file_cnt == @files) {
327             warn "$P: file '${file}' doesn't appear to be a patch.  "
328                 . "Add -f to options?\n";
329         }
330         @files = sort_and_uniq(@files);
331     }
332 }
333
334 @file_emails = uniq(@file_emails);
335
336 my @email_to = ();
337 my @list_to = ();
338 my @scm = ();
339 my @web = ();
340 my @subsystem = ();
341 my @status = ();
342
343 # Find responsible parties
344
345 foreach my $file (@files) {
346
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;
354
355         #Do not match excluded file patterns
356
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;
366                     }
367                 }
368             }
369         }
370
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;
385                             }
386                         }
387                     }
388                 }
389             }
390         }
391
392         $tvi = $end + 1;
393     }
394
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 *
408                     }
409                     $line =~ s/^([A-Z]):/$1:\t/g;
410                     print("$line\n");
411                 }
412                 print("\n");
413             }
414     }
415
416     if ($email && $email_git) {
417         vcs_file_signoffs($file);
418     }
419
420     if ($email && $email_git_blame) {
421         vcs_file_blame($file);
422     }
423 }
424
425 if ($keywords) {
426     @keyword_tvi = sort_and_uniq(@keyword_tvi);
427     foreach my $line (@keyword_tvi) {
428         add_categories($line);
429     }
430 }
431
432 if ($email) {
433     foreach my $chief (@penguin_chief) {
434         if ($chief =~ m/^(.*):(.*)/) {
435             my $email_address;
436
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);
442             }
443         }
444     }
445
446     foreach my $email (@file_emails) {
447         my ($name, $address) = parse_email($email);
448
449         my $tmp_email = format_email($name, $address, $email_usename);
450         push_email_address($tmp_email, '');
451         add_role($tmp_email, 'in file');
452     }
453 }
454
455 if ($email || $email_list) {
456     my @to = ();
457     if ($email) {
458         @to = (@to, @email_to);
459     }
460     if ($email_list) {
461         @to = (@to, @list_to);
462     }
463     output(merge_email(@to));
464 }
465
466 if ($scm) {
467     @scm = uniq(@scm);
468     output(@scm);
469 }
470
471 if ($status) {
472     @status = uniq(@status);
473     output(@status);
474 }
475
476 if ($subsystem) {
477     @subsystem = uniq(@subsystem);
478     output(@subsystem);
479 }
480
481 if ($web) {
482     @web = uniq(@web);
483     output(@web);
484 }
485
486 exit($exit);
487
488 sub file_match_pattern {
489     my ($file, $pattern) = @_;
490     if (substr($pattern, -1) eq "/") {
491         if ($file =~ m@^$pattern@) {
492             return 1;
493         }
494     } else {
495         if ($file =~ m@^$pattern@) {
496             my $s1 = ($file =~ tr@/@@);
497             my $s2 = ($pattern =~ tr@/@@);
498             if ($s1 == $s2) {
499                 return 1;
500             }
501         }
502     }
503     return 0;
504 }
505
506 sub usage {
507     print <<EOT;
508 usage: $P [options] patchfile
509        $P [options] -f file|directory
510 version: $V
511
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
536
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
541
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
548
549 Default options:
550   [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
551
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
576 EOT
577 }
578
579 sub top_of_kernel_tree {
580         my ($lk_path) = @_;
581
582         if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
583             $lk_path .= "/";
584         }
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;
602         }
603         return 0;
604 }
605
606 sub parse_email {
607     my ($formatted_email) = @_;
608
609     my $name = "";
610     my $address = "";
611
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;
619     }
620
621     $name =~ s/^\s+|\s+$//g;
622     $name =~ s/^\"|\"$//g;
623     $address =~ s/^\s+|\s+$//g;
624
625     if ($name =~ /[^\w \-]/i) {          ##has "must quote" chars
626         $name =~ s/(?<!\\)"/\\"/g;       ##escape quotes
627         $name = "\"$name\"";
628     }
629
630     return ($name, $address);
631 }
632
633 sub format_email {
634     my ($name, $address, $usename) = @_;
635
636     my $formatted_email;
637
638     $name =~ s/^\s+|\s+$//g;
639     $name =~ s/^\"|\"$//g;
640     $address =~ s/^\s+|\s+$//g;
641
642     if ($name =~ /[^\w \-]/i) {          ##has "must quote" chars
643         $name =~ s/(?<!\\)"/\\"/g;       ##escape quotes
644         $name = "\"$name\"";
645     }
646
647     if ($usename) {
648         if ("$name" eq "") {
649             $formatted_email = "$address";
650         } else {
651             $formatted_email = "$name <$address>";
652         }
653     } else {
654         $formatted_email = $address;
655     }
656
657     return $formatted_email;
658 }
659
660 sub find_first_section {
661     my $index = 0;
662
663     while ($index < @typevalue) {
664         my $tv = $typevalue[$index];
665         if (($tv =~ m/^(\C):\s*(.*)/)) {
666             last;
667         }
668         $index++;
669     }
670
671     return $index;
672 }
673
674 sub find_starting_index {
675     my ($index) = @_;
676
677     while ($index > 0) {
678         my $tv = $typevalue[$index];
679         if (!($tv =~ m/^(\C):\s*(.*)/)) {
680             last;
681         }
682         $index--;
683     }
684
685     return $index;
686 }
687
688 sub find_ending_index {
689     my ($index) = @_;
690
691     while ($index < @typevalue) {
692         my $tv = $typevalue[$index];
693         if (!($tv =~ m/^(\C):\s*(.*)/)) {
694             last;
695         }
696         $index++;
697     }
698
699     return $index;
700 }
701
702 sub get_maintainer_role {
703     my ($index) = @_;
704
705     my $i;
706     my $start = find_starting_index($index);
707     my $end = find_ending_index($index);
708
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 . "...";
715     }
716
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;
724             }
725         }
726     }
727
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";
741     }
742
743     return $role . ":" . $subsystem;
744 }
745
746 sub get_list_role {
747     my ($index) = @_;
748
749     my $i;
750     my $start = find_starting_index($index);
751     my $end = find_ending_index($index);
752
753     my $subsystem = $typevalue[$start];
754     if (length($subsystem) > 20) {
755         $subsystem = substr($subsystem, 0, 17);
756         $subsystem =~ s/\s*$//;
757         $subsystem = $subsystem . "...";
758     }
759
760     if ($subsystem eq "THE REST") {
761         $subsystem = "";
762     }
763
764     return $subsystem;
765 }
766
767 sub add_categories {
768     my ($index) = @_;
769
770     my $i;
771     my $start = find_starting_index($index);
772     my $end = find_ending_index($index);
773
774     push(@subsystem, $typevalue[$start]);
775
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);
785
786                 if ($list_role ne "") {
787                     $list_role = ":" . $list_role;
788                 }
789                 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
790                     $list_address = $1;
791                     $list_additional = $2;
792                 }
793                 if ($list_additional =~ m/subscribers-only/) {
794                     if ($email_subscriber_list) {
795                         push(@list_to, [$list_address, "subscriber list${list_role}"]);
796                     }
797                 } else {
798                     if ($email_list) {
799                         push(@list_to, [$list_address, "open list${list_role}"]);
800                     }
801                 }
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);
811                             }
812                         }
813                     }
814                 }
815                 if ($email_maintainer) {
816                     my $role = get_maintainer_role($i);
817                     push_email_addresses($pvalue, $role);
818                 }
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);
825             }
826         }
827     }
828 }
829
830 my %email_hash_name;
831 my %email_hash_address;
832
833 sub email_inuse {
834     my ($name, $address) = @_;
835
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}));
839
840     return 0;
841 }
842
843 sub push_email_address {
844     my ($line, $role) = @_;
845
846     my ($name, $address) = parse_email($line);
847
848     if ($address eq "") {
849         return 0;
850     }
851
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}++;
858     }
859
860     return 1;
861 }
862
863 sub push_email_addresses {
864     my ($address, $role) = @_;
865
866     my @address_list = ();
867
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);
874         }
875     } else {
876         if (!push_email_address($address, $role)) {
877             warn("Invalid MAINTAINERS address: '" . $address . "'\n");
878         }
879     }
880 }
881
882 sub add_role {
883     my ($line, $role) = @_;
884
885     my ($name, $address) = parse_email($line);
886     my $email = format_email($name, $address, $email_usename);
887
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/))
893             ) {
894                 if ($entry->[1] eq "") {
895                     $entry->[1] = "$role";
896                 } else {
897                     $entry->[1] = "$entry->[1],$role";
898                 }
899             }
900         } else {
901             if ($email eq $entry->[0]
902                 && ($role eq "" || !($entry->[1] =~ m/$role/))
903             ) {
904                 if ($entry->[1] eq "") {
905                     $entry->[1] = "$role";
906                 } else {
907                     $entry->[1] = "$entry->[1],$role";
908                 }
909             }
910         }
911     }
912 }
913
914 sub which {
915     my ($bin) = @_;
916
917     foreach my $path (split(/:/, $ENV{PATH})) {
918         if (-e "$path/$bin") {
919             return "$path/$bin";
920         }
921     }
922
923     return "";
924 }
925
926 sub mailmap {
927     my (@lines) = @_;
928     my %hash;
929
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);
937         }
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);
944                 }
945             }
946         }
947     }
948
949     return @lines;
950 }
951
952 sub git_execute_cmd {
953     my ($cmd) = @_;
954     my @lines = ();
955
956     my $output = `$cmd`;
957     $output =~ s/^\s*//gm;
958     @lines = split("\n", $output);
959
960     return @lines;
961 }
962
963 sub hg_execute_cmd {
964     my ($cmd) = @_;
965     my @lines = ();
966
967     my $output = `$cmd`;
968     @lines = split("\n", $output);
969
970     return @lines;
971 }
972
973 sub vcs_find_signers {
974     my ($cmd) = @_;
975     my @lines = ();
976     my $commits;
977
978     @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
979
980     my $pattern = $VCS_cmds{"commit_pattern"};
981
982     $commits = grep(/$pattern/, @lines);        # of commits
983
984     @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines);
985     if (!$email_git_penguin_chiefs) {
986         @lines = grep(!/${penguin_chiefs}/i, @lines);
987     }
988     # cut -f2- -d":"
989     s/.*:\s*(.+)\s*/$1/ for (@lines);
990
991 ## Reformat email addresses (with names) to avoid badly written signatures
992
993     foreach my $line (@lines) {
994         my ($name, $address) = parse_email($line);
995         $line = format_email($name, $address, 1);
996     }
997
998     return ($commits, @lines);
999 }
1000
1001 sub vcs_save_commits {
1002     my ($cmd) = @_;
1003     my @lines = ();
1004     my @commits = ();
1005
1006     @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1007
1008     foreach my $line (@lines) {
1009         if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
1010             push(@commits, $1);
1011         }
1012     }
1013
1014     return @commits;
1015 }
1016
1017 sub vcs_blame {
1018     my ($file) = @_;
1019     my $cmd;
1020     my @commits = ();
1021
1022     return @commits if (!(-f $file));
1023
1024     if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
1025         my @all_commits = ();
1026
1027         $cmd = $VCS_cmds{"blame_file_cmd"};
1028         $cmd =~ s/(\$\w+)/$1/eeg;               #interpolate $cmd
1029         @all_commits = vcs_save_commits($cmd);
1030
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]);
1039             }
1040         }
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));
1051         }
1052     } else {
1053         $cmd = $VCS_cmds{"blame_file_cmd"};
1054         $cmd =~ s/(\$\w+)/$1/eeg;               #interpolate $cmd
1055         @commits = vcs_save_commits($cmd);
1056     }
1057
1058     return @commits;
1059 }
1060
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;
1074     }
1075     return 0;
1076 }
1077
1078 sub vcs_assign {
1079     my ($role, $divisor, @lines) = @_;
1080
1081     my %hash;
1082     my $count = 0;
1083
1084     return if (@lines <= 0);
1085
1086     if ($divisor <= 0) {
1087         warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
1088         $divisor = 1;
1089     }
1090
1091     if ($email_remove_duplicates) {
1092         @lines = mailmap(@lines);
1093     }
1094
1095     @lines = sort(@lines);
1096
1097     # uniq -c
1098     $hash{$_}++ for @lines;
1099
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;
1104
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);
1116         }
1117     }
1118 }
1119
1120 sub vcs_file_signoffs {
1121     my ($file) = @_;
1122
1123     my @signers = ();
1124     my $commits;
1125
1126     return if (!vcs_exists());
1127
1128     my $cmd = $VCS_cmds{"find_signers_cmd"};
1129     $cmd =~ s/(\$\w+)/$1/eeg;           # interpolate $cmd
1130
1131     ($commits, @signers) = vcs_find_signers($cmd);
1132     vcs_assign("commit_signer", $commits, @signers);
1133 }
1134
1135 sub vcs_file_blame {
1136     my ($file) = @_;
1137
1138     my @signers = ();
1139     my @commits = ();
1140     my $total_commits;
1141
1142     return if (!vcs_exists());
1143
1144     @commits = vcs_blame($file);
1145     @commits = uniq(@commits);
1146     $total_commits = @commits;
1147
1148     foreach my $commit (@commits) {
1149         my $commit_count;
1150         my @commit_signers = ();
1151
1152         my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1153         $cmd =~ s/(\$\w+)/$1/eeg;       #interpolate $cmd
1154
1155         ($commit_count, @commit_signers) = vcs_find_signers($cmd);
1156         push(@signers, @commit_signers);
1157     }
1158
1159     if ($from_filename) {
1160         vcs_assign("commits", $total_commits, @signers);
1161     } else {
1162         vcs_assign("modified commits", $total_commits, @signers);
1163     }
1164 }
1165
1166 sub uniq {
1167     my (@parms) = @_;
1168
1169     my %saw;
1170     @parms = grep(!$saw{$_}++, @parms);
1171     return @parms;
1172 }
1173
1174 sub sort_and_uniq {
1175     my (@parms) = @_;
1176
1177     my %saw;
1178     @parms = sort @parms;
1179     @parms = grep(!$saw{$_}++, @parms);
1180     return @parms;
1181 }
1182
1183 sub clean_file_emails {
1184     my (@file_emails) = @_;
1185     my @fmt_emails = ();
1186
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 = "";
1192         }
1193
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];
1199
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";
1207             }
1208         }
1209
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) . '"';
1214         }
1215
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);
1220         }
1221
1222         my $fmt_email = format_email($name, $address, $email_usename);
1223         push(@fmt_emails, $fmt_email);
1224     }
1225     return @fmt_emails;
1226 }
1227
1228 sub merge_email {
1229     my @lines;
1230     my %saw;
1231
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);
1239             }
1240             $saw{$address} = 1;
1241         }
1242     }
1243
1244     return @lines;
1245 }
1246
1247 sub output {
1248     my (@parms) = @_;
1249
1250     if ($output_multiline) {
1251         foreach my $line (@parms) {
1252             print("${line}\n");
1253         }
1254     } else {
1255         print(join($output_separator, @parms));
1256         print("\n");
1257     }
1258 }
1259
1260 my $rfc822re;
1261
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.
1267
1268     my $specials = '()<>@,;:\\\\".\\[\\]';
1269     my $controls = '\\000-\\037\\177';
1270
1271     my $dtext = "[^\\[\\]\\r\\\\]";
1272     my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1273
1274     my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1275
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)*";
1281
1282     my $sub_domain = "(?:$atom|$domain_literal)";
1283     my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1284
1285     my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1286
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)";
1291
1292     my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1293     my $address = "(?:$mailbox|$group)";
1294
1295     return "$rfc822_lwsp*$address";
1296 }
1297
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.
1303
1304     while ($s =~ s/^((?:[^"\\]|\\.)*
1305                     (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1306                     \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1307     return $s;
1308 }
1309
1310 #   valid: returns true if the parameter is an RFC822 valid address
1311 #
1312 sub rfc822_valid {
1313     my $s = rfc822_strip_comments(shift);
1314
1315     if (!$rfc822re) {
1316         $rfc822re = make_rfc822re();
1317     }
1318
1319     return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1320 }
1321
1322 #   validlist: In scalar context, returns true if the parameter is an RFC822
1323 #              valid list of addresses.
1324 #
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.
1331
1332 sub rfc822_validlist {
1333     my $s = rfc822_strip_comments(shift);
1334
1335     if (!$rfc822re) {
1336         $rfc822re = make_rfc822re();
1337     }
1338     # * null list items are valid according to the RFC
1339     # * the '1' business is to aid in distinguishing failure from no results
1340
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);
1346         }
1347         return wantarray ? (scalar(@r), @r) : 1;
1348     }
1349     return wantarray ? () : 0;
1350 }