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_min_signatures = 1;
31 my $email_git_max_maintainers = 5;
32 my $email_git_min_percent = 5;
33 my $email_git_since = "1-year-ago";
34 my $email_hg_since = "-365";
35 my $email_remove_duplicates = 1;
36 my $output_multiline = 1;
37 my $output_separator = ", ";
39 my $output_rolestats = 0;
47 my $from_filename = 0;
48 my $pattern_depth = 0;
54 my @penguin_chief = ();
55 push(@penguin_chief, "Linus Torvalds:torvalds\@linux-foundation.org");
56 #Andrew wants in on most everything - 2009/01/14
57 #push(@penguin_chief, "Andrew Morton:akpm\@linux-foundation.org");
59 my @penguin_chief_names = ();
60 foreach my $chief (@penguin_chief) {
61 if ($chief =~ m/^(.*):(.*)/) {
64 push(@penguin_chief_names, $chief_name);
67 my $penguin_chiefs = "\(" . join("|", @penguin_chief_names) . "\)";
69 # Signature types of people who are either
70 # a) responsible for the code in question, or
71 # b) familiar enough with it to give relevant feedback
72 my @signature_tags = ();
73 push(@signature_tags, "Signed-off-by:");
74 push(@signature_tags, "Reviewed-by:");
75 push(@signature_tags, "Acked-by:");
76 my $signaturePattern = "\(" . join("|", @signature_tags) . "\)";
78 # rfc822 email address - preloaded methods go here.
79 my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
80 my $rfc822_char = '[\\000-\\377]';
82 # VCS command support: class-like functions and strings
87 "execute_cmd" => \&git_execute_cmd,
88 "available" => '(which("git") ne "") && (-d ".git")',
89 "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file",
90 "find_commit_signers_cmd" => "git log --no-color -1 \$commit",
91 "find_commit_author_cmd" => "git log -1 --format=\"%an <%ae>\" \$commit",
92 "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
93 "blame_file_cmd" => "git blame -l \$file",
94 "commit_pattern" => "^commit [0-9a-f]{40,40}",
95 "blame_commit_pattern" => "^([0-9a-f]+) "
99 "execute_cmd" => \&hg_execute_cmd,
100 "available" => '(which("hg") ne "") && (-d ".hg")',
101 "find_signers_cmd" =>
102 "hg log --date=\$email_hg_since" .
103 " --template='commit {node}\\n{desc}\\n' -- \$file",
104 "find_commit_signers_cmd" => "hg log --template='{desc}\\n' -r \$commit",
105 "find_commit_author_cmd" => "hg log -l 1 --template='{author}\\n' -r \$commit",
106 "blame_range_cmd" => "", # not supported
107 "blame_file_cmd" => "hg blame -c \$file",
108 "commit_pattern" => "^commit [0-9a-f]{40,40}",
109 "blame_commit_pattern" => "^([0-9a-f]+):"
112 if (-f "${lk_path}.get_maintainer.conf") {
114 open(my $conffile, '<', "${lk_path}.get_maintainer.conf")
115 or warn "$P: Can't open .get_maintainer.conf: $!\n";
116 while (<$conffile>) {
119 $line =~ s/\s*\n?$//g;
123 next if ($line =~ m/^\s*#/);
124 next if ($line =~ m/^\s*$/);
126 my @words = split(" ", $line);
127 foreach my $word (@words) {
128 last if ($word =~ m/^#/);
129 push (@conf_args, $word);
133 unshift(@ARGV, @conf_args) if @conf_args;
138 'git!' => \$email_git,
139 'git-all-signature-types!' => \$email_git_all_signature_types,
140 'git-blame!' => \$email_git_blame,
141 'git-chief-penguins!' => \$email_git_penguin_chiefs,
142 'git-min-signatures=i' => \$email_git_min_signatures,
143 'git-max-maintainers=i' => \$email_git_max_maintainers,
144 'git-min-percent=i' => \$email_git_min_percent,
145 'git-since=s' => \$email_git_since,
146 'hg-since=s' => \$email_hg_since,
147 'remove-duplicates!' => \$email_remove_duplicates,
148 'm!' => \$email_maintainer,
149 'n!' => \$email_usename,
150 'l!' => \$email_list,
151 's!' => \$email_subscriber_list,
152 'multiline!' => \$output_multiline,
153 'roles!' => \$output_roles,
154 'rolestats!' => \$output_rolestats,
155 'separator=s' => \$output_separator,
156 'subsystem!' => \$subsystem,
157 'status!' => \$status,
160 'pattern-depth=i' => \$pattern_depth,
161 'k|keywords!' => \$keywords,
162 'sections!' => \$sections,
163 'fe|file-emails!' => \$file_emails,
164 'f|file' => \$from_filename,
165 'v|version' => \$version,
166 'h|help|usage' => \$help,
168 die "$P: invalid argument - use --help if necessary\n";
177 print("${P} ${V}\n");
181 if (-t STDIN && !@ARGV) {
182 # We're talking to a terminal, but have no command line arguments.
183 die "$P: missing patchfile or -f file - use --help if necessary\n";
186 if ($output_separator ne ", ") {
187 $output_multiline = 0;
190 if ($output_rolestats) {
203 my $selections = $email + $scm + $status + $subsystem + $web;
204 if ($selections == 0) {
205 die "$P: Missing required option: email, scm, status, subsystem or web\n";
210 ($email_maintainer + $email_list + $email_subscriber_list +
211 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
212 die "$P: Please select at least 1 email option\n";
215 if (!top_of_kernel_tree($lk_path)) {
216 die "$P: The current directory does not appear to be "
217 . "a linux kernel source tree.\n";
220 if ($email_git_all_signature_types) {
221 $signaturePattern = "(.+?)[Bb][Yy]:";
224 ## Read MAINTAINERS for type/value pairs
229 open (my $maint, '<', "${lk_path}MAINTAINERS")
230 or die "$P: Can't open MAINTAINERS: $!\n";
234 if ($line =~ m/^(\C):\s*(.*)/) {
238 ##Filename pattern matching
239 if ($type eq "F" || $type eq "X") {
240 $value =~ s@\.@\\\.@g; ##Convert . to \.
241 $value =~ s/\*/\.\*/g; ##Convert * to .*
242 $value =~ s/\?/\./g; ##Convert ? to .
243 ##if pattern is a directory and it lacks a trailing slash, add one
245 $value =~ s@([^/])$@$1/@;
247 } elsif ($type eq "K") {
248 $keyword_hash{@typevalue} = $value;
250 push(@typevalue, "$type:$value");
251 } elsif (!/^(\s)*$/) {
253 push(@typevalue, $line);
260 if ($email_remove_duplicates) {
261 open(my $mailmap, '<', "${lk_path}.mailmap")
262 or warn "$P: Can't open .mailmap: $!\n";
266 next if ($line =~ m/^\s*#/);
267 next if ($line =~ m/^\s*$/);
269 my ($name, $address) = parse_email($line);
270 $line = format_email($name, $address, $email_usename);
272 next if ($line =~ m/^\s*$/);
274 if (exists($mailmap{$name})) {
275 my $obj = $mailmap{$name};
276 push(@$obj, $address);
278 my @arr = ($address);
279 $mailmap{$name} = \@arr;
285 ## use the filenames on the command line or find the filenames in the patchfiles
289 my @keyword_tvi = ();
290 my @file_emails = ();
293 push(@ARGV, "&STDIN");
296 foreach my $file (@ARGV) {
297 if ($file ne "&STDIN") {
298 ##if $file is a directory and it lacks a trailing slash, add one
300 $file =~ s@([^/])$@$1/@;
301 } elsif (!(-f $file)) {
302 die "$P: file '${file}' not found\n";
305 if ($from_filename) {
307 if (-f $file && ($keywords || $file_emails)) {
308 open(my $f, '<', $file)
309 or die "$P: Can't open $file: $!\n";
310 my $text = do { local($/) ; <$f> };
313 foreach my $line (keys %keyword_hash) {
314 if ($text =~ m/$keyword_hash{$line}/x) {
315 push(@keyword_tvi, $line);
320 my @poss_addr = $text =~ m$[A-Za-zÀ-ÿ\"\' \,\.\+-]*\s*[\,]*\s*[\(\<\{]{0,1}[A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+\.[A-Za-z0-9]+[\)\>\}]{0,1}$g;
321 push(@file_emails, clean_file_emails(@poss_addr));
325 my $file_cnt = @files;
328 open(my $patch, "< $file")
329 or die "$P: Can't open $file: $!\n";
332 if (m/^\+\+\+\s+(\S+)/) {
334 $filename =~ s@^[^/]*/@@;
336 $lastfile = $filename;
337 push(@files, $filename);
338 } elsif (m/^\@\@ -(\d+),(\d+)/) {
339 if ($email_git_blame) {
340 push(@range, "$lastfile:$1:$2");
342 } elsif ($keywords) {
343 foreach my $line (keys %keyword_hash) {
344 if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
345 push(@keyword_tvi, $line);
352 if ($file_cnt == @files) {
353 warn "$P: file '${file}' doesn't appear to be a patch. "
354 . "Add -f to options?\n";
356 @files = sort_and_uniq(@files);
360 @file_emails = uniq(@file_emails);
369 # Find responsible parties
371 foreach my $file (@files) {
374 my $tvi = find_first_section();
375 while ($tvi < @typevalue) {
376 my $start = find_starting_index($tvi);
377 my $end = find_ending_index($tvi);
381 #Do not match excluded file patterns
383 for ($i = $start; $i < $end; $i++) {
384 my $line = $typevalue[$i];
385 if ($line =~ m/^(\C):\s*(.*)/) {
389 if (file_match_pattern($file, $value)) {
398 for ($i = $start; $i < $end; $i++) {
399 my $line = $typevalue[$i];
400 if ($line =~ m/^(\C):\s*(.*)/) {
404 if (file_match_pattern($file, $value)) {
405 my $value_pd = ($value =~ tr@/@@);
406 my $file_pd = ($file =~ tr@/@@);
407 $value_pd++ if (substr($value,-1,1) ne "/");
408 if ($pattern_depth == 0 ||
409 (($file_pd - $value_pd) < $pattern_depth)) {
410 $hash{$tvi} = $value_pd;
421 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
422 add_categories($line);
425 my $start = find_starting_index($line);
426 my $end = find_ending_index($line);
427 for ($i = $start; $i < $end; $i++) {
428 my $line = $typevalue[$i];
429 if ($line =~ /^[FX]:/) { ##Restore file patterns
430 $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
431 $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
432 $line =~ s/\\\./\./g; ##Convert \. to .
433 $line =~ s/\.\*/\*/g; ##Convert .* to *
435 $line =~ s/^([A-Z]):/$1:\t/g;
442 if ($email && $email_git) {
443 vcs_file_signoffs($file);
446 if ($email && $email_git_blame) {
447 vcs_file_blame($file);
452 @keyword_tvi = sort_and_uniq(@keyword_tvi);
453 foreach my $line (@keyword_tvi) {
454 add_categories($line);
459 foreach my $chief (@penguin_chief) {
460 if ($chief =~ m/^(.*):(.*)/) {
463 $email_address = format_email($1, $2, $email_usename);
464 if ($email_git_penguin_chiefs) {
465 push(@email_to, [$email_address, 'chief penguin']);
467 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
472 foreach my $email (@file_emails) {
473 my ($name, $address) = parse_email($email);
475 my $tmp_email = format_email($name, $address, $email_usename);
476 push_email_address($tmp_email, '');
477 add_role($tmp_email, 'in file');
481 if ($email || $email_list) {
484 @to = (@to, @email_to);
487 @to = (@to, @list_to);
489 output(merge_email(@to));
498 @status = uniq(@status);
503 @subsystem = uniq(@subsystem);
514 sub file_match_pattern {
515 my ($file, $pattern) = @_;
516 if (substr($pattern, -1) eq "/") {
517 if ($file =~ m@^$pattern@) {
521 if ($file =~ m@^$pattern@) {
522 my $s1 = ($file =~ tr@/@@);
523 my $s2 = ($pattern =~ tr@/@@);
534 usage: $P [options] patchfile
535 $P [options] -f file|directory
538 MAINTAINER field selection options:
539 --email => print email address(es) if any
540 --git => include recent git \*-by: signers
541 --git-all-signature-types => include signers regardless of signature type
542 or use only ${signaturePattern} signers (default: $email_git_all_signature_types)
543 --git-chief-penguins => include ${penguin_chiefs}
544 --git-min-signatures => number of signatures required (default: $email_git_min_signatures)
545 --git-max-maintainers => maximum maintainers to add (default: $email_git_max_maintainers)
546 --git-min-percent => minimum percentage of commits required (default: $email_git_min_percent)
547 --git-blame => use git blame to find modified commits for patch or file
548 --git-since => git history to use (default: $email_git_since)
549 --hg-since => hg history to use (default: $email_hg_since)
550 --m => include maintainer(s) if any
551 --n => include name 'Full Name <addr\@domain.tld>'
552 --l => include list(s) if any
553 --s => include subscriber only list(s) if any
554 --remove-duplicates => minimize duplicate email names/addresses
555 --roles => show roles (status:subsystem, git-signer, list, etc...)
556 --rolestats => show roles and statistics (commits/total_commits, %)
557 --file-emails => add email addresses found in -f file (default: 0 (off))
558 --scm => print SCM tree(s) if any
559 --status => print status if any
560 --subsystem => print subsystem name if any
561 --web => print website(s) if any
564 --separator [, ] => separator for multiple entries on 1 line
565 using --separator also sets --nomultiline if --separator is not [, ]
566 --multiline => print 1 entry per line
569 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
570 --keywords => scan patch for keywords (default: 1 (on))
571 --sections => print the entire subsystem sections with pattern matches
572 --version => show version
573 --help => show this help information
576 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
579 Using "-f directory" may give unexpected results:
580 Used with "--git", git signators for _all_ files in and below
581 directory are examined as git recurses directories.
582 Any specified X: (exclude) pattern matches are _not_ ignored.
583 Used with "--nogit", directory is used as a pattern match,
584 no individual file within the directory or subdirectory
586 Used with "--git-blame", does not iterate all files in directory
587 Using "--git-blame" is slow and may add old committers and authors
588 that are no longer active maintainers to the output.
589 Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
590 other automated tools that expect only ["name"] <email address>
591 may not work because of additional output after <email address>.
592 Using "--rolestats" and "--git-blame" shows the #/total=% commits,
593 not the percentage of the entire file authored. # of commits is
594 not a good measure of amount of code authored. 1 major commit may
595 contain a thousand lines, 5 trivial commits may modify a single line.
596 If git is not installed, but mercurial (hg) is installed and an .hg
597 repository exists, the following options apply to mercurial:
599 --git-min-signatures, --git-max-maintainers, --git-min-percent, and
601 Use --hg-since not --git-since to control date selection
602 File ".get_maintainer.conf", if it exists in the linux kernel source root
603 directory, can change whatever get_maintainer defaults are desired.
604 Entries in this file can be any command line argument.
605 This file is prepended to any additional command line arguments.
606 Multiple lines and # comments are allowed.
610 sub top_of_kernel_tree {
613 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
616 if ( (-f "${lk_path}COPYING")
617 && (-f "${lk_path}CREDITS")
618 && (-f "${lk_path}Kbuild")
619 && (-f "${lk_path}MAINTAINERS")
620 && (-f "${lk_path}Makefile")
621 && (-f "${lk_path}README")
622 && (-d "${lk_path}Documentation")
623 && (-d "${lk_path}arch")
624 && (-d "${lk_path}include")
625 && (-d "${lk_path}drivers")
626 && (-d "${lk_path}fs")
627 && (-d "${lk_path}init")
628 && (-d "${lk_path}ipc")
629 && (-d "${lk_path}kernel")
630 && (-d "${lk_path}lib")
631 && (-d "${lk_path}scripts")) {
638 my ($formatted_email) = @_;
643 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
646 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
648 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
652 $name =~ s/^\s+|\s+$//g;
653 $name =~ s/^\"|\"$//g;
654 $address =~ s/^\s+|\s+$//g;
656 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
657 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
661 return ($name, $address);
665 my ($name, $address, $usename) = @_;
669 $name =~ s/^\s+|\s+$//g;
670 $name =~ s/^\"|\"$//g;
671 $address =~ s/^\s+|\s+$//g;
673 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
674 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
680 $formatted_email = "$address";
682 $formatted_email = "$name <$address>";
685 $formatted_email = $address;
688 return $formatted_email;
691 sub find_first_section {
694 while ($index < @typevalue) {
695 my $tv = $typevalue[$index];
696 if (($tv =~ m/^(\C):\s*(.*)/)) {
705 sub find_starting_index {
709 my $tv = $typevalue[$index];
710 if (!($tv =~ m/^(\C):\s*(.*)/)) {
719 sub find_ending_index {
722 while ($index < @typevalue) {
723 my $tv = $typevalue[$index];
724 if (!($tv =~ m/^(\C):\s*(.*)/)) {
733 sub get_maintainer_role {
737 my $start = find_starting_index($index);
738 my $end = find_ending_index($index);
741 my $subsystem = $typevalue[$start];
742 if (length($subsystem) > 20) {
743 $subsystem = substr($subsystem, 0, 17);
744 $subsystem =~ s/\s*$//;
745 $subsystem = $subsystem . "...";
748 for ($i = $start + 1; $i < $end; $i++) {
749 my $tv = $typevalue[$i];
750 if ($tv =~ m/^(\C):\s*(.*)/) {
760 if ($role eq "supported") {
762 } elsif ($role eq "maintained") {
763 $role = "maintainer";
764 } elsif ($role eq "odd fixes") {
766 } elsif ($role eq "orphan") {
767 $role = "orphan minder";
768 } elsif ($role eq "obsolete") {
769 $role = "obsolete minder";
770 } elsif ($role eq "buried alive in reporters") {
771 $role = "chief penguin";
774 return $role . ":" . $subsystem;
781 my $start = find_starting_index($index);
782 my $end = find_ending_index($index);
784 my $subsystem = $typevalue[$start];
785 if (length($subsystem) > 20) {
786 $subsystem = substr($subsystem, 0, 17);
787 $subsystem =~ s/\s*$//;
788 $subsystem = $subsystem . "...";
791 if ($subsystem eq "THE REST") {
802 my $start = find_starting_index($index);
803 my $end = find_ending_index($index);
805 push(@subsystem, $typevalue[$start]);
807 for ($i = $start + 1; $i < $end; $i++) {
808 my $tv = $typevalue[$i];
809 if ($tv =~ m/^(\C):\s*(.*)/) {
813 my $list_address = $pvalue;
814 my $list_additional = "";
815 my $list_role = get_list_role($i);
817 if ($list_role ne "") {
818 $list_role = ":" . $list_role;
820 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
822 $list_additional = $2;
824 if ($list_additional =~ m/subscribers-only/) {
825 if ($email_subscriber_list) {
826 push(@list_to, [$list_address, "subscriber list${list_role}"]);
830 push(@list_to, [$list_address, "open list${list_role}"]);
833 } elsif ($ptype eq "M") {
834 my ($name, $address) = parse_email($pvalue);
837 my $tv = $typevalue[$i - 1];
838 if ($tv =~ m/^(\C):\s*(.*)/) {
841 $pvalue = format_email($name, $address, $email_usename);
846 if ($email_maintainer) {
847 my $role = get_maintainer_role($i);
848 push_email_addresses($pvalue, $role);
850 } elsif ($ptype eq "T") {
852 } elsif ($ptype eq "W") {
854 } elsif ($ptype eq "S") {
855 push(@status, $pvalue);
862 my %email_hash_address;
865 my ($name, $address) = @_;
867 return 1 if (($name eq "") && ($address eq ""));
868 return 1 if (($name ne "") && exists($email_hash_name{$name}));
869 return 1 if (($address ne "") && exists($email_hash_address{$address}));
874 sub push_email_address {
875 my ($line, $role) = @_;
877 my ($name, $address) = parse_email($line);
879 if ($address eq "") {
883 if (!$email_remove_duplicates) {
884 push(@email_to, [format_email($name, $address, $email_usename), $role]);
885 } elsif (!email_inuse($name, $address)) {
886 push(@email_to, [format_email($name, $address, $email_usename), $role]);
887 $email_hash_name{$name}++;
888 $email_hash_address{$address}++;
894 sub push_email_addresses {
895 my ($address, $role) = @_;
897 my @address_list = ();
899 if (rfc822_valid($address)) {
900 push_email_address($address, $role);
901 } elsif (@address_list = rfc822_validlist($address)) {
902 my $array_count = shift(@address_list);
903 while (my $entry = shift(@address_list)) {
904 push_email_address($entry, $role);
907 if (!push_email_address($address, $role)) {
908 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
914 my ($line, $role) = @_;
916 my ($name, $address) = parse_email($line);
917 my $email = format_email($name, $address, $email_usename);
919 foreach my $entry (@email_to) {
920 if ($email_remove_duplicates) {
921 my ($entry_name, $entry_address) = parse_email($entry->[0]);
922 if (($name eq $entry_name || $address eq $entry_address)
923 && ($role eq "" || !($entry->[1] =~ m/$role/))
925 if ($entry->[1] eq "") {
926 $entry->[1] = "$role";
928 $entry->[1] = "$entry->[1],$role";
932 if ($email eq $entry->[0]
933 && ($role eq "" || !($entry->[1] =~ m/$role/))
935 if ($entry->[1] eq "") {
936 $entry->[1] = "$role";
938 $entry->[1] = "$entry->[1],$role";
948 foreach my $path (split(/:/, $ENV{PATH})) {
949 if (-e "$path/$bin") {
961 foreach my $line (@lines) {
962 my ($name, $address) = parse_email($line);
963 if (!exists($hash{$name})) {
964 $hash{$name} = $address;
965 } elsif ($address ne $hash{$name}) {
966 $address = $hash{$name};
967 $line = format_email($name, $address, $email_usename);
969 if (exists($mailmap{$name})) {
970 my $obj = $mailmap{$name};
971 foreach my $map_address (@$obj) {
972 if (($map_address eq $address) &&
973 ($map_address ne $hash{$name})) {
974 $line = format_email($name, $hash{$name}, $email_usename);
983 sub git_execute_cmd {
988 $output =~ s/^\s*//gm;
989 @lines = split("\n", $output);
999 @lines = split("\n", $output);
1004 sub vcs_find_signers {
1009 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1011 my $pattern = $VCS_cmds{"commit_pattern"};
1013 $commits = grep(/$pattern/, @lines); # of commits
1015 @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines);
1016 if (!$email_git_penguin_chiefs) {
1017 @lines = grep(!/${penguin_chiefs}/i, @lines);
1020 return (0, @lines) if !@lines;
1023 s/.*:\s*(.+)\s*/$1/ for (@lines);
1025 ## Reformat email addresses (with names) to avoid badly written signatures
1027 foreach my $line (@lines) {
1028 my ($name, $address) = parse_email($line);
1029 $line = format_email($name, $address, 1);
1032 return ($commits, @lines);
1035 sub vcs_find_author {
1039 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1041 if (!$email_git_penguin_chiefs) {
1042 @lines = grep(!/${penguin_chiefs}/i, @lines);
1045 return @lines if !@lines;
1047 ## Reformat email addresses (with names) to avoid badly written signatures
1049 foreach my $line (@lines) {
1050 my ($name, $address) = parse_email($line);
1051 $line = format_email($name, $address, 1);
1057 sub vcs_save_commits {
1062 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1064 foreach my $line (@lines) {
1065 if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
1078 return @commits if (!(-f $file));
1080 if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
1081 my @all_commits = ();
1083 $cmd = $VCS_cmds{"blame_file_cmd"};
1084 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1085 @all_commits = vcs_save_commits($cmd);
1087 foreach my $file_range_diff (@range) {
1088 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1090 my $diff_start = $2;
1091 my $diff_length = $3;
1092 next if ("$file" ne "$diff_file");
1093 for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
1094 push(@commits, $all_commits[$i]);
1098 foreach my $file_range_diff (@range) {
1099 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1101 my $diff_start = $2;
1102 my $diff_length = $3;
1103 next if ("$file" ne "$diff_file");
1104 $cmd = $VCS_cmds{"blame_range_cmd"};
1105 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1106 push(@commits, vcs_save_commits($cmd));
1109 $cmd = $VCS_cmds{"blame_file_cmd"};
1110 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1111 @commits = vcs_save_commits($cmd);
1114 foreach my $commit (@commits) {
1115 $commit =~ s/^\^//g;
1121 my $printed_novcs = 0;
1123 %VCS_cmds = %VCS_cmds_git;
1124 return 1 if eval $VCS_cmds{"available"};
1125 %VCS_cmds = %VCS_cmds_hg;
1126 return 1 if eval $VCS_cmds{"available"};
1128 if (!$printed_novcs) {
1129 warn("$P: No supported VCS found. Add --nogit to options?\n");
1130 warn("Using a git repository produces better results.\n");
1131 warn("Try Linus Torvalds' latest git repository using:\n");
1132 warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
1139 my ($role, $divisor, @lines) = @_;
1144 return if (@lines <= 0);
1146 if ($divisor <= 0) {
1147 warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
1151 if ($email_remove_duplicates) {
1152 @lines = mailmap(@lines);
1155 return if (@lines <= 0);
1157 @lines = sort(@lines);
1160 $hash{$_}++ for @lines;
1163 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
1164 my $sign_offs = $hash{$line};
1165 my $percent = $sign_offs * 100 / $divisor;
1167 $percent = 100 if ($percent > 100);
1169 last if ($sign_offs < $email_git_min_signatures ||
1170 $count > $email_git_max_maintainers ||
1171 $percent < $email_git_min_percent);
1172 push_email_address($line, '');
1173 if ($output_rolestats) {
1174 my $fmt_percent = sprintf("%.0f", $percent);
1175 add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
1177 add_role($line, $role);
1182 sub vcs_file_signoffs {
1188 return if (!vcs_exists());
1190 my $cmd = $VCS_cmds{"find_signers_cmd"};
1191 $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
1193 ($commits, @signers) = vcs_find_signers($cmd);
1194 vcs_assign("commit_signer", $commits, @signers);
1197 sub vcs_file_blame {
1201 my @all_commits = ();
1206 return if (!vcs_exists());
1208 @all_commits = vcs_blame($file);
1209 @commits = uniq(@all_commits);
1210 $total_commits = @commits;
1211 $total_lines = @all_commits;
1213 foreach my $commit (@commits) {
1215 my @commit_signers = ();
1217 my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1218 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1220 ($commit_count, @commit_signers) = vcs_find_signers($cmd);
1222 push(@signers, @commit_signers);
1225 if ($from_filename) {
1226 if ($output_rolestats) {
1228 foreach my $commit (@commits) {
1230 my $cmd = $VCS_cmds{"find_commit_author_cmd"};
1231 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1232 my @author = vcs_find_author($cmd);
1234 my $count = grep(/$commit/, @all_commits);
1235 for ($i = 0; $i < $count ; $i++) {
1236 push(@blame_signers, $author[0]);
1239 if (@blame_signers) {
1240 vcs_assign("authored lines", $total_lines, @blame_signers);
1243 vcs_assign("commits", $total_commits, @signers);
1245 vcs_assign("modified commits", $total_commits, @signers);
1253 @parms = grep(!$saw{$_}++, @parms);
1261 @parms = sort @parms;
1262 @parms = grep(!$saw{$_}++, @parms);
1266 sub clean_file_emails {
1267 my (@file_emails) = @_;
1268 my @fmt_emails = ();
1270 foreach my $email (@file_emails) {
1271 $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
1272 my ($name, $address) = parse_email($email);
1273 if ($name eq '"[,\.]"') {
1277 my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
1279 my $first = $nw[@nw - 3];
1280 my $middle = $nw[@nw - 2];
1281 my $last = $nw[@nw - 1];
1283 if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
1284 (length($first) == 2 && substr($first, -1) eq ".")) ||
1285 (length($middle) == 1 ||
1286 (length($middle) == 2 && substr($middle, -1) eq "."))) {
1287 $name = "$first $middle $last";
1289 $name = "$middle $last";
1293 if (substr($name, -1) =~ /[,\.]/) {
1294 $name = substr($name, 0, length($name) - 1);
1295 } elsif (substr($name, -2) =~ /[,\.]"/) {
1296 $name = substr($name, 0, length($name) - 2) . '"';
1299 if (substr($name, 0, 1) =~ /[,\.]/) {
1300 $name = substr($name, 1, length($name) - 1);
1301 } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
1302 $name = '"' . substr($name, 2, length($name) - 2);
1305 my $fmt_email = format_email($name, $address, $email_usename);
1306 push(@fmt_emails, $fmt_email);
1316 my ($address, $role) = @$_;
1317 if (!$saw{$address}) {
1318 if ($output_roles) {
1319 push(@lines, "$address ($role)");
1321 push(@lines, $address);
1333 if ($output_multiline) {
1334 foreach my $line (@parms) {
1338 print(join($output_separator, @parms));
1346 # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
1347 # comment. We must allow for rfc822_lwsp (or comments) after each of these.
1348 # This regexp will only work on addresses which have had comments stripped
1349 # and replaced with rfc822_lwsp.
1351 my $specials = '()<>@,;:\\\\".\\[\\]';
1352 my $controls = '\\000-\\037\\177';
1354 my $dtext = "[^\\[\\]\\r\\\\]";
1355 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1357 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1359 # Use zero-width assertion to spot the limit of an atom. A simple
1360 # $rfc822_lwsp* causes the regexp engine to hang occasionally.
1361 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
1362 my $word = "(?:$atom|$quoted_string)";
1363 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
1365 my $sub_domain = "(?:$atom|$domain_literal)";
1366 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1368 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1370 my $phrase = "$word*";
1371 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
1372 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
1373 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
1375 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1376 my $address = "(?:$mailbox|$group)";
1378 return "$rfc822_lwsp*$address";
1381 sub rfc822_strip_comments {
1383 # Recursively remove comments, and replace with a single space. The simpler
1384 # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
1385 # chars in atoms, for example.
1387 while ($s =~ s/^((?:[^"\\]|\\.)*
1388 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1389 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1393 # valid: returns true if the parameter is an RFC822 valid address
1396 my $s = rfc822_strip_comments(shift);
1399 $rfc822re = make_rfc822re();
1402 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1405 # validlist: In scalar context, returns true if the parameter is an RFC822
1406 # valid list of addresses.
1408 # In list context, returns an empty list on failure (an invalid
1409 # address was found); otherwise a list whose first element is the
1410 # number of addresses found and whose remaining elements are the
1411 # addresses. This is needed to disambiguate failure (invalid)
1412 # from success with no addresses found, because an empty string is
1415 sub rfc822_validlist {
1416 my $s = rfc822_strip_comments(shift);
1419 $rfc822re = make_rfc822re();
1421 # * null list items are valid according to the RFC
1422 # * the '1' business is to aid in distinguishing failure from no results
1425 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
1426 $s =~ m/^$rfc822_char*$/) {
1427 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
1430 return wantarray ? (scalar(@r), @r) : 1;
1432 return wantarray ? () : 0;