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 if (-f "${lk_path}.get_maintainer.conf") {
115 open(my $conffile, '<', "${lk_path}.get_maintainer.conf")
116 or warn "$P: Can't open .get_maintainer.conf: $!\n";
117 while (<$conffile>) {
120 $line =~ s/\s*\n?$//g;
124 next if ($line =~ m/^\s*#/);
125 next if ($line =~ m/^\s*$/);
127 my @words = split(" ", $line);
128 foreach my $word (@words) {
129 last if ($word =~ m/^#/);
130 push (@conf_args, $word);
134 unshift(@ARGV, @conf_args) if @conf_args;
139 'git!' => \$email_git,
140 'git-all-signature-types!' => \$email_git_all_signature_types,
141 'git-blame!' => \$email_git_blame,
142 'git-fallback!' => \$email_git_fallback,
143 'git-chief-penguins!' => \$email_git_penguin_chiefs,
144 'git-min-signatures=i' => \$email_git_min_signatures,
145 'git-max-maintainers=i' => \$email_git_max_maintainers,
146 'git-min-percent=i' => \$email_git_min_percent,
147 'git-since=s' => \$email_git_since,
148 'hg-since=s' => \$email_hg_since,
149 'remove-duplicates!' => \$email_remove_duplicates,
150 'm!' => \$email_maintainer,
151 'n!' => \$email_usename,
152 'l!' => \$email_list,
153 's!' => \$email_subscriber_list,
154 'multiline!' => \$output_multiline,
155 'roles!' => \$output_roles,
156 'rolestats!' => \$output_rolestats,
157 'separator=s' => \$output_separator,
158 'subsystem!' => \$subsystem,
159 'status!' => \$status,
162 'pattern-depth=i' => \$pattern_depth,
163 'k|keywords!' => \$keywords,
164 'sections!' => \$sections,
165 'fe|file-emails!' => \$file_emails,
166 'f|file' => \$from_filename,
167 'v|version' => \$version,
168 'h|help|usage' => \$help,
170 die "$P: invalid argument - use --help if necessary\n";
179 print("${P} ${V}\n");
183 if (-t STDIN && !@ARGV) {
184 # We're talking to a terminal, but have no command line arguments.
185 die "$P: missing patchfile or -f file - use --help if necessary\n";
188 if ($output_separator ne ", ") {
189 $output_multiline = 0;
192 if ($output_rolestats) {
205 my $selections = $email + $scm + $status + $subsystem + $web;
206 if ($selections == 0) {
207 die "$P: Missing required option: email, scm, status, subsystem or web\n";
212 ($email_maintainer + $email_list + $email_subscriber_list +
213 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
214 die "$P: Please select at least 1 email option\n";
217 if (!top_of_kernel_tree($lk_path)) {
218 die "$P: The current directory does not appear to be "
219 . "a linux kernel source tree.\n";
222 if ($email_git_all_signature_types) {
223 $signaturePattern = "(.+?)[Bb][Yy]:";
226 ## Read MAINTAINERS for type/value pairs
231 open (my $maint, '<', "${lk_path}MAINTAINERS")
232 or die "$P: Can't open MAINTAINERS: $!\n";
236 if ($line =~ m/^(\C):\s*(.*)/) {
240 ##Filename pattern matching
241 if ($type eq "F" || $type eq "X") {
242 $value =~ s@\.@\\\.@g; ##Convert . to \.
243 $value =~ s/\*/\.\*/g; ##Convert * to .*
244 $value =~ s/\?/\./g; ##Convert ? to .
245 ##if pattern is a directory and it lacks a trailing slash, add one
247 $value =~ s@([^/])$@$1/@;
249 } elsif ($type eq "K") {
250 $keyword_hash{@typevalue} = $value;
252 push(@typevalue, "$type:$value");
253 } elsif (!/^(\s)*$/) {
255 push(@typevalue, $line);
262 if ($email_remove_duplicates) {
263 open(my $mailmap, '<', "${lk_path}.mailmap")
264 or warn "$P: Can't open .mailmap: $!\n";
268 next if ($line =~ m/^\s*#/);
269 next if ($line =~ m/^\s*$/);
271 my ($name, $address) = parse_email($line);
272 $line = format_email($name, $address, $email_usename);
274 next if ($line =~ m/^\s*$/);
276 if (exists($mailmap{$name})) {
277 my $obj = $mailmap{$name};
278 push(@$obj, $address);
280 my @arr = ($address);
281 $mailmap{$name} = \@arr;
287 ## use the filenames on the command line or find the filenames in the patchfiles
291 my @keyword_tvi = ();
292 my @file_emails = ();
295 push(@ARGV, "&STDIN");
298 foreach my $file (@ARGV) {
299 if ($file ne "&STDIN") {
300 ##if $file is a directory and it lacks a trailing slash, add one
302 $file =~ s@([^/])$@$1/@;
303 } elsif (!(-f $file)) {
304 die "$P: file '${file}' not found\n";
307 if ($from_filename) {
309 if ($file ne "MAINTAINERS" && -f $file && ($keywords || $file_emails)) {
310 open(my $f, '<', $file)
311 or die "$P: Can't open $file: $!\n";
312 my $text = do { local($/) ; <$f> };
315 foreach my $line (keys %keyword_hash) {
316 if ($text =~ m/$keyword_hash{$line}/x) {
317 push(@keyword_tvi, $line);
322 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;
323 push(@file_emails, clean_file_emails(@poss_addr));
327 my $file_cnt = @files;
330 open(my $patch, "< $file")
331 or die "$P: Can't open $file: $!\n";
334 if (m/^\+\+\+\s+(\S+)/) {
336 $filename =~ s@^[^/]*/@@;
338 $lastfile = $filename;
339 push(@files, $filename);
340 } elsif (m/^\@\@ -(\d+),(\d+)/) {
341 if ($email_git_blame) {
342 push(@range, "$lastfile:$1:$2");
344 } elsif ($keywords) {
345 foreach my $line (keys %keyword_hash) {
346 if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
347 push(@keyword_tvi, $line);
354 if ($file_cnt == @files) {
355 warn "$P: file '${file}' doesn't appear to be a patch. "
356 . "Add -f to options?\n";
358 @files = sort_and_uniq(@files);
362 @file_emails = uniq(@file_emails);
371 # Find responsible parties
373 foreach my $file (@files) {
376 my $exact_pattern_match = 0;
377 my $tvi = find_first_section();
378 while ($tvi < @typevalue) {
379 my $start = find_starting_index($tvi);
380 my $end = find_ending_index($tvi);
384 #Do not match excluded file patterns
386 for ($i = $start; $i < $end; $i++) {
387 my $line = $typevalue[$i];
388 if ($line =~ m/^(\C):\s*(.*)/) {
392 if (file_match_pattern($file, $value)) {
401 for ($i = $start; $i < $end; $i++) {
402 my $line = $typevalue[$i];
403 if ($line =~ m/^(\C):\s*(.*)/) {
407 if (file_match_pattern($file, $value)) {
408 my $value_pd = ($value =~ tr@/@@);
409 my $file_pd = ($file =~ tr@/@@);
410 $value_pd++ if (substr($value,-1,1) ne "/");
411 $value_pd = -1 if ($value =~ /^\.\*/);
412 $exact_pattern_match = 1 if ($value_pd >= $file_pd);
413 if ($pattern_depth == 0 ||
414 (($file_pd - $value_pd) < $pattern_depth)) {
415 $hash{$tvi} = $value_pd;
426 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
427 add_categories($line);
430 my $start = find_starting_index($line);
431 my $end = find_ending_index($line);
432 for ($i = $start; $i < $end; $i++) {
433 my $line = $typevalue[$i];
434 if ($line =~ /^[FX]:/) { ##Restore file patterns
435 $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
436 $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
437 $line =~ s/\\\./\./g; ##Convert \. to .
438 $line =~ s/\.\*/\*/g; ##Convert .* to *
440 $line =~ s/^([A-Z]):/$1:\t/g;
448 ($email_git || ($email_git_fallback && !$exact_pattern_match))) {
449 vcs_file_signoffs($file);
452 if ($email && $email_git_blame) {
453 vcs_file_blame($file);
458 @keyword_tvi = sort_and_uniq(@keyword_tvi);
459 foreach my $line (@keyword_tvi) {
460 add_categories($line);
465 foreach my $chief (@penguin_chief) {
466 if ($chief =~ m/^(.*):(.*)/) {
469 $email_address = format_email($1, $2, $email_usename);
470 if ($email_git_penguin_chiefs) {
471 push(@email_to, [$email_address, 'chief penguin']);
473 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
478 foreach my $email (@file_emails) {
479 my ($name, $address) = parse_email($email);
481 my $tmp_email = format_email($name, $address, $email_usename);
482 push_email_address($tmp_email, '');
483 add_role($tmp_email, 'in file');
487 if ($email || $email_list) {
490 @to = (@to, @email_to);
493 @to = (@to, @list_to);
495 output(merge_email(@to));
504 @status = uniq(@status);
509 @subsystem = uniq(@subsystem);
520 sub file_match_pattern {
521 my ($file, $pattern) = @_;
522 if (substr($pattern, -1) eq "/") {
523 if ($file =~ m@^$pattern@) {
527 if ($file =~ m@^$pattern@) {
528 my $s1 = ($file =~ tr@/@@);
529 my $s2 = ($pattern =~ tr@/@@);
540 usage: $P [options] patchfile
541 $P [options] -f file|directory
544 MAINTAINER field selection options:
545 --email => print email address(es) if any
546 --git => include recent git \*-by: signers
547 --git-all-signature-types => include signers regardless of signature type
548 or use only ${signaturePattern} signers (default: $email_git_all_signature_types)
549 --git-fallback => use git when no exact MAINTAINERS pattern (default: $email_git_fallback)
550 --git-chief-penguins => include ${penguin_chiefs}
551 --git-min-signatures => number of signatures required (default: $email_git_min_signatures)
552 --git-max-maintainers => maximum maintainers to add (default: $email_git_max_maintainers)
553 --git-min-percent => minimum percentage of commits required (default: $email_git_min_percent)
554 --git-blame => use git blame to find modified commits for patch or file
555 --git-since => git history to use (default: $email_git_since)
556 --hg-since => hg history to use (default: $email_hg_since)
557 --m => include maintainer(s) if any
558 --n => include name 'Full Name <addr\@domain.tld>'
559 --l => include list(s) if any
560 --s => include subscriber only list(s) if any
561 --remove-duplicates => minimize duplicate email names/addresses
562 --roles => show roles (status:subsystem, git-signer, list, etc...)
563 --rolestats => show roles and statistics (commits/total_commits, %)
564 --file-emails => add email addresses found in -f file (default: 0 (off))
565 --scm => print SCM tree(s) if any
566 --status => print status if any
567 --subsystem => print subsystem name if any
568 --web => print website(s) if any
571 --separator [, ] => separator for multiple entries on 1 line
572 using --separator also sets --nomultiline if --separator is not [, ]
573 --multiline => print 1 entry per line
576 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
577 --keywords => scan patch for keywords (default: 1 (on))
578 --sections => print the entire subsystem sections with pattern matches
579 --version => show version
580 --help => show this help information
583 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
586 Using "-f directory" may give unexpected results:
587 Used with "--git", git signators for _all_ files in and below
588 directory are examined as git recurses directories.
589 Any specified X: (exclude) pattern matches are _not_ ignored.
590 Used with "--nogit", directory is used as a pattern match,
591 no individual file within the directory or subdirectory
593 Used with "--git-blame", does not iterate all files in directory
594 Using "--git-blame" is slow and may add old committers and authors
595 that are no longer active maintainers to the output.
596 Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
597 other automated tools that expect only ["name"] <email address>
598 may not work because of additional output after <email address>.
599 Using "--rolestats" and "--git-blame" shows the #/total=% commits,
600 not the percentage of the entire file authored. # of commits is
601 not a good measure of amount of code authored. 1 major commit may
602 contain a thousand lines, 5 trivial commits may modify a single line.
603 If git is not installed, but mercurial (hg) is installed and an .hg
604 repository exists, the following options apply to mercurial:
606 --git-min-signatures, --git-max-maintainers, --git-min-percent, and
608 Use --hg-since not --git-since to control date selection
609 File ".get_maintainer.conf", if it exists in the linux kernel source root
610 directory, can change whatever get_maintainer defaults are desired.
611 Entries in this file can be any command line argument.
612 This file is prepended to any additional command line arguments.
613 Multiple lines and # comments are allowed.
617 sub top_of_kernel_tree {
620 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
623 if ( (-f "${lk_path}COPYING")
624 && (-f "${lk_path}CREDITS")
625 && (-f "${lk_path}Kbuild")
626 && (-f "${lk_path}MAINTAINERS")
627 && (-f "${lk_path}Makefile")
628 && (-f "${lk_path}README")
629 && (-d "${lk_path}Documentation")
630 && (-d "${lk_path}arch")
631 && (-d "${lk_path}include")
632 && (-d "${lk_path}drivers")
633 && (-d "${lk_path}fs")
634 && (-d "${lk_path}init")
635 && (-d "${lk_path}ipc")
636 && (-d "${lk_path}kernel")
637 && (-d "${lk_path}lib")
638 && (-d "${lk_path}scripts")) {
645 my ($formatted_email) = @_;
650 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
653 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
655 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
659 $name =~ s/^\s+|\s+$//g;
660 $name =~ s/^\"|\"$//g;
661 $address =~ s/^\s+|\s+$//g;
663 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
664 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
668 return ($name, $address);
672 my ($name, $address, $usename) = @_;
676 $name =~ s/^\s+|\s+$//g;
677 $name =~ s/^\"|\"$//g;
678 $address =~ s/^\s+|\s+$//g;
680 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
681 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
687 $formatted_email = "$address";
689 $formatted_email = "$name <$address>";
692 $formatted_email = $address;
695 return $formatted_email;
698 sub find_first_section {
701 while ($index < @typevalue) {
702 my $tv = $typevalue[$index];
703 if (($tv =~ m/^(\C):\s*(.*)/)) {
712 sub find_starting_index {
716 my $tv = $typevalue[$index];
717 if (!($tv =~ m/^(\C):\s*(.*)/)) {
726 sub find_ending_index {
729 while ($index < @typevalue) {
730 my $tv = $typevalue[$index];
731 if (!($tv =~ m/^(\C):\s*(.*)/)) {
740 sub get_maintainer_role {
744 my $start = find_starting_index($index);
745 my $end = find_ending_index($index);
748 my $subsystem = $typevalue[$start];
749 if (length($subsystem) > 20) {
750 $subsystem = substr($subsystem, 0, 17);
751 $subsystem =~ s/\s*$//;
752 $subsystem = $subsystem . "...";
755 for ($i = $start + 1; $i < $end; $i++) {
756 my $tv = $typevalue[$i];
757 if ($tv =~ m/^(\C):\s*(.*)/) {
767 if ($role eq "supported") {
769 } elsif ($role eq "maintained") {
770 $role = "maintainer";
771 } elsif ($role eq "odd fixes") {
773 } elsif ($role eq "orphan") {
774 $role = "orphan minder";
775 } elsif ($role eq "obsolete") {
776 $role = "obsolete minder";
777 } elsif ($role eq "buried alive in reporters") {
778 $role = "chief penguin";
781 return $role . ":" . $subsystem;
788 my $start = find_starting_index($index);
789 my $end = find_ending_index($index);
791 my $subsystem = $typevalue[$start];
792 if (length($subsystem) > 20) {
793 $subsystem = substr($subsystem, 0, 17);
794 $subsystem =~ s/\s*$//;
795 $subsystem = $subsystem . "...";
798 if ($subsystem eq "THE REST") {
809 my $start = find_starting_index($index);
810 my $end = find_ending_index($index);
812 push(@subsystem, $typevalue[$start]);
814 for ($i = $start + 1; $i < $end; $i++) {
815 my $tv = $typevalue[$i];
816 if ($tv =~ m/^(\C):\s*(.*)/) {
820 my $list_address = $pvalue;
821 my $list_additional = "";
822 my $list_role = get_list_role($i);
824 if ($list_role ne "") {
825 $list_role = ":" . $list_role;
827 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
829 $list_additional = $2;
831 if ($list_additional =~ m/subscribers-only/) {
832 if ($email_subscriber_list) {
833 push(@list_to, [$list_address, "subscriber list${list_role}"]);
837 push(@list_to, [$list_address, "open list${list_role}"]);
840 } elsif ($ptype eq "M") {
841 my ($name, $address) = parse_email($pvalue);
844 my $tv = $typevalue[$i - 1];
845 if ($tv =~ m/^(\C):\s*(.*)/) {
848 $pvalue = format_email($name, $address, $email_usename);
853 if ($email_maintainer) {
854 my $role = get_maintainer_role($i);
855 push_email_addresses($pvalue, $role);
857 } elsif ($ptype eq "T") {
859 } elsif ($ptype eq "W") {
861 } elsif ($ptype eq "S") {
862 push(@status, $pvalue);
869 my %email_hash_address;
872 my ($name, $address) = @_;
874 return 1 if (($name eq "") && ($address eq ""));
875 return 1 if (($name ne "") && exists($email_hash_name{$name}));
876 return 1 if (($address ne "") && exists($email_hash_address{$address}));
881 sub push_email_address {
882 my ($line, $role) = @_;
884 my ($name, $address) = parse_email($line);
886 if ($address eq "") {
890 if (!$email_remove_duplicates) {
891 push(@email_to, [format_email($name, $address, $email_usename), $role]);
892 } elsif (!email_inuse($name, $address)) {
893 push(@email_to, [format_email($name, $address, $email_usename), $role]);
894 $email_hash_name{$name}++;
895 $email_hash_address{$address}++;
901 sub push_email_addresses {
902 my ($address, $role) = @_;
904 my @address_list = ();
906 if (rfc822_valid($address)) {
907 push_email_address($address, $role);
908 } elsif (@address_list = rfc822_validlist($address)) {
909 my $array_count = shift(@address_list);
910 while (my $entry = shift(@address_list)) {
911 push_email_address($entry, $role);
914 if (!push_email_address($address, $role)) {
915 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
921 my ($line, $role) = @_;
923 my ($name, $address) = parse_email($line);
924 my $email = format_email($name, $address, $email_usename);
926 foreach my $entry (@email_to) {
927 if ($email_remove_duplicates) {
928 my ($entry_name, $entry_address) = parse_email($entry->[0]);
929 if (($name eq $entry_name || $address eq $entry_address)
930 && ($role eq "" || !($entry->[1] =~ m/$role/))
932 if ($entry->[1] eq "") {
933 $entry->[1] = "$role";
935 $entry->[1] = "$entry->[1],$role";
939 if ($email eq $entry->[0]
940 && ($role eq "" || !($entry->[1] =~ m/$role/))
942 if ($entry->[1] eq "") {
943 $entry->[1] = "$role";
945 $entry->[1] = "$entry->[1],$role";
955 foreach my $path (split(/:/, $ENV{PATH})) {
956 if (-e "$path/$bin") {
968 foreach my $line (@lines) {
969 my ($name, $address) = parse_email($line);
970 if (!exists($hash{$name})) {
971 $hash{$name} = $address;
972 } elsif ($address ne $hash{$name}) {
973 $address = $hash{$name};
974 $line = format_email($name, $address, $email_usename);
976 if (exists($mailmap{$name})) {
977 my $obj = $mailmap{$name};
978 foreach my $map_address (@$obj) {
979 if (($map_address eq $address) &&
980 ($map_address ne $hash{$name})) {
981 $line = format_email($name, $hash{$name}, $email_usename);
990 sub git_execute_cmd {
995 $output =~ s/^\s*//gm;
996 @lines = split("\n", $output);
1001 sub hg_execute_cmd {
1005 my $output = `$cmd`;
1006 @lines = split("\n", $output);
1011 sub vcs_find_signers {
1016 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1018 my $pattern = $VCS_cmds{"commit_pattern"};
1020 $commits = grep(/$pattern/, @lines); # of commits
1022 @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines);
1023 if (!$email_git_penguin_chiefs) {
1024 @lines = grep(!/${penguin_chiefs}/i, @lines);
1027 return (0, @lines) if !@lines;
1030 s/.*:\s*(.+)\s*/$1/ for (@lines);
1032 ## Reformat email addresses (with names) to avoid badly written signatures
1034 foreach my $line (@lines) {
1035 my ($name, $address) = parse_email($line);
1036 $line = format_email($name, $address, 1);
1039 return ($commits, @lines);
1042 sub vcs_find_author {
1046 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1048 if (!$email_git_penguin_chiefs) {
1049 @lines = grep(!/${penguin_chiefs}/i, @lines);
1052 return @lines if !@lines;
1054 ## Reformat email addresses (with names) to avoid badly written signatures
1056 foreach my $line (@lines) {
1057 my ($name, $address) = parse_email($line);
1058 $line = format_email($name, $address, 1);
1064 sub vcs_save_commits {
1069 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1071 foreach my $line (@lines) {
1072 if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
1085 return @commits if (!(-f $file));
1087 if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
1088 my @all_commits = ();
1090 $cmd = $VCS_cmds{"blame_file_cmd"};
1091 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1092 @all_commits = vcs_save_commits($cmd);
1094 foreach my $file_range_diff (@range) {
1095 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1097 my $diff_start = $2;
1098 my $diff_length = $3;
1099 next if ("$file" ne "$diff_file");
1100 for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
1101 push(@commits, $all_commits[$i]);
1105 foreach my $file_range_diff (@range) {
1106 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1108 my $diff_start = $2;
1109 my $diff_length = $3;
1110 next if ("$file" ne "$diff_file");
1111 $cmd = $VCS_cmds{"blame_range_cmd"};
1112 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1113 push(@commits, vcs_save_commits($cmd));
1116 $cmd = $VCS_cmds{"blame_file_cmd"};
1117 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1118 @commits = vcs_save_commits($cmd);
1121 foreach my $commit (@commits) {
1122 $commit =~ s/^\^//g;
1128 my $printed_novcs = 0;
1130 %VCS_cmds = %VCS_cmds_git;
1131 return 1 if eval $VCS_cmds{"available"};
1132 %VCS_cmds = %VCS_cmds_hg;
1133 return 1 if eval $VCS_cmds{"available"};
1135 if (!$printed_novcs) {
1136 warn("$P: No supported VCS found. Add --nogit to options?\n");
1137 warn("Using a git repository produces better results.\n");
1138 warn("Try Linus Torvalds' latest git repository using:\n");
1139 warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
1146 my ($role, $divisor, @lines) = @_;
1151 return if (@lines <= 0);
1153 if ($divisor <= 0) {
1154 warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
1158 if ($email_remove_duplicates) {
1159 @lines = mailmap(@lines);
1162 return if (@lines <= 0);
1164 @lines = sort(@lines);
1167 $hash{$_}++ for @lines;
1170 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
1171 my $sign_offs = $hash{$line};
1172 my $percent = $sign_offs * 100 / $divisor;
1174 $percent = 100 if ($percent > 100);
1176 last if ($sign_offs < $email_git_min_signatures ||
1177 $count > $email_git_max_maintainers ||
1178 $percent < $email_git_min_percent);
1179 push_email_address($line, '');
1180 if ($output_rolestats) {
1181 my $fmt_percent = sprintf("%.0f", $percent);
1182 add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
1184 add_role($line, $role);
1189 sub vcs_file_signoffs {
1195 return if (!vcs_exists());
1197 my $cmd = $VCS_cmds{"find_signers_cmd"};
1198 $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
1200 ($commits, @signers) = vcs_find_signers($cmd);
1201 vcs_assign("commit_signer", $commits, @signers);
1204 sub vcs_file_blame {
1208 my @all_commits = ();
1213 return if (!vcs_exists());
1215 @all_commits = vcs_blame($file);
1216 @commits = uniq(@all_commits);
1217 $total_commits = @commits;
1218 $total_lines = @all_commits;
1220 foreach my $commit (@commits) {
1222 my @commit_signers = ();
1224 my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1225 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1227 ($commit_count, @commit_signers) = vcs_find_signers($cmd);
1229 push(@signers, @commit_signers);
1232 if ($from_filename) {
1233 if ($output_rolestats) {
1235 foreach my $commit (@commits) {
1237 my $cmd = $VCS_cmds{"find_commit_author_cmd"};
1238 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1239 my @author = vcs_find_author($cmd);
1241 my $count = grep(/$commit/, @all_commits);
1242 for ($i = 0; $i < $count ; $i++) {
1243 push(@blame_signers, $author[0]);
1246 if (@blame_signers) {
1247 vcs_assign("authored lines", $total_lines, @blame_signers);
1250 vcs_assign("commits", $total_commits, @signers);
1252 vcs_assign("modified commits", $total_commits, @signers);
1260 @parms = grep(!$saw{$_}++, @parms);
1268 @parms = sort @parms;
1269 @parms = grep(!$saw{$_}++, @parms);
1273 sub clean_file_emails {
1274 my (@file_emails) = @_;
1275 my @fmt_emails = ();
1277 foreach my $email (@file_emails) {
1278 $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
1279 my ($name, $address) = parse_email($email);
1280 if ($name eq '"[,\.]"') {
1284 my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
1286 my $first = $nw[@nw - 3];
1287 my $middle = $nw[@nw - 2];
1288 my $last = $nw[@nw - 1];
1290 if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
1291 (length($first) == 2 && substr($first, -1) eq ".")) ||
1292 (length($middle) == 1 ||
1293 (length($middle) == 2 && substr($middle, -1) eq "."))) {
1294 $name = "$first $middle $last";
1296 $name = "$middle $last";
1300 if (substr($name, -1) =~ /[,\.]/) {
1301 $name = substr($name, 0, length($name) - 1);
1302 } elsif (substr($name, -2) =~ /[,\.]"/) {
1303 $name = substr($name, 0, length($name) - 2) . '"';
1306 if (substr($name, 0, 1) =~ /[,\.]/) {
1307 $name = substr($name, 1, length($name) - 1);
1308 } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
1309 $name = '"' . substr($name, 2, length($name) - 2);
1312 my $fmt_email = format_email($name, $address, $email_usename);
1313 push(@fmt_emails, $fmt_email);
1323 my ($address, $role) = @$_;
1324 if (!$saw{$address}) {
1325 if ($output_roles) {
1326 push(@lines, "$address ($role)");
1328 push(@lines, $address);
1340 if ($output_multiline) {
1341 foreach my $line (@parms) {
1345 print(join($output_separator, @parms));
1353 # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
1354 # comment. We must allow for rfc822_lwsp (or comments) after each of these.
1355 # This regexp will only work on addresses which have had comments stripped
1356 # and replaced with rfc822_lwsp.
1358 my $specials = '()<>@,;:\\\\".\\[\\]';
1359 my $controls = '\\000-\\037\\177';
1361 my $dtext = "[^\\[\\]\\r\\\\]";
1362 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1364 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1366 # Use zero-width assertion to spot the limit of an atom. A simple
1367 # $rfc822_lwsp* causes the regexp engine to hang occasionally.
1368 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
1369 my $word = "(?:$atom|$quoted_string)";
1370 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
1372 my $sub_domain = "(?:$atom|$domain_literal)";
1373 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1375 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1377 my $phrase = "$word*";
1378 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
1379 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
1380 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
1382 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1383 my $address = "(?:$mailbox|$group)";
1385 return "$rfc822_lwsp*$address";
1388 sub rfc822_strip_comments {
1390 # Recursively remove comments, and replace with a single space. The simpler
1391 # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
1392 # chars in atoms, for example.
1394 while ($s =~ s/^((?:[^"\\]|\\.)*
1395 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1396 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1400 # valid: returns true if the parameter is an RFC822 valid address
1403 my $s = rfc822_strip_comments(shift);
1406 $rfc822re = make_rfc822re();
1409 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1412 # validlist: In scalar context, returns true if the parameter is an RFC822
1413 # valid list of addresses.
1415 # In list context, returns an empty list on failure (an invalid
1416 # address was found); otherwise a list whose first element is the
1417 # number of addresses found and whose remaining elements are the
1418 # addresses. This is needed to disambiguate failure (invalid)
1419 # from success with no addresses found, because an empty string is
1422 sub rfc822_validlist {
1423 my $s = rfc822_strip_comments(shift);
1426 $rfc822re = make_rfc822re();
1428 # * null list items are valid according to the RFC
1429 # * the '1' business is to aid in distinguishing failure from no results
1432 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
1433 $s =~ m/^$rfc822_char*$/) {
1434 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
1437 return wantarray ? (scalar(@r), @r) : 1;
1439 return wantarray ? () : 0;