blob: 473b6741d55f0cf6ce87876eff2d4307fcdff361 [file] [log] [blame]
Joe Perchescb7301c2009-04-07 20:40:12 -07001#!/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#
8# usage: perl scripts/get_maintainers.pl [OPTIONS] <patch>
9# perl scripts/get_maintainers.pl [OPTIONS] -f <file>
10#
11# Licensed under the terms of the GNU GPL License version 2
12
13use strict;
14
15my $P = $0;
Joe Perches0e70e832009-09-21 17:04:20 -070016my $V = '0.20';
Joe Perchescb7301c2009-04-07 20:40:12 -070017
18use Getopt::Long qw(:config no_auto_abbrev);
19
20my $lk_path = "./";
21my $email = 1;
22my $email_usename = 1;
23my $email_maintainer = 1;
24my $email_list = 1;
25my $email_subscriber_list = 0;
26my $email_git = 1;
27my $email_git_penguin_chiefs = 0;
28my $email_git_min_signatures = 1;
29my $email_git_max_maintainers = 5;
Joe Perchesafa81ee2009-07-29 15:04:28 -070030my $email_git_min_percent = 5;
Joe Perchescb7301c2009-04-07 20:40:12 -070031my $email_git_since = "1-year-ago";
Joe Perchesf5492662009-09-21 17:04:13 -070032my $email_git_blame = 0;
Joe Perches11ecf532009-09-21 17:04:22 -070033my $email_remove_duplicates = 1;
Joe Perchescb7301c2009-04-07 20:40:12 -070034my $output_multiline = 1;
35my $output_separator = ", ";
36my $scm = 0;
37my $web = 0;
38my $subsystem = 0;
39my $status = 0;
Joe Perches4a7fdb52009-04-10 12:28:57 -070040my $from_filename = 0;
Joe Perches3fb55652009-09-21 17:04:17 -070041my $pattern_depth = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070042my $version = 0;
43my $help = 0;
44
45my $exit = 0;
46
47my @penguin_chief = ();
48push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
49#Andrew wants in on most everything - 2009/01/14
50#push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
51
52my @penguin_chief_names = ();
53foreach my $chief (@penguin_chief) {
54 if ($chief =~ m/^(.*):(.*)/) {
55 my $chief_name = $1;
56 my $chief_addr = $2;
57 push(@penguin_chief_names, $chief_name);
58 }
59}
60my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
61
Joe Perches5f2441e2009-06-16 15:34:02 -070062# rfc822 email address - preloaded methods go here.
Joe Perches1b5e1cf2009-06-16 15:34:01 -070063my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
Joe Perchesdf4cc032009-06-16 15:34:04 -070064my $rfc822_char = '[\\000-\\377]';
Joe Perches1b5e1cf2009-06-16 15:34:01 -070065
Joe Perchescb7301c2009-04-07 20:40:12 -070066if (!GetOptions(
67 'email!' => \$email,
68 'git!' => \$email_git,
69 'git-chief-penguins!' => \$email_git_penguin_chiefs,
70 'git-min-signatures=i' => \$email_git_min_signatures,
71 'git-max-maintainers=i' => \$email_git_max_maintainers,
Joe Perchesafa81ee2009-07-29 15:04:28 -070072 'git-min-percent=i' => \$email_git_min_percent,
Joe Perchescb7301c2009-04-07 20:40:12 -070073 'git-since=s' => \$email_git_since,
Joe Perchesf5492662009-09-21 17:04:13 -070074 'git-blame!' => \$email_git_blame,
Joe Perches11ecf532009-09-21 17:04:22 -070075 'remove-duplicates!' => \$email_remove_duplicates,
Joe Perchescb7301c2009-04-07 20:40:12 -070076 'm!' => \$email_maintainer,
77 'n!' => \$email_usename,
78 'l!' => \$email_list,
79 's!' => \$email_subscriber_list,
80 'multiline!' => \$output_multiline,
81 'separator=s' => \$output_separator,
82 'subsystem!' => \$subsystem,
83 'status!' => \$status,
84 'scm!' => \$scm,
85 'web!' => \$web,
Joe Perches3fb55652009-09-21 17:04:17 -070086 'pattern-depth=i' => \$pattern_depth,
Joe Perches4a7fdb52009-04-10 12:28:57 -070087 'f|file' => \$from_filename,
Joe Perchescb7301c2009-04-07 20:40:12 -070088 'v|version' => \$version,
89 'h|help' => \$help,
90 )) {
91 usage();
92 die "$P: invalid argument\n";
93}
94
95if ($help != 0) {
96 usage();
97 exit 0;
98}
99
100if ($version != 0) {
101 print("${P} ${V}\n");
102 exit 0;
103}
104
Joe Perchescb7301c2009-04-07 20:40:12 -0700105if ($#ARGV < 0) {
106 usage();
107 die "$P: argument missing: patchfile or -f file please\n";
108}
109
Joe Perches42498312009-09-21 17:04:21 -0700110if ($output_separator ne ", ") {
111 $output_multiline = 0;
112}
113
Joe Perchescb7301c2009-04-07 20:40:12 -0700114my $selections = $email + $scm + $status + $subsystem + $web;
115if ($selections == 0) {
116 usage();
117 die "$P: Missing required option: email, scm, status, subsystem or web\n";
118}
119
Joe Perchesf5492662009-09-21 17:04:13 -0700120if ($email &&
121 ($email_maintainer + $email_list + $email_subscriber_list +
122 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700123 usage();
124 die "$P: Please select at least 1 email option\n";
125}
126
127if (!top_of_kernel_tree($lk_path)) {
128 die "$P: The current directory does not appear to be "
129 . "a linux kernel source tree.\n";
130}
131
132## Read MAINTAINERS for type/value pairs
133
134my @typevalue = ();
135open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
136while (<MAINT>) {
137 my $line = $_;
138
139 if ($line =~ m/^(\C):\s*(.*)/) {
140 my $type = $1;
141 my $value = $2;
142
143 ##Filename pattern matching
144 if ($type eq "F" || $type eq "X") {
145 $value =~ s@\.@\\\.@g; ##Convert . to \.
146 $value =~ s/\*/\.\*/g; ##Convert * to .*
147 $value =~ s/\?/\./g; ##Convert ? to .
Joe Perches870020f2009-07-29 15:04:28 -0700148 ##if pattern is a directory and it lacks a trailing slash, add one
149 if ((-d $value)) {
150 $value =~ s@([^/])$@$1/@;
151 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700152 }
153 push(@typevalue, "$type:$value");
154 } elsif (!/^(\s)*$/) {
155 $line =~ s/\n$//g;
156 push(@typevalue, $line);
157 }
158}
159close(MAINT);
160
Joe Perches8cbb3a72009-09-21 17:04:21 -0700161my %mailmap;
162
Joe Perches11ecf532009-09-21 17:04:22 -0700163if ($email_remove_duplicates) {
164 open(MAILMAP, "<${lk_path}.mailmap") || warn "$P: Can't open .mailmap\n";
165 while (<MAILMAP>) {
166 my $line = $_;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700167
Joe Perches11ecf532009-09-21 17:04:22 -0700168 next if ($line =~ m/^\s*#/);
169 next if ($line =~ m/^\s*$/);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700170
Joe Perches11ecf532009-09-21 17:04:22 -0700171 my ($name, $address) = parse_email($line);
172 $line = format_email($name, $address);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700173
Joe Perches11ecf532009-09-21 17:04:22 -0700174 next if ($line =~ m/^\s*$/);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700175
Joe Perches11ecf532009-09-21 17:04:22 -0700176 if (exists($mailmap{$name})) {
177 my $obj = $mailmap{$name};
178 push(@$obj, $address);
179 } else {
180 my @arr = ($address);
181 $mailmap{$name} = \@arr;
182 }
Joe Perches8cbb3a72009-09-21 17:04:21 -0700183 }
Joe Perches11ecf532009-09-21 17:04:22 -0700184 close(MAILMAP);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700185}
186
Joe Perches4a7fdb52009-04-10 12:28:57 -0700187## use the filenames on the command line or find the filenames in the patchfiles
Joe Perchescb7301c2009-04-07 20:40:12 -0700188
189my @files = ();
Joe Perchesf5492662009-09-21 17:04:13 -0700190my @range = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700191
Joe Perches4a7fdb52009-04-10 12:28:57 -0700192foreach my $file (@ARGV) {
Joe Perches870020f2009-07-29 15:04:28 -0700193 ##if $file is a directory and it lacks a trailing slash, add one
194 if ((-d $file)) {
195 $file =~ s@([^/])$@$1/@;
196 } elsif (!(-f $file)) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700197 die "$P: file '${file}' not found\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700198 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700199 if ($from_filename) {
200 push(@files, $file);
201 } else {
202 my $file_cnt = @files;
Joe Perchesf5492662009-09-21 17:04:13 -0700203 my $lastfile;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700204 open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
205 while (<PATCH>) {
206 if (m/^\+\+\+\s+(\S+)/) {
207 my $filename = $1;
208 $filename =~ s@^[^/]*/@@;
209 $filename =~ s@\n@@;
Joe Perchesf5492662009-09-21 17:04:13 -0700210 $lastfile = $filename;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700211 push(@files, $filename);
Joe Perchesf5492662009-09-21 17:04:13 -0700212 } elsif (m/^\@\@ -(\d+),(\d+)/) {
213 if ($email_git_blame) {
214 push(@range, "$lastfile:$1:$2");
215 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700216 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700217 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700218 close(PATCH);
219 if ($file_cnt == @files) {
Joe Perches7f29fd272009-06-16 15:34:04 -0700220 warn "$P: file '${file}' doesn't appear to be a patch. "
Joe Perches4a7fdb52009-04-10 12:28:57 -0700221 . "Add -f to options?\n";
222 }
223 @files = sort_and_uniq(@files);
Joe Perchescb7301c2009-04-07 20:40:12 -0700224 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700225}
226
227my @email_to = ();
Joe Perches290603c2009-06-16 15:33:58 -0700228my @list_to = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700229my @scm = ();
230my @web = ();
231my @subsystem = ();
232my @status = ();
233
234# Find responsible parties
235
236foreach my $file (@files) {
237
238#Do not match excluded file patterns
239
240 my $exclude = 0;
241 foreach my $line (@typevalue) {
Joe Perches290603c2009-06-16 15:33:58 -0700242 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700243 my $type = $1;
244 my $value = $2;
245 if ($type eq 'X') {
246 if (file_match_pattern($file, $value)) {
247 $exclude = 1;
Joe Perches1d606b42009-09-21 17:04:14 -0700248 last;
Joe Perchescb7301c2009-04-07 20:40:12 -0700249 }
250 }
251 }
252 }
253
254 if (!$exclude) {
255 my $tvi = 0;
Joe Perches1d606b42009-09-21 17:04:14 -0700256 my %hash;
Joe Perchescb7301c2009-04-07 20:40:12 -0700257 foreach my $line (@typevalue) {
Joe Perches290603c2009-06-16 15:33:58 -0700258 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700259 my $type = $1;
260 my $value = $2;
261 if ($type eq 'F') {
262 if (file_match_pattern($file, $value)) {
Joe Perches3fb55652009-09-21 17:04:17 -0700263 my $value_pd = ($value =~ tr@/@@);
264 my $file_pd = ($file =~ tr@/@@);
265 $value_pd++ if (substr($value,-1,1) ne "/");
266 if ($pattern_depth == 0 ||
267 (($file_pd - $value_pd) < $pattern_depth)) {
268 $hash{$tvi} = $value_pd;
269 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700270 }
271 }
272 }
273 $tvi++;
274 }
Joe Perches1d606b42009-09-21 17:04:14 -0700275 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
276 add_categories($line);
277 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700278 }
279
Joe Perches4a7fdb52009-04-10 12:28:57 -0700280 if ($email && $email_git) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700281 recent_git_signoffs($file);
282 }
283
Joe Perchesf5492662009-09-21 17:04:13 -0700284 if ($email && $email_git_blame) {
285 git_assign_blame($file);
286 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700287}
288
Joe Perchesf5f50782009-06-16 15:34:00 -0700289if ($email) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700290 foreach my $chief (@penguin_chief) {
291 if ($chief =~ m/^(.*):(.*)/) {
Joe Perchesf5f50782009-06-16 15:34:00 -0700292 my $email_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700293
294 $email_address = format_email($1, $2);
Joe Perchesf5f50782009-06-16 15:34:00 -0700295 if ($email_git_penguin_chiefs) {
296 push(@email_to, $email_address);
297 } else {
298 @email_to = grep(!/${email_address}/, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700299 }
300 }
301 }
302}
303
Joe Perches290603c2009-06-16 15:33:58 -0700304if ($email || $email_list) {
305 my @to = ();
306 if ($email) {
307 @to = (@to, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700308 }
Joe Perches290603c2009-06-16 15:33:58 -0700309 if ($email_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700310 @to = (@to, @list_to);
Joe Perches290603c2009-06-16 15:33:58 -0700311 }
312 output(uniq(@to));
Joe Perchescb7301c2009-04-07 20:40:12 -0700313}
314
315if ($scm) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700316 @scm = sort_and_uniq(@scm);
Joe Perchescb7301c2009-04-07 20:40:12 -0700317 output(@scm);
318}
319
320if ($status) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700321 @status = sort_and_uniq(@status);
Joe Perchescb7301c2009-04-07 20:40:12 -0700322 output(@status);
323}
324
325if ($subsystem) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700326 @subsystem = sort_and_uniq(@subsystem);
Joe Perchescb7301c2009-04-07 20:40:12 -0700327 output(@subsystem);
328}
329
330if ($web) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700331 @web = sort_and_uniq(@web);
Joe Perchescb7301c2009-04-07 20:40:12 -0700332 output(@web);
333}
334
335exit($exit);
336
337sub file_match_pattern {
338 my ($file, $pattern) = @_;
339 if (substr($pattern, -1) eq "/") {
340 if ($file =~ m@^$pattern@) {
341 return 1;
342 }
343 } else {
344 if ($file =~ m@^$pattern@) {
345 my $s1 = ($file =~ tr@/@@);
346 my $s2 = ($pattern =~ tr@/@@);
347 if ($s1 == $s2) {
348 return 1;
349 }
350 }
351 }
352 return 0;
353}
354
355sub usage {
356 print <<EOT;
357usage: $P [options] patchfile
Joe Perches870020f2009-07-29 15:04:28 -0700358 $P [options] -f file|directory
Joe Perchescb7301c2009-04-07 20:40:12 -0700359version: $V
360
361MAINTAINER field selection options:
362 --email => print email address(es) if any
363 --git => include recent git \*-by: signers
364 --git-chief-penguins => include ${penguin_chiefs}
365 --git-min-signatures => number of signatures required (default: 1)
366 --git-max-maintainers => maximum maintainers to add (default: 5)
Joe Perches3d202ae2009-07-29 15:04:29 -0700367 --git-min-percent => minimum percentage of commits required (default: 5)
Joe Perchescb7301c2009-04-07 20:40:12 -0700368 --git-since => git history to use (default: 1-year-ago)
Joe Perchesf5492662009-09-21 17:04:13 -0700369 --git-blame => use git blame to find modified commits for patch or file
Joe Perchescb7301c2009-04-07 20:40:12 -0700370 --m => include maintainer(s) if any
371 --n => include name 'Full Name <addr\@domain.tld>'
372 --l => include list(s) if any
373 --s => include subscriber only list(s) if any
Joe Perches11ecf532009-09-21 17:04:22 -0700374 --remove-duplicates => minimize duplicate email names/addresses
Joe Perchescb7301c2009-04-07 20:40:12 -0700375 --scm => print SCM tree(s) if any
376 --status => print status if any
377 --subsystem => print subsystem name if any
378 --web => print website(s) if any
379
380Output type options:
381 --separator [, ] => separator for multiple entries on 1 line
Joe Perches42498312009-09-21 17:04:21 -0700382 using --separator also sets --nomultiline if --separator is not [, ]
Joe Perchescb7301c2009-04-07 20:40:12 -0700383 --multiline => print 1 entry per line
384
Joe Perchescb7301c2009-04-07 20:40:12 -0700385Other options:
Joe Perches3fb55652009-09-21 17:04:17 -0700386 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
Joe Perchesf5f50782009-06-16 15:34:00 -0700387 --version => show version
Joe Perchescb7301c2009-04-07 20:40:12 -0700388 --help => show this help information
389
Joe Perches3fb55652009-09-21 17:04:17 -0700390Default options:
Joe Perches11ecf532009-09-21 17:04:22 -0700391 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
Joe Perches3fb55652009-09-21 17:04:17 -0700392
Joe Perches870020f2009-07-29 15:04:28 -0700393Notes:
394 Using "-f directory" may give unexpected results:
Joe Perchesf5492662009-09-21 17:04:13 -0700395 Used with "--git", git signators for _all_ files in and below
396 directory are examined as git recurses directories.
397 Any specified X: (exclude) pattern matches are _not_ ignored.
398 Used with "--nogit", directory is used as a pattern match,
399 no individual file within the directory or subdirectory
400 is matched.
401 Used with "--git-blame", does not iterate all files in directory
402 Using "--git-blame" is slow and may add old committers and authors
403 that are no longer active maintainers to the output.
Joe Perchescb7301c2009-04-07 20:40:12 -0700404EOT
405}
406
407sub top_of_kernel_tree {
408 my ($lk_path) = @_;
409
410 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
411 $lk_path .= "/";
412 }
413 if ( (-f "${lk_path}COPYING")
414 && (-f "${lk_path}CREDITS")
415 && (-f "${lk_path}Kbuild")
416 && (-f "${lk_path}MAINTAINERS")
417 && (-f "${lk_path}Makefile")
418 && (-f "${lk_path}README")
419 && (-d "${lk_path}Documentation")
420 && (-d "${lk_path}arch")
421 && (-d "${lk_path}include")
422 && (-d "${lk_path}drivers")
423 && (-d "${lk_path}fs")
424 && (-d "${lk_path}init")
425 && (-d "${lk_path}ipc")
426 && (-d "${lk_path}kernel")
427 && (-d "${lk_path}lib")
428 && (-d "${lk_path}scripts")) {
429 return 1;
430 }
431 return 0;
432}
433
Joe Perches0e70e832009-09-21 17:04:20 -0700434sub parse_email {
435 my ($formatted_email) = @_;
436
437 my $name = "";
438 my $address = "";
439
Joe Perches11ecf532009-09-21 17:04:22 -0700440 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700441 $name = $1;
442 $address = $2;
Joe Perches11ecf532009-09-21 17:04:22 -0700443 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700444 $address = $1;
Joe Perches11ecf532009-09-21 17:04:22 -0700445 } elsif ($formatted_email =~ /^(.+\@\S*)$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700446 $address = $1;
447 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700448
449 $name =~ s/^\s+|\s+$//g;
Joe Perchesd7895042009-06-16 15:34:02 -0700450 $name =~ s/^\"|\"$//g;
Joe Perches0e70e832009-09-21 17:04:20 -0700451 $address =~ s/^\s+|\s+$//g;
Joe Perchescb7301c2009-04-07 20:40:12 -0700452
453 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
454 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
Joe Perches0e70e832009-09-21 17:04:20 -0700455 $name = "\"$name\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700456 }
Joe Perches0e70e832009-09-21 17:04:20 -0700457
458 return ($name, $address);
459}
460
461sub format_email {
462 my ($name, $address) = @_;
463
464 my $formatted_email;
465
466 $name =~ s/^\s+|\s+$//g;
467 $name =~ s/^\"|\"$//g;
468 $address =~ s/^\s+|\s+$//g;
469
470 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
471 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
472 $name = "\"$name\"";
473 }
474
475 if ($email_usename) {
476 if ("$name" eq "") {
477 $formatted_email = "$address";
478 } else {
479 $formatted_email = "$name <${address}>";
480 }
481 } else {
482 $formatted_email = $address;
483 }
484
Joe Perchescb7301c2009-04-07 20:40:12 -0700485 return $formatted_email;
486}
487
488sub add_categories {
489 my ($index) = @_;
490
491 $index = $index - 1;
492 while ($index >= 0) {
493 my $tv = $typevalue[$index];
Joe Perches290603c2009-06-16 15:33:58 -0700494 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700495 my $ptype = $1;
496 my $pvalue = $2;
497 if ($ptype eq "L") {
Joe Perches290603c2009-06-16 15:33:58 -0700498 my $list_address = $pvalue;
499 my $list_additional = "";
500 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
501 $list_address = $1;
502 $list_additional = $2;
503 }
Joe Perchesbdf7c682009-06-16 15:33:59 -0700504 if ($list_additional =~ m/subscribers-only/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700505 if ($email_subscriber_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700506 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700507 }
508 } else {
509 if ($email_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700510 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700511 }
512 }
513 } elsif ($ptype eq "M") {
Joe Perches0e70e832009-09-21 17:04:20 -0700514 my ($name, $address) = parse_email($pvalue);
515 if ($name eq "") {
516 if ($index >= 0) {
517 my $tv = $typevalue[$index - 1];
518 if ($tv =~ m/^(\C):\s*(.*)/) {
519 if ($1 eq "P") {
520 $name = $2;
Joe Perches5f2441e2009-06-16 15:34:02 -0700521 }
522 }
523 }
524 }
Joe Perches0e70e832009-09-21 17:04:20 -0700525 if ($email_maintainer) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700526 push_email_addresses($pvalue);
Joe Perchescb7301c2009-04-07 20:40:12 -0700527 }
528 } elsif ($ptype eq "T") {
529 push(@scm, $pvalue);
530 } elsif ($ptype eq "W") {
531 push(@web, $pvalue);
532 } elsif ($ptype eq "S") {
533 push(@status, $pvalue);
534 }
535
536 $index--;
537 } else {
538 push(@subsystem,$tv);
539 $index = -1;
540 }
541 }
542}
543
Joe Perches11ecf532009-09-21 17:04:22 -0700544my %email_hash_name;
545my %email_hash_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700546
Joe Perches11ecf532009-09-21 17:04:22 -0700547sub email_inuse {
548 my ($name, $address) = @_;
Joe Perches0e70e832009-09-21 17:04:20 -0700549
Joe Perches11ecf532009-09-21 17:04:22 -0700550 return 1 if (($name eq "") && ($address eq ""));
551 return 1 if (($name ne "") && exists($email_hash_name{$name}));
552 return 1 if (($address ne "") && exists($email_hash_address{$address}));
553
Joe Perches0e70e832009-09-21 17:04:20 -0700554 return 0;
555}
556
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700557sub push_email_address {
Joe Perches0e70e832009-09-21 17:04:20 -0700558 my ($line) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700559
Joe Perches0e70e832009-09-21 17:04:20 -0700560 my ($name, $address) = parse_email($line);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700561
Joe Perches11ecf532009-09-21 17:04:22 -0700562 if (!$email_remove_duplicates) {
Joe Perches0e70e832009-09-21 17:04:20 -0700563 push(@email_to, format_email($name, $address));
Joe Perches11ecf532009-09-21 17:04:22 -0700564 } elsif (!email_inuse($name, $address)) {
565 push(@email_to, format_email($name, $address));
566 $email_hash_name{$name}++;
567 $email_hash_address{$address}++;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700568 }
569}
570
571sub push_email_addresses {
572 my ($address) = @_;
573
574 my @address_list = ();
575
Joe Perches5f2441e2009-06-16 15:34:02 -0700576 if (rfc822_valid($address)) {
577 push_email_address($address);
578 } elsif (@address_list = rfc822_validlist($address)) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700579 my $array_count = shift(@address_list);
580 while (my $entry = shift(@address_list)) {
581 push_email_address($entry);
582 }
Joe Perches5f2441e2009-06-16 15:34:02 -0700583 } else {
584 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700585 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700586}
587
Joe Perchescb7301c2009-04-07 20:40:12 -0700588sub which {
589 my ($bin) = @_;
590
Joe Perchesf5f50782009-06-16 15:34:00 -0700591 foreach my $path (split(/:/, $ENV{PATH})) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700592 if (-e "$path/$bin") {
593 return "$path/$bin";
594 }
595 }
596
597 return "";
598}
599
Joe Perches8cbb3a72009-09-21 17:04:21 -0700600sub mailmap {
601 my @lines = @_;
602 my %hash;
603
604 foreach my $line (@lines) {
605 my ($name, $address) = parse_email($line);
606 if (!exists($hash{$name})) {
607 $hash{$name} = $address;
Joe Perches11ecf532009-09-21 17:04:22 -0700608 } elsif ($address ne $hash{$name}) {
609 $address = $hash{$name};
610 $line = format_email($name, $address);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700611 }
612 if (exists($mailmap{$name})) {
613 my $obj = $mailmap{$name};
614 foreach my $map_address (@$obj) {
615 if (($map_address eq $address) &&
616 ($map_address ne $hash{$name})) {
617 $line = format_email($name, $hash{$name});
618 }
619 }
620 }
621 }
622
623 return @lines;
624}
625
Joe Perchescb7301c2009-04-07 20:40:12 -0700626sub recent_git_signoffs {
627 my ($file) = @_;
628
629 my $sign_offs = "";
630 my $cmd = "";
631 my $output = "";
632 my $count = 0;
633 my @lines = ();
Joe Perches0e70e832009-09-21 17:04:20 -0700634 my %hash;
Joe Perchesafa81ee2009-07-29 15:04:28 -0700635 my $total_sign_offs;
Joe Perchescb7301c2009-04-07 20:40:12 -0700636
637 if (which("git") eq "") {
Joe Perchesde2fc492009-06-16 15:34:01 -0700638 warn("$P: git not found. Add --nogit to options?\n");
639 return;
640 }
641 if (!(-d ".git")) {
Joe Perches5f2441e2009-06-16 15:34:02 -0700642 warn("$P: .git directory not found. Use a git repository for better results.\n");
643 warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
Joe Perchesde2fc492009-06-16 15:34:01 -0700644 return;
Joe Perchescb7301c2009-04-07 20:40:12 -0700645 }
646
647 $cmd = "git log --since=${email_git_since} -- ${file}";
Joe Perchescb7301c2009-04-07 20:40:12 -0700648
649 $output = `${cmd}`;
650 $output =~ s/^\s*//gm;
651
652 @lines = split("\n", $output);
Joe Perchesafa81ee2009-07-29 15:04:28 -0700653
Joe Perches0e70e832009-09-21 17:04:20 -0700654 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
655 if (!$email_git_penguin_chiefs) {
656 @lines = grep(!/${penguin_chiefs}/i, @lines);
657 }
658 # cut -f2- -d":"
659 s/.*:\s*(.+)\s*/$1/ for (@lines);
660
Joe Perches8cbb3a72009-09-21 17:04:21 -0700661 $total_sign_offs = @lines;
662
Joe Perches11ecf532009-09-21 17:04:22 -0700663 if ($email_remove_duplicates) {
664 @lines = mailmap(@lines);
665 }
Joe Perches0e70e832009-09-21 17:04:20 -0700666
Joe Perches0e70e832009-09-21 17:04:20 -0700667 @lines = sort(@lines);
Joe Perchesafa81ee2009-07-29 15:04:28 -0700668
Joe Perches11ecf532009-09-21 17:04:22 -0700669 # uniq -c
670 $hash{$_}++ for @lines;
671
672 # sort -rn
673 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
674 my $sign_offs = $hash{$line};
675 $count++;
676 last if ($sign_offs < $email_git_min_signatures ||
677 $count > $email_git_max_maintainers ||
678 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
679 push_email_address($line);
Joe Perchesf5492662009-09-21 17:04:13 -0700680 }
681}
682
683sub save_commits {
684 my ($cmd, @commits) = @_;
685 my $output;
686 my @lines = ();
687
688 $output = `${cmd}`;
689
690 @lines = split("\n", $output);
691 foreach my $line (@lines) {
692 if ($line =~ m/^(\w+) /) {
693 push (@commits, $1);
Joe Perchescb7301c2009-04-07 20:40:12 -0700694 }
695 }
Joe Perchesf5492662009-09-21 17:04:13 -0700696 return @commits;
697}
698
699sub git_assign_blame {
700 my ($file) = @_;
701
702 my @lines = ();
703 my @commits = ();
704 my $cmd;
705 my $output;
706 my %hash;
707 my $total_sign_offs;
708 my $count;
709
710 if (@range) {
711 foreach my $file_range_diff (@range) {
712 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
713 my $diff_file = $1;
714 my $diff_start = $2;
715 my $diff_length = $3;
716 next if (!("$file" eq "$diff_file"));
Joe Perches8cbb3a72009-09-21 17:04:21 -0700717 $cmd = "git blame -l -L $diff_start,+$diff_length $file";
Joe Perchesf5492662009-09-21 17:04:13 -0700718 @commits = save_commits($cmd, @commits);
719 }
720 } else {
721 if (-f $file) {
Joe Perches8cbb3a72009-09-21 17:04:21 -0700722 $cmd = "git blame -l $file";
Joe Perchesf5492662009-09-21 17:04:13 -0700723 @commits = save_commits($cmd, @commits);
724 }
725 }
726
727 $total_sign_offs = 0;
728 @commits = uniq(@commits);
729 foreach my $commit (@commits) {
730 $cmd = "git log -1 ${commit}";
Joe Perchesf5492662009-09-21 17:04:13 -0700731
732 $output = `${cmd}`;
733 $output =~ s/^\s*//gm;
734 @lines = split("\n", $output);
Joe Perches0e70e832009-09-21 17:04:20 -0700735
736 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
737 if (!$email_git_penguin_chiefs) {
738 @lines = grep(!/${penguin_chiefs}/i, @lines);
739 }
Joe Perches8cbb3a72009-09-21 17:04:21 -0700740
Joe Perches0e70e832009-09-21 17:04:20 -0700741 # cut -f2- -d":"
742 s/.*:\s*(.+)\s*/$1/ for (@lines);
743
Joe Perchesf5492662009-09-21 17:04:13 -0700744 $total_sign_offs += @lines;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700745
Joe Perches11ecf532009-09-21 17:04:22 -0700746 if ($email_remove_duplicates) {
747 @lines = mailmap(@lines);
748 }
Joe Perches8cbb3a72009-09-21 17:04:21 -0700749
750 $hash{$_}++ for @lines;
Joe Perchesf5492662009-09-21 17:04:13 -0700751 }
752
753 $count = 0;
754 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
755 my $sign_offs = $hash{$line};
756 $count++;
757 last if ($sign_offs < $email_git_min_signatures ||
758 $count > $email_git_max_maintainers ||
759 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
760 push_email_address($line);
761 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700762}
763
764sub uniq {
765 my @parms = @_;
766
767 my %saw;
768 @parms = grep(!$saw{$_}++, @parms);
769 return @parms;
770}
771
772sub sort_and_uniq {
773 my @parms = @_;
774
775 my %saw;
776 @parms = sort @parms;
777 @parms = grep(!$saw{$_}++, @parms);
778 return @parms;
779}
780
781sub output {
782 my @parms = @_;
783
784 if ($output_multiline) {
785 foreach my $line (@parms) {
786 print("${line}\n");
787 }
788 } else {
789 print(join($output_separator, @parms));
790 print("\n");
791 }
792}
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700793
794my $rfc822re;
795
796sub make_rfc822re {
797# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
798# comment. We must allow for rfc822_lwsp (or comments) after each of these.
799# This regexp will only work on addresses which have had comments stripped
800# and replaced with rfc822_lwsp.
801
802 my $specials = '()<>@,;:\\\\".\\[\\]';
803 my $controls = '\\000-\\037\\177';
804
805 my $dtext = "[^\\[\\]\\r\\\\]";
806 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
807
808 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
809
810# Use zero-width assertion to spot the limit of an atom. A simple
811# $rfc822_lwsp* causes the regexp engine to hang occasionally.
812 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
813 my $word = "(?:$atom|$quoted_string)";
814 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
815
816 my $sub_domain = "(?:$atom|$domain_literal)";
817 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
818
819 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
820
821 my $phrase = "$word*";
822 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
823 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
824 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
825
826 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
827 my $address = "(?:$mailbox|$group)";
828
829 return "$rfc822_lwsp*$address";
830}
831
832sub rfc822_strip_comments {
833 my $s = shift;
834# Recursively remove comments, and replace with a single space. The simpler
835# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
836# chars in atoms, for example.
837
838 while ($s =~ s/^((?:[^"\\]|\\.)*
839 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
840 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
841 return $s;
842}
843
844# valid: returns true if the parameter is an RFC822 valid address
845#
846sub rfc822_valid ($) {
847 my $s = rfc822_strip_comments(shift);
848
849 if (!$rfc822re) {
850 $rfc822re = make_rfc822re();
851 }
852
853 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
854}
855
856# validlist: In scalar context, returns true if the parameter is an RFC822
857# valid list of addresses.
858#
859# In list context, returns an empty list on failure (an invalid
860# address was found); otherwise a list whose first element is the
861# number of addresses found and whose remaining elements are the
862# addresses. This is needed to disambiguate failure (invalid)
863# from success with no addresses found, because an empty string is
864# a valid list.
865
866sub rfc822_validlist ($) {
867 my $s = rfc822_strip_comments(shift);
868
869 if (!$rfc822re) {
870 $rfc822re = make_rfc822re();
871 }
872 # * null list items are valid according to the RFC
873 # * the '1' business is to aid in distinguishing failure from no results
874
875 my @r;
876 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
877 $s =~ m/^$rfc822_char*$/) {
Joe Perches5f2441e2009-06-16 15:34:02 -0700878 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700879 push @r, $1;
880 }
881 return wantarray ? (scalar(@r), @r) : 1;
882 }
883 else {
884 return wantarray ? () : 0;
885 }
886}