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";
37 my $email_remove_duplicates = 1;
38 my $output_multiline = 1;
39 my $output_separator = ", ";
41 my $output_rolestats = 0;
49 my $from_filename = 0;
50 my $pattern_depth = 0;
58 my @penguin_chief = ();
59 push(@penguin_chief, "Linus Torvalds:torvalds\@linux-foundation.org");
60 #Andrew wants in on most everything - 2009/01/14
61 #push(@penguin_chief, "Andrew Morton:akpm\@linux-foundation.org");
63 my @penguin_chief_names = ();
64 foreach my $chief (@penguin_chief) {
65 if ($chief =~ m/^(.*):(.*)/) {
68 push(@penguin_chief_names, $chief_name);
71 my $penguin_chiefs = "\(" . join("|", @penguin_chief_names) . "\)";
73 # Signature types of people who are either
74 # a) responsible for the code in question, or
75 # b) familiar enough with it to give relevant feedback
76 my @signature_tags = ();
77 push(@signature_tags, "Signed-off-by:");
78 push(@signature_tags, "Reviewed-by:");
79 push(@signature_tags, "Acked-by:");
80 my $signaturePattern = "\(" . join("|", @signature_tags) . "\)";
82 # rfc822 email address - preloaded methods go here.
83 my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
84 my $rfc822_char = '[\\000-\\377]';
86 # VCS command support: class-like functions and strings
91 "execute_cmd" => \&git_execute_cmd,
92 "available" => '(which("git") ne "") && (-d ".git")',
93 "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file",
94 "find_commit_signers_cmd" => "git log --no-color -1 \$commit",
95 "find_commit_author_cmd" => "git log -1 --format=\"%an <%ae>\" \$commit",
96 "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
97 "blame_file_cmd" => "git blame -l \$file",
98 "commit_pattern" => "^commit [0-9a-f]{40,40}",
99 "blame_commit_pattern" => "^([0-9a-f]+) ",
100 "shortlog_cmd" => "git log --no-color --oneline --since=\$email_git_since --author=\"\$email\" -- \$file"
104 "execute_cmd" => \&hg_execute_cmd,
105 "available" => '(which("hg") ne "") && (-d ".hg")',
106 "find_signers_cmd" =>
107 "hg log --date=\$email_hg_since" .
108 " --template='commit {node}\\n{desc}\\n' -- \$file",
109 "find_commit_signers_cmd" => "hg log --template='{desc}\\n' -r \$commit",
110 "find_commit_author_cmd" => "hg log -l 1 --template='{author}\\n' -r \$commit",
111 "blame_range_cmd" => "", # not supported
112 "blame_file_cmd" => "hg blame -c \$file",
113 "commit_pattern" => "^commit [0-9a-f]{40,40}",
114 "blame_commit_pattern" => "^([0-9a-f]+):",
115 "shortlog_cmd" => "ht log --date=\$email_hg_since"
118 my $conf = which_conf(".get_maintainer.conf");
121 open(my $conffile, '<', "$conf")
122 or warn "$P: Can't find a readable .get_maintainer.conf file $!\n";
124 while (<$conffile>) {
127 $line =~ s/\s*\n?$//g;
131 next if ($line =~ m/^\s*#/);
132 next if ($line =~ m/^\s*$/);
134 my @words = split(" ", $line);
135 foreach my $word (@words) {
136 last if ($word =~ m/^#/);
137 push (@conf_args, $word);
141 unshift(@ARGV, @conf_args) if @conf_args;
146 'git!' => \$email_git,
147 'git-all-signature-types!' => \$email_git_all_signature_types,
148 'git-blame!' => \$email_git_blame,
149 'git-fallback!' => \$email_git_fallback,
150 'git-chief-penguins!' => \$email_git_penguin_chiefs,
151 'git-min-signatures=i' => \$email_git_min_signatures,
152 'git-max-maintainers=i' => \$email_git_max_maintainers,
153 'git-min-percent=i' => \$email_git_min_percent,
154 'git-since=s' => \$email_git_since,
155 'hg-since=s' => \$email_hg_since,
156 'i|interactive!' => \$interactive,
157 'remove-duplicates!' => \$email_remove_duplicates,
158 'm!' => \$email_maintainer,
159 'n!' => \$email_usename,
160 'l!' => \$email_list,
161 's!' => \$email_subscriber_list,
162 'multiline!' => \$output_multiline,
163 'roles!' => \$output_roles,
164 'rolestats!' => \$output_rolestats,
165 'separator=s' => \$output_separator,
166 'subsystem!' => \$subsystem,
167 'status!' => \$status,
170 'pattern-depth=i' => \$pattern_depth,
171 'k|keywords!' => \$keywords,
172 'sections!' => \$sections,
173 'fe|file-emails!' => \$file_emails,
174 'f|file' => \$from_filename,
175 'v|version' => \$version,
176 'h|help|usage' => \$help,
178 die "$P: invalid argument - use --help if necessary\n";
187 print("${P} ${V}\n");
191 if (-t STDIN && !@ARGV) {
192 # We're talking to a terminal, but have no command line arguments.
193 die "$P: missing patchfile or -f file - use --help if necessary\n";
196 if ($output_separator ne ", ") {
197 $output_multiline = 0;
200 if ($output_rolestats) {
213 my $selections = $email + $scm + $status + $subsystem + $web;
214 if ($selections == 0) {
215 die "$P: Missing required option: email, scm, status, subsystem or web\n";
220 ($email_maintainer + $email_list + $email_subscriber_list +
221 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
222 die "$P: Please select at least 1 email option\n";
225 if (!top_of_kernel_tree($lk_path)) {
226 die "$P: The current directory does not appear to be "
227 . "a linux kernel source tree.\n";
230 if ($email_git_all_signature_types) {
231 $signaturePattern = "(.+?)[Bb][Yy]:";
236 ## Read MAINTAINERS for type/value pairs
241 open (my $maint, '<', "${lk_path}MAINTAINERS")
242 or die "$P: Can't open MAINTAINERS: $!\n";
246 if ($line =~ m/^(\C):\s*(.*)/) {
250 ##Filename pattern matching
251 if ($type eq "F" || $type eq "X") {
252 $value =~ s@\.@\\\.@g; ##Convert . to \.
253 $value =~ s/\*/\.\*/g; ##Convert * to .*
254 $value =~ s/\?/\./g; ##Convert ? to .
255 ##if pattern is a directory and it lacks a trailing slash, add one
257 $value =~ s@([^/])$@$1/@;
259 } elsif ($type eq "K") {
260 $keyword_hash{@typevalue} = $value;
262 push(@typevalue, "$type:$value");
263 } elsif (!/^(\s)*$/) {
265 push(@typevalue, $line);
272 if ($email_remove_duplicates) {
273 open(my $mailmap, '<', "${lk_path}.mailmap")
274 or warn "$P: Can't open .mailmap: $!\n";
278 next if ($line =~ m/^\s*#/);
279 next if ($line =~ m/^\s*$/);
281 my ($name, $address) = parse_email($line);
282 $line = format_email($name, $address, $email_usename);
284 next if ($line =~ m/^\s*$/);
286 if (exists($mailmap{$name})) {
287 my $obj = $mailmap{$name};
288 push(@$obj, $address);
290 my @arr = ($address);
291 $mailmap{$name} = \@arr;
297 ## use the filenames on the command line or find the filenames in the patchfiles
301 my @keyword_tvi = ();
302 my @file_emails = ();
305 push(@ARGV, "&STDIN");
308 foreach my $file (@ARGV) {
309 if ($file ne "&STDIN") {
310 ##if $file is a directory and it lacks a trailing slash, add one
312 $file =~ s@([^/])$@$1/@;
313 } elsif (!(-f $file)) {
314 die "$P: file '${file}' not found\n";
317 if ($from_filename) {
319 if ($file ne "MAINTAINERS" && -f $file && ($keywords || $file_emails)) {
320 open(my $f, '<', $file)
321 or die "$P: Can't open $file: $!\n";
322 my $text = do { local($/) ; <$f> };
325 foreach my $line (keys %keyword_hash) {
326 if ($text =~ m/$keyword_hash{$line}/x) {
327 push(@keyword_tvi, $line);
332 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;
333 push(@file_emails, clean_file_emails(@poss_addr));
337 my $file_cnt = @files;
340 open(my $patch, "< $file")
341 or die "$P: Can't open $file: $!\n";
344 if (m/^\+\+\+\s+(\S+)/) {
346 $filename =~ s@^[^/]*/@@;
348 $lastfile = $filename;
349 push(@files, $filename);
350 } elsif (m/^\@\@ -(\d+),(\d+)/) {
351 if ($email_git_blame) {
352 push(@range, "$lastfile:$1:$2");
354 } elsif ($keywords) {
355 foreach my $line (keys %keyword_hash) {
356 if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
357 push(@keyword_tvi, $line);
364 if ($file_cnt == @files) {
365 warn "$P: file '${file}' doesn't appear to be a patch. "
366 . "Add -f to options?\n";
368 @files = sort_and_uniq(@files);
372 @file_emails = uniq(@file_emails);
381 # Find responsible parties
383 foreach my $file (@files) {
386 my $exact_pattern_match = 0;
387 my $tvi = find_first_section();
388 while ($tvi < @typevalue) {
389 my $start = find_starting_index($tvi);
390 my $end = find_ending_index($tvi);
394 #Do not match excluded file patterns
396 for ($i = $start; $i < $end; $i++) {
397 my $line = $typevalue[$i];
398 if ($line =~ m/^(\C):\s*(.*)/) {
402 if (file_match_pattern($file, $value)) {
411 for ($i = $start; $i < $end; $i++) {
412 my $line = $typevalue[$i];
413 if ($line =~ m/^(\C):\s*(.*)/) {
417 if (file_match_pattern($file, $value)) {
418 my $value_pd = ($value =~ tr@/@@);
419 my $file_pd = ($file =~ tr@/@@);
420 $value_pd++ if (substr($value,-1,1) ne "/");
421 $value_pd = -1 if ($value =~ /^\.\*/);
422 $exact_pattern_match = 1 if ($value_pd >= $file_pd);
423 if ($pattern_depth == 0 ||
424 (($file_pd - $value_pd) < $pattern_depth)) {
425 $hash{$tvi} = $value_pd;
436 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
437 add_categories($line);
440 my $start = find_starting_index($line);
441 my $end = find_ending_index($line);
442 for ($i = $start; $i < $end; $i++) {
443 my $line = $typevalue[$i];
444 if ($line =~ /^[FX]:/) { ##Restore file patterns
445 $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
446 $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
447 $line =~ s/\\\./\./g; ##Convert \. to .
448 $line =~ s/\.\*/\*/g; ##Convert .* to *
450 $line =~ s/^([A-Z]):/$1:\t/g;
458 ($email_git || ($email_git_fallback && !$exact_pattern_match))) {
459 vcs_file_signoffs($file);
461 if ($email && $email_git_blame) {
462 vcs_file_blame($file);
464 if ($email && $interactive){
465 vcs_file_shortlogs($file);
471 @keyword_tvi = sort_and_uniq(@keyword_tvi);
472 foreach my $line (@keyword_tvi) {
473 add_categories($line);
478 foreach my $chief (@penguin_chief) {
479 if ($chief =~ m/^(.*):(.*)/) {
482 $email_address = format_email($1, $2, $email_usename);
483 if ($email_git_penguin_chiefs) {
484 push(@email_to, [$email_address, 'chief penguin']);
486 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
491 foreach my $email (@file_emails) {
492 my ($name, $address) = parse_email($email);
494 my $tmp_email = format_email($name, $address, $email_usename);
495 push_email_address($tmp_email, '');
496 add_role($tmp_email, 'in file');
501 if ($email || $email_list) {
505 @email_to = @{vcs_interactive_menu(\@email_to)};
507 @to = (@to, @email_to);
510 @to = (@to, @list_to);
512 output(merge_email(@to));
520 @status = uniq(@status);
525 @subsystem = uniq(@subsystem);
536 sub file_match_pattern {
537 my ($file, $pattern) = @_;
538 if (substr($pattern, -1) eq "/") {
539 if ($file =~ m@^$pattern@) {
543 if ($file =~ m@^$pattern@) {
544 my $s1 = ($file =~ tr@/@@);
545 my $s2 = ($pattern =~ tr@/@@);
556 usage: $P [options] patchfile
557 $P [options] -f file|directory
560 MAINTAINER field selection options:
561 --email => print email address(es) if any
562 --git => include recent git \*-by: signers
563 --git-all-signature-types => include signers regardless of signature type
564 or use only ${signaturePattern} signers (default: $email_git_all_signature_types)
565 --git-fallback => use git when no exact MAINTAINERS pattern (default: $email_git_fallback)
566 --git-chief-penguins => include ${penguin_chiefs}
567 --git-min-signatures => number of signatures required (default: $email_git_min_signatures)
568 --git-max-maintainers => maximum maintainers to add (default: $email_git_max_maintainers)
569 --git-min-percent => minimum percentage of commits required (default: $email_git_min_percent)
570 --git-blame => use git blame to find modified commits for patch or file
571 --git-since => git history to use (default: $email_git_since)
572 --hg-since => hg history to use (default: $email_hg_since)
573 --interactive => display a menu (mostly useful if used with the --git option)
574 --m => include maintainer(s) if any
575 --n => include name 'Full Name <addr\@domain.tld>'
576 --l => include list(s) if any
577 --s => include subscriber only list(s) if any
578 --remove-duplicates => minimize duplicate email names/addresses
579 --roles => show roles (status:subsystem, git-signer, list, etc...)
580 --rolestats => show roles and statistics (commits/total_commits, %)
581 --file-emails => add email addresses found in -f file (default: 0 (off))
582 --scm => print SCM tree(s) if any
583 --status => print status if any
584 --subsystem => print subsystem name if any
585 --web => print website(s) if any
588 --separator [, ] => separator for multiple entries on 1 line
589 using --separator also sets --nomultiline if --separator is not [, ]
590 --multiline => print 1 entry per line
593 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
594 --keywords => scan patch for keywords (default: 1 (on))
595 --sections => print the entire subsystem sections with pattern matches
596 --version => show version
597 --help => show this help information
600 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
603 Using "-f directory" may give unexpected results:
604 Used with "--git", git signators for _all_ files in and below
605 directory are examined as git recurses directories.
606 Any specified X: (exclude) pattern matches are _not_ ignored.
607 Used with "--nogit", directory is used as a pattern match,
608 no individual file within the directory or subdirectory
610 Used with "--git-blame", does not iterate all files in directory
611 Using "--git-blame" is slow and may add old committers and authors
612 that are no longer active maintainers to the output.
613 Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
614 other automated tools that expect only ["name"] <email address>
615 may not work because of additional output after <email address>.
616 Using "--rolestats" and "--git-blame" shows the #/total=% commits,
617 not the percentage of the entire file authored. # of commits is
618 not a good measure of amount of code authored. 1 major commit may
619 contain a thousand lines, 5 trivial commits may modify a single line.
620 If git is not installed, but mercurial (hg) is installed and an .hg
621 repository exists, the following options apply to mercurial:
623 --git-min-signatures, --git-max-maintainers, --git-min-percent, and
625 Use --hg-since not --git-since to control date selection
626 File ".get_maintainer.conf", if it exists in the linux kernel source root
627 directory, can change whatever get_maintainer defaults are desired.
628 Entries in this file can be any command line argument.
629 This file is prepended to any additional command line arguments.
630 Multiple lines and # comments are allowed.
634 sub top_of_kernel_tree {
637 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
640 if ( (-f "${lk_path}COPYING")
641 && (-f "${lk_path}CREDITS")
642 && (-f "${lk_path}Kbuild")
643 && (-f "${lk_path}MAINTAINERS")
644 && (-f "${lk_path}Makefile")
645 && (-f "${lk_path}README")
646 && (-d "${lk_path}Documentation")
647 && (-d "${lk_path}arch")
648 && (-d "${lk_path}include")
649 && (-d "${lk_path}drivers")
650 && (-d "${lk_path}fs")
651 && (-d "${lk_path}init")
652 && (-d "${lk_path}ipc")
653 && (-d "${lk_path}kernel")
654 && (-d "${lk_path}lib")
655 && (-d "${lk_path}scripts")) {
662 my ($formatted_email) = @_;
667 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
670 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
672 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
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
685 return ($name, $address);
689 my ($name, $address, $usename) = @_;
693 $name =~ s/^\s+|\s+$//g;
694 $name =~ s/^\"|\"$//g;
695 $address =~ s/^\s+|\s+$//g;
697 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
698 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
704 $formatted_email = "$address";
706 $formatted_email = "$name <$address>";
709 $formatted_email = $address;
712 return $formatted_email;
715 sub find_first_section {
718 while ($index < @typevalue) {
719 my $tv = $typevalue[$index];
720 if (($tv =~ m/^(\C):\s*(.*)/)) {
729 sub find_starting_index {
733 my $tv = $typevalue[$index];
734 if (!($tv =~ m/^(\C):\s*(.*)/)) {
743 sub find_ending_index {
746 while ($index < @typevalue) {
747 my $tv = $typevalue[$index];
748 if (!($tv =~ m/^(\C):\s*(.*)/)) {
757 sub get_maintainer_role {
761 my $start = find_starting_index($index);
762 my $end = find_ending_index($index);
765 my $subsystem = $typevalue[$start];
766 if (length($subsystem) > 20) {
767 $subsystem = substr($subsystem, 0, 17);
768 $subsystem =~ s/\s*$//;
769 $subsystem = $subsystem . "...";
772 for ($i = $start + 1; $i < $end; $i++) {
773 my $tv = $typevalue[$i];
774 if ($tv =~ m/^(\C):\s*(.*)/) {
784 if ($role eq "supported") {
786 } elsif ($role eq "maintained") {
787 $role = "maintainer";
788 } elsif ($role eq "odd fixes") {
790 } elsif ($role eq "orphan") {
791 $role = "orphan minder";
792 } elsif ($role eq "obsolete") {
793 $role = "obsolete minder";
794 } elsif ($role eq "buried alive in reporters") {
795 $role = "chief penguin";
798 return $role . ":" . $subsystem;
805 my $start = find_starting_index($index);
806 my $end = find_ending_index($index);
808 my $subsystem = $typevalue[$start];
809 if (length($subsystem) > 20) {
810 $subsystem = substr($subsystem, 0, 17);
811 $subsystem =~ s/\s*$//;
812 $subsystem = $subsystem . "...";
815 if ($subsystem eq "THE REST") {
826 my $start = find_starting_index($index);
827 my $end = find_ending_index($index);
829 push(@subsystem, $typevalue[$start]);
831 for ($i = $start + 1; $i < $end; $i++) {
832 my $tv = $typevalue[$i];
833 if ($tv =~ m/^(\C):\s*(.*)/) {
837 my $list_address = $pvalue;
838 my $list_additional = "";
839 my $list_role = get_list_role($i);
841 if ($list_role ne "") {
842 $list_role = ":" . $list_role;
844 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
846 $list_additional = $2;
848 if ($list_additional =~ m/subscribers-only/) {
849 if ($email_subscriber_list) {
850 push(@list_to, [$list_address, "subscriber list${list_role}"]);
854 push(@list_to, [$list_address, "open list${list_role}"]);
857 } elsif ($ptype eq "M") {
858 my ($name, $address) = parse_email($pvalue);
861 my $tv = $typevalue[$i - 1];
862 if ($tv =~ m/^(\C):\s*(.*)/) {
865 $pvalue = format_email($name, $address, $email_usename);
870 if ($email_maintainer) {
871 my $role = get_maintainer_role($i);
872 push_email_addresses($pvalue, $role);
874 } elsif ($ptype eq "T") {
876 } elsif ($ptype eq "W") {
878 } elsif ($ptype eq "S") {
879 push(@status, $pvalue);
886 my %email_hash_address;
889 my ($name, $address) = @_;
891 return 1 if (($name eq "") && ($address eq ""));
892 return 1 if (($name ne "") && exists($email_hash_name{$name}));
893 return 1 if (($address ne "") && exists($email_hash_address{$address}));
898 sub push_email_address {
899 my ($line, $role) = @_;
901 my ($name, $address) = parse_email($line);
903 if ($address eq "") {
907 if (!$email_remove_duplicates) {
908 push(@email_to, [format_email($name, $address, $email_usename), $role]);
909 } elsif (!email_inuse($name, $address)) {
910 push(@email_to, [format_email($name, $address, $email_usename), $role]);
911 $email_hash_name{$name}++;
912 $email_hash_address{$address}++;
918 sub push_email_addresses {
919 my ($address, $role) = @_;
921 my @address_list = ();
923 if (rfc822_valid($address)) {
924 push_email_address($address, $role);
925 } elsif (@address_list = rfc822_validlist($address)) {
926 my $array_count = shift(@address_list);
927 while (my $entry = shift(@address_list)) {
928 push_email_address($entry, $role);
931 if (!push_email_address($address, $role)) {
932 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
938 my ($line, $role) = @_;
940 my ($name, $address) = parse_email($line);
941 my $email = format_email($name, $address, $email_usename);
943 foreach my $entry (@email_to) {
944 if ($email_remove_duplicates) {
945 my ($entry_name, $entry_address) = parse_email($entry->[0]);
946 if (($name eq $entry_name || $address eq $entry_address)
947 && ($role eq "" || !($entry->[1] =~ m/$role/))
949 if ($entry->[1] eq "") {
950 $entry->[1] = "$role";
952 $entry->[1] = "$entry->[1],$role";
956 if ($email eq $entry->[0]
957 && ($role eq "" || !($entry->[1] =~ m/$role/))
959 if ($entry->[1] eq "") {
960 $entry->[1] = "$role";
962 $entry->[1] = "$entry->[1],$role";
972 foreach my $path (split(/:/, $ENV{PATH})) {
973 if (-e "$path/$bin") {
984 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
985 if (-e "$path/$conf") {
986 return "$path/$conf";
997 foreach my $line (@lines) {
998 my ($name, $address) = parse_email($line);
999 if (!exists($hash{$name})) {
1000 $hash{$name} = $address;
1001 } elsif ($address ne $hash{$name}) {
1002 $address = $hash{$name};
1003 $line = format_email($name, $address, $email_usename);
1005 if (exists($mailmap{$name})) {
1006 my $obj = $mailmap{$name};
1007 foreach my $map_address (@$obj) {
1008 if (($map_address eq $address) &&
1009 ($map_address ne $hash{$name})) {
1010 $line = format_email($name, $hash{$name}, $email_usename);
1019 sub git_execute_cmd {
1023 my $output = `$cmd`;
1024 $output =~ s/^\s*//gm;
1025 @lines = split("\n", $output);
1030 sub hg_execute_cmd {
1034 my $output = `$cmd`;
1035 @lines = split("\n", $output);
1040 sub vcs_find_signers {
1045 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1047 my $pattern = $VCS_cmds{"commit_pattern"};
1049 $commits = grep(/$pattern/, @lines); # of commits
1051 @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines);
1052 if (!$email_git_penguin_chiefs) {
1053 @lines = grep(!/${penguin_chiefs}/i, @lines);
1056 return (0, @lines) if !@lines;
1059 s/.*:\s*(.+)\s*/$1/ for (@lines);
1061 ## Reformat email addresses (with names) to avoid badly written signatures
1063 foreach my $line (@lines) {
1064 my ($name, $address) = parse_email($line);
1065 $line = format_email($name, $address, 1);
1068 return ($commits, @lines);
1071 sub vcs_find_author {
1075 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1077 if (!$email_git_penguin_chiefs) {
1078 @lines = grep(!/${penguin_chiefs}/i, @lines);
1081 return @lines if !@lines;
1083 ## Reformat email addresses (with names) to avoid badly written signatures
1085 foreach my $line (@lines) {
1086 my ($name, $address) = parse_email($line);
1087 $line = format_email($name, $address, 1);
1093 sub vcs_save_commits {
1098 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1100 foreach my $line (@lines) {
1101 if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
1114 return @commits if (!(-f $file));
1116 if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
1117 my @all_commits = ();
1119 $cmd = $VCS_cmds{"blame_file_cmd"};
1120 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1121 @all_commits = vcs_save_commits($cmd);
1123 foreach my $file_range_diff (@range) {
1124 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1126 my $diff_start = $2;
1127 my $diff_length = $3;
1128 next if ("$file" ne "$diff_file");
1129 for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
1130 push(@commits, $all_commits[$i]);
1134 foreach my $file_range_diff (@range) {
1135 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1137 my $diff_start = $2;
1138 my $diff_length = $3;
1139 next if ("$file" ne "$diff_file");
1140 $cmd = $VCS_cmds{"blame_range_cmd"};
1141 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1142 push(@commits, vcs_save_commits($cmd));
1145 $cmd = $VCS_cmds{"blame_file_cmd"};
1146 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1147 @commits = vcs_save_commits($cmd);
1150 foreach my $commit (@commits) {
1151 $commit =~ s/^\^//g;
1157 my $printed_novcs = 0;
1159 %VCS_cmds = %VCS_cmds_git;
1160 return 1 if eval $VCS_cmds{"available"};
1161 %VCS_cmds = %VCS_cmds_hg;
1162 return 1 if eval $VCS_cmds{"available"};
1164 if (!$printed_novcs) {
1165 warn("$P: No supported VCS found. Add --nogit to options?\n");
1166 warn("Using a git repository produces better results.\n");
1167 warn("Try Linus Torvalds' latest git repository using:\n");
1168 warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
1174 sub vcs_interactive_menu {
1175 my $list_ref = shift;
1176 my @list = @$list_ref;
1178 return if (!vcs_exists());
1185 #select maintainers by default
1186 foreach my $entry (@list){
1187 my $role = $entry->[1];
1188 $selected{$count} = ($role =~ /maintainer:|supporter:/);
1195 foreach my $entry (@list){
1196 my $email = $entry->[0];
1197 my $role = $entry->[1];
1198 if ($selected{$count}){
1203 print STDERR "$count: $email,\t\t $role";
1205 if ($shortlog{$count}){
1206 my $entries_ref = vcs_get_shortlog($email);
1207 foreach my $entry_ref (@{$entries_ref}){
1208 my $filename = @{$entry_ref}[0];
1209 my @shortlog = @{@{$entry_ref}[1]};
1210 print STDERR "\tshortlog for $filename (authored commits: " . @shortlog . ").\n";
1211 foreach my $commit (@shortlog){
1212 print STDERR "\t $commit\n";
1220 print STDERR "Choose whom to cc by entering a commaseperated list of numbers and hitting enter.\n";
1221 print STDERR "To show a short list of commits, precede the number by a '?',\n";
1222 print STDERR "A blank line indicates that you are satisfied with your choice.\n";
1226 my @wish = split(/[, ]+/,$input);
1227 foreach my $nr (@wish){
1234 #skip out of bounds numbers
1235 next unless ($nr <= $count && $nr >= 0);
1238 $shortlog{$nr} = !$shortlog{$nr};
1240 $selected{$nr} = !$selected{$nr};
1242 #switch shortlog on if an entry get's selected
1243 if ($selected{$nr}){
1248 } while(length($input) > 0);
1250 #drop not selected entries
1253 foreach my $entry (@list){
1254 if ($selected{$count}){
1255 push(@new_emailto,$list[$count]);
1256 print STDERR "$count: ";
1257 print STDERR $email_to[$count]->[0];
1258 print STDERR ",\t\t ";
1259 print STDERR $email_to[$count]->[1];
1264 return \@new_emailto;
1267 sub vcs_get_shortlog {
1269 my ($name, $address) = parse_email($arg);
1270 return $shortlog_buffer{$address};
1273 sub vcs_file_shortlogs {
1275 print STDERR "shortlog processing $file:";
1276 foreach my $entry (@email_to){
1277 my ($name, $address) = parse_email($entry->[0]);
1279 my $commits_ref = vcs_email_shortlog($address, $file);
1280 push(@{$shortlog_buffer{$address}}, [ $file, $commits_ref ]);
1285 sub vcs_email_shortlog {
1289 my $cmd = $VCS_cmds{"shortlog_cmd"};
1290 $cmd =~ s/(\$\w+)/$1/eeg; #substitute variables
1291 my @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1296 my ($role, $divisor, @lines) = @_;
1301 return if (@lines <= 0);
1303 if ($divisor <= 0) {
1304 warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
1308 if ($email_remove_duplicates) {
1309 @lines = mailmap(@lines);
1312 return if (@lines <= 0);
1314 @lines = sort(@lines);
1317 $hash{$_}++ for @lines;
1320 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
1321 my $sign_offs = $hash{$line};
1322 my $percent = $sign_offs * 100 / $divisor;
1324 $percent = 100 if ($percent > 100);
1326 last if ($sign_offs < $email_git_min_signatures ||
1327 $count > $email_git_max_maintainers ||
1328 $percent < $email_git_min_percent);
1329 push_email_address($line, '');
1330 if ($output_rolestats) {
1331 my $fmt_percent = sprintf("%.0f", $percent);
1332 add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
1334 add_role($line, $role);
1339 sub vcs_file_signoffs {
1345 return if (!vcs_exists());
1347 my $cmd = $VCS_cmds{"find_signers_cmd"};
1348 $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
1350 ($commits, @signers) = vcs_find_signers($cmd);
1351 vcs_assign("commit_signer", $commits, @signers);
1354 sub vcs_file_blame {
1358 my @all_commits = ();
1363 return if (!vcs_exists());
1365 @all_commits = vcs_blame($file);
1366 @commits = uniq(@all_commits);
1367 $total_commits = @commits;
1368 $total_lines = @all_commits;
1370 foreach my $commit (@commits) {
1372 my @commit_signers = ();
1374 my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1375 $cmd =~ s/(\$\w+)/$1/eeg; #substitute variables in $cmd
1377 ($commit_count, @commit_signers) = vcs_find_signers($cmd);
1379 push(@signers, @commit_signers);
1382 if ($from_filename) {
1383 if ($output_rolestats) {
1385 foreach my $commit (@commits) {
1387 my $cmd = $VCS_cmds{"find_commit_author_cmd"};
1388 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1389 my @author = vcs_find_author($cmd);
1391 my $count = grep(/$commit/, @all_commits);
1392 for ($i = 0; $i < $count ; $i++) {
1393 push(@blame_signers, $author[0]);
1396 if (@blame_signers) {
1397 vcs_assign("authored lines", $total_lines, @blame_signers);
1400 vcs_assign("commits", $total_commits, @signers);
1402 vcs_assign("modified commits", $total_commits, @signers);
1410 @parms = grep(!$saw{$_}++, @parms);
1418 @parms = sort @parms;
1419 @parms = grep(!$saw{$_}++, @parms);
1423 sub clean_file_emails {
1424 my (@file_emails) = @_;
1425 my @fmt_emails = ();
1427 foreach my $email (@file_emails) {
1428 $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
1429 my ($name, $address) = parse_email($email);
1430 if ($name eq '"[,\.]"') {
1434 my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
1436 my $first = $nw[@nw - 3];
1437 my $middle = $nw[@nw - 2];
1438 my $last = $nw[@nw - 1];
1440 if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
1441 (length($first) == 2 && substr($first, -1) eq ".")) ||
1442 (length($middle) == 1 ||
1443 (length($middle) == 2 && substr($middle, -1) eq "."))) {
1444 $name = "$first $middle $last";
1446 $name = "$middle $last";
1450 if (substr($name, -1) =~ /[,\.]/) {
1451 $name = substr($name, 0, length($name) - 1);
1452 } elsif (substr($name, -2) =~ /[,\.]"/) {
1453 $name = substr($name, 0, length($name) - 2) . '"';
1456 if (substr($name, 0, 1) =~ /[,\.]/) {
1457 $name = substr($name, 1, length($name) - 1);
1458 } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
1459 $name = '"' . substr($name, 2, length($name) - 2);
1462 my $fmt_email = format_email($name, $address, $email_usename);
1463 push(@fmt_emails, $fmt_email);
1473 my ($address, $role) = @$_;
1474 if (!$saw{$address}) {
1475 if ($output_roles) {
1476 push(@lines, "$address ($role)");
1478 push(@lines, $address);
1490 if ($output_multiline) {
1491 foreach my $line (@parms) {
1495 print(join($output_separator, @parms));
1503 # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
1504 # comment. We must allow for rfc822_lwsp (or comments) after each of these.
1505 # This regexp will only work on addresses which have had comments stripped
1506 # and replaced with rfc822_lwsp.
1508 my $specials = '()<>@,;:\\\\".\\[\\]';
1509 my $controls = '\\000-\\037\\177';
1511 my $dtext = "[^\\[\\]\\r\\\\]";
1512 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1514 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1516 # Use zero-width assertion to spot the limit of an atom. A simple
1517 # $rfc822_lwsp* causes the regexp engine to hang occasionally.
1518 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
1519 my $word = "(?:$atom|$quoted_string)";
1520 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
1522 my $sub_domain = "(?:$atom|$domain_literal)";
1523 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1525 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1527 my $phrase = "$word*";
1528 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
1529 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
1530 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
1532 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1533 my $address = "(?:$mailbox|$group)";
1535 return "$rfc822_lwsp*$address";
1538 sub rfc822_strip_comments {
1540 # Recursively remove comments, and replace with a single space. The simpler
1541 # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
1542 # chars in atoms, for example.
1544 while ($s =~ s/^((?:[^"\\]|\\.)*
1545 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1546 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1550 # valid: returns true if the parameter is an RFC822 valid address
1553 my $s = rfc822_strip_comments(shift);
1556 $rfc822re = make_rfc822re();
1559 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1562 # validlist: In scalar context, returns true if the parameter is an RFC822
1563 # valid list of addresses.
1565 # In list context, returns an empty list on failure (an invalid
1566 # address was found); otherwise a list whose first element is the
1567 # number of addresses found and whose remaining elements are the
1568 # addresses. This is needed to disambiguate failure (invalid)
1569 # from success with no addresses found, because an empty string is
1572 sub rfc822_validlist {
1573 my $s = rfc822_strip_comments(shift);
1576 $rfc822re = make_rfc822re();
1578 # * null list items are valid according to the RFC
1579 # * the '1' business is to aid in distinguishing failure from no results
1582 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
1583 $s =~ m/^$rfc822_char*$/) {
1584 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
1587 return wantarray ? (scalar(@r), @r) : 1;
1589 return wantarray ? () : 0;