4e11c271e61366cc9afe7ad5d2f5d6788b763036
[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.22';
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 = 1;
27 my $email_git_penguin_chiefs = 0;
28 my $email_git_min_signatures = 1;
29 my $email_git_max_maintainers = 5;
30 my $email_git_min_percent = 5;
31 my $email_git_since = "1-year-ago";
32 my $email_git_blame = 0;
33 my $email_remove_duplicates = 1;
34 my $output_multiline = 1;
35 my $output_separator = ", ";
36 my $output_roles = 0;
37 my $output_rolestats = 0;
38 my $scm = 0;
39 my $web = 0;
40 my $subsystem = 0;
41 my $status = 0;
42 my $keywords = 1;
43 my $from_filename = 0;
44 my $pattern_depth = 0;
45 my $version = 0;
46 my $help = 0;
47
48 my $exit = 0;
49
50 my @penguin_chief = ();
51 push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
52 #Andrew wants in on most everything - 2009/01/14
53 #push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
54
55 my @penguin_chief_names = ();
56 foreach my $chief (@penguin_chief) {
57     if ($chief =~ m/^(.*):(.*)/) {
58         my $chief_name = $1;
59         my $chief_addr = $2;
60         push(@penguin_chief_names, $chief_name);
61     }
62 }
63 my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
64
65 # rfc822 email address - preloaded methods go here.
66 my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
67 my $rfc822_char = '[\\000-\\377]';
68
69 if (!GetOptions(
70                 'email!' => \$email,
71                 'git!' => \$email_git,
72                 'git-chief-penguins!' => \$email_git_penguin_chiefs,
73                 'git-min-signatures=i' => \$email_git_min_signatures,
74                 'git-max-maintainers=i' => \$email_git_max_maintainers,
75                 'git-min-percent=i' => \$email_git_min_percent,
76                 'git-since=s' => \$email_git_since,
77                 'git-blame!' => \$email_git_blame,
78                 'remove-duplicates!' => \$email_remove_duplicates,
79                 'm!' => \$email_maintainer,
80                 'n!' => \$email_usename,
81                 'l!' => \$email_list,
82                 's!' => \$email_subscriber_list,
83                 'multiline!' => \$output_multiline,
84                 'roles!' => \$output_roles,
85                 'rolestats!' => \$output_rolestats,
86                 'separator=s' => \$output_separator,
87                 'subsystem!' => \$subsystem,
88                 'status!' => \$status,
89                 'scm!' => \$scm,
90                 'web!' => \$web,
91                 'pattern-depth=i' => \$pattern_depth,
92                 'k|keywords!' => \$keywords,
93                 'f|file' => \$from_filename,
94                 'v|version' => \$version,
95                 'h|help' => \$help,
96                 )) {
97     die "$P: invalid argument - use --help if necessary\n";
98 }
99
100 if ($help != 0) {
101     usage();
102     exit 0;
103 }
104
105 if ($version != 0) {
106     print("${P} ${V}\n");
107     exit 0;
108 }
109
110 if ($#ARGV < 0) {
111     usage();
112     die "$P: argument missing: patchfile or -f file please\n";
113 }
114
115 if ($output_separator ne ", ") {
116     $output_multiline = 0;
117 }
118
119 if ($output_rolestats) {
120     $output_roles = 1;
121 }
122
123 my $selections = $email + $scm + $status + $subsystem + $web;
124 if ($selections == 0) {
125     usage();
126     die "$P:  Missing required option: email, scm, status, subsystem or web\n";
127 }
128
129 if ($email &&
130     ($email_maintainer + $email_list + $email_subscriber_list +
131      $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
132     usage();
133     die "$P: Please select at least 1 email option\n";
134 }
135
136 if (!top_of_kernel_tree($lk_path)) {
137     die "$P: The current directory does not appear to be "
138         . "a linux kernel source tree.\n";
139 }
140
141 ## Read MAINTAINERS for type/value pairs
142
143 my @typevalue = ();
144 my %keyword_hash;
145
146 open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
147 while (<MAINT>) {
148     my $line = $_;
149
150     if ($line =~ m/^(\C):\s*(.*)/) {
151         my $type = $1;
152         my $value = $2;
153
154         ##Filename pattern matching
155         if ($type eq "F" || $type eq "X") {
156             $value =~ s@\.@\\\.@g;       ##Convert . to \.
157             $value =~ s/\*/\.\*/g;       ##Convert * to .*
158             $value =~ s/\?/\./g;         ##Convert ? to .
159             ##if pattern is a directory and it lacks a trailing slash, add one
160             if ((-d $value)) {
161                 $value =~ s@([^/])$@$1/@;
162             }
163         } elsif ($type eq "K") {
164             $keyword_hash{@typevalue} = $value;
165         }
166         push(@typevalue, "$type:$value");
167     } elsif (!/^(\s)*$/) {
168         $line =~ s/\n$//g;
169         push(@typevalue, $line);
170     }
171 }
172 close(MAINT);
173
174 my %mailmap;
175
176 if ($email_remove_duplicates) {
177     open(MAILMAP, "<${lk_path}.mailmap") || warn "$P: Can't open .mailmap\n";
178     while (<MAILMAP>) {
179         my $line = $_;
180
181         next if ($line =~ m/^\s*#/);
182         next if ($line =~ m/^\s*$/);
183
184         my ($name, $address) = parse_email($line);
185         $line = format_email($name, $address);
186
187         next if ($line =~ m/^\s*$/);
188
189         if (exists($mailmap{$name})) {
190             my $obj = $mailmap{$name};
191             push(@$obj, $address);
192         } else {
193             my @arr = ($address);
194             $mailmap{$name} = \@arr;
195         }
196     }
197     close(MAILMAP);
198 }
199
200 ## use the filenames on the command line or find the filenames in the patchfiles
201
202 my @files = ();
203 my @range = ();
204 my @keyword_tvi = ();
205
206 foreach my $file (@ARGV) {
207     ##if $file is a directory and it lacks a trailing slash, add one
208     if ((-d $file)) {
209         $file =~ s@([^/])$@$1/@;
210     } elsif (!(-f $file)) {
211         die "$P: file '${file}' not found\n";
212     }
213     if ($from_filename) {
214         push(@files, $file);
215         if (-f $file && $keywords) {
216             open(FILE, "<$file") or die "$P: Can't open ${file}\n";
217             while (<FILE>) {
218                 my $patch_line = $_;
219                 foreach my $line (keys %keyword_hash) {
220                     if ($patch_line =~ m/^.*$keyword_hash{$line}/x) {
221                         push(@keyword_tvi, $line);
222                     }
223                 }
224             }
225             close(FILE);
226         }
227     } else {
228         my $file_cnt = @files;
229         my $lastfile;
230         open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
231         while (<PATCH>) {
232             my $patch_line = $_;
233             if (m/^\+\+\+\s+(\S+)/) {
234                 my $filename = $1;
235                 $filename =~ s@^[^/]*/@@;
236                 $filename =~ s@\n@@;
237                 $lastfile = $filename;
238                 push(@files, $filename);
239             } elsif (m/^\@\@ -(\d+),(\d+)/) {
240                 if ($email_git_blame) {
241                     push(@range, "$lastfile:$1:$2");
242                 }
243             } elsif ($keywords) {
244                 foreach my $line (keys %keyword_hash) {
245                     if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
246                         push(@keyword_tvi, $line);
247                     }
248                 }
249             }
250         }
251         close(PATCH);
252         if ($file_cnt == @files) {
253             warn "$P: file '${file}' doesn't appear to be a patch.  "
254                 . "Add -f to options?\n";
255         }
256         @files = sort_and_uniq(@files);
257     }
258 }
259
260 my @email_to = ();
261 my @list_to = ();
262 my @scm = ();
263 my @web = ();
264 my @subsystem = ();
265 my @status = ();
266
267 # Find responsible parties
268
269 foreach my $file (@files) {
270
271 #Do not match excluded file patterns
272
273     my $exclude = 0;
274     foreach my $line (@typevalue) {
275         if ($line =~ m/^(\C):\s*(.*)/) {
276             my $type = $1;
277             my $value = $2;
278             if ($type eq 'X') {
279                 if (file_match_pattern($file, $value)) {
280                     $exclude = 1;
281                     last;
282                 }
283             }
284         }
285     }
286
287     if (!$exclude) {
288         my $tvi = 0;
289         my %hash;
290         foreach my $line (@typevalue) {
291             if ($line =~ m/^(\C):\s*(.*)/) {
292                 my $type = $1;
293                 my $value = $2;
294                 if ($type eq 'F') {
295                     if (file_match_pattern($file, $value)) {
296                         my $value_pd = ($value =~ tr@/@@);
297                         my $file_pd = ($file  =~ tr@/@@);
298                         $value_pd++ if (substr($value,-1,1) ne "/");
299                         if ($pattern_depth == 0 ||
300                             (($file_pd - $value_pd) < $pattern_depth)) {
301                             $hash{$tvi} = $value_pd;
302                         }
303                     }
304                 }
305             }
306             $tvi++;
307         }
308         foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
309             add_categories($line);
310         }
311     }
312
313     if ($email && $email_git) {
314         recent_git_signoffs($file);
315     }
316
317     if ($email && $email_git_blame) {
318         git_assign_blame($file);
319     }
320 }
321
322 if ($keywords) {
323     @keyword_tvi = sort_and_uniq(@keyword_tvi);
324     foreach my $line (@keyword_tvi) {
325         add_categories($line);
326     }
327 }
328
329 if ($email) {
330     foreach my $chief (@penguin_chief) {
331         if ($chief =~ m/^(.*):(.*)/) {
332             my $email_address;
333
334             $email_address = format_email($1, $2);
335             if ($email_git_penguin_chiefs) {
336                 push(@email_to, [$email_address, 'chief penguin']);
337             } else {
338                 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
339             }
340         }
341     }
342 }
343
344 if ($email || $email_list) {
345     my @to = ();
346     if ($email) {
347         @to = (@to, @email_to);
348     }
349     if ($email_list) {
350         @to = (@to, @list_to);
351     }
352     output(merge_email(@to));
353 }
354
355 if ($scm) {
356     @scm = uniq(@scm);
357     output(@scm);
358 }
359
360 if ($status) {
361     @status = uniq(@status);
362     output(@status);
363 }
364
365 if ($subsystem) {
366     @subsystem = uniq(@subsystem);
367     output(@subsystem);
368 }
369
370 if ($web) {
371     @web = uniq(@web);
372     output(@web);
373 }
374
375 exit($exit);
376
377 sub file_match_pattern {
378     my ($file, $pattern) = @_;
379     if (substr($pattern, -1) eq "/") {
380         if ($file =~ m@^$pattern@) {
381             return 1;
382         }
383     } else {
384         if ($file =~ m@^$pattern@) {
385             my $s1 = ($file =~ tr@/@@);
386             my $s2 = ($pattern =~ tr@/@@);
387             if ($s1 == $s2) {
388                 return 1;
389             }
390         }
391     }
392     return 0;
393 }
394
395 sub usage {
396     print <<EOT;
397 usage: $P [options] patchfile
398        $P [options] -f file|directory
399 version: $V
400
401 MAINTAINER field selection options:
402   --email => print email address(es) if any
403     --git => include recent git \*-by: signers
404     --git-chief-penguins => include ${penguin_chiefs}
405     --git-min-signatures => number of signatures required (default: 1)
406     --git-max-maintainers => maximum maintainers to add (default: 5)
407     --git-min-percent => minimum percentage of commits required (default: 5)
408     --git-since => git history to use (default: 1-year-ago)
409     --git-blame => use git blame to find modified commits for patch or file
410     --m => include maintainer(s) if any
411     --n => include name 'Full Name <addr\@domain.tld>'
412     --l => include list(s) if any
413     --s => include subscriber only list(s) if any
414     --remove-duplicates => minimize duplicate email names/addresses
415     --roles => show roles (status:subsystem, git-signer, list, etc...)
416     --rolestats => show roles and statistics (commits/total_commits, %)
417   --scm => print SCM tree(s) if any
418   --status => print status if any
419   --subsystem => print subsystem name if any
420   --web => print website(s) if any
421
422 Output type options:
423   --separator [, ] => separator for multiple entries on 1 line
424     using --separator also sets --nomultiline if --separator is not [, ]
425   --multiline => print 1 entry per line
426
427 Other options:
428   --pattern-depth => Number of pattern directory traversals (default: 0 (all))
429   --keywords => scan patch for keywords (default: 1 (on))
430   --version => show version
431   --help => show this help information
432
433 Default options:
434   [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
435
436 Notes:
437   Using "-f directory" may give unexpected results:
438       Used with "--git", git signators for _all_ files in and below
439           directory are examined as git recurses directories.
440           Any specified X: (exclude) pattern matches are _not_ ignored.
441       Used with "--nogit", directory is used as a pattern match,
442          no individual file within the directory or subdirectory
443          is matched.
444       Used with "--git-blame", does not iterate all files in directory
445   Using "--git-blame" is slow and may add old committers and authors
446       that are no longer active maintainers to the output.
447   Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
448       other automated tools that expect only ["name"] <email address>
449       may not work because of additional output after <email address>.
450   Using "--rolestats" and "--git-blame" shows the #/total=% commits,
451       not the percentage of the entire file authored.  # of commits is
452       not a good measure of amount of code authored.  1 major commit may
453       contain a thousand lines, 5 trivial commits may modify a single line.
454 EOT
455 }
456
457 sub top_of_kernel_tree {
458         my ($lk_path) = @_;
459
460         if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
461             $lk_path .= "/";
462         }
463         if (   (-f "${lk_path}COPYING")
464             && (-f "${lk_path}CREDITS")
465             && (-f "${lk_path}Kbuild")
466             && (-f "${lk_path}MAINTAINERS")
467             && (-f "${lk_path}Makefile")
468             && (-f "${lk_path}README")
469             && (-d "${lk_path}Documentation")
470             && (-d "${lk_path}arch")
471             && (-d "${lk_path}include")
472             && (-d "${lk_path}drivers")
473             && (-d "${lk_path}fs")
474             && (-d "${lk_path}init")
475             && (-d "${lk_path}ipc")
476             && (-d "${lk_path}kernel")
477             && (-d "${lk_path}lib")
478             && (-d "${lk_path}scripts")) {
479                 return 1;
480         }
481         return 0;
482 }
483
484 sub parse_email {
485     my ($formatted_email) = @_;
486
487     my $name = "";
488     my $address = "";
489
490     if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
491         $name = $1;
492         $address = $2;
493     } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
494         $address = $1;
495     } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
496         $address = $1;
497     }
498
499     $name =~ s/^\s+|\s+$//g;
500     $name =~ s/^\"|\"$//g;
501     $address =~ s/^\s+|\s+$//g;
502
503     if ($name =~ /[^a-z0-9 \.\-]/i) {    ##has "must quote" chars
504         $name =~ s/(?<!\\)"/\\"/g;       ##escape quotes
505         $name = "\"$name\"";
506     }
507
508     return ($name, $address);
509 }
510
511 sub format_email {
512     my ($name, $address) = @_;
513
514     my $formatted_email;
515
516     $name =~ s/^\s+|\s+$//g;
517     $name =~ s/^\"|\"$//g;
518     $address =~ s/^\s+|\s+$//g;
519
520     if ($name =~ /[^a-z0-9 \.\-]/i) {    ##has "must quote" chars
521         $name =~ s/(?<!\\)"/\\"/g;       ##escape quotes
522         $name = "\"$name\"";
523     }
524
525     if ($email_usename) {
526         if ("$name" eq "") {
527             $formatted_email = "$address";
528         } else {
529             $formatted_email = "$name <${address}>";
530         }
531     } else {
532         $formatted_email = $address;
533     }
534
535     return $formatted_email;
536 }
537
538 sub find_starting_index {
539     my ($index) = @_;
540
541     while ($index > 0) {
542         my $tv = $typevalue[$index];
543         if (!($tv =~ m/^(\C):\s*(.*)/)) {
544             last;
545         }
546         $index--;
547     }
548
549     return $index;
550 }
551
552 sub find_ending_index {
553     my ($index) = @_;
554
555     while ($index < @typevalue) {
556         my $tv = $typevalue[$index];
557         if (!($tv =~ m/^(\C):\s*(.*)/)) {
558             last;
559         }
560         $index++;
561     }
562
563     return $index;
564 }
565
566 sub get_maintainer_role {
567     my ($index) = @_;
568
569     my $i;
570     my $start = find_starting_index($index);
571     my $end = find_ending_index($index);
572
573     my $role;
574     my $subsystem = $typevalue[$start];
575     if (length($subsystem) > 20) {
576         $subsystem = substr($subsystem, 0, 17);
577         $subsystem =~ s/\s*$//;
578         $subsystem = $subsystem . "...";
579     }
580
581     for ($i = $start + 1; $i < $end; $i++) {
582         my $tv = $typevalue[$i];
583         if ($tv =~ m/^(\C):\s*(.*)/) {
584             my $ptype = $1;
585             my $pvalue = $2;
586             if ($ptype eq "S") {
587                 $role = $pvalue;
588             }
589         }
590     }
591
592     $role = lc($role);
593     if      ($role eq "supported") {
594         $role = "supporter";
595     } elsif ($role eq "maintained") {
596         $role = "maintainer";
597     } elsif ($role eq "odd fixes") {
598         $role = "odd fixer";
599     } elsif ($role eq "orphan") {
600         $role = "orphan minder";
601     } elsif ($role eq "obsolete") {
602         $role = "obsolete minder";
603     } elsif ($role eq "buried alive in reporters") {
604         $role = "chief penguin";
605     }
606
607     return $role . ":" . $subsystem;
608 }
609
610 sub get_list_role {
611     my ($index) = @_;
612
613     my $i;
614     my $start = find_starting_index($index);
615     my $end = find_ending_index($index);
616
617     my $subsystem = $typevalue[$start];
618     if (length($subsystem) > 20) {
619         $subsystem = substr($subsystem, 0, 17);
620         $subsystem =~ s/\s*$//;
621         $subsystem = $subsystem . "...";
622     }
623
624     if ($subsystem eq "THE REST") {
625         $subsystem = "";
626     }
627
628     return $subsystem;
629 }
630
631 sub add_categories {
632     my ($index) = @_;
633
634     my $i;
635     my $start = find_starting_index($index);
636     my $end = find_ending_index($index);
637
638     push(@subsystem, $typevalue[$start]);
639
640     for ($i = $start + 1; $i < $end; $i++) {
641         my $tv = $typevalue[$i];
642         if ($tv =~ m/^(\C):\s*(.*)/) {
643             my $ptype = $1;
644             my $pvalue = $2;
645             if ($ptype eq "L") {
646                 my $list_address = $pvalue;
647                 my $list_additional = "";
648                 my $list_role = get_list_role($i);
649
650                 if ($list_role ne "") {
651                     $list_role = ":" . $list_role;
652                 }
653                 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
654                     $list_address = $1;
655                     $list_additional = $2;
656                 }
657                 if ($list_additional =~ m/subscribers-only/) {
658                     if ($email_subscriber_list) {
659                         push(@list_to, [$list_address, "subscriber list${list_role}"]);
660                     }
661                 } else {
662                     if ($email_list) {
663                         push(@list_to, [$list_address, "open list${list_role}"]);
664                     }
665                 }
666             } elsif ($ptype eq "M") {
667                 my ($name, $address) = parse_email($pvalue);
668                 if ($name eq "") {
669                     if ($i > 0) {
670                         my $tv = $typevalue[$i - 1];
671                         if ($tv =~ m/^(\C):\s*(.*)/) {
672                             if ($1 eq "P") {
673                                 $name = $2;
674                                 $pvalue = format_email($name, $address);
675                             }
676                         }
677                     }
678                 }
679                 if ($email_maintainer) {
680                     my $role = get_maintainer_role($i);
681                     push_email_addresses($pvalue, $role);
682                 }
683             } elsif ($ptype eq "T") {
684                 push(@scm, $pvalue);
685             } elsif ($ptype eq "W") {
686                 push(@web, $pvalue);
687             } elsif ($ptype eq "S") {
688                 push(@status, $pvalue);
689             }
690         }
691     }
692 }
693
694 my %email_hash_name;
695 my %email_hash_address;
696
697 sub email_inuse {
698     my ($name, $address) = @_;
699
700     return 1 if (($name eq "") && ($address eq ""));
701     return 1 if (($name ne "") && exists($email_hash_name{$name}));
702     return 1 if (($address ne "") && exists($email_hash_address{$address}));
703
704     return 0;
705 }
706
707 sub push_email_address {
708     my ($line, $role) = @_;
709
710     my ($name, $address) = parse_email($line);
711
712     if ($address eq "") {
713         return 0;
714     }
715
716     if (!$email_remove_duplicates) {
717         push(@email_to, [format_email($name, $address), $role]);
718     } elsif (!email_inuse($name, $address)) {
719         push(@email_to, [format_email($name, $address), $role]);
720         $email_hash_name{$name}++;
721         $email_hash_address{$address}++;
722     }
723
724     return 1;
725 }
726
727 sub push_email_addresses {
728     my ($address, $role) = @_;
729
730     my @address_list = ();
731
732     if (rfc822_valid($address)) {
733         push_email_address($address, $role);
734     } elsif (@address_list = rfc822_validlist($address)) {
735         my $array_count = shift(@address_list);
736         while (my $entry = shift(@address_list)) {
737             push_email_address($entry, $role);
738         }
739     } else {
740         if (!push_email_address($address, $role)) {
741             warn("Invalid MAINTAINERS address: '" . $address . "'\n");
742         }
743     }
744 }
745
746 sub add_role {
747     my ($line, $role) = @_;
748
749     my ($name, $address) = parse_email($line);
750     my $email = format_email($name, $address);
751
752     foreach my $entry (@email_to) {
753         if ($email_remove_duplicates) {
754             my ($entry_name, $entry_address) = parse_email($entry->[0]);
755             if ($name eq $entry_name || $address eq $entry_address) {
756                 if ($entry->[1] eq "") {
757                     $entry->[1] = "$role";
758                 } else {
759                     $entry->[1] = "$entry->[1],$role";
760                 }
761             }
762         } else {
763             if ($email eq $entry->[0]) {
764                 if ($entry->[1] eq "") {
765                     $entry->[1] = "$role";
766                 } else {
767                     $entry->[1] = "$entry->[1],$role";
768                 }
769             }
770         }
771     }
772 }
773
774 sub which {
775     my ($bin) = @_;
776
777     foreach my $path (split(/:/, $ENV{PATH})) {
778         if (-e "$path/$bin") {
779             return "$path/$bin";
780         }
781     }
782
783     return "";
784 }
785
786 sub mailmap {
787     my @lines = @_;
788     my %hash;
789
790     foreach my $line (@lines) {
791         my ($name, $address) = parse_email($line);
792         if (!exists($hash{$name})) {
793             $hash{$name} = $address;
794         } elsif ($address ne $hash{$name}) {
795             $address = $hash{$name};
796             $line = format_email($name, $address);
797         }
798         if (exists($mailmap{$name})) {
799             my $obj = $mailmap{$name};
800             foreach my $map_address (@$obj) {
801                 if (($map_address eq $address) &&
802                     ($map_address ne $hash{$name})) {
803                     $line = format_email($name, $hash{$name});
804                 }
805             }
806         }
807     }
808
809     return @lines;
810 }
811
812 sub recent_git_signoffs {
813     my ($file) = @_;
814
815     my $sign_offs = "";
816     my $cmd = "";
817     my $output = "";
818     my $count = 0;
819     my @lines = ();
820     my %hash;
821     my $total_sign_offs;
822
823     if (which("git") eq "") {
824         warn("$P: git not found.  Add --nogit to options?\n");
825         return;
826     }
827     if (!(-d ".git")) {
828         warn("$P: .git directory not found.  Use a git repository for better results.\n");
829         warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
830         return;
831     }
832
833     $cmd = "git log --since=${email_git_since} -- ${file}";
834
835     $output = `${cmd}`;
836     $output =~ s/^\s*//gm;
837
838     @lines = split("\n", $output);
839
840     @lines = grep(/^[-_         a-z]+by:.*\@.*$/i, @lines);
841     if (!$email_git_penguin_chiefs) {
842         @lines = grep(!/${penguin_chiefs}/i, @lines);
843     }
844     # cut -f2- -d":"
845     s/.*:\s*(.+)\s*/$1/ for (@lines);
846
847     $total_sign_offs = @lines;
848     foreach my $line (@lines) {
849         my ($name, $address) = parse_email($line);
850         $line = format_email($name, $address);
851     }
852
853     if ($email_remove_duplicates) {
854         @lines = mailmap(@lines);
855     }
856
857     @lines = sort(@lines);
858
859     # uniq -c
860     $hash{$_}++ for @lines;
861
862     # sort -rn
863     foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
864         my $sign_offs = $hash{$line};
865         my $role;
866
867         $count++;
868         last if ($sign_offs < $email_git_min_signatures ||
869                  $count > $email_git_max_maintainers ||
870                  $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
871         push_email_address($line, '');
872         $role = "git-signer";
873         if ($output_rolestats) {
874             my $percent = sprintf("%.0f", $sign_offs * 100 / $total_sign_offs);
875             $role = "$role:$sign_offs/$total_sign_offs=$percent%";
876         }
877         add_role($line, $role);
878     }
879 }
880
881 sub save_commits {
882     my ($cmd, @commits) = @_;
883     my $output;
884     my @lines = ();
885
886     $output = `${cmd}`;
887
888     @lines = split("\n", $output);
889     foreach my $line (@lines) {
890         if ($line =~ m/^(\w+) /) {
891             push (@commits, $1);
892         }
893     }
894     return @commits;
895 }
896
897 sub git_assign_blame {
898     my ($file) = @_;
899
900     my @lines = ();
901     my @commits = ();
902     my $cmd;
903     my $output;
904     my %hash;
905     my $total_sign_offs;
906     my $count;
907
908     if (@range) {
909         foreach my $file_range_diff (@range) {
910             next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
911             my $diff_file = $1;
912             my $diff_start = $2;
913             my $diff_length = $3;
914             next if (!("$file" eq "$diff_file"));
915             $cmd = "git blame -l -L $diff_start,+$diff_length $file";
916             @commits = save_commits($cmd, @commits);
917         }
918     } else {
919         if (-f $file) {
920             $cmd = "git blame -l $file";
921             @commits = save_commits($cmd, @commits);
922         }
923     }
924
925     $total_sign_offs = 0;
926     @commits = uniq(@commits);
927     foreach my $commit (@commits) {
928         $cmd = "git log -1 ${commit}";
929
930         $output = `${cmd}`;
931         $output =~ s/^\s*//gm;
932         @lines = split("\n", $output);
933
934         @lines = grep(/^[-_     a-z]+by:.*\@.*$/i, @lines);
935         if (!$email_git_penguin_chiefs) {
936             @lines = grep(!/${penguin_chiefs}/i, @lines);
937         }
938
939         # cut -f2- -d":"
940         s/.*:\s*(.+)\s*/$1/ for (@lines);
941
942         $total_sign_offs += @lines;
943
944         if ($email_remove_duplicates) {
945             @lines = mailmap(@lines);
946         }
947
948         $hash{$_}++ for @lines;
949     }
950
951     $count = 0;
952     foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
953         my $sign_offs = $hash{$line};
954         my $role;
955
956         $count++;
957         last if ($sign_offs < $email_git_min_signatures ||
958                  $count > $email_git_max_maintainers ||
959                  $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
960         push_email_address($line, '');
961         if ($from_filename) {
962             $role = "commits";
963         } else {
964             $role = "modified commits";
965         }
966         if ($output_rolestats) {
967             my $percent = sprintf("%.0f", $sign_offs * 100 / $total_sign_offs);
968             $role = "$role:$sign_offs/$total_sign_offs=$percent%";
969         }
970         add_role($line, $role);
971     }
972 }
973
974 sub uniq {
975     my @parms = @_;
976
977     my %saw;
978     @parms = grep(!$saw{$_}++, @parms);
979     return @parms;
980 }
981
982 sub sort_and_uniq {
983     my @parms = @_;
984
985     my %saw;
986     @parms = sort @parms;
987     @parms = grep(!$saw{$_}++, @parms);
988     return @parms;
989 }
990
991 sub merge_email {
992     my @lines;
993     my %saw;
994
995     for (@_) {
996         my ($address, $role) = @$_;
997         if (!$saw{$address}) {
998             if ($output_roles) {
999                 push @lines, "$address ($role)";
1000             } else {
1001                 push @lines, $address;
1002             }
1003             $saw{$address} = 1;
1004         }
1005     }
1006
1007     return @lines;
1008 }
1009
1010 sub output {
1011     my @parms = @_;
1012
1013     if ($output_multiline) {
1014         foreach my $line (@parms) {
1015             print("${line}\n");
1016         }
1017     } else {
1018         print(join($output_separator, @parms));
1019         print("\n");
1020     }
1021 }
1022
1023 my $rfc822re;
1024
1025 sub make_rfc822re {
1026 #   Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
1027 #   comment.  We must allow for rfc822_lwsp (or comments) after each of these.
1028 #   This regexp will only work on addresses which have had comments stripped
1029 #   and replaced with rfc822_lwsp.
1030
1031     my $specials = '()<>@,;:\\\\".\\[\\]';
1032     my $controls = '\\000-\\037\\177';
1033
1034     my $dtext = "[^\\[\\]\\r\\\\]";
1035     my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1036
1037     my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1038
1039 #   Use zero-width assertion to spot the limit of an atom.  A simple
1040 #   $rfc822_lwsp* causes the regexp engine to hang occasionally.
1041     my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
1042     my $word = "(?:$atom|$quoted_string)";
1043     my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
1044
1045     my $sub_domain = "(?:$atom|$domain_literal)";
1046     my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1047
1048     my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1049
1050     my $phrase = "$word*";
1051     my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
1052     my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
1053     my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
1054
1055     my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1056     my $address = "(?:$mailbox|$group)";
1057
1058     return "$rfc822_lwsp*$address";
1059 }
1060
1061 sub rfc822_strip_comments {
1062     my $s = shift;
1063 #   Recursively remove comments, and replace with a single space.  The simpler
1064 #   regexps in the Email Addressing FAQ are imperfect - they will miss escaped
1065 #   chars in atoms, for example.
1066
1067     while ($s =~ s/^((?:[^"\\]|\\.)*
1068                     (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1069                     \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1070     return $s;
1071 }
1072
1073 #   valid: returns true if the parameter is an RFC822 valid address
1074 #
1075 sub rfc822_valid ($) {
1076     my $s = rfc822_strip_comments(shift);
1077
1078     if (!$rfc822re) {
1079         $rfc822re = make_rfc822re();
1080     }
1081
1082     return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1083 }
1084
1085 #   validlist: In scalar context, returns true if the parameter is an RFC822
1086 #              valid list of addresses.
1087 #
1088 #              In list context, returns an empty list on failure (an invalid
1089 #              address was found); otherwise a list whose first element is the
1090 #              number of addresses found and whose remaining elements are the
1091 #              addresses.  This is needed to disambiguate failure (invalid)
1092 #              from success with no addresses found, because an empty string is
1093 #              a valid list.
1094
1095 sub rfc822_validlist ($) {
1096     my $s = rfc822_strip_comments(shift);
1097
1098     if (!$rfc822re) {
1099         $rfc822re = make_rfc822re();
1100     }
1101     # * null list items are valid according to the RFC
1102     # * the '1' business is to aid in distinguishing failure from no results
1103
1104     my @r;
1105     if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
1106         $s =~ m/^$rfc822_char*$/) {
1107         while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
1108             push @r, $1;
1109         }
1110         return wantarray ? (scalar(@r), @r) : 1;
1111     }
1112     else {
1113         return wantarray ? () : 0;
1114     }
1115 }