2 # (c) 2007, Joe Perches <joe@perches.com>
3 # created from checkpatch.pl
5 # Print selected MAINTAINERS information for
6 # the files modified in a patch or for a file
8 # usage: perl scripts/get_maintainer.pl [OPTIONS] <patch>
9 # perl scripts/get_maintainer.pl [OPTIONS] -f <file>
11 # Licensed under the terms of the GNU GPL License version 2
18 use Getopt::Long qw(:config no_auto_abbrev);
22 my $email_usename = 1;
23 my $email_maintainer = 1;
25 my $email_subscriber_list = 0;
26 my $email_git_penguin_chiefs = 0;
28 my $email_git_all_signature_types = 0;
29 my $email_git_blame = 0;
30 my $email_git_fallback = 1;
31 my $email_git_min_signatures = 1;
32 my $email_git_max_maintainers = 5;
33 my $email_git_min_percent = 5;
34 my $email_git_since = "1-year-ago";
35 my $email_hg_since = "-365";
36 my $email_remove_duplicates = 1;
37 my $output_multiline = 1;
38 my $output_separator = ", ";
40 my $output_rolestats = 0;
48 my $from_filename = 0;
49 my $pattern_depth = 0;
55 my @penguin_chief = ();
56 push(@penguin_chief, "Linus Torvalds:torvalds\@linux-foundation.org");
57 #Andrew wants in on most everything - 2009/01/14
58 #push(@penguin_chief, "Andrew Morton:akpm\@linux-foundation.org");
60 my @penguin_chief_names = ();
61 foreach my $chief (@penguin_chief) {
62 if ($chief =~ m/^(.*):(.*)/) {
65 push(@penguin_chief_names, $chief_name);
68 my $penguin_chiefs = "\(" . join("|", @penguin_chief_names) . "\)";
70 # Signature types of people who are either
71 # a) responsible for the code in question, or
72 # b) familiar enough with it to give relevant feedback
73 my @signature_tags = ();
74 push(@signature_tags, "Signed-off-by:");
75 push(@signature_tags, "Reviewed-by:");
76 push(@signature_tags, "Acked-by:");
77 my $signaturePattern = "\(" . join("|", @signature_tags) . "\)";
79 # rfc822 email address - preloaded methods go here.
80 my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
81 my $rfc822_char = '[\\000-\\377]';
83 # VCS command support: class-like functions and strings
88 "execute_cmd" => \&git_execute_cmd,
89 "available" => '(which("git") ne "") && (-d ".git")',
90 "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file",
91 "find_commit_signers_cmd" => "git log --no-color -1 \$commit",
92 "find_commit_author_cmd" => "git log -1 --format=\"%an <%ae>\" \$commit",
93 "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
94 "blame_file_cmd" => "git blame -l \$file",
95 "commit_pattern" => "^commit [0-9a-f]{40,40}",
96 "blame_commit_pattern" => "^([0-9a-f]+) "
100 "execute_cmd" => \&hg_execute_cmd,
101 "available" => '(which("hg") ne "") && (-d ".hg")',
102 "find_signers_cmd" =>
103 "hg log --date=\$email_hg_since" .
104 " --template='commit {node}\\n{desc}\\n' -- \$file",
105 "find_commit_signers_cmd" => "hg log --template='{desc}\\n' -r \$commit",
106 "find_commit_author_cmd" => "hg log -l 1 --template='{author}\\n' -r \$commit",
107 "blame_range_cmd" => "", # not supported
108 "blame_file_cmd" => "hg blame -c \$file",
109 "commit_pattern" => "^commit [0-9a-f]{40,40}",
110 "blame_commit_pattern" => "^([0-9a-f]+):"
113 my $conf = which_conf(".get_maintainer.conf");
116 open(my $conffile, '<', "$conf")
117 or warn "$P: Can't find a readable .get_maintainer.conf file $!\n";
119 while (<$conffile>) {
122 $line =~ s/\s*\n?$//g;
126 next if ($line =~ m/^\s*#/);
127 next if ($line =~ m/^\s*$/);
129 my @words = split(" ", $line);
130 foreach my $word (@words) {
131 last if ($word =~ m/^#/);
132 push (@conf_args, $word);
136 unshift(@ARGV, @conf_args) if @conf_args;
141 'git!' => \$email_git,
142 'git-all-signature-types!' => \$email_git_all_signature_types,
143 'git-blame!' => \$email_git_blame,
144 'git-fallback!' => \$email_git_fallback,
145 'git-chief-penguins!' => \$email_git_penguin_chiefs,
146 'git-min-signatures=i' => \$email_git_min_signatures,
147 'git-max-maintainers=i' => \$email_git_max_maintainers,
148 'git-min-percent=i' => \$email_git_min_percent,
149 'git-since=s' => \$email_git_since,
150 'hg-since=s' => \$email_hg_since,
151 'remove-duplicates!' => \$email_remove_duplicates,
152 'm!' => \$email_maintainer,
153 'n!' => \$email_usename,
154 'l!' => \$email_list,
155 's!' => \$email_subscriber_list,
156 'multiline!' => \$output_multiline,
157 'roles!' => \$output_roles,
158 'rolestats!' => \$output_rolestats,
159 'separator=s' => \$output_separator,
160 'subsystem!' => \$subsystem,
161 'status!' => \$status,
164 'pattern-depth=i' => \$pattern_depth,
165 'k|keywords!' => \$keywords,
166 'sections!' => \$sections,
167 'fe|file-emails!' => \$file_emails,
168 'f|file' => \$from_filename,
169 'v|version' => \$version,
170 'h|help|usage' => \$help,
172 die "$P: invalid argument - use --help if necessary\n";
181 print("${P} ${V}\n");
185 if (-t STDIN && !@ARGV) {
186 # We're talking to a terminal, but have no command line arguments.
187 die "$P: missing patchfile or -f file - use --help if necessary\n";
190 if ($output_separator ne ", ") {
191 $output_multiline = 0;
194 if ($output_rolestats) {
207 my $selections = $email + $scm + $status + $subsystem + $web;
208 if ($selections == 0) {
209 die "$P: Missing required option: email, scm, status, subsystem or web\n";
214 ($email_maintainer + $email_list + $email_subscriber_list +
215 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
216 die "$P: Please select at least 1 email option\n";
219 if (!top_of_kernel_tree($lk_path)) {
220 die "$P: The current directory does not appear to be "
221 . "a linux kernel source tree.\n";
224 if ($email_git_all_signature_types) {
225 $signaturePattern = "(.+?)[Bb][Yy]:";
228 ## Read MAINTAINERS for type/value pairs
233 open (my $maint, '<', "${lk_path}MAINTAINERS")
234 or die "$P: Can't open MAINTAINERS: $!\n";
238 if ($line =~ m/^(\C):\s*(.*)/) {
242 ##Filename pattern matching
243 if ($type eq "F" || $type eq "X") {
244 $value =~ s@\.@\\\.@g; ##Convert . to \.
245 $value =~ s/\*/\.\*/g; ##Convert * to .*
246 $value =~ s/\?/\./g; ##Convert ? to .
247 ##if pattern is a directory and it lacks a trailing slash, add one
249 $value =~ s@([^/])$@$1/@;
251 } elsif ($type eq "K") {
252 $keyword_hash{@typevalue} = $value;
254 push(@typevalue, "$type:$value");
255 } elsif (!/^(\s)*$/) {
257 push(@typevalue, $line);
264 if ($email_remove_duplicates) {
265 open(my $mailmap, '<', "${lk_path}.mailmap")
266 or warn "$P: Can't open .mailmap: $!\n";
270 next if ($line =~ m/^\s*#/);
271 next if ($line =~ m/^\s*$/);
273 my ($name, $address) = parse_email($line);
274 $line = format_email($name, $address, $email_usename);
276 next if ($line =~ m/^\s*$/);
278 if (exists($mailmap{$name})) {
279 my $obj = $mailmap{$name};
280 push(@$obj, $address);
282 my @arr = ($address);
283 $mailmap{$name} = \@arr;
289 ## use the filenames on the command line or find the filenames in the patchfiles
293 my @keyword_tvi = ();
294 my @file_emails = ();
297 push(@ARGV, "&STDIN");
300 foreach my $file (@ARGV) {
301 if ($file ne "&STDIN") {
302 ##if $file is a directory and it lacks a trailing slash, add one
304 $file =~ s@([^/])$@$1/@;
305 } elsif (!(-f $file)) {
306 die "$P: file '${file}' not found\n";
309 if ($from_filename) {
311 if ($file ne "MAINTAINERS" && -f $file && ($keywords || $file_emails)) {
312 open(my $f, '<', $file)
313 or die "$P: Can't open $file: $!\n";
314 my $text = do { local($/) ; <$f> };
317 foreach my $line (keys %keyword_hash) {
318 if ($text =~ m/$keyword_hash{$line}/x) {
319 push(@keyword_tvi, $line);
324 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;
325 push(@file_emails, clean_file_emails(@poss_addr));
329 my $file_cnt = @files;
332 open(my $patch, "< $file")
333 or die "$P: Can't open $file: $!\n";
336 if (m/^\+\+\+\s+(\S+)/) {
338 $filename =~ s@^[^/]*/@@;
340 $lastfile = $filename;
341 push(@files, $filename);
342 } elsif (m/^\@\@ -(\d+),(\d+)/) {
343 if ($email_git_blame) {
344 push(@range, "$lastfile:$1:$2");
346 } elsif ($keywords) {
347 foreach my $line (keys %keyword_hash) {
348 if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
349 push(@keyword_tvi, $line);
356 if ($file_cnt == @files) {
357 warn "$P: file '${file}' doesn't appear to be a patch. "
358 . "Add -f to options?\n";
360 @files = sort_and_uniq(@files);
364 @file_emails = uniq(@file_emails);
373 # Find responsible parties
375 foreach my $file (@files) {
378 my $exact_pattern_match = 0;
379 my $tvi = find_first_section();
380 while ($tvi < @typevalue) {
381 my $start = find_starting_index($tvi);
382 my $end = find_ending_index($tvi);
386 #Do not match excluded file patterns
388 for ($i = $start; $i < $end; $i++) {
389 my $line = $typevalue[$i];
390 if ($line =~ m/^(\C):\s*(.*)/) {
394 if (file_match_pattern($file, $value)) {
403 for ($i = $start; $i < $end; $i++) {
404 my $line = $typevalue[$i];
405 if ($line =~ m/^(\C):\s*(.*)/) {
409 if (file_match_pattern($file, $value)) {
410 my $value_pd = ($value =~ tr@/@@);
411 my $file_pd = ($file =~ tr@/@@);
412 $value_pd++ if (substr($value,-1,1) ne "/");
413 $value_pd = -1 if ($value =~ /^\.\*/);
414 $exact_pattern_match = 1 if ($value_pd >= $file_pd);
415 if ($pattern_depth == 0 ||
416 (($file_pd - $value_pd) < $pattern_depth)) {
417 $hash{$tvi} = $value_pd;
428 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
429 add_categories($line);
432 my $start = find_starting_index($line);
433 my $end = find_ending_index($line);
434 for ($i = $start; $i < $end; $i++) {
435 my $line = $typevalue[$i];
436 if ($line =~ /^[FX]:/) { ##Restore file patterns
437 $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
438 $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
439 $line =~ s/\\\./\./g; ##Convert \. to .
440 $line =~ s/\.\*/\*/g; ##Convert .* to *
442 $line =~ s/^([A-Z]):/$1:\t/g;
450 ($email_git || ($email_git_fallback && !$exact_pattern_match))) {
451 vcs_file_signoffs($file);
454 if ($email && $email_git_blame) {
455 vcs_file_blame($file);
460 @keyword_tvi = sort_and_uniq(@keyword_tvi);
461 foreach my $line (@keyword_tvi) {
462 add_categories($line);
467 foreach my $chief (@penguin_chief) {
468 if ($chief =~ m/^(.*):(.*)/) {
471 $email_address = format_email($1, $2, $email_usename);
472 if ($email_git_penguin_chiefs) {
473 push(@email_to, [$email_address, 'chief penguin']);
475 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
480 foreach my $email (@file_emails) {
481 my ($name, $address) = parse_email($email);
483 my $tmp_email = format_email($name, $address, $email_usename);
484 push_email_address($tmp_email, '');
485 add_role($tmp_email, 'in file');
489 if ($email || $email_list) {
492 @to = (@to, @email_to);
495 @to = (@to, @list_to);
497 output(merge_email(@to));
506 @status = uniq(@status);
511 @subsystem = uniq(@subsystem);
522 sub file_match_pattern {
523 my ($file, $pattern) = @_;
524 if (substr($pattern, -1) eq "/") {
525 if ($file =~ m@^$pattern@) {
529 if ($file =~ m@^$pattern@) {
530 my $s1 = ($file =~ tr@/@@);
531 my $s2 = ($pattern =~ tr@/@@);
542 usage: $P [options] patchfile
543 $P [options] -f file|directory
546 MAINTAINER field selection options:
547 --email => print email address(es) if any
548 --git => include recent git \*-by: signers
549 --git-all-signature-types => include signers regardless of signature type
550 or use only ${signaturePattern} signers (default: $email_git_all_signature_types)
551 --git-fallback => use git when no exact MAINTAINERS pattern (default: $email_git_fallback)
552 --git-chief-penguins => include ${penguin_chiefs}
553 --git-min-signatures => number of signatures required (default: $email_git_min_signatures)
554 --git-max-maintainers => maximum maintainers to add (default: $email_git_max_maintainers)
555 --git-min-percent => minimum percentage of commits required (default: $email_git_min_percent)
556 --git-blame => use git blame to find modified commits for patch or file
557 --git-since => git history to use (default: $email_git_since)
558 --hg-since => hg history to use (default: $email_hg_since)
559 --m => include maintainer(s) if any
560 --n => include name 'Full Name <addr\@domain.tld>'
561 --l => include list(s) if any
562 --s => include subscriber only list(s) if any
563 --remove-duplicates => minimize duplicate email names/addresses
564 --roles => show roles (status:subsystem, git-signer, list, etc...)
565 --rolestats => show roles and statistics (commits/total_commits, %)
566 --file-emails => add email addresses found in -f file (default: 0 (off))
567 --scm => print SCM tree(s) if any
568 --status => print status if any
569 --subsystem => print subsystem name if any
570 --web => print website(s) if any
573 --separator [, ] => separator for multiple entries on 1 line
574 using --separator also sets --nomultiline if --separator is not [, ]
575 --multiline => print 1 entry per line
578 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
579 --keywords => scan patch for keywords (default: 1 (on))
580 --sections => print the entire subsystem sections with pattern matches
581 --version => show version
582 --help => show this help information
585 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
588 Using "-f directory" may give unexpected results:
589 Used with "--git", git signators for _all_ files in and below
590 directory are examined as git recurses directories.
591 Any specified X: (exclude) pattern matches are _not_ ignored.
592 Used with "--nogit", directory is used as a pattern match,
593 no individual file within the directory or subdirectory
595 Used with "--git-blame", does not iterate all files in directory
596 Using "--git-blame" is slow and may add old committers and authors
597 that are no longer active maintainers to the output.
598 Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
599 other automated tools that expect only ["name"] <email address>
600 may not work because of additional output after <email address>.
601 Using "--rolestats" and "--git-blame" shows the #/total=% commits,
602 not the percentage of the entire file authored. # of commits is
603 not a good measure of amount of code authored. 1 major commit may
604 contain a thousand lines, 5 trivial commits may modify a single line.
605 If git is not installed, but mercurial (hg) is installed and an .hg
606 repository exists, the following options apply to mercurial:
608 --git-min-signatures, --git-max-maintainers, --git-min-percent, and
610 Use --hg-since not --git-since to control date selection
611 File ".get_maintainer.conf", if it exists in the linux kernel source root
612 directory, can change whatever get_maintainer defaults are desired.
613 Entries in this file can be any command line argument.
614 This file is prepended to any additional command line arguments.
615 Multiple lines and # comments are allowed.
619 sub top_of_kernel_tree {
622 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
625 if ( (-f "${lk_path}COPYING")
626 && (-f "${lk_path}CREDITS")
627 && (-f "${lk_path}Kbuild")
628 && (-f "${lk_path}MAINTAINERS")
629 && (-f "${lk_path}Makefile")
630 && (-f "${lk_path}README")
631 && (-d "${lk_path}Documentation")
632 && (-d "${lk_path}arch")
633 && (-d "${lk_path}include")
634 && (-d "${lk_path}drivers")
635 && (-d "${lk_path}fs")
636 && (-d "${lk_path}init")
637 && (-d "${lk_path}ipc")
638 && (-d "${lk_path}kernel")
639 && (-d "${lk_path}lib")
640 && (-d "${lk_path}scripts")) {
647 my ($formatted_email) = @_;
652 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
655 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
657 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
661 $name =~ s/^\s+|\s+$//g;
662 $name =~ s/^\"|\"$//g;
663 $address =~ s/^\s+|\s+$//g;
665 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
666 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
670 return ($name, $address);
674 my ($name, $address, $usename) = @_;
678 $name =~ s/^\s+|\s+$//g;
679 $name =~ s/^\"|\"$//g;
680 $address =~ s/^\s+|\s+$//g;
682 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
683 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
689 $formatted_email = "$address";
691 $formatted_email = "$name <$address>";
694 $formatted_email = $address;
697 return $formatted_email;
700 sub find_first_section {
703 while ($index < @typevalue) {
704 my $tv = $typevalue[$index];
705 if (($tv =~ m/^(\C):\s*(.*)/)) {
714 sub find_starting_index {
718 my $tv = $typevalue[$index];
719 if (!($tv =~ m/^(\C):\s*(.*)/)) {
728 sub find_ending_index {
731 while ($index < @typevalue) {
732 my $tv = $typevalue[$index];
733 if (!($tv =~ m/^(\C):\s*(.*)/)) {
742 sub get_maintainer_role {
746 my $start = find_starting_index($index);
747 my $end = find_ending_index($index);
750 my $subsystem = $typevalue[$start];
751 if (length($subsystem) > 20) {
752 $subsystem = substr($subsystem, 0, 17);
753 $subsystem =~ s/\s*$//;
754 $subsystem = $subsystem . "...";
757 for ($i = $start + 1; $i < $end; $i++) {
758 my $tv = $typevalue[$i];
759 if ($tv =~ m/^(\C):\s*(.*)/) {
769 if ($role eq "supported") {
771 } elsif ($role eq "maintained") {
772 $role = "maintainer";
773 } elsif ($role eq "odd fixes") {
775 } elsif ($role eq "orphan") {
776 $role = "orphan minder";
777 } elsif ($role eq "obsolete") {
778 $role = "obsolete minder";
779 } elsif ($role eq "buried alive in reporters") {
780 $role = "chief penguin";
783 return $role . ":" . $subsystem;
790 my $start = find_starting_index($index);
791 my $end = find_ending_index($index);
793 my $subsystem = $typevalue[$start];
794 if (length($subsystem) > 20) {
795 $subsystem = substr($subsystem, 0, 17);
796 $subsystem =~ s/\s*$//;
797 $subsystem = $subsystem . "...";
800 if ($subsystem eq "THE REST") {
811 my $start = find_starting_index($index);
812 my $end = find_ending_index($index);
814 push(@subsystem, $typevalue[$start]);
816 for ($i = $start + 1; $i < $end; $i++) {
817 my $tv = $typevalue[$i];
818 if ($tv =~ m/^(\C):\s*(.*)/) {
822 my $list_address = $pvalue;
823 my $list_additional = "";
824 my $list_role = get_list_role($i);
826 if ($list_role ne "") {
827 $list_role = ":" . $list_role;
829 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
831 $list_additional = $2;
833 if ($list_additional =~ m/subscribers-only/) {
834 if ($email_subscriber_list) {
835 push(@list_to, [$list_address, "subscriber list${list_role}"]);
839 push(@list_to, [$list_address, "open list${list_role}"]);
842 } elsif ($ptype eq "M") {
843 my ($name, $address) = parse_email($pvalue);
846 my $tv = $typevalue[$i - 1];
847 if ($tv =~ m/^(\C):\s*(.*)/) {
850 $pvalue = format_email($name, $address, $email_usename);
855 if ($email_maintainer) {
856 my $role = get_maintainer_role($i);
857 push_email_addresses($pvalue, $role);
859 } elsif ($ptype eq "T") {
861 } elsif ($ptype eq "W") {
863 } elsif ($ptype eq "S") {
864 push(@status, $pvalue);
871 my %email_hash_address;
874 my ($name, $address) = @_;
876 return 1 if (($name eq "") && ($address eq ""));
877 return 1 if (($name ne "") && exists($email_hash_name{$name}));
878 return 1 if (($address ne "") && exists($email_hash_address{$address}));
883 sub push_email_address {
884 my ($line, $role) = @_;
886 my ($name, $address) = parse_email($line);
888 if ($address eq "") {
892 if (!$email_remove_duplicates) {
893 push(@email_to, [format_email($name, $address, $email_usename), $role]);
894 } elsif (!email_inuse($name, $address)) {
895 push(@email_to, [format_email($name, $address, $email_usename), $role]);
896 $email_hash_name{$name}++;
897 $email_hash_address{$address}++;
903 sub push_email_addresses {
904 my ($address, $role) = @_;
906 my @address_list = ();
908 if (rfc822_valid($address)) {
909 push_email_address($address, $role);
910 } elsif (@address_list = rfc822_validlist($address)) {
911 my $array_count = shift(@address_list);
912 while (my $entry = shift(@address_list)) {
913 push_email_address($entry, $role);
916 if (!push_email_address($address, $role)) {
917 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
923 my ($line, $role) = @_;
925 my ($name, $address) = parse_email($line);
926 my $email = format_email($name, $address, $email_usename);
928 foreach my $entry (@email_to) {
929 if ($email_remove_duplicates) {
930 my ($entry_name, $entry_address) = parse_email($entry->[0]);
931 if (($name eq $entry_name || $address eq $entry_address)
932 && ($role eq "" || !($entry->[1] =~ m/$role/))
934 if ($entry->[1] eq "") {
935 $entry->[1] = "$role";
937 $entry->[1] = "$entry->[1],$role";
941 if ($email eq $entry->[0]
942 && ($role eq "" || !($entry->[1] =~ m/$role/))
944 if ($entry->[1] eq "") {
945 $entry->[1] = "$role";
947 $entry->[1] = "$entry->[1],$role";
957 foreach my $path (split(/:/, $ENV{PATH})) {
958 if (-e "$path/$bin") {
969 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
970 if (-e "$path/$conf") {
971 return "$path/$conf";
982 foreach my $line (@lines) {
983 my ($name, $address) = parse_email($line);
984 if (!exists($hash{$name})) {
985 $hash{$name} = $address;
986 } elsif ($address ne $hash{$name}) {
987 $address = $hash{$name};
988 $line = format_email($name, $address, $email_usename);
990 if (exists($mailmap{$name})) {
991 my $obj = $mailmap{$name};
992 foreach my $map_address (@$obj) {
993 if (($map_address eq $address) &&
994 ($map_address ne $hash{$name})) {
995 $line = format_email($name, $hash{$name}, $email_usename);
1004 sub git_execute_cmd {
1008 my $output = `$cmd`;
1009 $output =~ s/^\s*//gm;
1010 @lines = split("\n", $output);
1015 sub hg_execute_cmd {
1019 my $output = `$cmd`;
1020 @lines = split("\n", $output);
1025 sub vcs_find_signers {
1030 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1032 my $pattern = $VCS_cmds{"commit_pattern"};
1034 $commits = grep(/$pattern/, @lines); # of commits
1036 @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines);
1037 if (!$email_git_penguin_chiefs) {
1038 @lines = grep(!/${penguin_chiefs}/i, @lines);
1041 return (0, @lines) if !@lines;
1044 s/.*:\s*(.+)\s*/$1/ for (@lines);
1046 ## Reformat email addresses (with names) to avoid badly written signatures
1048 foreach my $line (@lines) {
1049 my ($name, $address) = parse_email($line);
1050 $line = format_email($name, $address, 1);
1053 return ($commits, @lines);
1056 sub vcs_find_author {
1060 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1062 if (!$email_git_penguin_chiefs) {
1063 @lines = grep(!/${penguin_chiefs}/i, @lines);
1066 return @lines if !@lines;
1068 ## Reformat email addresses (with names) to avoid badly written signatures
1070 foreach my $line (@lines) {
1071 my ($name, $address) = parse_email($line);
1072 $line = format_email($name, $address, 1);
1078 sub vcs_save_commits {
1083 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1085 foreach my $line (@lines) {
1086 if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
1099 return @commits if (!(-f $file));
1101 if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
1102 my @all_commits = ();
1104 $cmd = $VCS_cmds{"blame_file_cmd"};
1105 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1106 @all_commits = vcs_save_commits($cmd);
1108 foreach my $file_range_diff (@range) {
1109 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1111 my $diff_start = $2;
1112 my $diff_length = $3;
1113 next if ("$file" ne "$diff_file");
1114 for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
1115 push(@commits, $all_commits[$i]);
1119 foreach my $file_range_diff (@range) {
1120 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1122 my $diff_start = $2;
1123 my $diff_length = $3;
1124 next if ("$file" ne "$diff_file");
1125 $cmd = $VCS_cmds{"blame_range_cmd"};
1126 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1127 push(@commits, vcs_save_commits($cmd));
1130 $cmd = $VCS_cmds{"blame_file_cmd"};
1131 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1132 @commits = vcs_save_commits($cmd);
1135 foreach my $commit (@commits) {
1136 $commit =~ s/^\^//g;
1142 my $printed_novcs = 0;
1144 %VCS_cmds = %VCS_cmds_git;
1145 return 1 if eval $VCS_cmds{"available"};
1146 %VCS_cmds = %VCS_cmds_hg;
1147 return 1 if eval $VCS_cmds{"available"};
1149 if (!$printed_novcs) {
1150 warn("$P: No supported VCS found. Add --nogit to options?\n");
1151 warn("Using a git repository produces better results.\n");
1152 warn("Try Linus Torvalds' latest git repository using:\n");
1153 warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
1160 my ($role, $divisor, @lines) = @_;
1165 return if (@lines <= 0);
1167 if ($divisor <= 0) {
1168 warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
1172 if ($email_remove_duplicates) {
1173 @lines = mailmap(@lines);
1176 return if (@lines <= 0);
1178 @lines = sort(@lines);
1181 $hash{$_}++ for @lines;
1184 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
1185 my $sign_offs = $hash{$line};
1186 my $percent = $sign_offs * 100 / $divisor;
1188 $percent = 100 if ($percent > 100);
1190 last if ($sign_offs < $email_git_min_signatures ||
1191 $count > $email_git_max_maintainers ||
1192 $percent < $email_git_min_percent);
1193 push_email_address($line, '');
1194 if ($output_rolestats) {
1195 my $fmt_percent = sprintf("%.0f", $percent);
1196 add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
1198 add_role($line, $role);
1203 sub vcs_file_signoffs {
1209 return if (!vcs_exists());
1211 my $cmd = $VCS_cmds{"find_signers_cmd"};
1212 $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
1214 ($commits, @signers) = vcs_find_signers($cmd);
1215 vcs_assign("commit_signer", $commits, @signers);
1218 sub vcs_file_blame {
1222 my @all_commits = ();
1227 return if (!vcs_exists());
1229 @all_commits = vcs_blame($file);
1230 @commits = uniq(@all_commits);
1231 $total_commits = @commits;
1232 $total_lines = @all_commits;
1234 foreach my $commit (@commits) {
1236 my @commit_signers = ();
1238 my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1239 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1241 ($commit_count, @commit_signers) = vcs_find_signers($cmd);
1243 push(@signers, @commit_signers);
1246 if ($from_filename) {
1247 if ($output_rolestats) {
1249 foreach my $commit (@commits) {
1251 my $cmd = $VCS_cmds{"find_commit_author_cmd"};
1252 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1253 my @author = vcs_find_author($cmd);
1255 my $count = grep(/$commit/, @all_commits);
1256 for ($i = 0; $i < $count ; $i++) {
1257 push(@blame_signers, $author[0]);
1260 if (@blame_signers) {
1261 vcs_assign("authored lines", $total_lines, @blame_signers);
1264 vcs_assign("commits", $total_commits, @signers);
1266 vcs_assign("modified commits", $total_commits, @signers);
1274 @parms = grep(!$saw{$_}++, @parms);
1282 @parms = sort @parms;
1283 @parms = grep(!$saw{$_}++, @parms);
1287 sub clean_file_emails {
1288 my (@file_emails) = @_;
1289 my @fmt_emails = ();
1291 foreach my $email (@file_emails) {
1292 $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
1293 my ($name, $address) = parse_email($email);
1294 if ($name eq '"[,\.]"') {
1298 my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
1300 my $first = $nw[@nw - 3];
1301 my $middle = $nw[@nw - 2];
1302 my $last = $nw[@nw - 1];
1304 if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
1305 (length($first) == 2 && substr($first, -1) eq ".")) ||
1306 (length($middle) == 1 ||
1307 (length($middle) == 2 && substr($middle, -1) eq "."))) {
1308 $name = "$first $middle $last";
1310 $name = "$middle $last";
1314 if (substr($name, -1) =~ /[,\.]/) {
1315 $name = substr($name, 0, length($name) - 1);
1316 } elsif (substr($name, -2) =~ /[,\.]"/) {
1317 $name = substr($name, 0, length($name) - 2) . '"';
1320 if (substr($name, 0, 1) =~ /[,\.]/) {
1321 $name = substr($name, 1, length($name) - 1);
1322 } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
1323 $name = '"' . substr($name, 2, length($name) - 2);
1326 my $fmt_email = format_email($name, $address, $email_usename);
1327 push(@fmt_emails, $fmt_email);
1337 my ($address, $role) = @$_;
1338 if (!$saw{$address}) {
1339 if ($output_roles) {
1340 push(@lines, "$address ($role)");
1342 push(@lines, $address);
1354 if ($output_multiline) {
1355 foreach my $line (@parms) {
1359 print(join($output_separator, @parms));
1367 # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
1368 # comment. We must allow for rfc822_lwsp (or comments) after each of these.
1369 # This regexp will only work on addresses which have had comments stripped
1370 # and replaced with rfc822_lwsp.
1372 my $specials = '()<>@,;:\\\\".\\[\\]';
1373 my $controls = '\\000-\\037\\177';
1375 my $dtext = "[^\\[\\]\\r\\\\]";
1376 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1378 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1380 # Use zero-width assertion to spot the limit of an atom. A simple
1381 # $rfc822_lwsp* causes the regexp engine to hang occasionally.
1382 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
1383 my $word = "(?:$atom|$quoted_string)";
1384 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
1386 my $sub_domain = "(?:$atom|$domain_literal)";
1387 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1389 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1391 my $phrase = "$word*";
1392 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
1393 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
1394 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
1396 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1397 my $address = "(?:$mailbox|$group)";
1399 return "$rfc822_lwsp*$address";
1402 sub rfc822_strip_comments {
1404 # Recursively remove comments, and replace with a single space. The simpler
1405 # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
1406 # chars in atoms, for example.
1408 while ($s =~ s/^((?:[^"\\]|\\.)*
1409 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1410 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1414 # valid: returns true if the parameter is an RFC822 valid address
1417 my $s = rfc822_strip_comments(shift);
1420 $rfc822re = make_rfc822re();
1423 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1426 # validlist: In scalar context, returns true if the parameter is an RFC822
1427 # valid list of addresses.
1429 # In list context, returns an empty list on failure (an invalid
1430 # address was found); otherwise a list whose first element is the
1431 # number of addresses found and whose remaining elements are the
1432 # addresses. This is needed to disambiguate failure (invalid)
1433 # from success with no addresses found, because an empty string is
1436 sub rfc822_validlist {
1437 my $s = rfc822_strip_comments(shift);
1440 $rfc822re = make_rfc822re();
1442 # * null list items are valid according to the RFC
1443 # * the '1' business is to aid in distinguishing failure from no results
1446 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
1447 $s =~ m/^$rfc822_char*$/) {
1448 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
1451 return wantarray ? (scalar(@r), @r) : 1;
1453 return wantarray ? () : 0;