Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
| 2 | # (c) 2007, Joe Perches <joe@perches.com> |
| 3 | # created from checkpatch.pl |
| 4 | # |
| 5 | # Print selected MAINTAINERS information for |
| 6 | # the files modified in a patch or for a file |
| 7 | # |
Roel Kluin | 3bd7bf5 | 2009-11-11 14:26:13 -0800 | [diff] [blame] | 8 | # usage: perl scripts/get_maintainer.pl [OPTIONS] <patch> |
| 9 | # perl scripts/get_maintainer.pl [OPTIONS] -f <file> |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 10 | # |
| 11 | # Licensed under the terms of the GNU GPL License version 2 |
| 12 | |
| 13 | use strict; |
| 14 | |
| 15 | my $P = $0; |
Joe Perches | 63ab52d | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 16 | my $V = '0.25'; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 17 | |
| 18 | use Getopt::Long qw(:config no_auto_abbrev); |
| 19 | |
| 20 | my $lk_path = "./"; |
| 21 | my $email = 1; |
| 22 | my $email_usename = 1; |
| 23 | my $email_maintainer = 1; |
| 24 | my $email_list = 1; |
| 25 | my $email_subscriber_list = 0; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 26 | my $email_git_penguin_chiefs = 0; |
Joe Perches | e3e9d11 | 2010-10-26 14:22:53 -0700 | [diff] [blame^] | 27 | my $email_git = 0; |
Florian Mickler | 0fa0559 | 2010-05-24 14:33:20 -0700 | [diff] [blame] | 28 | my $email_git_all_signature_types = 0; |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 29 | my $email_git_blame = 0; |
Joe Perches | e3e9d11 | 2010-10-26 14:22:53 -0700 | [diff] [blame^] | 30 | my $email_git_fallback = 1; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 31 | my $email_git_min_signatures = 1; |
| 32 | my $email_git_max_maintainers = 5; |
Joe Perches | afa81ee | 2009-07-29 15:04:28 -0700 | [diff] [blame] | 33 | my $email_git_min_percent = 5; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 34 | my $email_git_since = "1-year-ago"; |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 35 | my $email_hg_since = "-365"; |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 36 | my $email_remove_duplicates = 1; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 37 | my $output_multiline = 1; |
| 38 | my $output_separator = ", "; |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 39 | my $output_roles = 0; |
| 40 | my $output_rolestats = 0; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 41 | my $scm = 0; |
| 42 | my $web = 0; |
| 43 | my $subsystem = 0; |
| 44 | my $status = 0; |
Joe Perches | dcf36a9 | 2009-10-26 16:49:47 -0700 | [diff] [blame] | 45 | my $keywords = 1; |
Joe Perches | 4b76c9d | 2010-03-05 13:43:03 -0800 | [diff] [blame] | 46 | my $sections = 0; |
Joe Perches | 03372db | 2010-03-05 13:43:00 -0800 | [diff] [blame] | 47 | my $file_emails = 0; |
Joe Perches | 4a7fdb5 | 2009-04-10 12:28:57 -0700 | [diff] [blame] | 48 | my $from_filename = 0; |
Joe Perches | 3fb5565 | 2009-09-21 17:04:17 -0700 | [diff] [blame] | 49 | my $pattern_depth = 0; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 50 | my $version = 0; |
| 51 | my $help = 0; |
| 52 | |
| 53 | my $exit = 0; |
| 54 | |
| 55 | my @penguin_chief = (); |
Joe Perches | e4d26b0 | 2010-05-24 14:33:17 -0700 | [diff] [blame] | 56 | push(@penguin_chief, "Linus Torvalds:torvalds\@linux-foundation.org"); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 57 | #Andrew wants in on most everything - 2009/01/14 |
Joe Perches | e4d26b0 | 2010-05-24 14:33:17 -0700 | [diff] [blame] | 58 | #push(@penguin_chief, "Andrew Morton:akpm\@linux-foundation.org"); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 59 | |
| 60 | my @penguin_chief_names = (); |
| 61 | foreach my $chief (@penguin_chief) { |
| 62 | if ($chief =~ m/^(.*):(.*)/) { |
| 63 | my $chief_name = $1; |
| 64 | my $chief_addr = $2; |
| 65 | push(@penguin_chief_names, $chief_name); |
| 66 | } |
| 67 | } |
Joe Perches | e4d26b0 | 2010-05-24 14:33:17 -0700 | [diff] [blame] | 68 | my $penguin_chiefs = "\(" . join("|", @penguin_chief_names) . "\)"; |
| 69 | |
| 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) . "\)"; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 78 | |
Joe Perches | 5f2441e | 2009-06-16 15:34:02 -0700 | [diff] [blame] | 79 | # rfc822 email address - preloaded methods go here. |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 80 | my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])"; |
Joe Perches | df4cc03 | 2009-06-16 15:34:04 -0700 | [diff] [blame] | 81 | my $rfc822_char = '[\\000-\\377]'; |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 82 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 83 | # VCS command support: class-like functions and strings |
| 84 | |
| 85 | my %VCS_cmds; |
| 86 | |
| 87 | my %VCS_cmds_git = ( |
| 88 | "execute_cmd" => \&git_execute_cmd, |
| 89 | "available" => '(which("git") ne "") && (-d ".git")', |
Richard Kennedy | 99cf611 | 2010-02-02 13:44:07 -0800 | [diff] [blame] | 90 | "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file", |
| 91 | "find_commit_signers_cmd" => "git log --no-color -1 \$commit", |
Joe Perches | 63ab52d | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 92 | "find_commit_author_cmd" => "git log -1 --format=\"%an <%ae>\" \$commit", |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 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]+) " |
| 97 | ); |
| 98 | |
| 99 | my %VCS_cmds_hg = ( |
| 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", |
Joe Perches | 63ab52d | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 106 | "find_commit_author_cmd" => "hg log -l 1 --template='{author}\\n' -r \$commit", |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 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]+):" |
| 111 | ); |
| 112 | |
Joe Perches | 368669d | 2010-05-24 14:33:19 -0700 | [diff] [blame] | 113 | if (-f "${lk_path}.get_maintainer.conf") { |
| 114 | my @conf_args; |
| 115 | open(my $conffile, '<', "${lk_path}.get_maintainer.conf") |
| 116 | or warn "$P: Can't open .get_maintainer.conf: $!\n"; |
| 117 | while (<$conffile>) { |
| 118 | my $line = $_; |
| 119 | |
| 120 | $line =~ s/\s*\n?$//g; |
| 121 | $line =~ s/^\s*//g; |
| 122 | $line =~ s/\s+/ /g; |
| 123 | |
| 124 | next if ($line =~ m/^\s*#/); |
| 125 | next if ($line =~ m/^\s*$/); |
| 126 | |
| 127 | my @words = split(" ", $line); |
| 128 | foreach my $word (@words) { |
| 129 | last if ($word =~ m/^#/); |
| 130 | push (@conf_args, $word); |
| 131 | } |
| 132 | } |
| 133 | close($conffile); |
| 134 | unshift(@ARGV, @conf_args) if @conf_args; |
| 135 | } |
| 136 | |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 137 | if (!GetOptions( |
| 138 | 'email!' => \$email, |
| 139 | 'git!' => \$email_git, |
Joe Perches | e4d26b0 | 2010-05-24 14:33:17 -0700 | [diff] [blame] | 140 | 'git-all-signature-types!' => \$email_git_all_signature_types, |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 141 | 'git-blame!' => \$email_git_blame, |
Joe Perches | e3e9d11 | 2010-10-26 14:22:53 -0700 | [diff] [blame^] | 142 | 'git-fallback!' => \$email_git_fallback, |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 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, |
Joe Perches | afa81ee | 2009-07-29 15:04:28 -0700 | [diff] [blame] | 146 | 'git-min-percent=i' => \$email_git_min_percent, |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 147 | 'git-since=s' => \$email_git_since, |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 148 | 'hg-since=s' => \$email_hg_since, |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 149 | 'remove-duplicates!' => \$email_remove_duplicates, |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 150 | 'm!' => \$email_maintainer, |
| 151 | 'n!' => \$email_usename, |
| 152 | 'l!' => \$email_list, |
| 153 | 's!' => \$email_subscriber_list, |
| 154 | 'multiline!' => \$output_multiline, |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 155 | 'roles!' => \$output_roles, |
| 156 | 'rolestats!' => \$output_rolestats, |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 157 | 'separator=s' => \$output_separator, |
| 158 | 'subsystem!' => \$subsystem, |
| 159 | 'status!' => \$status, |
| 160 | 'scm!' => \$scm, |
| 161 | 'web!' => \$web, |
Joe Perches | 3fb5565 | 2009-09-21 17:04:17 -0700 | [diff] [blame] | 162 | 'pattern-depth=i' => \$pattern_depth, |
Joe Perches | dcf36a9 | 2009-10-26 16:49:47 -0700 | [diff] [blame] | 163 | 'k|keywords!' => \$keywords, |
Joe Perches | 4b76c9d | 2010-03-05 13:43:03 -0800 | [diff] [blame] | 164 | 'sections!' => \$sections, |
Joe Perches | 03372db | 2010-03-05 13:43:00 -0800 | [diff] [blame] | 165 | 'fe|file-emails!' => \$file_emails, |
Joe Perches | 4a7fdb5 | 2009-04-10 12:28:57 -0700 | [diff] [blame] | 166 | 'f|file' => \$from_filename, |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 167 | 'v|version' => \$version, |
Joe Perches | 64f77f3 | 2010-03-05 13:43:04 -0800 | [diff] [blame] | 168 | 'h|help|usage' => \$help, |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 169 | )) { |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 170 | die "$P: invalid argument - use --help if necessary\n"; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | if ($help != 0) { |
| 174 | usage(); |
| 175 | exit 0; |
| 176 | } |
| 177 | |
| 178 | if ($version != 0) { |
| 179 | print("${P} ${V}\n"); |
| 180 | exit 0; |
| 181 | } |
| 182 | |
Joe Perches | 64f77f3 | 2010-03-05 13:43:04 -0800 | [diff] [blame] | 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"; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Joe Perches | 4249831 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 188 | if ($output_separator ne ", ") { |
| 189 | $output_multiline = 0; |
| 190 | } |
| 191 | |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 192 | if ($output_rolestats) { |
| 193 | $output_roles = 1; |
| 194 | } |
| 195 | |
Joe Perches | 4b76c9d | 2010-03-05 13:43:03 -0800 | [diff] [blame] | 196 | if ($sections) { |
| 197 | $email = 0; |
| 198 | $email_list = 0; |
| 199 | $scm = 0; |
| 200 | $status = 0; |
| 201 | $subsystem = 0; |
| 202 | $web = 0; |
| 203 | $keywords = 0; |
| 204 | } else { |
| 205 | my $selections = $email + $scm + $status + $subsystem + $web; |
| 206 | if ($selections == 0) { |
Joe Perches | 4b76c9d | 2010-03-05 13:43:03 -0800 | [diff] [blame] | 207 | die "$P: Missing required option: email, scm, status, subsystem or web\n"; |
| 208 | } |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 211 | if ($email && |
| 212 | ($email_maintainer + $email_list + $email_subscriber_list + |
| 213 | $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) { |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 214 | die "$P: Please select at least 1 email option\n"; |
| 215 | } |
| 216 | |
| 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"; |
| 220 | } |
| 221 | |
Joe Perches | e4d26b0 | 2010-05-24 14:33:17 -0700 | [diff] [blame] | 222 | if ($email_git_all_signature_types) { |
| 223 | $signaturePattern = "(.+?)[Bb][Yy]:"; |
| 224 | } |
| 225 | |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 226 | ## Read MAINTAINERS for type/value pairs |
| 227 | |
| 228 | my @typevalue = (); |
Joe Perches | dcf36a9 | 2009-10-26 16:49:47 -0700 | [diff] [blame] | 229 | my %keyword_hash; |
| 230 | |
Stephen Hemminger | 22dd5b0 | 2010-03-05 13:43:06 -0800 | [diff] [blame] | 231 | open (my $maint, '<', "${lk_path}MAINTAINERS") |
| 232 | or die "$P: Can't open MAINTAINERS: $!\n"; |
| 233 | while (<$maint>) { |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 234 | my $line = $_; |
| 235 | |
| 236 | if ($line =~ m/^(\C):\s*(.*)/) { |
| 237 | my $type = $1; |
| 238 | my $value = $2; |
| 239 | |
| 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 . |
Joe Perches | 870020f | 2009-07-29 15:04:28 -0700 | [diff] [blame] | 245 | ##if pattern is a directory and it lacks a trailing slash, add one |
| 246 | if ((-d $value)) { |
| 247 | $value =~ s@([^/])$@$1/@; |
| 248 | } |
Joe Perches | dcf36a9 | 2009-10-26 16:49:47 -0700 | [diff] [blame] | 249 | } elsif ($type eq "K") { |
| 250 | $keyword_hash{@typevalue} = $value; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 251 | } |
| 252 | push(@typevalue, "$type:$value"); |
| 253 | } elsif (!/^(\s)*$/) { |
| 254 | $line =~ s/\n$//g; |
| 255 | push(@typevalue, $line); |
| 256 | } |
| 257 | } |
Stephen Hemminger | 22dd5b0 | 2010-03-05 13:43:06 -0800 | [diff] [blame] | 258 | close($maint); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 259 | |
Joe Perches | 8cbb3a7 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 260 | my %mailmap; |
| 261 | |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 262 | if ($email_remove_duplicates) { |
Stephen Hemminger | 22dd5b0 | 2010-03-05 13:43:06 -0800 | [diff] [blame] | 263 | open(my $mailmap, '<', "${lk_path}.mailmap") |
| 264 | or warn "$P: Can't open .mailmap: $!\n"; |
| 265 | while (<$mailmap>) { |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 266 | my $line = $_; |
Joe Perches | 8cbb3a7 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 267 | |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 268 | next if ($line =~ m/^\s*#/); |
| 269 | next if ($line =~ m/^\s*$/); |
Joe Perches | 8cbb3a7 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 270 | |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 271 | my ($name, $address) = parse_email($line); |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 272 | $line = format_email($name, $address, $email_usename); |
Joe Perches | 8cbb3a7 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 273 | |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 274 | next if ($line =~ m/^\s*$/); |
Joe Perches | 8cbb3a7 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 275 | |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 276 | if (exists($mailmap{$name})) { |
| 277 | my $obj = $mailmap{$name}; |
| 278 | push(@$obj, $address); |
| 279 | } else { |
| 280 | my @arr = ($address); |
| 281 | $mailmap{$name} = \@arr; |
| 282 | } |
Joe Perches | 8cbb3a7 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 283 | } |
Stephen Hemminger | 22dd5b0 | 2010-03-05 13:43:06 -0800 | [diff] [blame] | 284 | close($mailmap); |
Joe Perches | 8cbb3a7 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Joe Perches | 4a7fdb5 | 2009-04-10 12:28:57 -0700 | [diff] [blame] | 287 | ## use the filenames on the command line or find the filenames in the patchfiles |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 288 | |
| 289 | my @files = (); |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 290 | my @range = (); |
Joe Perches | dcf36a9 | 2009-10-26 16:49:47 -0700 | [diff] [blame] | 291 | my @keyword_tvi = (); |
Joe Perches | 03372db | 2010-03-05 13:43:00 -0800 | [diff] [blame] | 292 | my @file_emails = (); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 293 | |
Joe Perches | 64f77f3 | 2010-03-05 13:43:04 -0800 | [diff] [blame] | 294 | if (!@ARGV) { |
| 295 | push(@ARGV, "&STDIN"); |
| 296 | } |
| 297 | |
Joe Perches | 4a7fdb5 | 2009-04-10 12:28:57 -0700 | [diff] [blame] | 298 | foreach my $file (@ARGV) { |
Joe Perches | 64f77f3 | 2010-03-05 13:43:04 -0800 | [diff] [blame] | 299 | if ($file ne "&STDIN") { |
| 300 | ##if $file is a directory and it lacks a trailing slash, add one |
| 301 | if ((-d $file)) { |
| 302 | $file =~ s@([^/])$@$1/@; |
| 303 | } elsif (!(-f $file)) { |
| 304 | die "$P: file '${file}' not found\n"; |
| 305 | } |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 306 | } |
Joe Perches | 4a7fdb5 | 2009-04-10 12:28:57 -0700 | [diff] [blame] | 307 | if ($from_filename) { |
| 308 | push(@files, $file); |
Joe Perches | fab9ed1 | 2010-10-26 14:22:52 -0700 | [diff] [blame] | 309 | if ($file ne "MAINTAINERS" && -f $file && ($keywords || $file_emails)) { |
Stephen Hemminger | 22dd5b0 | 2010-03-05 13:43:06 -0800 | [diff] [blame] | 310 | open(my $f, '<', $file) |
| 311 | or die "$P: Can't open $file: $!\n"; |
| 312 | my $text = do { local($/) ; <$f> }; |
| 313 | close($f); |
Joe Perches | 03372db | 2010-03-05 13:43:00 -0800 | [diff] [blame] | 314 | if ($keywords) { |
| 315 | foreach my $line (keys %keyword_hash) { |
| 316 | if ($text =~ m/$keyword_hash{$line}/x) { |
| 317 | push(@keyword_tvi, $line); |
| 318 | } |
Joe Perches | dcf36a9 | 2009-10-26 16:49:47 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
Joe Perches | 03372db | 2010-03-05 13:43:00 -0800 | [diff] [blame] | 321 | if ($file_emails) { |
| 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)); |
| 324 | } |
Joe Perches | dcf36a9 | 2009-10-26 16:49:47 -0700 | [diff] [blame] | 325 | } |
Joe Perches | 4a7fdb5 | 2009-04-10 12:28:57 -0700 | [diff] [blame] | 326 | } else { |
| 327 | my $file_cnt = @files; |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 328 | my $lastfile; |
Stephen Hemminger | 22dd5b0 | 2010-03-05 13:43:06 -0800 | [diff] [blame] | 329 | |
Wolfram Sang | 3a4df13 | 2010-03-23 13:35:18 -0700 | [diff] [blame] | 330 | open(my $patch, "< $file") |
Stephen Hemminger | 22dd5b0 | 2010-03-05 13:43:06 -0800 | [diff] [blame] | 331 | or die "$P: Can't open $file: $!\n"; |
| 332 | while (<$patch>) { |
Joe Perches | dcf36a9 | 2009-10-26 16:49:47 -0700 | [diff] [blame] | 333 | my $patch_line = $_; |
Joe Perches | 4a7fdb5 | 2009-04-10 12:28:57 -0700 | [diff] [blame] | 334 | if (m/^\+\+\+\s+(\S+)/) { |
| 335 | my $filename = $1; |
| 336 | $filename =~ s@^[^/]*/@@; |
| 337 | $filename =~ s@\n@@; |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 338 | $lastfile = $filename; |
Joe Perches | 4a7fdb5 | 2009-04-10 12:28:57 -0700 | [diff] [blame] | 339 | push(@files, $filename); |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 340 | } elsif (m/^\@\@ -(\d+),(\d+)/) { |
| 341 | if ($email_git_blame) { |
| 342 | push(@range, "$lastfile:$1:$2"); |
| 343 | } |
Joe Perches | dcf36a9 | 2009-10-26 16:49:47 -0700 | [diff] [blame] | 344 | } elsif ($keywords) { |
| 345 | foreach my $line (keys %keyword_hash) { |
| 346 | if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) { |
| 347 | push(@keyword_tvi, $line); |
| 348 | } |
| 349 | } |
Joe Perches | 4a7fdb5 | 2009-04-10 12:28:57 -0700 | [diff] [blame] | 350 | } |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 351 | } |
Stephen Hemminger | 22dd5b0 | 2010-03-05 13:43:06 -0800 | [diff] [blame] | 352 | close($patch); |
| 353 | |
Joe Perches | 4a7fdb5 | 2009-04-10 12:28:57 -0700 | [diff] [blame] | 354 | if ($file_cnt == @files) { |
Joe Perches | 7f29fd27 | 2009-06-16 15:34:04 -0700 | [diff] [blame] | 355 | warn "$P: file '${file}' doesn't appear to be a patch. " |
Joe Perches | 4a7fdb5 | 2009-04-10 12:28:57 -0700 | [diff] [blame] | 356 | . "Add -f to options?\n"; |
| 357 | } |
| 358 | @files = sort_and_uniq(@files); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 359 | } |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Joe Perches | 03372db | 2010-03-05 13:43:00 -0800 | [diff] [blame] | 362 | @file_emails = uniq(@file_emails); |
| 363 | |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 364 | my @email_to = (); |
Joe Perches | 290603c | 2009-06-16 15:33:58 -0700 | [diff] [blame] | 365 | my @list_to = (); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 366 | my @scm = (); |
| 367 | my @web = (); |
| 368 | my @subsystem = (); |
| 369 | my @status = (); |
| 370 | |
| 371 | # Find responsible parties |
| 372 | |
| 373 | foreach my $file (@files) { |
| 374 | |
Joe Perches | 272a897 | 2010-01-08 14:42:48 -0800 | [diff] [blame] | 375 | my %hash; |
Joe Perches | e3e9d11 | 2010-10-26 14:22:53 -0700 | [diff] [blame^] | 376 | my $exact_pattern_match = 0; |
Joe Perches | 272a897 | 2010-01-08 14:42:48 -0800 | [diff] [blame] | 377 | my $tvi = find_first_section(); |
| 378 | while ($tvi < @typevalue) { |
| 379 | my $start = find_starting_index($tvi); |
| 380 | my $end = find_ending_index($tvi); |
| 381 | my $exclude = 0; |
| 382 | my $i; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 383 | |
Joe Perches | 272a897 | 2010-01-08 14:42:48 -0800 | [diff] [blame] | 384 | #Do not match excluded file patterns |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 385 | |
Joe Perches | 272a897 | 2010-01-08 14:42:48 -0800 | [diff] [blame] | 386 | for ($i = $start; $i < $end; $i++) { |
| 387 | my $line = $typevalue[$i]; |
Joe Perches | 290603c | 2009-06-16 15:33:58 -0700 | [diff] [blame] | 388 | if ($line =~ m/^(\C):\s*(.*)/) { |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 389 | my $type = $1; |
| 390 | my $value = $2; |
Joe Perches | 272a897 | 2010-01-08 14:42:48 -0800 | [diff] [blame] | 391 | if ($type eq 'X') { |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 392 | if (file_match_pattern($file, $value)) { |
Joe Perches | 272a897 | 2010-01-08 14:42:48 -0800 | [diff] [blame] | 393 | $exclude = 1; |
Joe Perches | 3c840c1 | 2010-03-05 13:43:07 -0800 | [diff] [blame] | 394 | last; |
Joe Perches | 272a897 | 2010-01-08 14:42:48 -0800 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (!$exclude) { |
| 401 | for ($i = $start; $i < $end; $i++) { |
| 402 | my $line = $typevalue[$i]; |
| 403 | if ($line =~ m/^(\C):\s*(.*)/) { |
| 404 | my $type = $1; |
| 405 | my $value = $2; |
| 406 | if ($type eq 'F') { |
| 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 "/"); |
Joe Perches | e3e9d11 | 2010-10-26 14:22:53 -0700 | [diff] [blame^] | 411 | $value_pd = -1 if ($value =~ /^\.\*/); |
| 412 | $exact_pattern_match = 1 if ($value_pd >= $file_pd); |
Joe Perches | 272a897 | 2010-01-08 14:42:48 -0800 | [diff] [blame] | 413 | if ($pattern_depth == 0 || |
| 414 | (($file_pd - $value_pd) < $pattern_depth)) { |
| 415 | $hash{$tvi} = $value_pd; |
| 416 | } |
Joe Perches | 3fb5565 | 2009-09-21 17:04:17 -0700 | [diff] [blame] | 417 | } |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | } |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 421 | } |
Joe Perches | 272a897 | 2010-01-08 14:42:48 -0800 | [diff] [blame] | 422 | |
Joe Perches | 3c840c1 | 2010-03-05 13:43:07 -0800 | [diff] [blame] | 423 | $tvi = $end + 1; |
Joe Perches | 272a897 | 2010-01-08 14:42:48 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) { |
| 427 | add_categories($line); |
Joe Perches | 6ffd948 | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 428 | if ($sections) { |
| 429 | my $i; |
| 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 * |
Joe Perches | 4b76c9d | 2010-03-05 13:43:03 -0800 | [diff] [blame] | 439 | } |
Joe Perches | 6ffd948 | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 440 | $line =~ s/^([A-Z]):/$1:\t/g; |
| 441 | print("$line\n"); |
Joe Perches | 4b76c9d | 2010-03-05 13:43:03 -0800 | [diff] [blame] | 442 | } |
Joe Perches | 6ffd948 | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 443 | print("\n"); |
| 444 | } |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Joe Perches | e3e9d11 | 2010-10-26 14:22:53 -0700 | [diff] [blame^] | 447 | if ($email && |
| 448 | ($email_git || ($email_git_fallback && !$exact_pattern_match))) { |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 449 | vcs_file_signoffs($file); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 452 | if ($email && $email_git_blame) { |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 453 | vcs_file_blame($file); |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 454 | } |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Joe Perches | dcf36a9 | 2009-10-26 16:49:47 -0700 | [diff] [blame] | 457 | if ($keywords) { |
| 458 | @keyword_tvi = sort_and_uniq(@keyword_tvi); |
| 459 | foreach my $line (@keyword_tvi) { |
| 460 | add_categories($line); |
| 461 | } |
| 462 | } |
| 463 | |
Joe Perches | f5f5078 | 2009-06-16 15:34:00 -0700 | [diff] [blame] | 464 | if ($email) { |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 465 | foreach my $chief (@penguin_chief) { |
| 466 | if ($chief =~ m/^(.*):(.*)/) { |
Joe Perches | f5f5078 | 2009-06-16 15:34:00 -0700 | [diff] [blame] | 467 | my $email_address; |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 468 | |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 469 | $email_address = format_email($1, $2, $email_usename); |
Joe Perches | f5f5078 | 2009-06-16 15:34:00 -0700 | [diff] [blame] | 470 | if ($email_git_penguin_chiefs) { |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 471 | push(@email_to, [$email_address, 'chief penguin']); |
Joe Perches | f5f5078 | 2009-06-16 15:34:00 -0700 | [diff] [blame] | 472 | } else { |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 473 | @email_to = grep($_->[0] !~ /${email_address}/, @email_to); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | } |
Joe Perches | 03372db | 2010-03-05 13:43:00 -0800 | [diff] [blame] | 477 | |
| 478 | foreach my $email (@file_emails) { |
| 479 | my ($name, $address) = parse_email($email); |
| 480 | |
| 481 | my $tmp_email = format_email($name, $address, $email_usename); |
| 482 | push_email_address($tmp_email, ''); |
| 483 | add_role($tmp_email, 'in file'); |
| 484 | } |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Joe Perches | 290603c | 2009-06-16 15:33:58 -0700 | [diff] [blame] | 487 | if ($email || $email_list) { |
| 488 | my @to = (); |
| 489 | if ($email) { |
| 490 | @to = (@to, @email_to); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 491 | } |
Joe Perches | 290603c | 2009-06-16 15:33:58 -0700 | [diff] [blame] | 492 | if ($email_list) { |
Joe Perches | 290603c | 2009-06-16 15:33:58 -0700 | [diff] [blame] | 493 | @to = (@to, @list_to); |
Joe Perches | 290603c | 2009-06-16 15:33:58 -0700 | [diff] [blame] | 494 | } |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 495 | output(merge_email(@to)); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | if ($scm) { |
Joe Perches | b781655 | 2009-09-21 17:04:24 -0700 | [diff] [blame] | 499 | @scm = uniq(@scm); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 500 | output(@scm); |
| 501 | } |
| 502 | |
| 503 | if ($status) { |
Joe Perches | b781655 | 2009-09-21 17:04:24 -0700 | [diff] [blame] | 504 | @status = uniq(@status); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 505 | output(@status); |
| 506 | } |
| 507 | |
| 508 | if ($subsystem) { |
Joe Perches | b781655 | 2009-09-21 17:04:24 -0700 | [diff] [blame] | 509 | @subsystem = uniq(@subsystem); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 510 | output(@subsystem); |
| 511 | } |
| 512 | |
| 513 | if ($web) { |
Joe Perches | b781655 | 2009-09-21 17:04:24 -0700 | [diff] [blame] | 514 | @web = uniq(@web); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 515 | output(@web); |
| 516 | } |
| 517 | |
| 518 | exit($exit); |
| 519 | |
| 520 | sub file_match_pattern { |
| 521 | my ($file, $pattern) = @_; |
| 522 | if (substr($pattern, -1) eq "/") { |
| 523 | if ($file =~ m@^$pattern@) { |
| 524 | return 1; |
| 525 | } |
| 526 | } else { |
| 527 | if ($file =~ m@^$pattern@) { |
| 528 | my $s1 = ($file =~ tr@/@@); |
| 529 | my $s2 = ($pattern =~ tr@/@@); |
| 530 | if ($s1 == $s2) { |
| 531 | return 1; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | sub usage { |
| 539 | print <<EOT; |
| 540 | usage: $P [options] patchfile |
Joe Perches | 870020f | 2009-07-29 15:04:28 -0700 | [diff] [blame] | 541 | $P [options] -f file|directory |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 542 | version: $V |
| 543 | |
| 544 | MAINTAINER field selection options: |
| 545 | --email => print email address(es) if any |
| 546 | --git => include recent git \*-by: signers |
Joe Perches | e4d26b0 | 2010-05-24 14:33:17 -0700 | [diff] [blame] | 547 | --git-all-signature-types => include signers regardless of signature type |
| 548 | or use only ${signaturePattern} signers (default: $email_git_all_signature_types) |
Joe Perches | e3e9d11 | 2010-10-26 14:22:53 -0700 | [diff] [blame^] | 549 | --git-fallback => use git when no exact MAINTAINERS pattern (default: $email_git_fallback) |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 550 | --git-chief-penguins => include ${penguin_chiefs} |
Joe Perches | e4d26b0 | 2010-05-24 14:33:17 -0700 | [diff] [blame] | 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) |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 554 | --git-blame => use git blame to find modified commits for patch or file |
Joe Perches | e4d26b0 | 2010-05-24 14:33:17 -0700 | [diff] [blame] | 555 | --git-since => git history to use (default: $email_git_since) |
| 556 | --hg-since => hg history to use (default: $email_hg_since) |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 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 |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 561 | --remove-duplicates => minimize duplicate email names/addresses |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 562 | --roles => show roles (status:subsystem, git-signer, list, etc...) |
| 563 | --rolestats => show roles and statistics (commits/total_commits, %) |
Joe Perches | 03372db | 2010-03-05 13:43:00 -0800 | [diff] [blame] | 564 | --file-emails => add email addresses found in -f file (default: 0 (off)) |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 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 |
| 569 | |
| 570 | Output type options: |
| 571 | --separator [, ] => separator for multiple entries on 1 line |
Joe Perches | 4249831 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 572 | using --separator also sets --nomultiline if --separator is not [, ] |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 573 | --multiline => print 1 entry per line |
| 574 | |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 575 | Other options: |
Joe Perches | 3fb5565 | 2009-09-21 17:04:17 -0700 | [diff] [blame] | 576 | --pattern-depth => Number of pattern directory traversals (default: 0 (all)) |
Joe Perches | dcf36a9 | 2009-10-26 16:49:47 -0700 | [diff] [blame] | 577 | --keywords => scan patch for keywords (default: 1 (on)) |
Joe Perches | 4b76c9d | 2010-03-05 13:43:03 -0800 | [diff] [blame] | 578 | --sections => print the entire subsystem sections with pattern matches |
Joe Perches | f5f5078 | 2009-06-16 15:34:00 -0700 | [diff] [blame] | 579 | --version => show version |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 580 | --help => show this help information |
| 581 | |
Joe Perches | 3fb5565 | 2009-09-21 17:04:17 -0700 | [diff] [blame] | 582 | Default options: |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 583 | [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates] |
Joe Perches | 3fb5565 | 2009-09-21 17:04:17 -0700 | [diff] [blame] | 584 | |
Joe Perches | 870020f | 2009-07-29 15:04:28 -0700 | [diff] [blame] | 585 | Notes: |
| 586 | Using "-f directory" may give unexpected results: |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 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, |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 591 | no individual file within the directory or subdirectory |
| 592 | is matched. |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 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. |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 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. |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 603 | If git is not installed, but mercurial (hg) is installed and an .hg |
| 604 | repository exists, the following options apply to mercurial: |
| 605 | --git, |
| 606 | --git-min-signatures, --git-max-maintainers, --git-min-percent, and |
| 607 | --git-blame |
| 608 | Use --hg-since not --git-since to control date selection |
Joe Perches | 368669d | 2010-05-24 14:33:19 -0700 | [diff] [blame] | 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. |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 614 | EOT |
| 615 | } |
| 616 | |
| 617 | sub top_of_kernel_tree { |
| 618 | my ($lk_path) = @_; |
| 619 | |
| 620 | if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") { |
| 621 | $lk_path .= "/"; |
| 622 | } |
| 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")) { |
| 639 | return 1; |
| 640 | } |
| 641 | return 0; |
| 642 | } |
| 643 | |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 644 | sub parse_email { |
| 645 | my ($formatted_email) = @_; |
| 646 | |
| 647 | my $name = ""; |
| 648 | my $address = ""; |
| 649 | |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 650 | if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) { |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 651 | $name = $1; |
| 652 | $address = $2; |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 653 | } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) { |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 654 | $address = $1; |
Joe Perches | b781655 | 2009-09-21 17:04:24 -0700 | [diff] [blame] | 655 | } elsif ($formatted_email =~ /^(.+\@\S*).*$/) { |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 656 | $address = $1; |
| 657 | } |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 658 | |
| 659 | $name =~ s/^\s+|\s+$//g; |
Joe Perches | d789504 | 2009-06-16 15:34:02 -0700 | [diff] [blame] | 660 | $name =~ s/^\"|\"$//g; |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 661 | $address =~ s/^\s+|\s+$//g; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 662 | |
Stephen Hemminger | a63ceb4 | 2010-03-05 13:43:06 -0800 | [diff] [blame] | 663 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 664 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 665 | $name = "\"$name\""; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 666 | } |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 667 | |
| 668 | return ($name, $address); |
| 669 | } |
| 670 | |
| 671 | sub format_email { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 672 | my ($name, $address, $usename) = @_; |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 673 | |
| 674 | my $formatted_email; |
| 675 | |
| 676 | $name =~ s/^\s+|\s+$//g; |
| 677 | $name =~ s/^\"|\"$//g; |
| 678 | $address =~ s/^\s+|\s+$//g; |
| 679 | |
Stephen Hemminger | a63ceb4 | 2010-03-05 13:43:06 -0800 | [diff] [blame] | 680 | if ($name =~ /[^\w \-]/i) { ##has "must quote" chars |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 681 | $name =~ s/(?<!\\)"/\\"/g; ##escape quotes |
| 682 | $name = "\"$name\""; |
| 683 | } |
| 684 | |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 685 | if ($usename) { |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 686 | if ("$name" eq "") { |
| 687 | $formatted_email = "$address"; |
| 688 | } else { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 689 | $formatted_email = "$name <$address>"; |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 690 | } |
| 691 | } else { |
| 692 | $formatted_email = $address; |
| 693 | } |
| 694 | |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 695 | return $formatted_email; |
| 696 | } |
| 697 | |
Joe Perches | 272a897 | 2010-01-08 14:42:48 -0800 | [diff] [blame] | 698 | sub find_first_section { |
| 699 | my $index = 0; |
| 700 | |
| 701 | while ($index < @typevalue) { |
| 702 | my $tv = $typevalue[$index]; |
| 703 | if (($tv =~ m/^(\C):\s*(.*)/)) { |
| 704 | last; |
| 705 | } |
| 706 | $index++; |
| 707 | } |
| 708 | |
| 709 | return $index; |
| 710 | } |
| 711 | |
Joe Perches | b781655 | 2009-09-21 17:04:24 -0700 | [diff] [blame] | 712 | sub find_starting_index { |
Joe Perches | b781655 | 2009-09-21 17:04:24 -0700 | [diff] [blame] | 713 | my ($index) = @_; |
| 714 | |
| 715 | while ($index > 0) { |
| 716 | my $tv = $typevalue[$index]; |
| 717 | if (!($tv =~ m/^(\C):\s*(.*)/)) { |
| 718 | last; |
| 719 | } |
| 720 | $index--; |
| 721 | } |
| 722 | |
| 723 | return $index; |
| 724 | } |
| 725 | |
| 726 | sub find_ending_index { |
| 727 | my ($index) = @_; |
| 728 | |
| 729 | while ($index < @typevalue) { |
| 730 | my $tv = $typevalue[$index]; |
| 731 | if (!($tv =~ m/^(\C):\s*(.*)/)) { |
| 732 | last; |
| 733 | } |
| 734 | $index++; |
| 735 | } |
| 736 | |
| 737 | return $index; |
| 738 | } |
| 739 | |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 740 | sub get_maintainer_role { |
| 741 | my ($index) = @_; |
| 742 | |
| 743 | my $i; |
| 744 | my $start = find_starting_index($index); |
| 745 | my $end = find_ending_index($index); |
| 746 | |
| 747 | my $role; |
| 748 | my $subsystem = $typevalue[$start]; |
| 749 | if (length($subsystem) > 20) { |
| 750 | $subsystem = substr($subsystem, 0, 17); |
| 751 | $subsystem =~ s/\s*$//; |
| 752 | $subsystem = $subsystem . "..."; |
| 753 | } |
| 754 | |
| 755 | for ($i = $start + 1; $i < $end; $i++) { |
| 756 | my $tv = $typevalue[$i]; |
| 757 | if ($tv =~ m/^(\C):\s*(.*)/) { |
| 758 | my $ptype = $1; |
| 759 | my $pvalue = $2; |
| 760 | if ($ptype eq "S") { |
| 761 | $role = $pvalue; |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | $role = lc($role); |
| 767 | if ($role eq "supported") { |
| 768 | $role = "supporter"; |
| 769 | } elsif ($role eq "maintained") { |
| 770 | $role = "maintainer"; |
| 771 | } elsif ($role eq "odd fixes") { |
| 772 | $role = "odd fixer"; |
| 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"; |
| 779 | } |
| 780 | |
| 781 | return $role . ":" . $subsystem; |
| 782 | } |
| 783 | |
| 784 | sub get_list_role { |
| 785 | my ($index) = @_; |
| 786 | |
| 787 | my $i; |
| 788 | my $start = find_starting_index($index); |
| 789 | my $end = find_ending_index($index); |
| 790 | |
| 791 | my $subsystem = $typevalue[$start]; |
| 792 | if (length($subsystem) > 20) { |
| 793 | $subsystem = substr($subsystem, 0, 17); |
| 794 | $subsystem =~ s/\s*$//; |
| 795 | $subsystem = $subsystem . "..."; |
| 796 | } |
| 797 | |
| 798 | if ($subsystem eq "THE REST") { |
| 799 | $subsystem = ""; |
| 800 | } |
| 801 | |
| 802 | return $subsystem; |
| 803 | } |
| 804 | |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 805 | sub add_categories { |
| 806 | my ($index) = @_; |
| 807 | |
Joe Perches | b781655 | 2009-09-21 17:04:24 -0700 | [diff] [blame] | 808 | my $i; |
| 809 | my $start = find_starting_index($index); |
| 810 | my $end = find_ending_index($index); |
| 811 | |
| 812 | push(@subsystem, $typevalue[$start]); |
| 813 | |
| 814 | for ($i = $start + 1; $i < $end; $i++) { |
| 815 | my $tv = $typevalue[$i]; |
Joe Perches | 290603c | 2009-06-16 15:33:58 -0700 | [diff] [blame] | 816 | if ($tv =~ m/^(\C):\s*(.*)/) { |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 817 | my $ptype = $1; |
| 818 | my $pvalue = $2; |
| 819 | if ($ptype eq "L") { |
Joe Perches | 290603c | 2009-06-16 15:33:58 -0700 | [diff] [blame] | 820 | my $list_address = $pvalue; |
| 821 | my $list_additional = ""; |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 822 | my $list_role = get_list_role($i); |
| 823 | |
| 824 | if ($list_role ne "") { |
| 825 | $list_role = ":" . $list_role; |
| 826 | } |
Joe Perches | 290603c | 2009-06-16 15:33:58 -0700 | [diff] [blame] | 827 | if ($list_address =~ m/([^\s]+)\s+(.*)$/) { |
| 828 | $list_address = $1; |
| 829 | $list_additional = $2; |
| 830 | } |
Joe Perches | bdf7c68 | 2009-06-16 15:33:59 -0700 | [diff] [blame] | 831 | if ($list_additional =~ m/subscribers-only/) { |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 832 | if ($email_subscriber_list) { |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 833 | push(@list_to, [$list_address, "subscriber list${list_role}"]); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 834 | } |
| 835 | } else { |
| 836 | if ($email_list) { |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 837 | push(@list_to, [$list_address, "open list${list_role}"]); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | } elsif ($ptype eq "M") { |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 841 | my ($name, $address) = parse_email($pvalue); |
| 842 | if ($name eq "") { |
Joe Perches | b781655 | 2009-09-21 17:04:24 -0700 | [diff] [blame] | 843 | if ($i > 0) { |
| 844 | my $tv = $typevalue[$i - 1]; |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 845 | if ($tv =~ m/^(\C):\s*(.*)/) { |
| 846 | if ($1 eq "P") { |
| 847 | $name = $2; |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 848 | $pvalue = format_email($name, $address, $email_usename); |
Joe Perches | 5f2441e | 2009-06-16 15:34:02 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | } |
| 852 | } |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 853 | if ($email_maintainer) { |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 854 | my $role = get_maintainer_role($i); |
| 855 | push_email_addresses($pvalue, $role); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 856 | } |
| 857 | } elsif ($ptype eq "T") { |
| 858 | push(@scm, $pvalue); |
| 859 | } elsif ($ptype eq "W") { |
| 860 | push(@web, $pvalue); |
| 861 | } elsif ($ptype eq "S") { |
| 862 | push(@status, $pvalue); |
| 863 | } |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 868 | my %email_hash_name; |
| 869 | my %email_hash_address; |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 870 | |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 871 | sub email_inuse { |
| 872 | my ($name, $address) = @_; |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 873 | |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 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})); |
| 877 | |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 878 | return 0; |
| 879 | } |
| 880 | |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 881 | sub push_email_address { |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 882 | my ($line, $role) = @_; |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 883 | |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 884 | my ($name, $address) = parse_email($line); |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 885 | |
Joe Perches | b781655 | 2009-09-21 17:04:24 -0700 | [diff] [blame] | 886 | if ($address eq "") { |
| 887 | return 0; |
| 888 | } |
| 889 | |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 890 | if (!$email_remove_duplicates) { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 891 | push(@email_to, [format_email($name, $address, $email_usename), $role]); |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 892 | } elsif (!email_inuse($name, $address)) { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 893 | push(@email_to, [format_email($name, $address, $email_usename), $role]); |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 894 | $email_hash_name{$name}++; |
| 895 | $email_hash_address{$address}++; |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 896 | } |
Joe Perches | b781655 | 2009-09-21 17:04:24 -0700 | [diff] [blame] | 897 | |
| 898 | return 1; |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | sub push_email_addresses { |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 902 | my ($address, $role) = @_; |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 903 | |
| 904 | my @address_list = (); |
| 905 | |
Joe Perches | 5f2441e | 2009-06-16 15:34:02 -0700 | [diff] [blame] | 906 | if (rfc822_valid($address)) { |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 907 | push_email_address($address, $role); |
Joe Perches | 5f2441e | 2009-06-16 15:34:02 -0700 | [diff] [blame] | 908 | } elsif (@address_list = rfc822_validlist($address)) { |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 909 | my $array_count = shift(@address_list); |
| 910 | while (my $entry = shift(@address_list)) { |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 911 | push_email_address($entry, $role); |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 912 | } |
Joe Perches | 5f2441e | 2009-06-16 15:34:02 -0700 | [diff] [blame] | 913 | } else { |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 914 | if (!push_email_address($address, $role)) { |
Joe Perches | b781655 | 2009-09-21 17:04:24 -0700 | [diff] [blame] | 915 | warn("Invalid MAINTAINERS address: '" . $address . "'\n"); |
| 916 | } |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 917 | } |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 920 | sub add_role { |
| 921 | my ($line, $role) = @_; |
| 922 | |
| 923 | my ($name, $address) = parse_email($line); |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 924 | my $email = format_email($name, $address, $email_usename); |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 925 | |
| 926 | foreach my $entry (@email_to) { |
| 927 | if ($email_remove_duplicates) { |
| 928 | my ($entry_name, $entry_address) = parse_email($entry->[0]); |
Joe Perches | 03372db | 2010-03-05 13:43:00 -0800 | [diff] [blame] | 929 | if (($name eq $entry_name || $address eq $entry_address) |
| 930 | && ($role eq "" || !($entry->[1] =~ m/$role/)) |
| 931 | ) { |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 932 | if ($entry->[1] eq "") { |
| 933 | $entry->[1] = "$role"; |
| 934 | } else { |
| 935 | $entry->[1] = "$entry->[1],$role"; |
| 936 | } |
| 937 | } |
| 938 | } else { |
Joe Perches | 03372db | 2010-03-05 13:43:00 -0800 | [diff] [blame] | 939 | if ($email eq $entry->[0] |
| 940 | && ($role eq "" || !($entry->[1] =~ m/$role/)) |
| 941 | ) { |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 942 | if ($entry->[1] eq "") { |
| 943 | $entry->[1] = "$role"; |
| 944 | } else { |
| 945 | $entry->[1] = "$entry->[1],$role"; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 952 | sub which { |
| 953 | my ($bin) = @_; |
| 954 | |
Joe Perches | f5f5078 | 2009-06-16 15:34:00 -0700 | [diff] [blame] | 955 | foreach my $path (split(/:/, $ENV{PATH})) { |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 956 | if (-e "$path/$bin") { |
| 957 | return "$path/$bin"; |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | return ""; |
| 962 | } |
| 963 | |
Joe Perches | 8cbb3a7 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 964 | sub mailmap { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 965 | my (@lines) = @_; |
Joe Perches | 8cbb3a7 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 966 | my %hash; |
| 967 | |
| 968 | foreach my $line (@lines) { |
| 969 | my ($name, $address) = parse_email($line); |
| 970 | if (!exists($hash{$name})) { |
| 971 | $hash{$name} = $address; |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 972 | } elsif ($address ne $hash{$name}) { |
| 973 | $address = $hash{$name}; |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 974 | $line = format_email($name, $address, $email_usename); |
Joe Perches | 8cbb3a7 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 975 | } |
| 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})) { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 981 | $line = format_email($name, $hash{$name}, $email_usename); |
Joe Perches | 8cbb3a7 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | return @lines; |
| 988 | } |
| 989 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 990 | sub git_execute_cmd { |
| 991 | my ($cmd) = @_; |
| 992 | my @lines = (); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 993 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 994 | my $output = `$cmd`; |
| 995 | $output =~ s/^\s*//gm; |
| 996 | @lines = split("\n", $output); |
| 997 | |
| 998 | return @lines; |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 999 | } |
| 1000 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1001 | sub hg_execute_cmd { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1002 | my ($cmd) = @_; |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1003 | my @lines = (); |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1004 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1005 | my $output = `$cmd`; |
| 1006 | @lines = split("\n", $output); |
| 1007 | |
| 1008 | return @lines; |
| 1009 | } |
| 1010 | |
| 1011 | sub vcs_find_signers { |
| 1012 | my ($cmd) = @_; |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1013 | my @lines = (); |
| 1014 | my $commits; |
| 1015 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1016 | @lines = &{$VCS_cmds{"execute_cmd"}}($cmd); |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 1017 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1018 | my $pattern = $VCS_cmds{"commit_pattern"}; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 1019 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1020 | $commits = grep(/$pattern/, @lines); # of commits |
Joe Perches | afa81ee | 2009-07-29 15:04:28 -0700 | [diff] [blame] | 1021 | |
Joe Perches | e4d26b0 | 2010-05-24 14:33:17 -0700 | [diff] [blame] | 1022 | @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines); |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 1023 | if (!$email_git_penguin_chiefs) { |
| 1024 | @lines = grep(!/${penguin_chiefs}/i, @lines); |
| 1025 | } |
Joe Perches | 63ab52d | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 1026 | |
| 1027 | return (0, @lines) if !@lines; |
| 1028 | |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 1029 | # cut -f2- -d":" |
| 1030 | s/.*:\s*(.+)\s*/$1/ for (@lines); |
| 1031 | |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1032 | ## Reformat email addresses (with names) to avoid badly written signatures |
| 1033 | |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 1034 | foreach my $line (@lines) { |
| 1035 | my ($name, $address) = parse_email($line); |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1036 | $line = format_email($name, $address, 1); |
| 1037 | } |
| 1038 | |
| 1039 | return ($commits, @lines); |
| 1040 | } |
| 1041 | |
Joe Perches | 63ab52d | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 1042 | sub vcs_find_author { |
| 1043 | my ($cmd) = @_; |
| 1044 | my @lines = (); |
| 1045 | |
| 1046 | @lines = &{$VCS_cmds{"execute_cmd"}}($cmd); |
| 1047 | |
| 1048 | if (!$email_git_penguin_chiefs) { |
| 1049 | @lines = grep(!/${penguin_chiefs}/i, @lines); |
| 1050 | } |
| 1051 | |
| 1052 | return @lines if !@lines; |
| 1053 | |
| 1054 | ## Reformat email addresses (with names) to avoid badly written signatures |
| 1055 | |
| 1056 | foreach my $line (@lines) { |
| 1057 | my ($name, $address) = parse_email($line); |
| 1058 | $line = format_email($name, $address, 1); |
| 1059 | } |
| 1060 | |
| 1061 | return @lines; |
| 1062 | } |
| 1063 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1064 | sub vcs_save_commits { |
| 1065 | my ($cmd) = @_; |
| 1066 | my @lines = (); |
| 1067 | my @commits = (); |
| 1068 | |
| 1069 | @lines = &{$VCS_cmds{"execute_cmd"}}($cmd); |
| 1070 | |
| 1071 | foreach my $line (@lines) { |
| 1072 | if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) { |
| 1073 | push(@commits, $1); |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | return @commits; |
| 1078 | } |
| 1079 | |
| 1080 | sub vcs_blame { |
| 1081 | my ($file) = @_; |
| 1082 | my $cmd; |
| 1083 | my @commits = (); |
| 1084 | |
| 1085 | return @commits if (!(-f $file)); |
| 1086 | |
| 1087 | if (@range && $VCS_cmds{"blame_range_cmd"} eq "") { |
| 1088 | my @all_commits = (); |
| 1089 | |
| 1090 | $cmd = $VCS_cmds{"blame_file_cmd"}; |
| 1091 | $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd |
| 1092 | @all_commits = vcs_save_commits($cmd); |
| 1093 | |
| 1094 | foreach my $file_range_diff (@range) { |
| 1095 | next if (!($file_range_diff =~ m/(.+):(.+):(.+)/)); |
| 1096 | my $diff_file = $1; |
| 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]); |
| 1102 | } |
| 1103 | } |
| 1104 | } elsif (@range) { |
| 1105 | foreach my $file_range_diff (@range) { |
| 1106 | next if (!($file_range_diff =~ m/(.+):(.+):(.+)/)); |
| 1107 | my $diff_file = $1; |
| 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)); |
| 1114 | } |
| 1115 | } else { |
| 1116 | $cmd = $VCS_cmds{"blame_file_cmd"}; |
| 1117 | $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd |
| 1118 | @commits = vcs_save_commits($cmd); |
| 1119 | } |
| 1120 | |
Joe Perches | 63ab52d | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 1121 | foreach my $commit (@commits) { |
| 1122 | $commit =~ s/^\^//g; |
| 1123 | } |
| 1124 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1125 | return @commits; |
| 1126 | } |
| 1127 | |
| 1128 | my $printed_novcs = 0; |
| 1129 | sub vcs_exists { |
| 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"}; |
| 1134 | %VCS_cmds = (); |
| 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"); |
| 1140 | $printed_novcs = 1; |
| 1141 | } |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | sub vcs_assign { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1146 | my ($role, $divisor, @lines) = @_; |
| 1147 | |
| 1148 | my %hash; |
| 1149 | my $count = 0; |
| 1150 | |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1151 | return if (@lines <= 0); |
| 1152 | |
| 1153 | if ($divisor <= 0) { |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1154 | warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n"); |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1155 | $divisor = 1; |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 1156 | } |
Joe Perches | 8cbb3a7 | 2009-09-21 17:04:21 -0700 | [diff] [blame] | 1157 | |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 1158 | if ($email_remove_duplicates) { |
| 1159 | @lines = mailmap(@lines); |
| 1160 | } |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 1161 | |
Joe Perches | 63ab52d | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 1162 | return if (@lines <= 0); |
| 1163 | |
Joe Perches | 0e70e83 | 2009-09-21 17:04:20 -0700 | [diff] [blame] | 1164 | @lines = sort(@lines); |
Joe Perches | afa81ee | 2009-07-29 15:04:28 -0700 | [diff] [blame] | 1165 | |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 1166 | # uniq -c |
| 1167 | $hash{$_}++ for @lines; |
| 1168 | |
| 1169 | # sort -rn |
| 1170 | foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) { |
| 1171 | my $sign_offs = $hash{$line}; |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1172 | my $percent = $sign_offs * 100 / $divisor; |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 1173 | |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1174 | $percent = 100 if ($percent > 100); |
Joe Perches | 11ecf53 | 2009-09-21 17:04:22 -0700 | [diff] [blame] | 1175 | $count++; |
| 1176 | last if ($sign_offs < $email_git_min_signatures || |
| 1177 | $count > $email_git_max_maintainers || |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1178 | $percent < $email_git_min_percent); |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 1179 | push_email_address($line, ''); |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 1180 | if ($output_rolestats) { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1181 | my $fmt_percent = sprintf("%.0f", $percent); |
| 1182 | add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%"); |
| 1183 | } else { |
| 1184 | add_role($line, $role); |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 1185 | } |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 1186 | } |
| 1187 | } |
| 1188 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1189 | sub vcs_file_signoffs { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1190 | my ($file) = @_; |
| 1191 | |
| 1192 | my @signers = (); |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1193 | my $commits; |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1194 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1195 | return if (!vcs_exists()); |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1196 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1197 | my $cmd = $VCS_cmds{"find_signers_cmd"}; |
| 1198 | $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd |
| 1199 | |
| 1200 | ($commits, @signers) = vcs_find_signers($cmd); |
| 1201 | vcs_assign("commit_signer", $commits, @signers); |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1202 | } |
| 1203 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1204 | sub vcs_file_blame { |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 1205 | my ($file) = @_; |
| 1206 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1207 | my @signers = (); |
Joe Perches | 63ab52d | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 1208 | my @all_commits = (); |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1209 | my @commits = (); |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1210 | my $total_commits; |
Joe Perches | 63ab52d | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 1211 | my $total_lines; |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 1212 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1213 | return if (!vcs_exists()); |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 1214 | |
Joe Perches | 63ab52d | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 1215 | @all_commits = vcs_blame($file); |
| 1216 | @commits = uniq(@all_commits); |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1217 | $total_commits = @commits; |
Joe Perches | 63ab52d | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 1218 | $total_lines = @all_commits; |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1219 | |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 1220 | foreach my $commit (@commits) { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1221 | my $commit_count; |
| 1222 | my @commit_signers = (); |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 1223 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1224 | my $cmd = $VCS_cmds{"find_commit_signers_cmd"}; |
| 1225 | $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd |
| 1226 | |
| 1227 | ($commit_count, @commit_signers) = vcs_find_signers($cmd); |
Joe Perches | 63ab52d | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 1228 | |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1229 | push(@signers, @commit_signers); |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1232 | if ($from_filename) { |
Joe Perches | 63ab52d | 2010-10-26 14:22:51 -0700 | [diff] [blame] | 1233 | if ($output_rolestats) { |
| 1234 | my @blame_signers; |
| 1235 | foreach my $commit (@commits) { |
| 1236 | my $i; |
| 1237 | my $cmd = $VCS_cmds{"find_commit_author_cmd"}; |
| 1238 | $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd |
| 1239 | my @author = vcs_find_author($cmd); |
| 1240 | next if !@author; |
| 1241 | my $count = grep(/$commit/, @all_commits); |
| 1242 | for ($i = 0; $i < $count ; $i++) { |
| 1243 | push(@blame_signers, $author[0]); |
| 1244 | } |
| 1245 | } |
| 1246 | if (@blame_signers) { |
| 1247 | vcs_assign("authored lines", $total_lines, @blame_signers); |
| 1248 | } |
| 1249 | } |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1250 | vcs_assign("commits", $total_commits, @signers); |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1251 | } else { |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1252 | vcs_assign("modified commits", $total_commits, @signers); |
Joe Perches | f549266 | 2009-09-21 17:04:13 -0700 | [diff] [blame] | 1253 | } |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | sub uniq { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1257 | my (@parms) = @_; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 1258 | |
| 1259 | my %saw; |
| 1260 | @parms = grep(!$saw{$_}++, @parms); |
| 1261 | return @parms; |
| 1262 | } |
| 1263 | |
| 1264 | sub sort_and_uniq { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1265 | my (@parms) = @_; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 1266 | |
| 1267 | my %saw; |
| 1268 | @parms = sort @parms; |
| 1269 | @parms = grep(!$saw{$_}++, @parms); |
| 1270 | return @parms; |
| 1271 | } |
| 1272 | |
Joe Perches | 03372db | 2010-03-05 13:43:00 -0800 | [diff] [blame] | 1273 | sub clean_file_emails { |
| 1274 | my (@file_emails) = @_; |
| 1275 | my @fmt_emails = (); |
| 1276 | |
| 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 '"[,\.]"') { |
| 1281 | $name = ""; |
| 1282 | } |
| 1283 | |
| 1284 | my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name); |
| 1285 | if (@nw > 2) { |
| 1286 | my $first = $nw[@nw - 3]; |
| 1287 | my $middle = $nw[@nw - 2]; |
| 1288 | my $last = $nw[@nw - 1]; |
| 1289 | |
| 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"; |
| 1295 | } else { |
| 1296 | $name = "$middle $last"; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 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) . '"'; |
| 1304 | } |
| 1305 | |
| 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); |
| 1310 | } |
| 1311 | |
| 1312 | my $fmt_email = format_email($name, $address, $email_usename); |
| 1313 | push(@fmt_emails, $fmt_email); |
| 1314 | } |
| 1315 | return @fmt_emails; |
| 1316 | } |
| 1317 | |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 1318 | sub merge_email { |
| 1319 | my @lines; |
| 1320 | my %saw; |
| 1321 | |
| 1322 | for (@_) { |
| 1323 | my ($address, $role) = @$_; |
| 1324 | if (!$saw{$address}) { |
| 1325 | if ($output_roles) { |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1326 | push(@lines, "$address ($role)"); |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 1327 | } else { |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1328 | push(@lines, $address); |
Joe Perches | 3c7385b | 2009-12-14 18:00:46 -0800 | [diff] [blame] | 1329 | } |
| 1330 | $saw{$address} = 1; |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | return @lines; |
| 1335 | } |
| 1336 | |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 1337 | sub output { |
Joe Perches | a8af243 | 2009-12-14 18:00:49 -0800 | [diff] [blame] | 1338 | my (@parms) = @_; |
Joe Perches | cb7301c | 2009-04-07 20:40:12 -0700 | [diff] [blame] | 1339 | |
| 1340 | if ($output_multiline) { |
| 1341 | foreach my $line (@parms) { |
| 1342 | print("${line}\n"); |
| 1343 | } |
| 1344 | } else { |
| 1345 | print(join($output_separator, @parms)); |
| 1346 | print("\n"); |
| 1347 | } |
| 1348 | } |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 1349 | |
| 1350 | my $rfc822re; |
| 1351 | |
| 1352 | sub make_rfc822re { |
| 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. |
| 1357 | |
| 1358 | my $specials = '()<>@,;:\\\\".\\[\\]'; |
| 1359 | my $controls = '\\000-\\037\\177'; |
| 1360 | |
| 1361 | my $dtext = "[^\\[\\]\\r\\\\]"; |
| 1362 | my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*"; |
| 1363 | |
| 1364 | my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*"; |
| 1365 | |
| 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)*"; |
| 1371 | |
| 1372 | my $sub_domain = "(?:$atom|$domain_literal)"; |
| 1373 | my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*"; |
| 1374 | |
| 1375 | my $addr_spec = "$localpart\@$rfc822_lwsp*$domain"; |
| 1376 | |
| 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)"; |
| 1381 | |
| 1382 | my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*"; |
| 1383 | my $address = "(?:$mailbox|$group)"; |
| 1384 | |
| 1385 | return "$rfc822_lwsp*$address"; |
| 1386 | } |
| 1387 | |
| 1388 | sub rfc822_strip_comments { |
| 1389 | my $s = shift; |
| 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. |
| 1393 | |
| 1394 | while ($s =~ s/^((?:[^"\\]|\\.)* |
| 1395 | (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*) |
| 1396 | \((?:[^()\\]|\\.)*\)/$1 /osx) {} |
| 1397 | return $s; |
| 1398 | } |
| 1399 | |
| 1400 | # valid: returns true if the parameter is an RFC822 valid address |
| 1401 | # |
Stephen Hemminger | 22dd5b0 | 2010-03-05 13:43:06 -0800 | [diff] [blame] | 1402 | sub rfc822_valid { |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 1403 | my $s = rfc822_strip_comments(shift); |
| 1404 | |
| 1405 | if (!$rfc822re) { |
| 1406 | $rfc822re = make_rfc822re(); |
| 1407 | } |
| 1408 | |
| 1409 | return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/; |
| 1410 | } |
| 1411 | |
| 1412 | # validlist: In scalar context, returns true if the parameter is an RFC822 |
| 1413 | # valid list of addresses. |
| 1414 | # |
| 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 |
| 1420 | # a valid list. |
| 1421 | |
Stephen Hemminger | 22dd5b0 | 2010-03-05 13:43:06 -0800 | [diff] [blame] | 1422 | sub rfc822_validlist { |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 1423 | my $s = rfc822_strip_comments(shift); |
| 1424 | |
| 1425 | if (!$rfc822re) { |
| 1426 | $rfc822re = make_rfc822re(); |
| 1427 | } |
| 1428 | # * null list items are valid according to the RFC |
| 1429 | # * the '1' business is to aid in distinguishing failure from no results |
| 1430 | |
| 1431 | my @r; |
| 1432 | if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so && |
| 1433 | $s =~ m/^$rfc822_char*$/) { |
Joe Perches | 5f2441e | 2009-06-16 15:34:02 -0700 | [diff] [blame] | 1434 | while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) { |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1435 | push(@r, $1); |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 1436 | } |
| 1437 | return wantarray ? (scalar(@r), @r) : 1; |
| 1438 | } |
Joe Perches | 60db31a | 2009-12-14 18:00:50 -0800 | [diff] [blame] | 1439 | return wantarray ? () : 0; |
Joe Perches | 1b5e1cf | 2009-06-16 15:34:01 -0700 | [diff] [blame] | 1440 | } |