blob: 1200d724e73b1e26dfa94fb584d5a6f595c9ba36 [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 Perchescb7301c2009-04-07 20:40:12 -070033my $output_multiline = 1;
34my $output_separator = ", ";
35my $scm = 0;
36my $web = 0;
37my $subsystem = 0;
38my $status = 0;
Joe Perches4a7fdb52009-04-10 12:28:57 -070039my $from_filename = 0;
Joe Perches3fb55652009-09-21 17:04:17 -070040my $pattern_depth = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070041my $version = 0;
42my $help = 0;
43
44my $exit = 0;
45
46my @penguin_chief = ();
47push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
48#Andrew wants in on most everything - 2009/01/14
49#push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
50
51my @penguin_chief_names = ();
52foreach my $chief (@penguin_chief) {
53 if ($chief =~ m/^(.*):(.*)/) {
54 my $chief_name = $1;
55 my $chief_addr = $2;
56 push(@penguin_chief_names, $chief_name);
57 }
58}
59my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
60
Joe Perches5f2441e2009-06-16 15:34:02 -070061# rfc822 email address - preloaded methods go here.
Joe Perches1b5e1cf2009-06-16 15:34:01 -070062my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
Joe Perchesdf4cc032009-06-16 15:34:04 -070063my $rfc822_char = '[\\000-\\377]';
Joe Perches1b5e1cf2009-06-16 15:34:01 -070064
Joe Perchescb7301c2009-04-07 20:40:12 -070065if (!GetOptions(
66 'email!' => \$email,
67 'git!' => \$email_git,
68 'git-chief-penguins!' => \$email_git_penguin_chiefs,
69 'git-min-signatures=i' => \$email_git_min_signatures,
70 'git-max-maintainers=i' => \$email_git_max_maintainers,
Joe Perchesafa81ee2009-07-29 15:04:28 -070071 'git-min-percent=i' => \$email_git_min_percent,
Joe Perchescb7301c2009-04-07 20:40:12 -070072 'git-since=s' => \$email_git_since,
Joe Perchesf5492662009-09-21 17:04:13 -070073 'git-blame!' => \$email_git_blame,
Joe Perchescb7301c2009-04-07 20:40:12 -070074 'm!' => \$email_maintainer,
75 'n!' => \$email_usename,
76 'l!' => \$email_list,
77 's!' => \$email_subscriber_list,
78 'multiline!' => \$output_multiline,
79 'separator=s' => \$output_separator,
80 'subsystem!' => \$subsystem,
81 'status!' => \$status,
82 'scm!' => \$scm,
83 'web!' => \$web,
Joe Perches3fb55652009-09-21 17:04:17 -070084 'pattern-depth=i' => \$pattern_depth,
Joe Perches4a7fdb52009-04-10 12:28:57 -070085 'f|file' => \$from_filename,
Joe Perchescb7301c2009-04-07 20:40:12 -070086 'v|version' => \$version,
87 'h|help' => \$help,
88 )) {
89 usage();
90 die "$P: invalid argument\n";
91}
92
93if ($help != 0) {
94 usage();
95 exit 0;
96}
97
98if ($version != 0) {
99 print("${P} ${V}\n");
100 exit 0;
101}
102
Joe Perchescb7301c2009-04-07 20:40:12 -0700103if ($#ARGV < 0) {
104 usage();
105 die "$P: argument missing: patchfile or -f file please\n";
106}
107
108my $selections = $email + $scm + $status + $subsystem + $web;
109if ($selections == 0) {
110 usage();
111 die "$P: Missing required option: email, scm, status, subsystem or web\n";
112}
113
Joe Perchesf5492662009-09-21 17:04:13 -0700114if ($email &&
115 ($email_maintainer + $email_list + $email_subscriber_list +
116 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700117 usage();
118 die "$P: Please select at least 1 email option\n";
119}
120
121if (!top_of_kernel_tree($lk_path)) {
122 die "$P: The current directory does not appear to be "
123 . "a linux kernel source tree.\n";
124}
125
126## Read MAINTAINERS for type/value pairs
127
128my @typevalue = ();
129open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
130while (<MAINT>) {
131 my $line = $_;
132
133 if ($line =~ m/^(\C):\s*(.*)/) {
134 my $type = $1;
135 my $value = $2;
136
137 ##Filename pattern matching
138 if ($type eq "F" || $type eq "X") {
139 $value =~ s@\.@\\\.@g; ##Convert . to \.
140 $value =~ s/\*/\.\*/g; ##Convert * to .*
141 $value =~ s/\?/\./g; ##Convert ? to .
Joe Perches870020f2009-07-29 15:04:28 -0700142 ##if pattern is a directory and it lacks a trailing slash, add one
143 if ((-d $value)) {
144 $value =~ s@([^/])$@$1/@;
145 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700146 }
147 push(@typevalue, "$type:$value");
148 } elsif (!/^(\s)*$/) {
149 $line =~ s/\n$//g;
150 push(@typevalue, $line);
151 }
152}
153close(MAINT);
154
Joe Perches4a7fdb52009-04-10 12:28:57 -0700155## use the filenames on the command line or find the filenames in the patchfiles
Joe Perchescb7301c2009-04-07 20:40:12 -0700156
157my @files = ();
Joe Perchesf5492662009-09-21 17:04:13 -0700158my @range = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700159
Joe Perches4a7fdb52009-04-10 12:28:57 -0700160foreach my $file (@ARGV) {
Joe Perches870020f2009-07-29 15:04:28 -0700161 ##if $file is a directory and it lacks a trailing slash, add one
162 if ((-d $file)) {
163 $file =~ s@([^/])$@$1/@;
164 } elsif (!(-f $file)) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700165 die "$P: file '${file}' not found\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700166 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700167 if ($from_filename) {
168 push(@files, $file);
169 } else {
170 my $file_cnt = @files;
Joe Perchesf5492662009-09-21 17:04:13 -0700171 my $lastfile;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700172 open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
173 while (<PATCH>) {
174 if (m/^\+\+\+\s+(\S+)/) {
175 my $filename = $1;
176 $filename =~ s@^[^/]*/@@;
177 $filename =~ s@\n@@;
Joe Perchesf5492662009-09-21 17:04:13 -0700178 $lastfile = $filename;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700179 push(@files, $filename);
Joe Perchesf5492662009-09-21 17:04:13 -0700180 } elsif (m/^\@\@ -(\d+),(\d+)/) {
181 if ($email_git_blame) {
182 push(@range, "$lastfile:$1:$2");
183 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700184 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700185 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700186 close(PATCH);
187 if ($file_cnt == @files) {
Joe Perches7f29fd272009-06-16 15:34:04 -0700188 warn "$P: file '${file}' doesn't appear to be a patch. "
Joe Perches4a7fdb52009-04-10 12:28:57 -0700189 . "Add -f to options?\n";
190 }
191 @files = sort_and_uniq(@files);
Joe Perchescb7301c2009-04-07 20:40:12 -0700192 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700193}
194
195my @email_to = ();
Joe Perches290603c2009-06-16 15:33:58 -0700196my @list_to = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700197my @scm = ();
198my @web = ();
199my @subsystem = ();
200my @status = ();
201
202# Find responsible parties
203
204foreach my $file (@files) {
205
206#Do not match excluded file patterns
207
208 my $exclude = 0;
209 foreach my $line (@typevalue) {
Joe Perches290603c2009-06-16 15:33:58 -0700210 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700211 my $type = $1;
212 my $value = $2;
213 if ($type eq 'X') {
214 if (file_match_pattern($file, $value)) {
215 $exclude = 1;
Joe Perches1d606b42009-09-21 17:04:14 -0700216 last;
Joe Perchescb7301c2009-04-07 20:40:12 -0700217 }
218 }
219 }
220 }
221
222 if (!$exclude) {
223 my $tvi = 0;
Joe Perches1d606b42009-09-21 17:04:14 -0700224 my %hash;
Joe Perchescb7301c2009-04-07 20:40:12 -0700225 foreach my $line (@typevalue) {
Joe Perches290603c2009-06-16 15:33:58 -0700226 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700227 my $type = $1;
228 my $value = $2;
229 if ($type eq 'F') {
230 if (file_match_pattern($file, $value)) {
Joe Perches3fb55652009-09-21 17:04:17 -0700231 my $value_pd = ($value =~ tr@/@@);
232 my $file_pd = ($file =~ tr@/@@);
233 $value_pd++ if (substr($value,-1,1) ne "/");
234 if ($pattern_depth == 0 ||
235 (($file_pd - $value_pd) < $pattern_depth)) {
236 $hash{$tvi} = $value_pd;
237 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700238 }
239 }
240 }
241 $tvi++;
242 }
Joe Perches1d606b42009-09-21 17:04:14 -0700243 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
244 add_categories($line);
245 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700246 }
247
Joe Perches4a7fdb52009-04-10 12:28:57 -0700248 if ($email && $email_git) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700249 recent_git_signoffs($file);
250 }
251
Joe Perchesf5492662009-09-21 17:04:13 -0700252 if ($email && $email_git_blame) {
253 git_assign_blame($file);
254 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700255}
256
Joe Perchesf5f50782009-06-16 15:34:00 -0700257if ($email) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700258 foreach my $chief (@penguin_chief) {
259 if ($chief =~ m/^(.*):(.*)/) {
Joe Perchesf5f50782009-06-16 15:34:00 -0700260 my $email_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700261
262 $email_address = format_email($1, $2);
Joe Perchesf5f50782009-06-16 15:34:00 -0700263 if ($email_git_penguin_chiefs) {
264 push(@email_to, $email_address);
265 } else {
266 @email_to = grep(!/${email_address}/, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700267 }
268 }
269 }
270}
271
Joe Perches290603c2009-06-16 15:33:58 -0700272if ($email || $email_list) {
273 my @to = ();
274 if ($email) {
275 @to = (@to, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700276 }
Joe Perches290603c2009-06-16 15:33:58 -0700277 if ($email_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700278 @to = (@to, @list_to);
Joe Perches290603c2009-06-16 15:33:58 -0700279 }
280 output(uniq(@to));
Joe Perchescb7301c2009-04-07 20:40:12 -0700281}
282
283if ($scm) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700284 @scm = sort_and_uniq(@scm);
Joe Perchescb7301c2009-04-07 20:40:12 -0700285 output(@scm);
286}
287
288if ($status) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700289 @status = sort_and_uniq(@status);
Joe Perchescb7301c2009-04-07 20:40:12 -0700290 output(@status);
291}
292
293if ($subsystem) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700294 @subsystem = sort_and_uniq(@subsystem);
Joe Perchescb7301c2009-04-07 20:40:12 -0700295 output(@subsystem);
296}
297
298if ($web) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700299 @web = sort_and_uniq(@web);
Joe Perchescb7301c2009-04-07 20:40:12 -0700300 output(@web);
301}
302
303exit($exit);
304
305sub file_match_pattern {
306 my ($file, $pattern) = @_;
307 if (substr($pattern, -1) eq "/") {
308 if ($file =~ m@^$pattern@) {
309 return 1;
310 }
311 } else {
312 if ($file =~ m@^$pattern@) {
313 my $s1 = ($file =~ tr@/@@);
314 my $s2 = ($pattern =~ tr@/@@);
315 if ($s1 == $s2) {
316 return 1;
317 }
318 }
319 }
320 return 0;
321}
322
323sub usage {
324 print <<EOT;
325usage: $P [options] patchfile
Joe Perches870020f2009-07-29 15:04:28 -0700326 $P [options] -f file|directory
Joe Perchescb7301c2009-04-07 20:40:12 -0700327version: $V
328
329MAINTAINER field selection options:
330 --email => print email address(es) if any
331 --git => include recent git \*-by: signers
332 --git-chief-penguins => include ${penguin_chiefs}
333 --git-min-signatures => number of signatures required (default: 1)
334 --git-max-maintainers => maximum maintainers to add (default: 5)
Joe Perches3d202ae2009-07-29 15:04:29 -0700335 --git-min-percent => minimum percentage of commits required (default: 5)
Joe Perchescb7301c2009-04-07 20:40:12 -0700336 --git-since => git history to use (default: 1-year-ago)
Joe Perchesf5492662009-09-21 17:04:13 -0700337 --git-blame => use git blame to find modified commits for patch or file
Joe Perchescb7301c2009-04-07 20:40:12 -0700338 --m => include maintainer(s) if any
339 --n => include name 'Full Name <addr\@domain.tld>'
340 --l => include list(s) if any
341 --s => include subscriber only list(s) if any
342 --scm => print SCM tree(s) if any
343 --status => print status if any
344 --subsystem => print subsystem name if any
345 --web => print website(s) if any
346
347Output type options:
348 --separator [, ] => separator for multiple entries on 1 line
349 --multiline => print 1 entry per line
350
Joe Perchescb7301c2009-04-07 20:40:12 -0700351Other options:
Joe Perches3fb55652009-09-21 17:04:17 -0700352 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
Joe Perchesf5f50782009-06-16 15:34:00 -0700353 --version => show version
Joe Perchescb7301c2009-04-07 20:40:12 -0700354 --help => show this help information
355
Joe Perches3fb55652009-09-21 17:04:17 -0700356Default options:
357 [--email --git --m --n --l --multiline --pattern-depth=0]
358
Joe Perches870020f2009-07-29 15:04:28 -0700359Notes:
360 Using "-f directory" may give unexpected results:
Joe Perchesf5492662009-09-21 17:04:13 -0700361 Used with "--git", git signators for _all_ files in and below
362 directory are examined as git recurses directories.
363 Any specified X: (exclude) pattern matches are _not_ ignored.
364 Used with "--nogit", directory is used as a pattern match,
365 no individual file within the directory or subdirectory
366 is matched.
367 Used with "--git-blame", does not iterate all files in directory
368 Using "--git-blame" is slow and may add old committers and authors
369 that are no longer active maintainers to the output.
Joe Perchescb7301c2009-04-07 20:40:12 -0700370EOT
371}
372
373sub top_of_kernel_tree {
374 my ($lk_path) = @_;
375
376 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
377 $lk_path .= "/";
378 }
379 if ( (-f "${lk_path}COPYING")
380 && (-f "${lk_path}CREDITS")
381 && (-f "${lk_path}Kbuild")
382 && (-f "${lk_path}MAINTAINERS")
383 && (-f "${lk_path}Makefile")
384 && (-f "${lk_path}README")
385 && (-d "${lk_path}Documentation")
386 && (-d "${lk_path}arch")
387 && (-d "${lk_path}include")
388 && (-d "${lk_path}drivers")
389 && (-d "${lk_path}fs")
390 && (-d "${lk_path}init")
391 && (-d "${lk_path}ipc")
392 && (-d "${lk_path}kernel")
393 && (-d "${lk_path}lib")
394 && (-d "${lk_path}scripts")) {
395 return 1;
396 }
397 return 0;
398}
399
Joe Perches0e70e832009-09-21 17:04:20 -0700400sub parse_email {
401 my ($formatted_email) = @_;
402
403 my $name = "";
404 my $address = "";
405
406 if ($formatted_email =~ /^([^<]+)<(.*\@.*)>$/) {
407 $name = $1;
408 $address = $2;
409 } elsif ($formatted_email =~ /^<(.*\@.*)>$/) {
410 $address = $1;
411 } elsif ($formatted_email =~ /^(.*\@.*)$/) {
412 $address = $1;
413 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700414
415 $name =~ s/^\s+|\s+$//g;
Joe Perchesd7895042009-06-16 15:34:02 -0700416 $name =~ s/^\"|\"$//g;
Joe Perches0e70e832009-09-21 17:04:20 -0700417 $address =~ s/^\s+|\s+$//g;
Joe Perchescb7301c2009-04-07 20:40:12 -0700418
419 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
420 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
Joe Perches0e70e832009-09-21 17:04:20 -0700421 $name = "\"$name\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700422 }
Joe Perches0e70e832009-09-21 17:04:20 -0700423
424 return ($name, $address);
425}
426
427sub format_email {
428 my ($name, $address) = @_;
429
430 my $formatted_email;
431
432 $name =~ s/^\s+|\s+$//g;
433 $name =~ s/^\"|\"$//g;
434 $address =~ s/^\s+|\s+$//g;
435
436 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
437 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
438 $name = "\"$name\"";
439 }
440
441 if ($email_usename) {
442 if ("$name" eq "") {
443 $formatted_email = "$address";
444 } else {
445 $formatted_email = "$name <${address}>";
446 }
447 } else {
448 $formatted_email = $address;
449 }
450
Joe Perchescb7301c2009-04-07 20:40:12 -0700451 return $formatted_email;
452}
453
454sub add_categories {
455 my ($index) = @_;
456
457 $index = $index - 1;
458 while ($index >= 0) {
459 my $tv = $typevalue[$index];
Joe Perches290603c2009-06-16 15:33:58 -0700460 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700461 my $ptype = $1;
462 my $pvalue = $2;
463 if ($ptype eq "L") {
Joe Perches290603c2009-06-16 15:33:58 -0700464 my $list_address = $pvalue;
465 my $list_additional = "";
466 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
467 $list_address = $1;
468 $list_additional = $2;
469 }
Joe Perchesbdf7c682009-06-16 15:33:59 -0700470 if ($list_additional =~ m/subscribers-only/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700471 if ($email_subscriber_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700472 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700473 }
474 } else {
475 if ($email_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700476 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700477 }
478 }
479 } elsif ($ptype eq "M") {
Joe Perches0e70e832009-09-21 17:04:20 -0700480 my ($name, $address) = parse_email($pvalue);
481 if ($name eq "") {
482 if ($index >= 0) {
483 my $tv = $typevalue[$index - 1];
484 if ($tv =~ m/^(\C):\s*(.*)/) {
485 if ($1 eq "P") {
486 $name = $2;
Joe Perches5f2441e2009-06-16 15:34:02 -0700487 }
488 }
489 }
490 }
Joe Perches0e70e832009-09-21 17:04:20 -0700491 if ($email_maintainer) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700492 push_email_addresses($pvalue);
Joe Perchescb7301c2009-04-07 20:40:12 -0700493 }
494 } elsif ($ptype eq "T") {
495 push(@scm, $pvalue);
496 } elsif ($ptype eq "W") {
497 push(@web, $pvalue);
498 } elsif ($ptype eq "S") {
499 push(@status, $pvalue);
500 }
501
502 $index--;
503 } else {
504 push(@subsystem,$tv);
505 $index = -1;
506 }
507 }
508}
509
Joe Perches0e70e832009-09-21 17:04:20 -0700510sub email_address_inuse {
511 my ($test_address) = @_;
512
513 foreach my $line (@email_to) {
514 my ($name, $address) = parse_email($line);
515
516 return 1 if ($address eq $test_address);
517 }
518 return 0;
519}
520
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700521sub push_email_address {
Joe Perches0e70e832009-09-21 17:04:20 -0700522 my ($line) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700523
Joe Perches0e70e832009-09-21 17:04:20 -0700524 my ($name, $address) = parse_email($line);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700525
Joe Perches0e70e832009-09-21 17:04:20 -0700526 if (!email_address_inuse($address)) {
527 push(@email_to, format_email($name, $address));
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700528 }
529}
530
531sub push_email_addresses {
532 my ($address) = @_;
533
534 my @address_list = ();
535
Joe Perches5f2441e2009-06-16 15:34:02 -0700536 if (rfc822_valid($address)) {
537 push_email_address($address);
538 } elsif (@address_list = rfc822_validlist($address)) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700539 my $array_count = shift(@address_list);
540 while (my $entry = shift(@address_list)) {
541 push_email_address($entry);
542 }
Joe Perches5f2441e2009-06-16 15:34:02 -0700543 } else {
544 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700545 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700546}
547
Joe Perchescb7301c2009-04-07 20:40:12 -0700548sub which {
549 my ($bin) = @_;
550
Joe Perchesf5f50782009-06-16 15:34:00 -0700551 foreach my $path (split(/:/, $ENV{PATH})) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700552 if (-e "$path/$bin") {
553 return "$path/$bin";
554 }
555 }
556
557 return "";
558}
559
560sub recent_git_signoffs {
561 my ($file) = @_;
562
563 my $sign_offs = "";
564 my $cmd = "";
565 my $output = "";
566 my $count = 0;
567 my @lines = ();
Joe Perches0e70e832009-09-21 17:04:20 -0700568 my %hash;
Joe Perchesafa81ee2009-07-29 15:04:28 -0700569 my $total_sign_offs;
Joe Perchescb7301c2009-04-07 20:40:12 -0700570
571 if (which("git") eq "") {
Joe Perchesde2fc492009-06-16 15:34:01 -0700572 warn("$P: git not found. Add --nogit to options?\n");
573 return;
574 }
575 if (!(-d ".git")) {
Joe Perches5f2441e2009-06-16 15:34:02 -0700576 warn("$P: .git directory not found. Use a git repository for better results.\n");
577 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 -0700578 return;
Joe Perchescb7301c2009-04-07 20:40:12 -0700579 }
580
581 $cmd = "git log --since=${email_git_since} -- ${file}";
Joe Perchescb7301c2009-04-07 20:40:12 -0700582
583 $output = `${cmd}`;
584 $output =~ s/^\s*//gm;
585
586 @lines = split("\n", $output);
Joe Perchesafa81ee2009-07-29 15:04:28 -0700587
Joe Perches0e70e832009-09-21 17:04:20 -0700588 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
589 if (!$email_git_penguin_chiefs) {
590 @lines = grep(!/${penguin_chiefs}/i, @lines);
591 }
592 # cut -f2- -d":"
593 s/.*:\s*(.+)\s*/$1/ for (@lines);
594
595 @lines = mailmap(@lines);
596
597 $total_sign_offs = @lines;
598 @lines = sort(@lines);
599 # uniq -c
Joe Perchesafa81ee2009-07-29 15:04:28 -0700600 foreach my $line (@lines) {
Joe Perches0e70e832009-09-21 17:04:20 -0700601 $hash{$line}++;
602 }
603 # sort -rn
604 @lines = ();
605 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
606 push(@lines,"$hash{$line} $line");
Joe Perchesafa81ee2009-07-29 15:04:28 -0700607 }
608
Joe Perchescb7301c2009-04-07 20:40:12 -0700609 foreach my $line (@lines) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700610 if ($line =~ m/([0-9]+)\s+(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700611 my $sign_offs = $1;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700612 $line = $2;
Joe Perchescb7301c2009-04-07 20:40:12 -0700613 $count++;
614 if ($sign_offs < $email_git_min_signatures ||
Joe Perchesafa81ee2009-07-29 15:04:28 -0700615 $count > $email_git_max_maintainers ||
616 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700617 last;
618 }
Joe Perches0e70e832009-09-21 17:04:20 -0700619 push_email_address($line);
Joe Perchescb7301c2009-04-07 20:40:12 -0700620 }
Joe Perchesf5492662009-09-21 17:04:13 -0700621 }
622}
623
624sub save_commits {
625 my ($cmd, @commits) = @_;
626 my $output;
627 my @lines = ();
628
629 $output = `${cmd}`;
630
631 @lines = split("\n", $output);
632 foreach my $line (@lines) {
633 if ($line =~ m/^(\w+) /) {
634 push (@commits, $1);
Joe Perchescb7301c2009-04-07 20:40:12 -0700635 }
636 }
Joe Perchesf5492662009-09-21 17:04:13 -0700637 return @commits;
638}
639
640sub git_assign_blame {
641 my ($file) = @_;
642
643 my @lines = ();
644 my @commits = ();
645 my $cmd;
646 my $output;
647 my %hash;
648 my $total_sign_offs;
649 my $count;
650
651 if (@range) {
652 foreach my $file_range_diff (@range) {
653 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
654 my $diff_file = $1;
655 my $diff_start = $2;
656 my $diff_length = $3;
657 next if (!("$file" eq "$diff_file"));
658 $cmd = "git blame -l -L $diff_start,+$diff_length $file\n";
659 @commits = save_commits($cmd, @commits);
660 }
661 } else {
662 if (-f $file) {
663 $cmd = "git blame -l $file\n";
664 @commits = save_commits($cmd, @commits);
665 }
666 }
667
668 $total_sign_offs = 0;
669 @commits = uniq(@commits);
670 foreach my $commit (@commits) {
671 $cmd = "git log -1 ${commit}";
Joe Perchesf5492662009-09-21 17:04:13 -0700672
673 $output = `${cmd}`;
674 $output =~ s/^\s*//gm;
675 @lines = split("\n", $output);
Joe Perches0e70e832009-09-21 17:04:20 -0700676
677 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
678 if (!$email_git_penguin_chiefs) {
679 @lines = grep(!/${penguin_chiefs}/i, @lines);
680 }
681 # cut -f2- -d":"
682 s/.*:\s*(.+)\s*/$1/ for (@lines);
683
Joe Perchesf5492662009-09-21 17:04:13 -0700684 $hash{$_}++ for @lines;
685 $total_sign_offs += @lines;
686 }
687
688 $count = 0;
689 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
690 my $sign_offs = $hash{$line};
691 $count++;
692 last if ($sign_offs < $email_git_min_signatures ||
693 $count > $email_git_max_maintainers ||
694 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
695 push_email_address($line);
696 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700697}
698
699sub uniq {
700 my @parms = @_;
701
702 my %saw;
703 @parms = grep(!$saw{$_}++, @parms);
704 return @parms;
705}
706
707sub sort_and_uniq {
708 my @parms = @_;
709
710 my %saw;
711 @parms = sort @parms;
712 @parms = grep(!$saw{$_}++, @parms);
713 return @parms;
714}
715
716sub output {
717 my @parms = @_;
718
719 if ($output_multiline) {
720 foreach my $line (@parms) {
721 print("${line}\n");
722 }
723 } else {
724 print(join($output_separator, @parms));
725 print("\n");
726 }
727}
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700728
729my $rfc822re;
730
731sub make_rfc822re {
732# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
733# comment. We must allow for rfc822_lwsp (or comments) after each of these.
734# This regexp will only work on addresses which have had comments stripped
735# and replaced with rfc822_lwsp.
736
737 my $specials = '()<>@,;:\\\\".\\[\\]';
738 my $controls = '\\000-\\037\\177';
739
740 my $dtext = "[^\\[\\]\\r\\\\]";
741 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
742
743 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
744
745# Use zero-width assertion to spot the limit of an atom. A simple
746# $rfc822_lwsp* causes the regexp engine to hang occasionally.
747 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
748 my $word = "(?:$atom|$quoted_string)";
749 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
750
751 my $sub_domain = "(?:$atom|$domain_literal)";
752 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
753
754 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
755
756 my $phrase = "$word*";
757 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
758 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
759 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
760
761 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
762 my $address = "(?:$mailbox|$group)";
763
764 return "$rfc822_lwsp*$address";
765}
766
767sub rfc822_strip_comments {
768 my $s = shift;
769# Recursively remove comments, and replace with a single space. The simpler
770# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
771# chars in atoms, for example.
772
773 while ($s =~ s/^((?:[^"\\]|\\.)*
774 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
775 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
776 return $s;
777}
778
779# valid: returns true if the parameter is an RFC822 valid address
780#
781sub rfc822_valid ($) {
782 my $s = rfc822_strip_comments(shift);
783
784 if (!$rfc822re) {
785 $rfc822re = make_rfc822re();
786 }
787
788 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
789}
790
791# validlist: In scalar context, returns true if the parameter is an RFC822
792# valid list of addresses.
793#
794# In list context, returns an empty list on failure (an invalid
795# address was found); otherwise a list whose first element is the
796# number of addresses found and whose remaining elements are the
797# addresses. This is needed to disambiguate failure (invalid)
798# from success with no addresses found, because an empty string is
799# a valid list.
800
801sub rfc822_validlist ($) {
802 my $s = rfc822_strip_comments(shift);
803
804 if (!$rfc822re) {
805 $rfc822re = make_rfc822re();
806 }
807 # * null list items are valid according to the RFC
808 # * the '1' business is to aid in distinguishing failure from no results
809
810 my @r;
811 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
812 $s =~ m/^$rfc822_char*$/) {
Joe Perches5f2441e2009-06-16 15:34:02 -0700813 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700814 push @r, $1;
815 }
816 return wantarray ? (scalar(@r), @r) : 1;
817 }
818 else {
819 return wantarray ? () : 0;
820 }
821}