]> bbs.cooldavid.org Git - net-next-2.6.git/blob - scripts/get_maintainer.pl
scripts/get_maintainer.pl: Add --git-blame --rolestats "Authored lines" information
[net-next-2.6.git] / 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.25';
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 = 0;
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     "find_commit_author_cmd" => "git log -1 --format=\"%an <%ae>\" \$commit",
92     "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
93     "blame_file_cmd" => "git blame -l \$file",
94     "commit_pattern" => "^commit [0-9a-f]{40,40}",
95     "blame_commit_pattern" => "^([0-9a-f]+) "
96 );
97
98 my %VCS_cmds_hg = (
99     "execute_cmd" => \&hg_execute_cmd,
100     "available" => '(which("hg") ne "") && (-d ".hg")',
101     "find_signers_cmd" =>
102         "hg log --date=\$email_hg_since" .
103                 " --template='commit {node}\\n{desc}\\n' -- \$file",
104     "find_commit_signers_cmd" => "hg log --template='{desc}\\n' -r \$commit",
105     "find_commit_author_cmd" => "hg log -l 1 --template='{author}\\n' -r \$commit",
106     "blame_range_cmd" => "",            # not supported
107     "blame_file_cmd" => "hg blame -c \$file",
108     "commit_pattern" => "^commit [0-9a-f]{40,40}",
109     "blame_commit_pattern" => "^([0-9a-f]+):"
110 );
111
112 if (-f "${lk_path}.get_maintainer.conf") {
113     my @conf_args;
114     open(my $conffile, '<', "${lk_path}.get_maintainer.conf")
115         or warn "$P: Can't open .get_maintainer.conf: $!\n";
116     while (<$conffile>) {
117         my $line = $_;
118
119         $line =~ s/\s*\n?$//g;
120         $line =~ s/^\s*//g;
121         $line =~ s/\s+/ /g;
122
123         next if ($line =~ m/^\s*#/);
124         next if ($line =~ m/^\s*$/);
125
126         my @words = split(" ", $line);
127         foreach my $word (@words) {
128             last if ($word =~ m/^#/);
129             push (@conf_args, $word);
130         }
131     }
132     close($conffile);
133     unshift(@ARGV, @conf_args) if @conf_args;
134 }
135
136 if (!GetOptions(
137                 'email!' => \$email,
138                 'git!' => \$email_git,
139                 'git-all-signature-types!' => \$email_git_all_signature_types,
140                 'git-blame!' => \$email_git_blame,
141                 'git-chief-penguins!' => \$email_git_penguin_chiefs,
142                 'git-min-signatures=i' => \$email_git_min_signatures,
143                 'git-max-maintainers=i' => \$email_git_max_maintainers,
144                 'git-min-percent=i' => \$email_git_min_percent,
145                 'git-since=s' => \$email_git_since,
146                 'hg-since=s' => \$email_hg_since,
147                 'remove-duplicates!' => \$email_remove_duplicates,
148                 'm!' => \$email_maintainer,
149                 'n!' => \$email_usename,
150                 'l!' => \$email_list,
151                 's!' => \$email_subscriber_list,
152                 'multiline!' => \$output_multiline,
153                 'roles!' => \$output_roles,
154                 'rolestats!' => \$output_rolestats,
155                 'separator=s' => \$output_separator,
156                 'subsystem!' => \$subsystem,
157                 'status!' => \$status,
158                 'scm!' => \$scm,
159                 'web!' => \$web,
160                 'pattern-depth=i' => \$pattern_depth,
161                 'k|keywords!' => \$keywords,
162                 'sections!' => \$sections,
163                 'fe|file-emails!' => \$file_emails,
164                 'f|file' => \$from_filename,
165                 'v|version' => \$version,
166                 'h|help|usage' => \$help,
167                 )) {
168     die "$P: invalid argument - use --help if necessary\n";
169 }
170
171 if ($help != 0) {
172     usage();
173     exit 0;
174 }
175
176 if ($version != 0) {
177     print("${P} ${V}\n");
178     exit 0;
179 }
180
181 if (-t STDIN && !@ARGV) {
182     # We're talking to a terminal, but have no command line arguments.
183     die "$P: missing patchfile or -f file - use --help if necessary\n";
184 }
185
186 if ($output_separator ne ", ") {
187     $output_multiline = 0;
188 }
189
190 if ($output_rolestats) {
191     $output_roles = 1;
192 }
193
194 if ($sections) {
195     $email = 0;
196     $email_list = 0;
197     $scm = 0;
198     $status = 0;
199     $subsystem = 0;
200     $web = 0;
201     $keywords = 0;
202 } else {
203     my $selections = $email + $scm + $status + $subsystem + $web;
204     if ($selections == 0) {
205         die "$P:  Missing required option: email, scm, status, subsystem or web\n";
206     }
207 }
208
209 if ($email &&
210     ($email_maintainer + $email_list + $email_subscriber_list +
211      $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
212     die "$P: Please select at least 1 email option\n";
213 }
214
215 if (!top_of_kernel_tree($lk_path)) {
216     die "$P: The current directory does not appear to be "
217         . "a linux kernel source tree.\n";
218 }
219
220 if ($email_git_all_signature_types) {
221     $signaturePattern = "(.+?)[Bb][Yy]:";
222 }
223
224 ## Read MAINTAINERS for type/value pairs
225
226 my @typevalue = ();
227 my %keyword_hash;
228
229 open (my $maint, '<', "${lk_path}MAINTAINERS")
230     or die "$P: Can't open MAINTAINERS: $!\n";
231 while (<$maint>) {
232     my $line = $_;
233
234     if ($line =~ m/^(\C):\s*(.*)/) {
235         my $type = $1;
236         my $value = $2;
237
238         ##Filename pattern matching
239         if ($type eq "F" || $type eq "X") {
240             $value =~ s@\.@\\\.@g;       ##Convert . to \.
241             $value =~ s/\*/\.\*/g;       ##Convert * to .*
242             $value =~ s/\?/\./g;         ##Convert ? to .
243             ##if pattern is a directory and it lacks a trailing slash, add one
244             if ((-d $value)) {
245                 $value =~ s@([^/])$@$1/@;
246             }
247         } elsif ($type eq "K") {
248             $keyword_hash{@typevalue} = $value;
249         }
250         push(@typevalue, "$type:$value");
251     } elsif (!/^(\s)*$/) {
252         $line =~ s/\n$//g;
253         push(@typevalue, $line);
254     }
255 }
256 close($maint);
257
258 my %mailmap;
259
260 if ($email_remove_duplicates) {
261     open(my $mailmap, '<', "${lk_path}.mailmap")
262         or warn "$P: Can't open .mailmap: $!\n";
263     while (<$mailmap>) {
264         my $line = $_;
265
266         next if ($line =~ m/^\s*#/);
267         next if ($line =~ m/^\s*$/);
268
269         my ($name, $address) = parse_email($line);
270         $line = format_email($name, $address, $email_usename);
271
272         next if ($line =~ m/^\s*$/);
273
274         if (exists($mailmap{$name})) {
275             my $obj = $mailmap{$name};
276             push(@$obj, $address);
277         } else {
278             my @arr = ($address);
279             $mailmap{$name} = \@arr;
280         }
281     }
282     close($mailmap);
283 }
284
285 ## use the filenames on the command line or find the filenames in the patchfiles
286
287 my @files = ();
288 my @range = ();
289 my @keyword_tvi = ();
290 my @file_emails = ();
291
292 if (!@ARGV) {
293     push(@ARGV, "&STDIN");
294 }
295
296 foreach my $file (@ARGV) {
297     if ($file ne "&STDIN") {
298         ##if $file is a directory and it lacks a trailing slash, add one
299         if ((-d $file)) {
300             $file =~ s@([^/])$@$1/@;
301         } elsif (!(-f $file)) {
302             die "$P: file '${file}' not found\n";
303         }
304     }
305     if ($from_filename) {
306         push(@files, $file);
307         if (-f $file && ($keywords || $file_emails)) {
308             open(my $f, '<', $file)
309                 or die "$P: Can't open $file: $!\n";
310             my $text = do { local($/) ; <$f> };
311             close($f);
312             if ($keywords) {
313                 foreach my $line (keys %keyword_hash) {
314                     if ($text =~ m/$keyword_hash{$line}/x) {
315                         push(@keyword_tvi, $line);
316                     }
317                 }
318             }
319             if ($file_emails) {
320                 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;
321                 push(@file_emails, clean_file_emails(@poss_addr));
322             }
323         }
324     } else {
325         my $file_cnt = @files;
326         my $lastfile;
327
328         open(my $patch, "< $file")
329             or die "$P: Can't open $file: $!\n";
330         while (<$patch>) {
331             my $patch_line = $_;
332             if (m/^\+\+\+\s+(\S+)/) {
333                 my $filename = $1;
334                 $filename =~ s@^[^/]*/@@;
335                 $filename =~ s@\n@@;
336                 $lastfile = $filename;
337                 push(@files, $filename);
338             } elsif (m/^\@\@ -(\d+),(\d+)/) {
339                 if ($email_git_blame) {
340                     push(@range, "$lastfile:$1:$2");
341                 }
342             } elsif ($keywords) {
343                 foreach my $line (keys %keyword_hash) {
344                     if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
345                         push(@keyword_tvi, $line);
346                     }
347                 }
348             }
349         }
350         close($patch);
351
352         if ($file_cnt == @files) {
353             warn "$P: file '${file}' doesn't appear to be a patch.  "
354                 . "Add -f to options?\n";
355         }
356         @files = sort_and_uniq(@files);
357     }
358 }
359
360 @file_emails = uniq(@file_emails);
361
362 my @email_to = ();
363 my @list_to = ();
364 my @scm = ();
365 my @web = ();
366 my @subsystem = ();
367 my @status = ();
368
369 # Find responsible parties
370
371 foreach my $file (@files) {
372
373     my %hash;
374     my $tvi = find_first_section();
375     while ($tvi < @typevalue) {
376         my $start = find_starting_index($tvi);
377         my $end = find_ending_index($tvi);
378         my $exclude = 0;
379         my $i;
380
381         #Do not match excluded file patterns
382
383         for ($i = $start; $i < $end; $i++) {
384             my $line = $typevalue[$i];
385             if ($line =~ m/^(\C):\s*(.*)/) {
386                 my $type = $1;
387                 my $value = $2;
388                 if ($type eq 'X') {
389                     if (file_match_pattern($file, $value)) {
390                         $exclude = 1;
391                         last;
392                     }
393                 }
394             }
395         }
396
397         if (!$exclude) {
398             for ($i = $start; $i < $end; $i++) {
399                 my $line = $typevalue[$i];
400                 if ($line =~ m/^(\C):\s*(.*)/) {
401                     my $type = $1;
402                     my $value = $2;
403                     if ($type eq 'F') {
404                         if (file_match_pattern($file, $value)) {
405                             my $value_pd = ($value =~ tr@/@@);
406                             my $file_pd = ($file  =~ tr@/@@);
407                             $value_pd++ if (substr($value,-1,1) ne "/");
408                             if ($pattern_depth == 0 ||
409                                 (($file_pd - $value_pd) < $pattern_depth)) {
410                                 $hash{$tvi} = $value_pd;
411                             }
412                         }
413                     }
414                 }
415             }
416         }
417
418         $tvi = $end + 1;
419     }
420
421     foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
422         add_categories($line);
423             if ($sections) {
424                 my $i;
425                 my $start = find_starting_index($line);
426                 my $end = find_ending_index($line);
427                 for ($i = $start; $i < $end; $i++) {
428                     my $line = $typevalue[$i];
429                     if ($line =~ /^[FX]:/) {            ##Restore file patterns
430                         $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
431                         $line =~ s/([^\\])\.$/$1\?/g;   ##Convert . back to ?
432                         $line =~ s/\\\./\./g;           ##Convert \. to .
433                         $line =~ s/\.\*/\*/g;           ##Convert .* to *
434                     }
435                     $line =~ s/^([A-Z]):/$1:\t/g;
436                     print("$line\n");
437                 }
438                 print("\n");
439             }
440     }
441
442     if ($email && $email_git) {
443         vcs_file_signoffs($file);
444     }
445
446     if ($email && $email_git_blame) {
447         vcs_file_blame($file);
448     }
449 }
450
451 if ($keywords) {
452     @keyword_tvi = sort_and_uniq(@keyword_tvi);
453     foreach my $line (@keyword_tvi) {
454         add_categories($line);
455     }
456 }
457
458 if ($email) {
459     foreach my $chief (@penguin_chief) {
460         if ($chief =~ m/^(.*):(.*)/) {
461             my $email_address;
462
463             $email_address = format_email($1, $2, $email_usename);
464             if ($email_git_penguin_chiefs) {
465                 push(@email_to, [$email_address, 'chief penguin']);
466             } else {
467                 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
468             }
469         }
470     }
471
472     foreach my $email (@file_emails) {
473         my ($name, $address) = parse_email($email);
474
475         my $tmp_email = format_email($name, $address, $email_usename);
476         push_email_address($tmp_email, '');
477         add_role($tmp_email, 'in file');
478     }
479 }
480
481 if ($email || $email_list) {
482     my @to = ();
483     if ($email) {
484         @to = (@to, @email_to);
485     }
486     if ($email_list) {
487         @to = (@to, @list_to);
488     }
489     output(merge_email(@to));
490 }
491
492 if ($scm) {
493     @scm = uniq(@scm);
494     output(@scm);
495 }
496
497 if ($status) {
498     @status = uniq(@status);
499     output(@status);
500 }
501
502 if ($subsystem) {
503     @subsystem = uniq(@subsystem);
504     output(@subsystem);
505 }
506
507 if ($web) {
508     @web = uniq(@web);
509     output(@web);
510 }
511
512 exit($exit);
513
514 sub file_match_pattern {
515     my ($file, $pattern) = @_;
516     if (substr($pattern, -1) eq "/") {
517         if ($file =~ m@^$pattern@) {
518             return 1;
519         }
520     } else {
521         if ($file =~ m@^$pattern@) {
522             my $s1 = ($file =~ tr@/@@);
523             my $s2 = ($pattern =~ tr@/@@);
524             if ($s1 == $s2) {
525                 return 1;
526             }
527         }
528     }
529     return 0;
530 }
531
532 sub usage {
533     print <<EOT;
534 usage: $P [options] patchfile
535        $P [options] -f file|directory
536 version: $V
537
538 MAINTAINER field selection options:
539   --email => print email address(es) if any
540     --git => include recent git \*-by: signers
541     --git-all-signature-types => include signers regardless of signature type
542         or use only ${signaturePattern} signers (default: $email_git_all_signature_types)
543     --git-chief-penguins => include ${penguin_chiefs}
544     --git-min-signatures => number of signatures required (default: $email_git_min_signatures)
545     --git-max-maintainers => maximum maintainers to add (default: $email_git_max_maintainers)
546     --git-min-percent => minimum percentage of commits required (default: $email_git_min_percent)
547     --git-blame => use git blame to find modified commits for patch or file
548     --git-since => git history to use (default: $email_git_since)
549     --hg-since => hg history to use (default: $email_hg_since)
550     --m => include maintainer(s) if any
551     --n => include name 'Full Name <addr\@domain.tld>'
552     --l => include list(s) if any
553     --s => include subscriber only list(s) if any
554     --remove-duplicates => minimize duplicate email names/addresses
555     --roles => show roles (status:subsystem, git-signer, list, etc...)
556     --rolestats => show roles and statistics (commits/total_commits, %)
557     --file-emails => add email addresses found in -f file (default: 0 (off))
558   --scm => print SCM tree(s) if any
559   --status => print status if any
560   --subsystem => print subsystem name if any
561   --web => print website(s) if any
562
563 Output type options:
564   --separator [, ] => separator for multiple entries on 1 line
565     using --separator also sets --nomultiline if --separator is not [, ]
566   --multiline => print 1 entry per line
567
568 Other options:
569   --pattern-depth => Number of pattern directory traversals (default: 0 (all))
570   --keywords => scan patch for keywords (default: 1 (on))
571   --sections => print the entire subsystem sections with pattern matches
572   --version => show version
573   --help => show this help information
574
575 Default options:
576   [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
577
578 Notes:
579   Using "-f directory" may give unexpected results:
580       Used with "--git", git signators for _all_ files in and below
581           directory are examined as git recurses directories.
582           Any specified X: (exclude) pattern matches are _not_ ignored.
583       Used with "--nogit", directory is used as a pattern match,
584           no individual file within the directory or subdirectory
585           is matched.
586       Used with "--git-blame", does not iterate all files in directory
587   Using "--git-blame" is slow and may add old committers and authors
588       that are no longer active maintainers to the output.
589   Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
590       other automated tools that expect only ["name"] <email address>
591       may not work because of additional output after <email address>.
592   Using "--rolestats" and "--git-blame" shows the #/total=% commits,
593       not the percentage of the entire file authored.  # of commits is
594       not a good measure of amount of code authored.  1 major commit may
595       contain a thousand lines, 5 trivial commits may modify a single line.
596   If git is not installed, but mercurial (hg) is installed and an .hg
597       repository exists, the following options apply to mercurial:
598           --git,
599           --git-min-signatures, --git-max-maintainers, --git-min-percent, and
600           --git-blame
601       Use --hg-since not --git-since to control date selection
602   File ".get_maintainer.conf", if it exists in the linux kernel source root
603       directory, can change whatever get_maintainer defaults are desired.
604       Entries in this file can be any command line argument.
605       This file is prepended to any additional command line arguments.
606       Multiple lines and # comments are allowed.
607 EOT
608 }
609
610 sub top_of_kernel_tree {
611         my ($lk_path) = @_;
612
613         if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
614             $lk_path .= "/";
615         }
616         if (   (-f "${lk_path}COPYING")
617             && (-f "${lk_path}CREDITS")
618             && (-f "${lk_path}Kbuild")
619             && (-f "${lk_path}MAINTAINERS")
620             && (-f "${lk_path}Makefile")
621             && (-f "${lk_path}README")
622             && (-d "${lk_path}Documentation")
623             && (-d "${lk_path}arch")
624             && (-d "${lk_path}include")
625             && (-d "${lk_path}drivers")
626             && (-d "${lk_path}fs")
627             && (-d "${lk_path}init")
628             && (-d "${lk_path}ipc")
629             && (-d "${lk_path}kernel")
630             && (-d "${lk_path}lib")
631             && (-d "${lk_path}scripts")) {
632                 return 1;
633         }
634         return 0;
635 }
636
637 sub parse_email {
638     my ($formatted_email) = @_;
639
640     my $name = "";
641     my $address = "";
642
643     if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
644         $name = $1;
645         $address = $2;
646     } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
647         $address = $1;
648     } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
649         $address = $1;
650     }
651
652     $name =~ s/^\s+|\s+$//g;
653     $name =~ s/^\"|\"$//g;
654     $address =~ s/^\s+|\s+$//g;
655
656     if ($name =~ /[^\w \-]/i) {          ##has "must quote" chars
657         $name =~ s/(?<!\\)"/\\"/g;       ##escape quotes
658         $name = "\"$name\"";
659     }
660
661     return ($name, $address);
662 }
663
664 sub format_email {
665     my ($name, $address, $usename) = @_;
666
667     my $formatted_email;
668
669     $name =~ s/^\s+|\s+$//g;
670     $name =~ s/^\"|\"$//g;
671     $address =~ s/^\s+|\s+$//g;
672
673     if ($name =~ /[^\w \-]/i) {          ##has "must quote" chars
674         $name =~ s/(?<!\\)"/\\"/g;       ##escape quotes
675         $name = "\"$name\"";
676     }
677
678     if ($usename) {
679         if ("$name" eq "") {
680             $formatted_email = "$address";
681         } else {
682             $formatted_email = "$name <$address>";
683         }
684     } else {
685         $formatted_email = $address;
686     }
687
688     return $formatted_email;
689 }
690
691 sub find_first_section {
692     my $index = 0;
693
694     while ($index < @typevalue) {
695         my $tv = $typevalue[$index];
696         if (($tv =~ m/^(\C):\s*(.*)/)) {
697             last;
698         }
699         $index++;
700     }
701
702     return $index;
703 }
704
705 sub find_starting_index {
706     my ($index) = @_;
707
708     while ($index > 0) {
709         my $tv = $typevalue[$index];
710         if (!($tv =~ m/^(\C):\s*(.*)/)) {
711             last;
712         }
713         $index--;
714     }
715
716     return $index;
717 }
718
719 sub find_ending_index {
720     my ($index) = @_;
721
722     while ($index < @typevalue) {
723         my $tv = $typevalue[$index];
724         if (!($tv =~ m/^(\C):\s*(.*)/)) {
725             last;
726         }
727         $index++;
728     }
729
730     return $index;
731 }
732
733 sub get_maintainer_role {
734     my ($index) = @_;
735
736     my $i;
737     my $start = find_starting_index($index);
738     my $end = find_ending_index($index);
739
740     my $role;
741     my $subsystem = $typevalue[$start];
742     if (length($subsystem) > 20) {
743         $subsystem = substr($subsystem, 0, 17);
744         $subsystem =~ s/\s*$//;
745         $subsystem = $subsystem . "...";
746     }
747
748     for ($i = $start + 1; $i < $end; $i++) {
749         my $tv = $typevalue[$i];
750         if ($tv =~ m/^(\C):\s*(.*)/) {
751             my $ptype = $1;
752             my $pvalue = $2;
753             if ($ptype eq "S") {
754                 $role = $pvalue;
755             }
756         }
757     }
758
759     $role = lc($role);
760     if      ($role eq "supported") {
761         $role = "supporter";
762     } elsif ($role eq "maintained") {
763         $role = "maintainer";
764     } elsif ($role eq "odd fixes") {
765         $role = "odd fixer";
766     } elsif ($role eq "orphan") {
767         $role = "orphan minder";
768     } elsif ($role eq "obsolete") {
769         $role = "obsolete minder";
770     } elsif ($role eq "buried alive in reporters") {
771         $role = "chief penguin";
772     }
773
774     return $role . ":" . $subsystem;
775 }
776
777 sub get_list_role {
778     my ($index) = @_;
779
780     my $i;
781     my $start = find_starting_index($index);
782     my $end = find_ending_index($index);
783
784     my $subsystem = $typevalue[$start];
785     if (length($subsystem) > 20) {
786         $subsystem = substr($subsystem, 0, 17);
787         $subsystem =~ s/\s*$//;
788         $subsystem = $subsystem . "...";
789     }
790
791     if ($subsystem eq "THE REST") {
792         $subsystem = "";
793     }
794
795     return $subsystem;
796 }
797
798 sub add_categories {
799     my ($index) = @_;
800
801     my $i;
802     my $start = find_starting_index($index);
803     my $end = find_ending_index($index);
804
805     push(@subsystem, $typevalue[$start]);
806
807     for ($i = $start + 1; $i < $end; $i++) {
808         my $tv = $typevalue[$i];
809         if ($tv =~ m/^(\C):\s*(.*)/) {
810             my $ptype = $1;
811             my $pvalue = $2;
812             if ($ptype eq "L") {
813                 my $list_address = $pvalue;
814                 my $list_additional = "";
815                 my $list_role = get_list_role($i);
816
817                 if ($list_role ne "") {
818                     $list_role = ":" . $list_role;
819                 }
820                 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
821                     $list_address = $1;
822                     $list_additional = $2;
823                 }
824                 if ($list_additional =~ m/subscribers-only/) {
825                     if ($email_subscriber_list) {
826                         push(@list_to, [$list_address, "subscriber list${list_role}"]);
827                     }
828                 } else {
829                     if ($email_list) {
830                         push(@list_to, [$list_address, "open list${list_role}"]);
831                     }
832                 }
833             } elsif ($ptype eq "M") {
834                 my ($name, $address) = parse_email($pvalue);
835                 if ($name eq "") {
836                     if ($i > 0) {
837                         my $tv = $typevalue[$i - 1];
838                         if ($tv =~ m/^(\C):\s*(.*)/) {
839                             if ($1 eq "P") {
840                                 $name = $2;
841                                 $pvalue = format_email($name, $address, $email_usename);
842                             }
843                         }
844                     }
845                 }
846                 if ($email_maintainer) {
847                     my $role = get_maintainer_role($i);
848                     push_email_addresses($pvalue, $role);
849                 }
850             } elsif ($ptype eq "T") {
851                 push(@scm, $pvalue);
852             } elsif ($ptype eq "W") {
853                 push(@web, $pvalue);
854             } elsif ($ptype eq "S") {
855                 push(@status, $pvalue);
856             }
857         }
858     }
859 }
860
861 my %email_hash_name;
862 my %email_hash_address;
863
864 sub email_inuse {
865     my ($name, $address) = @_;
866
867     return 1 if (($name eq "") && ($address eq ""));
868     return 1 if (($name ne "") && exists($email_hash_name{$name}));
869     return 1 if (($address ne "") && exists($email_hash_address{$address}));
870
871     return 0;
872 }
873
874 sub push_email_address {
875     my ($line, $role) = @_;
876
877     my ($name, $address) = parse_email($line);
878
879     if ($address eq "") {
880         return 0;
881     }
882
883     if (!$email_remove_duplicates) {
884         push(@email_to, [format_email($name, $address, $email_usename), $role]);
885     } elsif (!email_inuse($name, $address)) {
886         push(@email_to, [format_email($name, $address, $email_usename), $role]);
887         $email_hash_name{$name}++;
888         $email_hash_address{$address}++;
889     }
890
891     return 1;
892 }
893
894 sub push_email_addresses {
895     my ($address, $role) = @_;
896
897     my @address_list = ();
898
899     if (rfc822_valid($address)) {
900         push_email_address($address, $role);
901     } elsif (@address_list = rfc822_validlist($address)) {
902         my $array_count = shift(@address_list);
903         while (my $entry = shift(@address_list)) {
904             push_email_address($entry, $role);
905         }
906     } else {
907         if (!push_email_address($address, $role)) {
908             warn("Invalid MAINTAINERS address: '" . $address . "'\n");
909         }
910     }
911 }
912
913 sub add_role {
914     my ($line, $role) = @_;
915
916     my ($name, $address) = parse_email($line);
917     my $email = format_email($name, $address, $email_usename);
918
919     foreach my $entry (@email_to) {
920         if ($email_remove_duplicates) {
921             my ($entry_name, $entry_address) = parse_email($entry->[0]);
922             if (($name eq $entry_name || $address eq $entry_address)
923                 && ($role eq "" || !($entry->[1] =~ m/$role/))
924             ) {
925                 if ($entry->[1] eq "") {
926                     $entry->[1] = "$role";
927                 } else {
928                     $entry->[1] = "$entry->[1],$role";
929                 }
930             }
931         } else {
932             if ($email eq $entry->[0]
933                 && ($role eq "" || !($entry->[1] =~ m/$role/))
934             ) {
935                 if ($entry->[1] eq "") {
936                     $entry->[1] = "$role";
937                 } else {
938                     $entry->[1] = "$entry->[1],$role";
939                 }
940             }
941         }
942     }
943 }
944
945 sub which {
946     my ($bin) = @_;
947
948     foreach my $path (split(/:/, $ENV{PATH})) {
949         if (-e "$path/$bin") {
950             return "$path/$bin";
951         }
952     }
953
954     return "";
955 }
956
957 sub mailmap {
958     my (@lines) = @_;
959     my %hash;
960
961     foreach my $line (@lines) {
962         my ($name, $address) = parse_email($line);
963         if (!exists($hash{$name})) {
964             $hash{$name} = $address;
965         } elsif ($address ne $hash{$name}) {
966             $address = $hash{$name};
967             $line = format_email($name, $address, $email_usename);
968         }
969         if (exists($mailmap{$name})) {
970             my $obj = $mailmap{$name};
971             foreach my $map_address (@$obj) {
972                 if (($map_address eq $address) &&
973                     ($map_address ne $hash{$name})) {
974                     $line = format_email($name, $hash{$name}, $email_usename);
975                 }
976             }
977         }
978     }
979
980     return @lines;
981 }
982
983 sub git_execute_cmd {
984     my ($cmd) = @_;
985     my @lines = ();
986
987     my $output = `$cmd`;
988     $output =~ s/^\s*//gm;
989     @lines = split("\n", $output);
990
991     return @lines;
992 }
993
994 sub hg_execute_cmd {
995     my ($cmd) = @_;
996     my @lines = ();
997
998     my $output = `$cmd`;
999     @lines = split("\n", $output);
1000
1001     return @lines;
1002 }
1003
1004 sub vcs_find_signers {
1005     my ($cmd) = @_;
1006     my @lines = ();
1007     my $commits;
1008
1009     @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1010
1011     my $pattern = $VCS_cmds{"commit_pattern"};
1012
1013     $commits = grep(/$pattern/, @lines);        # of commits
1014
1015     @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines);
1016     if (!$email_git_penguin_chiefs) {
1017         @lines = grep(!/${penguin_chiefs}/i, @lines);
1018     }
1019
1020     return (0, @lines) if !@lines;
1021
1022     # cut -f2- -d":"
1023     s/.*:\s*(.+)\s*/$1/ for (@lines);
1024
1025 ## Reformat email addresses (with names) to avoid badly written signatures
1026
1027     foreach my $line (@lines) {
1028         my ($name, $address) = parse_email($line);
1029         $line = format_email($name, $address, 1);
1030     }
1031
1032     return ($commits, @lines);
1033 }
1034
1035 sub vcs_find_author {
1036     my ($cmd) = @_;
1037     my @lines = ();
1038
1039     @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1040
1041     if (!$email_git_penguin_chiefs) {
1042         @lines = grep(!/${penguin_chiefs}/i, @lines);
1043     }
1044
1045     return @lines if !@lines;
1046
1047 ## Reformat email addresses (with names) to avoid badly written signatures
1048
1049     foreach my $line (@lines) {
1050         my ($name, $address) = parse_email($line);
1051         $line = format_email($name, $address, 1);
1052     }
1053
1054     return @lines;
1055 }
1056
1057 sub vcs_save_commits {
1058     my ($cmd) = @_;
1059     my @lines = ();
1060     my @commits = ();
1061
1062     @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1063
1064     foreach my $line (@lines) {
1065         if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
1066             push(@commits, $1);
1067         }
1068     }
1069
1070     return @commits;
1071 }
1072
1073 sub vcs_blame {
1074     my ($file) = @_;
1075     my $cmd;
1076     my @commits = ();
1077
1078     return @commits if (!(-f $file));
1079
1080     if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
1081         my @all_commits = ();
1082
1083         $cmd = $VCS_cmds{"blame_file_cmd"};
1084         $cmd =~ s/(\$\w+)/$1/eeg;               #interpolate $cmd
1085         @all_commits = vcs_save_commits($cmd);
1086
1087         foreach my $file_range_diff (@range) {
1088             next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1089             my $diff_file = $1;
1090             my $diff_start = $2;
1091             my $diff_length = $3;
1092             next if ("$file" ne "$diff_file");
1093             for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
1094                 push(@commits, $all_commits[$i]);
1095             }
1096         }
1097     } elsif (@range) {
1098         foreach my $file_range_diff (@range) {
1099             next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1100             my $diff_file = $1;
1101             my $diff_start = $2;
1102             my $diff_length = $3;
1103             next if ("$file" ne "$diff_file");
1104             $cmd = $VCS_cmds{"blame_range_cmd"};
1105             $cmd =~ s/(\$\w+)/$1/eeg;           #interpolate $cmd
1106             push(@commits, vcs_save_commits($cmd));
1107         }
1108     } else {
1109         $cmd = $VCS_cmds{"blame_file_cmd"};
1110         $cmd =~ s/(\$\w+)/$1/eeg;               #interpolate $cmd
1111         @commits = vcs_save_commits($cmd);
1112     }
1113
1114     foreach my $commit (@commits) {
1115         $commit =~ s/^\^//g;
1116     }
1117
1118     return @commits;
1119 }
1120
1121 my $printed_novcs = 0;
1122 sub vcs_exists {
1123     %VCS_cmds = %VCS_cmds_git;
1124     return 1 if eval $VCS_cmds{"available"};
1125     %VCS_cmds = %VCS_cmds_hg;
1126     return 1 if eval $VCS_cmds{"available"};
1127     %VCS_cmds = ();
1128     if (!$printed_novcs) {
1129         warn("$P: No supported VCS found.  Add --nogit to options?\n");
1130         warn("Using a git repository produces better results.\n");
1131         warn("Try Linus Torvalds' latest git repository using:\n");
1132         warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
1133         $printed_novcs = 1;
1134     }
1135     return 0;
1136 }
1137
1138 sub vcs_assign {
1139     my ($role, $divisor, @lines) = @_;
1140
1141     my %hash;
1142     my $count = 0;
1143
1144     return if (@lines <= 0);
1145
1146     if ($divisor <= 0) {
1147         warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
1148         $divisor = 1;
1149     }
1150
1151     if ($email_remove_duplicates) {
1152         @lines = mailmap(@lines);
1153     }
1154
1155     return if (@lines <= 0);
1156
1157     @lines = sort(@lines);
1158
1159     # uniq -c
1160     $hash{$_}++ for @lines;
1161
1162     # sort -rn
1163     foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
1164         my $sign_offs = $hash{$line};
1165         my $percent = $sign_offs * 100 / $divisor;
1166
1167         $percent = 100 if ($percent > 100);
1168         $count++;
1169         last if ($sign_offs < $email_git_min_signatures ||
1170                  $count > $email_git_max_maintainers ||
1171                  $percent < $email_git_min_percent);
1172         push_email_address($line, '');
1173         if ($output_rolestats) {
1174             my $fmt_percent = sprintf("%.0f", $percent);
1175             add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
1176         } else {
1177             add_role($line, $role);
1178         }
1179     }
1180 }
1181
1182 sub vcs_file_signoffs {
1183     my ($file) = @_;
1184
1185     my @signers = ();
1186     my $commits;
1187
1188     return if (!vcs_exists());
1189
1190     my $cmd = $VCS_cmds{"find_signers_cmd"};
1191     $cmd =~ s/(\$\w+)/$1/eeg;           # interpolate $cmd
1192
1193     ($commits, @signers) = vcs_find_signers($cmd);
1194     vcs_assign("commit_signer", $commits, @signers);
1195 }
1196
1197 sub vcs_file_blame {
1198     my ($file) = @_;
1199
1200     my @signers = ();
1201     my @all_commits = ();
1202     my @commits = ();
1203     my $total_commits;
1204     my $total_lines;
1205
1206     return if (!vcs_exists());
1207
1208     @all_commits = vcs_blame($file);
1209     @commits = uniq(@all_commits);
1210     $total_commits = @commits;
1211     $total_lines = @all_commits;
1212
1213     foreach my $commit (@commits) {
1214         my $commit_count;
1215         my @commit_signers = ();
1216
1217         my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1218         $cmd =~ s/(\$\w+)/$1/eeg;       #interpolate $cmd
1219
1220         ($commit_count, @commit_signers) = vcs_find_signers($cmd);
1221
1222         push(@signers, @commit_signers);
1223     }
1224
1225     if ($from_filename) {
1226         if ($output_rolestats) {
1227             my @blame_signers;
1228             foreach my $commit (@commits) {
1229                 my $i;
1230                 my $cmd = $VCS_cmds{"find_commit_author_cmd"};
1231                 $cmd =~ s/(\$\w+)/$1/eeg;       #interpolate $cmd
1232                 my @author = vcs_find_author($cmd);
1233                 next if !@author;
1234                 my $count = grep(/$commit/, @all_commits);
1235                 for ($i = 0; $i < $count ; $i++) {
1236                     push(@blame_signers, $author[0]);
1237                 }
1238             }
1239             if (@blame_signers) {
1240                 vcs_assign("authored lines", $total_lines, @blame_signers);
1241             }
1242         }
1243         vcs_assign("commits", $total_commits, @signers);
1244     } else {
1245         vcs_assign("modified commits", $total_commits, @signers);
1246     }
1247 }
1248
1249 sub uniq {
1250     my (@parms) = @_;
1251
1252     my %saw;
1253     @parms = grep(!$saw{$_}++, @parms);
1254     return @parms;
1255 }
1256
1257 sub sort_and_uniq {
1258     my (@parms) = @_;
1259
1260     my %saw;
1261     @parms = sort @parms;
1262     @parms = grep(!$saw{$_}++, @parms);
1263     return @parms;
1264 }
1265
1266 sub clean_file_emails {
1267     my (@file_emails) = @_;
1268     my @fmt_emails = ();
1269
1270     foreach my $email (@file_emails) {
1271         $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
1272         my ($name, $address) = parse_email($email);
1273         if ($name eq '"[,\.]"') {
1274             $name = "";
1275         }
1276
1277         my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
1278         if (@nw > 2) {
1279             my $first = $nw[@nw - 3];
1280             my $middle = $nw[@nw - 2];
1281             my $last = $nw[@nw - 1];
1282
1283             if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
1284                  (length($first) == 2 && substr($first, -1) eq ".")) ||
1285                 (length($middle) == 1 ||
1286                  (length($middle) == 2 && substr($middle, -1) eq "."))) {
1287                 $name = "$first $middle $last";
1288             } else {
1289                 $name = "$middle $last";
1290             }
1291         }
1292
1293         if (substr($name, -1) =~ /[,\.]/) {
1294             $name = substr($name, 0, length($name) - 1);
1295         } elsif (substr($name, -2) =~ /[,\.]"/) {
1296             $name = substr($name, 0, length($name) - 2) . '"';
1297         }
1298
1299         if (substr($name, 0, 1) =~ /[,\.]/) {
1300             $name = substr($name, 1, length($name) - 1);
1301         } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
1302             $name = '"' . substr($name, 2, length($name) - 2);
1303         }
1304
1305         my $fmt_email = format_email($name, $address, $email_usename);
1306         push(@fmt_emails, $fmt_email);
1307     }
1308     return @fmt_emails;
1309 }
1310
1311 sub merge_email {
1312     my @lines;
1313     my %saw;
1314
1315     for (@_) {
1316         my ($address, $role) = @$_;
1317         if (!$saw{$address}) {
1318             if ($output_roles) {
1319                 push(@lines, "$address ($role)");
1320             } else {
1321                 push(@lines, $address);
1322             }
1323             $saw{$address} = 1;
1324         }
1325     }
1326
1327     return @lines;
1328 }
1329
1330 sub output {
1331     my (@parms) = @_;
1332
1333     if ($output_multiline) {
1334         foreach my $line (@parms) {
1335             print("${line}\n");
1336         }
1337     } else {
1338         print(join($output_separator, @parms));
1339         print("\n");
1340     }
1341 }
1342
1343 my $rfc822re;
1344
1345 sub make_rfc822re {
1346 #   Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
1347 #   comment.  We must allow for rfc822_lwsp (or comments) after each of these.
1348 #   This regexp will only work on addresses which have had comments stripped
1349 #   and replaced with rfc822_lwsp.
1350
1351     my $specials = '()<>@,;:\\\\".\\[\\]';
1352     my $controls = '\\000-\\037\\177';
1353
1354     my $dtext = "[^\\[\\]\\r\\\\]";
1355     my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1356
1357     my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1358
1359 #   Use zero-width assertion to spot the limit of an atom.  A simple
1360 #   $rfc822_lwsp* causes the regexp engine to hang occasionally.
1361     my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
1362     my $word = "(?:$atom|$quoted_string)";
1363     my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
1364
1365     my $sub_domain = "(?:$atom|$domain_literal)";
1366     my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1367
1368     my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1369
1370     my $phrase = "$word*";
1371     my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
1372     my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
1373     my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
1374
1375     my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1376     my $address = "(?:$mailbox|$group)";
1377
1378     return "$rfc822_lwsp*$address";
1379 }
1380
1381 sub rfc822_strip_comments {
1382     my $s = shift;
1383 #   Recursively remove comments, and replace with a single space.  The simpler
1384 #   regexps in the Email Addressing FAQ are imperfect - they will miss escaped
1385 #   chars in atoms, for example.
1386
1387     while ($s =~ s/^((?:[^"\\]|\\.)*
1388                     (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1389                     \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1390     return $s;
1391 }
1392
1393 #   valid: returns true if the parameter is an RFC822 valid address
1394 #
1395 sub rfc822_valid {
1396     my $s = rfc822_strip_comments(shift);
1397
1398     if (!$rfc822re) {
1399         $rfc822re = make_rfc822re();
1400     }
1401
1402     return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1403 }
1404
1405 #   validlist: In scalar context, returns true if the parameter is an RFC822
1406 #              valid list of addresses.
1407 #
1408 #              In list context, returns an empty list on failure (an invalid
1409 #              address was found); otherwise a list whose first element is the
1410 #              number of addresses found and whose remaining elements are the
1411 #              addresses.  This is needed to disambiguate failure (invalid)
1412 #              from success with no addresses found, because an empty string is
1413 #              a valid list.
1414
1415 sub rfc822_validlist {
1416     my $s = rfc822_strip_comments(shift);
1417
1418     if (!$rfc822re) {
1419         $rfc822re = make_rfc822re();
1420     }
1421     # * null list items are valid according to the RFC
1422     # * the '1' business is to aid in distinguishing failure from no results
1423
1424     my @r;
1425     if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
1426         $s =~ m/^$rfc822_char*$/) {
1427         while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
1428             push(@r, $1);
1429         }
1430         return wantarray ? (scalar(@r), @r) : 1;
1431     }
1432     return wantarray ? () : 0;
1433 }