scripts/get_maintainer.pl: support M: lines with names and multiple entries per M...
[linux-2.6-block.git] / scripts / get_maintainer.pl
CommitLineData
cb7301c7
JP
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#
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;
290603c1 16my $V = '0.16';
cb7301c7
JP
17
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;
30my $email_git_since = "1-year-ago";
31my $output_multiline = 1;
32my $output_separator = ", ";
33my $scm = 0;
34my $web = 0;
35my $subsystem = 0;
36my $status = 0;
4a7fdb5f 37my $from_filename = 0;
cb7301c7
JP
38my $version = 0;
39my $help = 0;
40
41my $exit = 0;
42
43my @penguin_chief = ();
44push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
45#Andrew wants in on most everything - 2009/01/14
46#push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
47
48my @penguin_chief_names = ();
49foreach my $chief (@penguin_chief) {
50 if ($chief =~ m/^(.*):(.*)/) {
51 my $chief_name = $1;
52 my $chief_addr = $2;
53 push(@penguin_chief_names, $chief_name);
54 }
55}
56my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
57
1b5e1cf6
JP
58# rfc822 - preloaded methods go here.
59my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
60my $rfc822_char = '[\\000-\\177]';
61
cb7301c7
JP
62if (!GetOptions(
63 'email!' => \$email,
64 'git!' => \$email_git,
65 'git-chief-penguins!' => \$email_git_penguin_chiefs,
66 'git-min-signatures=i' => \$email_git_min_signatures,
67 'git-max-maintainers=i' => \$email_git_max_maintainers,
68 'git-since=s' => \$email_git_since,
69 'm!' => \$email_maintainer,
70 'n!' => \$email_usename,
71 'l!' => \$email_list,
72 's!' => \$email_subscriber_list,
73 'multiline!' => \$output_multiline,
74 'separator=s' => \$output_separator,
75 'subsystem!' => \$subsystem,
76 'status!' => \$status,
77 'scm!' => \$scm,
78 'web!' => \$web,
4a7fdb5f 79 'f|file' => \$from_filename,
cb7301c7
JP
80 'v|version' => \$version,
81 'h|help' => \$help,
82 )) {
83 usage();
84 die "$P: invalid argument\n";
85}
86
87if ($help != 0) {
88 usage();
89 exit 0;
90}
91
92if ($version != 0) {
93 print("${P} ${V}\n");
94 exit 0;
95}
96
cb7301c7
JP
97if ($#ARGV < 0) {
98 usage();
99 die "$P: argument missing: patchfile or -f file please\n";
100}
101
102my $selections = $email + $scm + $status + $subsystem + $web;
103if ($selections == 0) {
104 usage();
105 die "$P: Missing required option: email, scm, status, subsystem or web\n";
106}
107
108if ($email && ($email_maintainer + $email_list + $email_subscriber_list
109 + $email_git + $email_git_penguin_chiefs) == 0) {
110 usage();
111 die "$P: Please select at least 1 email option\n";
112}
113
114if (!top_of_kernel_tree($lk_path)) {
115 die "$P: The current directory does not appear to be "
116 . "a linux kernel source tree.\n";
117}
118
119## Read MAINTAINERS for type/value pairs
120
121my @typevalue = ();
122open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
123while (<MAINT>) {
124 my $line = $_;
125
126 if ($line =~ m/^(\C):\s*(.*)/) {
127 my $type = $1;
128 my $value = $2;
129
130 ##Filename pattern matching
131 if ($type eq "F" || $type eq "X") {
132 $value =~ s@\.@\\\.@g; ##Convert . to \.
133 $value =~ s/\*/\.\*/g; ##Convert * to .*
134 $value =~ s/\?/\./g; ##Convert ? to .
135 }
136 push(@typevalue, "$type:$value");
137 } elsif (!/^(\s)*$/) {
138 $line =~ s/\n$//g;
139 push(@typevalue, $line);
140 }
141}
142close(MAINT);
143
4a7fdb5f 144## use the filenames on the command line or find the filenames in the patchfiles
cb7301c7
JP
145
146my @files = ();
147
4a7fdb5f
JP
148foreach my $file (@ARGV) {
149 next if ((-d $file));
150 if (!(-f $file)) {
151 die "$P: file '${file}' not found\n";
cb7301c7 152 }
4a7fdb5f
JP
153 if ($from_filename) {
154 push(@files, $file);
155 } else {
156 my $file_cnt = @files;
157 open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
158 while (<PATCH>) {
159 if (m/^\+\+\+\s+(\S+)/) {
160 my $filename = $1;
161 $filename =~ s@^[^/]*/@@;
162 $filename =~ s@\n@@;
163 push(@files, $filename);
164 }
cb7301c7 165 }
4a7fdb5f
JP
166 close(PATCH);
167 if ($file_cnt == @files) {
168 die "$P: file '${file}' doesn't appear to be a patch. "
169 . "Add -f to options?\n";
170 }
171 @files = sort_and_uniq(@files);
cb7301c7 172 }
cb7301c7
JP
173}
174
175my @email_to = ();
290603c1 176my @list_to = ();
cb7301c7
JP
177my @scm = ();
178my @web = ();
179my @subsystem = ();
180my @status = ();
181
182# Find responsible parties
183
184foreach my $file (@files) {
185
186#Do not match excluded file patterns
187
188 my $exclude = 0;
189 foreach my $line (@typevalue) {
290603c1 190 if ($line =~ m/^(\C):\s*(.*)/) {
cb7301c7
JP
191 my $type = $1;
192 my $value = $2;
193 if ($type eq 'X') {
194 if (file_match_pattern($file, $value)) {
195 $exclude = 1;
196 }
197 }
198 }
199 }
200
201 if (!$exclude) {
202 my $tvi = 0;
203 foreach my $line (@typevalue) {
290603c1 204 if ($line =~ m/^(\C):\s*(.*)/) {
cb7301c7
JP
205 my $type = $1;
206 my $value = $2;
207 if ($type eq 'F') {
208 if (file_match_pattern($file, $value)) {
209 add_categories($tvi);
210 }
211 }
212 }
213 $tvi++;
214 }
215 }
216
4a7fdb5f 217 if ($email && $email_git) {
cb7301c7
JP
218 recent_git_signoffs($file);
219 }
220
221}
222
f5f5078d 223if ($email) {
cb7301c7
JP
224 foreach my $chief (@penguin_chief) {
225 if ($chief =~ m/^(.*):(.*)/) {
f5f5078d 226 my $email_address;
cb7301c7 227 if ($email_usename) {
f5f5078d 228 $email_address = format_email($1, $2);
cb7301c7 229 } else {
f5f5078d
JP
230 $email_address = $2;
231 }
232 if ($email_git_penguin_chiefs) {
233 push(@email_to, $email_address);
234 } else {
235 @email_to = grep(!/${email_address}/, @email_to);
cb7301c7
JP
236 }
237 }
238 }
239}
240
290603c1
JP
241if ($email || $email_list) {
242 my @to = ();
243 if ($email) {
244 @to = (@to, @email_to);
cb7301c7 245 }
290603c1 246 if ($email_list) {
290603c1 247 @to = (@to, @list_to);
290603c1
JP
248 }
249 output(uniq(@to));
cb7301c7
JP
250}
251
252if ($scm) {
4a7fdb5f 253 @scm = sort_and_uniq(@scm);
cb7301c7
JP
254 output(@scm);
255}
256
257if ($status) {
4a7fdb5f 258 @status = sort_and_uniq(@status);
cb7301c7
JP
259 output(@status);
260}
261
262if ($subsystem) {
4a7fdb5f 263 @subsystem = sort_and_uniq(@subsystem);
cb7301c7
JP
264 output(@subsystem);
265}
266
267if ($web) {
4a7fdb5f 268 @web = sort_and_uniq(@web);
cb7301c7
JP
269 output(@web);
270}
271
272exit($exit);
273
274sub file_match_pattern {
275 my ($file, $pattern) = @_;
276 if (substr($pattern, -1) eq "/") {
277 if ($file =~ m@^$pattern@) {
278 return 1;
279 }
280 } else {
281 if ($file =~ m@^$pattern@) {
282 my $s1 = ($file =~ tr@/@@);
283 my $s2 = ($pattern =~ tr@/@@);
284 if ($s1 == $s2) {
285 return 1;
286 }
287 }
288 }
289 return 0;
290}
291
292sub usage {
293 print <<EOT;
294usage: $P [options] patchfile
295 $P [options] -f file
296version: $V
297
298MAINTAINER field selection options:
299 --email => print email address(es) if any
300 --git => include recent git \*-by: signers
301 --git-chief-penguins => include ${penguin_chiefs}
302 --git-min-signatures => number of signatures required (default: 1)
303 --git-max-maintainers => maximum maintainers to add (default: 5)
304 --git-since => git history to use (default: 1-year-ago)
305 --m => include maintainer(s) if any
306 --n => include name 'Full Name <addr\@domain.tld>'
307 --l => include list(s) if any
308 --s => include subscriber only list(s) if any
309 --scm => print SCM tree(s) if any
310 --status => print status if any
311 --subsystem => print subsystem name if any
312 --web => print website(s) if any
313
314Output type options:
315 --separator [, ] => separator for multiple entries on 1 line
316 --multiline => print 1 entry per line
317
318Default options:
290603c1 319 [--email --git --m --n --l --multiline]
cb7301c7
JP
320
321Other options:
f5f5078d 322 --version => show version
cb7301c7
JP
323 --help => show this help information
324
325EOT
326}
327
328sub top_of_kernel_tree {
329 my ($lk_path) = @_;
330
331 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
332 $lk_path .= "/";
333 }
334 if ( (-f "${lk_path}COPYING")
335 && (-f "${lk_path}CREDITS")
336 && (-f "${lk_path}Kbuild")
337 && (-f "${lk_path}MAINTAINERS")
338 && (-f "${lk_path}Makefile")
339 && (-f "${lk_path}README")
340 && (-d "${lk_path}Documentation")
341 && (-d "${lk_path}arch")
342 && (-d "${lk_path}include")
343 && (-d "${lk_path}drivers")
344 && (-d "${lk_path}fs")
345 && (-d "${lk_path}init")
346 && (-d "${lk_path}ipc")
347 && (-d "${lk_path}kernel")
348 && (-d "${lk_path}lib")
349 && (-d "${lk_path}scripts")) {
350 return 1;
351 }
352 return 0;
353}
354
355sub format_email {
356 my ($name, $email) = @_;
357
358 $name =~ s/^\s+|\s+$//g;
359 $email =~ s/^\s+|\s+$//g;
360
361 my $formatted_email = "";
362
363 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
364 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
365 $formatted_email = "\"${name}\"\ \<${email}\>";
366 } else {
367 $formatted_email = "${name} \<${email}\>";
368 }
369 return $formatted_email;
370}
371
372sub add_categories {
373 my ($index) = @_;
374
375 $index = $index - 1;
376 while ($index >= 0) {
377 my $tv = $typevalue[$index];
290603c1 378 if ($tv =~ m/^(\C):\s*(.*)/) {
cb7301c7
JP
379 my $ptype = $1;
380 my $pvalue = $2;
381 if ($ptype eq "L") {
290603c1
JP
382 my $list_address = $pvalue;
383 my $list_additional = "";
384 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
385 $list_address = $1;
386 $list_additional = $2;
387 }
bdf7c685 388 if ($list_additional =~ m/subscribers-only/) {
cb7301c7 389 if ($email_subscriber_list) {
290603c1 390 push(@list_to, $list_address);
cb7301c7
JP
391 }
392 } else {
393 if ($email_list) {
290603c1 394 push(@list_to, $list_address);
cb7301c7
JP
395 }
396 }
397 } elsif ($ptype eq "M") {
398 if ($email_maintainer) {
1b5e1cf6 399 push_email_addresses($pvalue);
cb7301c7
JP
400 }
401 } elsif ($ptype eq "T") {
402 push(@scm, $pvalue);
403 } elsif ($ptype eq "W") {
404 push(@web, $pvalue);
405 } elsif ($ptype eq "S") {
406 push(@status, $pvalue);
407 }
408
409 $index--;
410 } else {
411 push(@subsystem,$tv);
412 $index = -1;
413 }
414 }
415}
416
1b5e1cf6
JP
417sub push_email_address {
418 my ($email_address) = @_;
419
420 my $email_name = "";
421 if ($email_address =~ m/([^<]+)<(.*\@.*)>$/) {
422 $email_name = $1;
423 $email_address = $2;
424 }
425
426 if ($email_usename && $email_name) {
427 push(@email_to, format_email($email_name, $email_address));
428 } else {
429 push(@email_to, $email_address);
430 }
431}
432
433sub push_email_addresses {
434 my ($address) = @_;
435
436 my @address_list = ();
437
438 if (@address_list = rfc822_validlist($address)) {
439 my $array_count = shift(@address_list);
440 while (my $entry = shift(@address_list)) {
441 push_email_address($entry);
442 }
443 }
444
445}
446
cb7301c7
JP
447sub which {
448 my ($bin) = @_;
449
f5f5078d 450 foreach my $path (split(/:/, $ENV{PATH})) {
cb7301c7
JP
451 if (-e "$path/$bin") {
452 return "$path/$bin";
453 }
454 }
455
456 return "";
457}
458
459sub recent_git_signoffs {
460 my ($file) = @_;
461
462 my $sign_offs = "";
463 my $cmd = "";
464 my $output = "";
465 my $count = 0;
466 my @lines = ();
467
468 if (which("git") eq "") {
de2fc492
JP
469 warn("$P: git not found. Add --nogit to options?\n");
470 return;
471 }
472 if (!(-d ".git")) {
473 warn("$P: .git repository not found.\n");
474 warn("Use a .git repository for better results.\n");
475 warn("ie: git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
476 return;
cb7301c7
JP
477 }
478
479 $cmd = "git log --since=${email_git_since} -- ${file}";
de2fc492
JP
480 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
481 if (!$email_git_penguin_chiefs) {
482 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
483 }
4a7fdb5f 484 $cmd .= " | cut -f2- -d\":\"";
cb7301c7
JP
485 $cmd .= " | sort | uniq -c | sort -rn";
486
487 $output = `${cmd}`;
488 $output =~ s/^\s*//gm;
489
490 @lines = split("\n", $output);
491 foreach my $line (@lines) {
4a7fdb5f 492 if ($line =~ m/([0-9]+)\s+(.*)/) {
cb7301c7 493 my $sign_offs = $1;
4a7fdb5f 494 $line = $2;
cb7301c7
JP
495 $count++;
496 if ($sign_offs < $email_git_min_signatures ||
497 $count > $email_git_max_maintainers) {
498 last;
499 }
500 } else {
501 die("$P: Unexpected git output: ${line}\n");
502 }
4a7fdb5f 503 if ($line =~ m/(.+)<(.+)>/) {
cb7301c7
JP
504 my $git_name = $1;
505 my $git_addr = $2;
cb7301c7
JP
506 if ($email_usename) {
507 push(@email_to, format_email($git_name, $git_addr));
508 } else {
509 push(@email_to, $git_addr);
510 }
4a7fdb5f 511 } elsif ($line =~ m/<(.+)>/) {
cb7301c7
JP
512 my $git_addr = $1;
513 push(@email_to, $git_addr);
514 } else {
515 push(@email_to, $line);
516 }
517 }
cb7301c7
JP
518}
519
520sub uniq {
521 my @parms = @_;
522
523 my %saw;
524 @parms = grep(!$saw{$_}++, @parms);
525 return @parms;
526}
527
528sub sort_and_uniq {
529 my @parms = @_;
530
531 my %saw;
532 @parms = sort @parms;
533 @parms = grep(!$saw{$_}++, @parms);
534 return @parms;
535}
536
537sub output {
538 my @parms = @_;
539
540 if ($output_multiline) {
541 foreach my $line (@parms) {
542 print("${line}\n");
543 }
544 } else {
545 print(join($output_separator, @parms));
546 print("\n");
547 }
548}
1b5e1cf6
JP
549
550my $rfc822re;
551
552sub make_rfc822re {
553# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
554# comment. We must allow for rfc822_lwsp (or comments) after each of these.
555# This regexp will only work on addresses which have had comments stripped
556# and replaced with rfc822_lwsp.
557
558 my $specials = '()<>@,;:\\\\".\\[\\]';
559 my $controls = '\\000-\\037\\177';
560
561 my $dtext = "[^\\[\\]\\r\\\\]";
562 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
563
564 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
565
566# Use zero-width assertion to spot the limit of an atom. A simple
567# $rfc822_lwsp* causes the regexp engine to hang occasionally.
568 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
569 my $word = "(?:$atom|$quoted_string)";
570 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
571
572 my $sub_domain = "(?:$atom|$domain_literal)";
573 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
574
575 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
576
577 my $phrase = "$word*";
578 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
579 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
580 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
581
582 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
583 my $address = "(?:$mailbox|$group)";
584
585 return "$rfc822_lwsp*$address";
586}
587
588sub rfc822_strip_comments {
589 my $s = shift;
590# Recursively remove comments, and replace with a single space. The simpler
591# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
592# chars in atoms, for example.
593
594 while ($s =~ s/^((?:[^"\\]|\\.)*
595 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
596 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
597 return $s;
598}
599
600# valid: returns true if the parameter is an RFC822 valid address
601#
602sub rfc822_valid ($) {
603 my $s = rfc822_strip_comments(shift);
604
605 if (!$rfc822re) {
606 $rfc822re = make_rfc822re();
607 }
608
609 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
610}
611
612# validlist: In scalar context, returns true if the parameter is an RFC822
613# valid list of addresses.
614#
615# In list context, returns an empty list on failure (an invalid
616# address was found); otherwise a list whose first element is the
617# number of addresses found and whose remaining elements are the
618# addresses. This is needed to disambiguate failure (invalid)
619# from success with no addresses found, because an empty string is
620# a valid list.
621
622sub rfc822_validlist ($) {
623 my $s = rfc822_strip_comments(shift);
624
625 if (!$rfc822re) {
626 $rfc822re = make_rfc822re();
627 }
628 # * null list items are valid according to the RFC
629 # * the '1' business is to aid in distinguishing failure from no results
630
631 my @r;
632 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
633 $s =~ m/^$rfc822_char*$/) {
634 while($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
635 push @r, $1;
636 }
637 return wantarray ? (scalar(@r), @r) : 1;
638 }
639 else {
640 return wantarray ? () : 0;
641 }
642}