blob: 446803efe620c214a3da4641cd13f749318f6d00 [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
Joe Perches42498312009-09-21 17:04:21 -0700108if ($output_separator ne ", ") {
109 $output_multiline = 0;
110}
111
Joe Perchescb7301c2009-04-07 20:40:12 -0700112my $selections = $email + $scm + $status + $subsystem + $web;
113if ($selections == 0) {
114 usage();
115 die "$P: Missing required option: email, scm, status, subsystem or web\n";
116}
117
Joe Perchesf5492662009-09-21 17:04:13 -0700118if ($email &&
119 ($email_maintainer + $email_list + $email_subscriber_list +
120 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700121 usage();
122 die "$P: Please select at least 1 email option\n";
123}
124
125if (!top_of_kernel_tree($lk_path)) {
126 die "$P: The current directory does not appear to be "
127 . "a linux kernel source tree.\n";
128}
129
130## Read MAINTAINERS for type/value pairs
131
132my @typevalue = ();
133open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
134while (<MAINT>) {
135 my $line = $_;
136
137 if ($line =~ m/^(\C):\s*(.*)/) {
138 my $type = $1;
139 my $value = $2;
140
141 ##Filename pattern matching
142 if ($type eq "F" || $type eq "X") {
143 $value =~ s@\.@\\\.@g; ##Convert . to \.
144 $value =~ s/\*/\.\*/g; ##Convert * to .*
145 $value =~ s/\?/\./g; ##Convert ? to .
Joe Perches870020f2009-07-29 15:04:28 -0700146 ##if pattern is a directory and it lacks a trailing slash, add one
147 if ((-d $value)) {
148 $value =~ s@([^/])$@$1/@;
149 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700150 }
151 push(@typevalue, "$type:$value");
152 } elsif (!/^(\s)*$/) {
153 $line =~ s/\n$//g;
154 push(@typevalue, $line);
155 }
156}
157close(MAINT);
158
Joe Perches8cbb3a72009-09-21 17:04:21 -0700159my %mailmap;
160
161open(MAILMAP, "<${lk_path}.mailmap") || warn "$P: Can't open .mailmap\n";
162while (<MAILMAP>) {
163 my $line = $_;
164
165 next if ($line =~ m/^\s*#/);
166 next if ($line =~ m/^\s*$/);
167
168 my ($name, $address) = parse_email($line);
169 $line = format_email($name, $address);
170
171 next if ($line =~ m/^\s*$/);
172
173 if (exists($mailmap{$name})) {
174 my $obj = $mailmap{$name};
175 push(@$obj, $address);
176 } else {
177 my @arr = ($address);
178 $mailmap{$name} = \@arr;
179 }
180}
181close(MAILMAP);
182
183foreach my $name (sort {$mailmap{$a} <=> $mailmap{$b}} keys %mailmap) {
184 my $obj = $mailmap{$name};
185 foreach my $address (@$obj) {
186 }
187}
188
Joe Perches4a7fdb52009-04-10 12:28:57 -0700189## use the filenames on the command line or find the filenames in the patchfiles
Joe Perchescb7301c2009-04-07 20:40:12 -0700190
191my @files = ();
Joe Perchesf5492662009-09-21 17:04:13 -0700192my @range = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700193
Joe Perches4a7fdb52009-04-10 12:28:57 -0700194foreach my $file (@ARGV) {
Joe Perches870020f2009-07-29 15:04:28 -0700195 ##if $file is a directory and it lacks a trailing slash, add one
196 if ((-d $file)) {
197 $file =~ s@([^/])$@$1/@;
198 } elsif (!(-f $file)) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700199 die "$P: file '${file}' not found\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700200 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700201 if ($from_filename) {
202 push(@files, $file);
203 } else {
204 my $file_cnt = @files;
Joe Perchesf5492662009-09-21 17:04:13 -0700205 my $lastfile;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700206 open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
207 while (<PATCH>) {
208 if (m/^\+\+\+\s+(\S+)/) {
209 my $filename = $1;
210 $filename =~ s@^[^/]*/@@;
211 $filename =~ s@\n@@;
Joe Perchesf5492662009-09-21 17:04:13 -0700212 $lastfile = $filename;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700213 push(@files, $filename);
Joe Perchesf5492662009-09-21 17:04:13 -0700214 } elsif (m/^\@\@ -(\d+),(\d+)/) {
215 if ($email_git_blame) {
216 push(@range, "$lastfile:$1:$2");
217 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700218 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700219 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700220 close(PATCH);
221 if ($file_cnt == @files) {
Joe Perches7f29fd272009-06-16 15:34:04 -0700222 warn "$P: file '${file}' doesn't appear to be a patch. "
Joe Perches4a7fdb52009-04-10 12:28:57 -0700223 . "Add -f to options?\n";
224 }
225 @files = sort_and_uniq(@files);
Joe Perchescb7301c2009-04-07 20:40:12 -0700226 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700227}
228
229my @email_to = ();
Joe Perches290603c2009-06-16 15:33:58 -0700230my @list_to = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700231my @scm = ();
232my @web = ();
233my @subsystem = ();
234my @status = ();
235
236# Find responsible parties
237
238foreach my $file (@files) {
239
240#Do not match excluded file patterns
241
242 my $exclude = 0;
243 foreach my $line (@typevalue) {
Joe Perches290603c2009-06-16 15:33:58 -0700244 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700245 my $type = $1;
246 my $value = $2;
247 if ($type eq 'X') {
248 if (file_match_pattern($file, $value)) {
249 $exclude = 1;
Joe Perches1d606b42009-09-21 17:04:14 -0700250 last;
Joe Perchescb7301c2009-04-07 20:40:12 -0700251 }
252 }
253 }
254 }
255
256 if (!$exclude) {
257 my $tvi = 0;
Joe Perches1d606b42009-09-21 17:04:14 -0700258 my %hash;
Joe Perchescb7301c2009-04-07 20:40:12 -0700259 foreach my $line (@typevalue) {
Joe Perches290603c2009-06-16 15:33:58 -0700260 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700261 my $type = $1;
262 my $value = $2;
263 if ($type eq 'F') {
264 if (file_match_pattern($file, $value)) {
Joe Perches3fb55652009-09-21 17:04:17 -0700265 my $value_pd = ($value =~ tr@/@@);
266 my $file_pd = ($file =~ tr@/@@);
267 $value_pd++ if (substr($value,-1,1) ne "/");
268 if ($pattern_depth == 0 ||
269 (($file_pd - $value_pd) < $pattern_depth)) {
270 $hash{$tvi} = $value_pd;
271 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700272 }
273 }
274 }
275 $tvi++;
276 }
Joe Perches1d606b42009-09-21 17:04:14 -0700277 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
278 add_categories($line);
279 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700280 }
281
Joe Perches4a7fdb52009-04-10 12:28:57 -0700282 if ($email && $email_git) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700283 recent_git_signoffs($file);
284 }
285
Joe Perchesf5492662009-09-21 17:04:13 -0700286 if ($email && $email_git_blame) {
287 git_assign_blame($file);
288 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700289}
290
Joe Perchesf5f50782009-06-16 15:34:00 -0700291if ($email) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700292 foreach my $chief (@penguin_chief) {
293 if ($chief =~ m/^(.*):(.*)/) {
Joe Perchesf5f50782009-06-16 15:34:00 -0700294 my $email_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700295
296 $email_address = format_email($1, $2);
Joe Perchesf5f50782009-06-16 15:34:00 -0700297 if ($email_git_penguin_chiefs) {
298 push(@email_to, $email_address);
299 } else {
300 @email_to = grep(!/${email_address}/, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700301 }
302 }
303 }
304}
305
Joe Perches290603c2009-06-16 15:33:58 -0700306if ($email || $email_list) {
307 my @to = ();
308 if ($email) {
309 @to = (@to, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700310 }
Joe Perches290603c2009-06-16 15:33:58 -0700311 if ($email_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700312 @to = (@to, @list_to);
Joe Perches290603c2009-06-16 15:33:58 -0700313 }
314 output(uniq(@to));
Joe Perchescb7301c2009-04-07 20:40:12 -0700315}
316
317if ($scm) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700318 @scm = sort_and_uniq(@scm);
Joe Perchescb7301c2009-04-07 20:40:12 -0700319 output(@scm);
320}
321
322if ($status) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700323 @status = sort_and_uniq(@status);
Joe Perchescb7301c2009-04-07 20:40:12 -0700324 output(@status);
325}
326
327if ($subsystem) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700328 @subsystem = sort_and_uniq(@subsystem);
Joe Perchescb7301c2009-04-07 20:40:12 -0700329 output(@subsystem);
330}
331
332if ($web) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700333 @web = sort_and_uniq(@web);
Joe Perchescb7301c2009-04-07 20:40:12 -0700334 output(@web);
335}
336
337exit($exit);
338
339sub file_match_pattern {
340 my ($file, $pattern) = @_;
341 if (substr($pattern, -1) eq "/") {
342 if ($file =~ m@^$pattern@) {
343 return 1;
344 }
345 } else {
346 if ($file =~ m@^$pattern@) {
347 my $s1 = ($file =~ tr@/@@);
348 my $s2 = ($pattern =~ tr@/@@);
349 if ($s1 == $s2) {
350 return 1;
351 }
352 }
353 }
354 return 0;
355}
356
357sub usage {
358 print <<EOT;
359usage: $P [options] patchfile
Joe Perches870020f2009-07-29 15:04:28 -0700360 $P [options] -f file|directory
Joe Perchescb7301c2009-04-07 20:40:12 -0700361version: $V
362
363MAINTAINER field selection options:
364 --email => print email address(es) if any
365 --git => include recent git \*-by: signers
366 --git-chief-penguins => include ${penguin_chiefs}
367 --git-min-signatures => number of signatures required (default: 1)
368 --git-max-maintainers => maximum maintainers to add (default: 5)
Joe Perches3d202ae2009-07-29 15:04:29 -0700369 --git-min-percent => minimum percentage of commits required (default: 5)
Joe Perchescb7301c2009-04-07 20:40:12 -0700370 --git-since => git history to use (default: 1-year-ago)
Joe Perchesf5492662009-09-21 17:04:13 -0700371 --git-blame => use git blame to find modified commits for patch or file
Joe Perchescb7301c2009-04-07 20:40:12 -0700372 --m => include maintainer(s) if any
373 --n => include name 'Full Name <addr\@domain.tld>'
374 --l => include list(s) if any
375 --s => include subscriber only list(s) if any
376 --scm => print SCM tree(s) if any
377 --status => print status if any
378 --subsystem => print subsystem name if any
379 --web => print website(s) if any
380
381Output type options:
382 --separator [, ] => separator for multiple entries on 1 line
Joe Perches42498312009-09-21 17:04:21 -0700383 using --separator also sets --nomultiline if --separator is not [, ]
Joe Perchescb7301c2009-04-07 20:40:12 -0700384 --multiline => print 1 entry per line
385
Joe Perchescb7301c2009-04-07 20:40:12 -0700386Other options:
Joe Perches3fb55652009-09-21 17:04:17 -0700387 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
Joe Perchesf5f50782009-06-16 15:34:00 -0700388 --version => show version
Joe Perchescb7301c2009-04-07 20:40:12 -0700389 --help => show this help information
390
Joe Perches3fb55652009-09-21 17:04:17 -0700391Default options:
392 [--email --git --m --n --l --multiline --pattern-depth=0]
393
Joe Perches870020f2009-07-29 15:04:28 -0700394Notes:
395 Using "-f directory" may give unexpected results:
Joe Perchesf5492662009-09-21 17:04:13 -0700396 Used with "--git", git signators for _all_ files in and below
397 directory are examined as git recurses directories.
398 Any specified X: (exclude) pattern matches are _not_ ignored.
399 Used with "--nogit", directory is used as a pattern match,
400 no individual file within the directory or subdirectory
401 is matched.
402 Used with "--git-blame", does not iterate all files in directory
403 Using "--git-blame" is slow and may add old committers and authors
404 that are no longer active maintainers to the output.
Joe Perchescb7301c2009-04-07 20:40:12 -0700405EOT
406}
407
408sub top_of_kernel_tree {
409 my ($lk_path) = @_;
410
411 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
412 $lk_path .= "/";
413 }
414 if ( (-f "${lk_path}COPYING")
415 && (-f "${lk_path}CREDITS")
416 && (-f "${lk_path}Kbuild")
417 && (-f "${lk_path}MAINTAINERS")
418 && (-f "${lk_path}Makefile")
419 && (-f "${lk_path}README")
420 && (-d "${lk_path}Documentation")
421 && (-d "${lk_path}arch")
422 && (-d "${lk_path}include")
423 && (-d "${lk_path}drivers")
424 && (-d "${lk_path}fs")
425 && (-d "${lk_path}init")
426 && (-d "${lk_path}ipc")
427 && (-d "${lk_path}kernel")
428 && (-d "${lk_path}lib")
429 && (-d "${lk_path}scripts")) {
430 return 1;
431 }
432 return 0;
433}
434
Joe Perches0e70e832009-09-21 17:04:20 -0700435sub parse_email {
436 my ($formatted_email) = @_;
437
438 my $name = "";
439 my $address = "";
440
Joe Perches8cbb3a72009-09-21 17:04:21 -0700441 if ($formatted_email =~ /^([^<]+)<(.*\@.*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700442 $name = $1;
443 $address = $2;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700444 } elsif ($formatted_email =~ /^\s*<(.*\@.*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700445 $address = $1;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700446 } elsif ($formatted_email =~ /^\s*(.*\@.*)$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700447 $address = $1;
448 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700449
450 $name =~ s/^\s+|\s+$//g;
Joe Perchesd7895042009-06-16 15:34:02 -0700451 $name =~ s/^\"|\"$//g;
Joe Perches0e70e832009-09-21 17:04:20 -0700452 $address =~ s/^\s+|\s+$//g;
Joe Perchescb7301c2009-04-07 20:40:12 -0700453
454 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
455 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
Joe Perches0e70e832009-09-21 17:04:20 -0700456 $name = "\"$name\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700457 }
Joe Perches0e70e832009-09-21 17:04:20 -0700458
459 return ($name, $address);
460}
461
462sub format_email {
463 my ($name, $address) = @_;
464
465 my $formatted_email;
466
467 $name =~ s/^\s+|\s+$//g;
468 $name =~ s/^\"|\"$//g;
469 $address =~ s/^\s+|\s+$//g;
470
471 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
472 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
473 $name = "\"$name\"";
474 }
475
476 if ($email_usename) {
477 if ("$name" eq "") {
478 $formatted_email = "$address";
479 } else {
480 $formatted_email = "$name <${address}>";
481 }
482 } else {
483 $formatted_email = $address;
484 }
485
Joe Perchescb7301c2009-04-07 20:40:12 -0700486 return $formatted_email;
487}
488
489sub add_categories {
490 my ($index) = @_;
491
492 $index = $index - 1;
493 while ($index >= 0) {
494 my $tv = $typevalue[$index];
Joe Perches290603c2009-06-16 15:33:58 -0700495 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700496 my $ptype = $1;
497 my $pvalue = $2;
498 if ($ptype eq "L") {
Joe Perches290603c2009-06-16 15:33:58 -0700499 my $list_address = $pvalue;
500 my $list_additional = "";
501 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
502 $list_address = $1;
503 $list_additional = $2;
504 }
Joe Perchesbdf7c682009-06-16 15:33:59 -0700505 if ($list_additional =~ m/subscribers-only/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700506 if ($email_subscriber_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700507 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700508 }
509 } else {
510 if ($email_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700511 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700512 }
513 }
514 } elsif ($ptype eq "M") {
Joe Perches0e70e832009-09-21 17:04:20 -0700515 my ($name, $address) = parse_email($pvalue);
516 if ($name eq "") {
517 if ($index >= 0) {
518 my $tv = $typevalue[$index - 1];
519 if ($tv =~ m/^(\C):\s*(.*)/) {
520 if ($1 eq "P") {
521 $name = $2;
Joe Perches5f2441e2009-06-16 15:34:02 -0700522 }
523 }
524 }
525 }
Joe Perches0e70e832009-09-21 17:04:20 -0700526 if ($email_maintainer) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700527 push_email_addresses($pvalue);
Joe Perchescb7301c2009-04-07 20:40:12 -0700528 }
529 } elsif ($ptype eq "T") {
530 push(@scm, $pvalue);
531 } elsif ($ptype eq "W") {
532 push(@web, $pvalue);
533 } elsif ($ptype eq "S") {
534 push(@status, $pvalue);
535 }
536
537 $index--;
538 } else {
539 push(@subsystem,$tv);
540 $index = -1;
541 }
542 }
543}
544
Joe Perches0e70e832009-09-21 17:04:20 -0700545sub email_address_inuse {
546 my ($test_address) = @_;
547
548 foreach my $line (@email_to) {
549 my ($name, $address) = parse_email($line);
550
551 return 1 if ($address eq $test_address);
552 }
553 return 0;
554}
555
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700556sub push_email_address {
Joe Perches0e70e832009-09-21 17:04:20 -0700557 my ($line) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700558
Joe Perches0e70e832009-09-21 17:04:20 -0700559 my ($name, $address) = parse_email($line);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700560
Joe Perches0e70e832009-09-21 17:04:20 -0700561 if (!email_address_inuse($address)) {
562 push(@email_to, format_email($name, $address));
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700563 }
564}
565
566sub push_email_addresses {
567 my ($address) = @_;
568
569 my @address_list = ();
570
Joe Perches5f2441e2009-06-16 15:34:02 -0700571 if (rfc822_valid($address)) {
572 push_email_address($address);
573 } elsif (@address_list = rfc822_validlist($address)) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700574 my $array_count = shift(@address_list);
575 while (my $entry = shift(@address_list)) {
576 push_email_address($entry);
577 }
Joe Perches5f2441e2009-06-16 15:34:02 -0700578 } else {
579 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700580 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700581}
582
Joe Perchescb7301c2009-04-07 20:40:12 -0700583sub which {
584 my ($bin) = @_;
585
Joe Perchesf5f50782009-06-16 15:34:00 -0700586 foreach my $path (split(/:/, $ENV{PATH})) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700587 if (-e "$path/$bin") {
588 return "$path/$bin";
589 }
590 }
591
592 return "";
593}
594
Joe Perches8cbb3a72009-09-21 17:04:21 -0700595sub mailmap {
596 my @lines = @_;
597 my %hash;
598
599 foreach my $line (@lines) {
600 my ($name, $address) = parse_email($line);
601 if (!exists($hash{$name})) {
602 $hash{$name} = $address;
603 }
604 if (exists($mailmap{$name})) {
605 my $obj = $mailmap{$name};
606 foreach my $map_address (@$obj) {
607 if (($map_address eq $address) &&
608 ($map_address ne $hash{$name})) {
609 $line = format_email($name, $hash{$name});
610 }
611 }
612 }
613 }
614
615 return @lines;
616}
617
Joe Perchescb7301c2009-04-07 20:40:12 -0700618sub recent_git_signoffs {
619 my ($file) = @_;
620
621 my $sign_offs = "";
622 my $cmd = "";
623 my $output = "";
624 my $count = 0;
625 my @lines = ();
Joe Perches0e70e832009-09-21 17:04:20 -0700626 my %hash;
Joe Perchesafa81ee2009-07-29 15:04:28 -0700627 my $total_sign_offs;
Joe Perchescb7301c2009-04-07 20:40:12 -0700628
629 if (which("git") eq "") {
Joe Perchesde2fc492009-06-16 15:34:01 -0700630 warn("$P: git not found. Add --nogit to options?\n");
631 return;
632 }
633 if (!(-d ".git")) {
Joe Perches5f2441e2009-06-16 15:34:02 -0700634 warn("$P: .git directory not found. Use a git repository for better results.\n");
635 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 -0700636 return;
Joe Perchescb7301c2009-04-07 20:40:12 -0700637 }
638
639 $cmd = "git log --since=${email_git_since} -- ${file}";
Joe Perchescb7301c2009-04-07 20:40:12 -0700640
641 $output = `${cmd}`;
642 $output =~ s/^\s*//gm;
643
644 @lines = split("\n", $output);
Joe Perchesafa81ee2009-07-29 15:04:28 -0700645
Joe Perches0e70e832009-09-21 17:04:20 -0700646 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
647 if (!$email_git_penguin_chiefs) {
648 @lines = grep(!/${penguin_chiefs}/i, @lines);
649 }
650 # cut -f2- -d":"
651 s/.*:\s*(.+)\s*/$1/ for (@lines);
652
Joe Perches8cbb3a72009-09-21 17:04:21 -0700653 $total_sign_offs = @lines;
654
Joe Perches0e70e832009-09-21 17:04:20 -0700655 @lines = mailmap(@lines);
656
Joe Perches0e70e832009-09-21 17:04:20 -0700657 @lines = sort(@lines);
658 # uniq -c
Joe Perchesafa81ee2009-07-29 15:04:28 -0700659 foreach my $line (@lines) {
Joe Perches0e70e832009-09-21 17:04:20 -0700660 $hash{$line}++;
661 }
662 # sort -rn
663 @lines = ();
664 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
665 push(@lines,"$hash{$line} $line");
Joe Perchesafa81ee2009-07-29 15:04:28 -0700666 }
667
Joe Perchescb7301c2009-04-07 20:40:12 -0700668 foreach my $line (@lines) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700669 if ($line =~ m/([0-9]+)\s+(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700670 my $sign_offs = $1;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700671 $line = $2;
Joe Perchescb7301c2009-04-07 20:40:12 -0700672 $count++;
673 if ($sign_offs < $email_git_min_signatures ||
Joe Perchesafa81ee2009-07-29 15:04:28 -0700674 $count > $email_git_max_maintainers ||
675 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700676 last;
677 }
Joe Perches0e70e832009-09-21 17:04:20 -0700678 push_email_address($line);
Joe Perchescb7301c2009-04-07 20:40:12 -0700679 }
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
746 @lines = mailmap(@lines);
747
748 $hash{$_}++ for @lines;
Joe Perchesf5492662009-09-21 17:04:13 -0700749 }
750
751 $count = 0;
752 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
753 my $sign_offs = $hash{$line};
754 $count++;
755 last if ($sign_offs < $email_git_min_signatures ||
756 $count > $email_git_max_maintainers ||
757 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
758 push_email_address($line);
759 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700760}
761
762sub uniq {
763 my @parms = @_;
764
765 my %saw;
766 @parms = grep(!$saw{$_}++, @parms);
767 return @parms;
768}
769
770sub sort_and_uniq {
771 my @parms = @_;
772
773 my %saw;
774 @parms = sort @parms;
775 @parms = grep(!$saw{$_}++, @parms);
776 return @parms;
777}
778
779sub output {
780 my @parms = @_;
781
782 if ($output_multiline) {
783 foreach my $line (@parms) {
784 print("${line}\n");
785 }
786 } else {
787 print(join($output_separator, @parms));
788 print("\n");
789 }
790}
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700791
792my $rfc822re;
793
794sub make_rfc822re {
795# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
796# comment. We must allow for rfc822_lwsp (or comments) after each of these.
797# This regexp will only work on addresses which have had comments stripped
798# and replaced with rfc822_lwsp.
799
800 my $specials = '()<>@,;:\\\\".\\[\\]';
801 my $controls = '\\000-\\037\\177';
802
803 my $dtext = "[^\\[\\]\\r\\\\]";
804 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
805
806 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
807
808# Use zero-width assertion to spot the limit of an atom. A simple
809# $rfc822_lwsp* causes the regexp engine to hang occasionally.
810 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
811 my $word = "(?:$atom|$quoted_string)";
812 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
813
814 my $sub_domain = "(?:$atom|$domain_literal)";
815 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
816
817 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
818
819 my $phrase = "$word*";
820 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
821 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
822 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
823
824 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
825 my $address = "(?:$mailbox|$group)";
826
827 return "$rfc822_lwsp*$address";
828}
829
830sub rfc822_strip_comments {
831 my $s = shift;
832# Recursively remove comments, and replace with a single space. The simpler
833# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
834# chars in atoms, for example.
835
836 while ($s =~ s/^((?:[^"\\]|\\.)*
837 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
838 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
839 return $s;
840}
841
842# valid: returns true if the parameter is an RFC822 valid address
843#
844sub rfc822_valid ($) {
845 my $s = rfc822_strip_comments(shift);
846
847 if (!$rfc822re) {
848 $rfc822re = make_rfc822re();
849 }
850
851 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
852}
853
854# validlist: In scalar context, returns true if the parameter is an RFC822
855# valid list of addresses.
856#
857# In list context, returns an empty list on failure (an invalid
858# address was found); otherwise a list whose first element is the
859# number of addresses found and whose remaining elements are the
860# addresses. This is needed to disambiguate failure (invalid)
861# from success with no addresses found, because an empty string is
862# a valid list.
863
864sub rfc822_validlist ($) {
865 my $s = rfc822_strip_comments(shift);
866
867 if (!$rfc822re) {
868 $rfc822re = make_rfc822re();
869 }
870 # * null list items are valid according to the RFC
871 # * the '1' business is to aid in distinguishing failure from no results
872
873 my @r;
874 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
875 $s =~ m/^$rfc822_char*$/) {
Joe Perches5f2441e2009-06-16 15:34:02 -0700876 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700877 push @r, $1;
878 }
879 return wantarray ? (scalar(@r), @r) : 1;
880 }
881 else {
882 return wantarray ? () : 0;
883 }
884}