blob: 5132949500c1008cbbde23b34df056de923a1aa9 [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 Perches3fb55652009-09-21 17:04:17 -070016my $V = '0.19';
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 Perchescb7301c2009-04-07 20:40:12 -0700261 if ($email_usename) {
Joe Perchesf5f50782009-06-16 15:34:00 -0700262 $email_address = format_email($1, $2);
Joe Perchescb7301c2009-04-07 20:40:12 -0700263 } else {
Joe Perchesf5f50782009-06-16 15:34:00 -0700264 $email_address = $2;
265 }
266 if ($email_git_penguin_chiefs) {
267 push(@email_to, $email_address);
268 } else {
269 @email_to = grep(!/${email_address}/, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700270 }
271 }
272 }
273}
274
Joe Perches290603c2009-06-16 15:33:58 -0700275if ($email || $email_list) {
276 my @to = ();
277 if ($email) {
278 @to = (@to, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700279 }
Joe Perches290603c2009-06-16 15:33:58 -0700280 if ($email_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700281 @to = (@to, @list_to);
Joe Perches290603c2009-06-16 15:33:58 -0700282 }
283 output(uniq(@to));
Joe Perchescb7301c2009-04-07 20:40:12 -0700284}
285
286if ($scm) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700287 @scm = sort_and_uniq(@scm);
Joe Perchescb7301c2009-04-07 20:40:12 -0700288 output(@scm);
289}
290
291if ($status) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700292 @status = sort_and_uniq(@status);
Joe Perchescb7301c2009-04-07 20:40:12 -0700293 output(@status);
294}
295
296if ($subsystem) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700297 @subsystem = sort_and_uniq(@subsystem);
Joe Perchescb7301c2009-04-07 20:40:12 -0700298 output(@subsystem);
299}
300
301if ($web) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700302 @web = sort_and_uniq(@web);
Joe Perchescb7301c2009-04-07 20:40:12 -0700303 output(@web);
304}
305
306exit($exit);
307
308sub file_match_pattern {
309 my ($file, $pattern) = @_;
310 if (substr($pattern, -1) eq "/") {
311 if ($file =~ m@^$pattern@) {
312 return 1;
313 }
314 } else {
315 if ($file =~ m@^$pattern@) {
316 my $s1 = ($file =~ tr@/@@);
317 my $s2 = ($pattern =~ tr@/@@);
318 if ($s1 == $s2) {
319 return 1;
320 }
321 }
322 }
323 return 0;
324}
325
326sub usage {
327 print <<EOT;
328usage: $P [options] patchfile
Joe Perches870020f2009-07-29 15:04:28 -0700329 $P [options] -f file|directory
Joe Perchescb7301c2009-04-07 20:40:12 -0700330version: $V
331
332MAINTAINER field selection options:
333 --email => print email address(es) if any
334 --git => include recent git \*-by: signers
335 --git-chief-penguins => include ${penguin_chiefs}
336 --git-min-signatures => number of signatures required (default: 1)
337 --git-max-maintainers => maximum maintainers to add (default: 5)
Joe Perches3d202ae2009-07-29 15:04:29 -0700338 --git-min-percent => minimum percentage of commits required (default: 5)
Joe Perchescb7301c2009-04-07 20:40:12 -0700339 --git-since => git history to use (default: 1-year-ago)
Joe Perchesf5492662009-09-21 17:04:13 -0700340 --git-blame => use git blame to find modified commits for patch or file
Joe Perchescb7301c2009-04-07 20:40:12 -0700341 --m => include maintainer(s) if any
342 --n => include name 'Full Name <addr\@domain.tld>'
343 --l => include list(s) if any
344 --s => include subscriber only list(s) if any
345 --scm => print SCM tree(s) if any
346 --status => print status if any
347 --subsystem => print subsystem name if any
348 --web => print website(s) if any
349
350Output type options:
351 --separator [, ] => separator for multiple entries on 1 line
352 --multiline => print 1 entry per line
353
Joe Perchescb7301c2009-04-07 20:40:12 -0700354Other options:
Joe Perches3fb55652009-09-21 17:04:17 -0700355 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
Joe Perchesf5f50782009-06-16 15:34:00 -0700356 --version => show version
Joe Perchescb7301c2009-04-07 20:40:12 -0700357 --help => show this help information
358
Joe Perches3fb55652009-09-21 17:04:17 -0700359Default options:
360 [--email --git --m --n --l --multiline --pattern-depth=0]
361
Joe Perches870020f2009-07-29 15:04:28 -0700362Notes:
363 Using "-f directory" may give unexpected results:
Joe Perchesf5492662009-09-21 17:04:13 -0700364 Used with "--git", git signators for _all_ files in and below
365 directory are examined as git recurses directories.
366 Any specified X: (exclude) pattern matches are _not_ ignored.
367 Used with "--nogit", directory is used as a pattern match,
368 no individual file within the directory or subdirectory
369 is matched.
370 Used with "--git-blame", does not iterate all files in directory
371 Using "--git-blame" is slow and may add old committers and authors
372 that are no longer active maintainers to the output.
Joe Perchescb7301c2009-04-07 20:40:12 -0700373EOT
374}
375
376sub top_of_kernel_tree {
377 my ($lk_path) = @_;
378
379 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
380 $lk_path .= "/";
381 }
382 if ( (-f "${lk_path}COPYING")
383 && (-f "${lk_path}CREDITS")
384 && (-f "${lk_path}Kbuild")
385 && (-f "${lk_path}MAINTAINERS")
386 && (-f "${lk_path}Makefile")
387 && (-f "${lk_path}README")
388 && (-d "${lk_path}Documentation")
389 && (-d "${lk_path}arch")
390 && (-d "${lk_path}include")
391 && (-d "${lk_path}drivers")
392 && (-d "${lk_path}fs")
393 && (-d "${lk_path}init")
394 && (-d "${lk_path}ipc")
395 && (-d "${lk_path}kernel")
396 && (-d "${lk_path}lib")
397 && (-d "${lk_path}scripts")) {
398 return 1;
399 }
400 return 0;
401}
402
403sub format_email {
404 my ($name, $email) = @_;
405
406 $name =~ s/^\s+|\s+$//g;
Joe Perchesd7895042009-06-16 15:34:02 -0700407 $name =~ s/^\"|\"$//g;
Joe Perchescb7301c2009-04-07 20:40:12 -0700408 $email =~ s/^\s+|\s+$//g;
409
410 my $formatted_email = "";
411
412 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
413 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
414 $formatted_email = "\"${name}\"\ \<${email}\>";
415 } else {
416 $formatted_email = "${name} \<${email}\>";
417 }
418 return $formatted_email;
419}
420
421sub add_categories {
422 my ($index) = @_;
423
424 $index = $index - 1;
425 while ($index >= 0) {
426 my $tv = $typevalue[$index];
Joe Perches290603c2009-06-16 15:33:58 -0700427 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700428 my $ptype = $1;
429 my $pvalue = $2;
430 if ($ptype eq "L") {
Joe Perches290603c2009-06-16 15:33:58 -0700431 my $list_address = $pvalue;
432 my $list_additional = "";
433 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
434 $list_address = $1;
435 $list_additional = $2;
436 }
Joe Perchesbdf7c682009-06-16 15:33:59 -0700437 if ($list_additional =~ m/subscribers-only/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700438 if ($email_subscriber_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700439 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700440 }
441 } else {
442 if ($email_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700443 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700444 }
445 }
446 } elsif ($ptype eq "M") {
Joe Perches5f2441e2009-06-16 15:34:02 -0700447 my $p_used = 0;
448 if ($index >= 0) {
449 my $tv = $typevalue[$index - 1];
450 if ($tv =~ m/^(\C):\s*(.*)/) {
451 if ($1 eq "P") {
452 if ($email_usename) {
453 push_email_address(format_email($2, $pvalue));
454 $p_used = 1;
455 }
456 }
457 }
458 }
459 if (!$p_used) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700460 push_email_addresses($pvalue);
Joe Perchescb7301c2009-04-07 20:40:12 -0700461 }
462 } elsif ($ptype eq "T") {
463 push(@scm, $pvalue);
464 } elsif ($ptype eq "W") {
465 push(@web, $pvalue);
466 } elsif ($ptype eq "S") {
467 push(@status, $pvalue);
468 }
469
470 $index--;
471 } else {
472 push(@subsystem,$tv);
473 $index = -1;
474 }
475 }
476}
477
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700478sub push_email_address {
479 my ($email_address) = @_;
480
481 my $email_name = "";
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700482
Joe Perches0a79c492009-06-16 15:34:03 -0700483 if ($email_maintainer) {
Joe Perchesf5492662009-09-21 17:04:13 -0700484 if ($email_address =~ m/([^<]+)<(.*\@.*)>$/) {
485 $email_name = $1;
486 $email_address = $2;
487 if ($email_usename) {
488 push(@email_to, format_email($email_name, $email_address));
489 } else {
490 push(@email_to, $email_address);
491 }
492 } elsif ($email_address =~ m/<(.+)>/) {
493 $email_address = $1;
494 push(@email_to, $email_address);
Joe Perches0a79c492009-06-16 15:34:03 -0700495 } else {
496 push(@email_to, $email_address);
497 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700498 }
499}
500
501sub push_email_addresses {
502 my ($address) = @_;
503
504 my @address_list = ();
505
Joe Perches5f2441e2009-06-16 15:34:02 -0700506 if (rfc822_valid($address)) {
507 push_email_address($address);
508 } elsif (@address_list = rfc822_validlist($address)) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700509 my $array_count = shift(@address_list);
510 while (my $entry = shift(@address_list)) {
511 push_email_address($entry);
512 }
Joe Perches5f2441e2009-06-16 15:34:02 -0700513 } else {
514 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700515 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700516}
517
Joe Perchescb7301c2009-04-07 20:40:12 -0700518sub which {
519 my ($bin) = @_;
520
Joe Perchesf5f50782009-06-16 15:34:00 -0700521 foreach my $path (split(/:/, $ENV{PATH})) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700522 if (-e "$path/$bin") {
523 return "$path/$bin";
524 }
525 }
526
527 return "";
528}
529
530sub recent_git_signoffs {
531 my ($file) = @_;
532
533 my $sign_offs = "";
534 my $cmd = "";
535 my $output = "";
536 my $count = 0;
537 my @lines = ();
Joe Perchesafa81ee2009-07-29 15:04:28 -0700538 my $total_sign_offs;
Joe Perchescb7301c2009-04-07 20:40:12 -0700539
540 if (which("git") eq "") {
Joe Perchesde2fc492009-06-16 15:34:01 -0700541 warn("$P: git not found. Add --nogit to options?\n");
542 return;
543 }
544 if (!(-d ".git")) {
Joe Perches5f2441e2009-06-16 15:34:02 -0700545 warn("$P: .git directory not found. Use a git repository for better results.\n");
546 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 -0700547 return;
Joe Perchescb7301c2009-04-07 20:40:12 -0700548 }
549
550 $cmd = "git log --since=${email_git_since} -- ${file}";
Joe Perchesde2fc492009-06-16 15:34:01 -0700551 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
552 if (!$email_git_penguin_chiefs) {
553 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
554 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700555 $cmd .= " | cut -f2- -d\":\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700556 $cmd .= " | sort | uniq -c | sort -rn";
557
558 $output = `${cmd}`;
559 $output =~ s/^\s*//gm;
560
561 @lines = split("\n", $output);
Joe Perchesafa81ee2009-07-29 15:04:28 -0700562
563 $total_sign_offs = 0;
564 foreach my $line (@lines) {
565 if ($line =~ m/([0-9]+)\s+(.*)/) {
566 $total_sign_offs += $1;
567 } else {
568 die("$P: Unexpected git output: ${line}\n");
569 }
570 }
571
Joe Perchescb7301c2009-04-07 20:40:12 -0700572 foreach my $line (@lines) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700573 if ($line =~ m/([0-9]+)\s+(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700574 my $sign_offs = $1;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700575 $line = $2;
Joe Perchescb7301c2009-04-07 20:40:12 -0700576 $count++;
577 if ($sign_offs < $email_git_min_signatures ||
Joe Perchesafa81ee2009-07-29 15:04:28 -0700578 $count > $email_git_max_maintainers ||
579 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700580 last;
581 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700582 }
Joe Perchesf5492662009-09-21 17:04:13 -0700583 push_email_address($line);
584 }
585}
586
587sub save_commits {
588 my ($cmd, @commits) = @_;
589 my $output;
590 my @lines = ();
591
592 $output = `${cmd}`;
593
594 @lines = split("\n", $output);
595 foreach my $line (@lines) {
596 if ($line =~ m/^(\w+) /) {
597 push (@commits, $1);
Joe Perchescb7301c2009-04-07 20:40:12 -0700598 }
599 }
Joe Perchesf5492662009-09-21 17:04:13 -0700600 return @commits;
601}
602
603sub git_assign_blame {
604 my ($file) = @_;
605
606 my @lines = ();
607 my @commits = ();
608 my $cmd;
609 my $output;
610 my %hash;
611 my $total_sign_offs;
612 my $count;
613
614 if (@range) {
615 foreach my $file_range_diff (@range) {
616 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
617 my $diff_file = $1;
618 my $diff_start = $2;
619 my $diff_length = $3;
620 next if (!("$file" eq "$diff_file"));
621 $cmd = "git blame -l -L $diff_start,+$diff_length $file\n";
622 @commits = save_commits($cmd, @commits);
623 }
624 } else {
625 if (-f $file) {
626 $cmd = "git blame -l $file\n";
627 @commits = save_commits($cmd, @commits);
628 }
629 }
630
631 $total_sign_offs = 0;
632 @commits = uniq(@commits);
633 foreach my $commit (@commits) {
634 $cmd = "git log -1 ${commit}";
635 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
636 if (!$email_git_penguin_chiefs) {
637 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
638 }
639 $cmd .= " | cut -f2- -d\":\"";
640
641 $output = `${cmd}`;
642 $output =~ s/^\s*//gm;
643 @lines = split("\n", $output);
644 $hash{$_}++ for @lines;
645 $total_sign_offs += @lines;
646 }
647
648 $count = 0;
649 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
650 my $sign_offs = $hash{$line};
651 $count++;
652 last if ($sign_offs < $email_git_min_signatures ||
653 $count > $email_git_max_maintainers ||
654 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
655 push_email_address($line);
656 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700657}
658
659sub uniq {
660 my @parms = @_;
661
662 my %saw;
663 @parms = grep(!$saw{$_}++, @parms);
664 return @parms;
665}
666
667sub sort_and_uniq {
668 my @parms = @_;
669
670 my %saw;
671 @parms = sort @parms;
672 @parms = grep(!$saw{$_}++, @parms);
673 return @parms;
674}
675
676sub output {
677 my @parms = @_;
678
679 if ($output_multiline) {
680 foreach my $line (@parms) {
681 print("${line}\n");
682 }
683 } else {
684 print(join($output_separator, @parms));
685 print("\n");
686 }
687}
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700688
689my $rfc822re;
690
691sub make_rfc822re {
692# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
693# comment. We must allow for rfc822_lwsp (or comments) after each of these.
694# This regexp will only work on addresses which have had comments stripped
695# and replaced with rfc822_lwsp.
696
697 my $specials = '()<>@,;:\\\\".\\[\\]';
698 my $controls = '\\000-\\037\\177';
699
700 my $dtext = "[^\\[\\]\\r\\\\]";
701 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
702
703 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
704
705# Use zero-width assertion to spot the limit of an atom. A simple
706# $rfc822_lwsp* causes the regexp engine to hang occasionally.
707 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
708 my $word = "(?:$atom|$quoted_string)";
709 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
710
711 my $sub_domain = "(?:$atom|$domain_literal)";
712 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
713
714 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
715
716 my $phrase = "$word*";
717 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
718 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
719 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
720
721 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
722 my $address = "(?:$mailbox|$group)";
723
724 return "$rfc822_lwsp*$address";
725}
726
727sub rfc822_strip_comments {
728 my $s = shift;
729# Recursively remove comments, and replace with a single space. The simpler
730# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
731# chars in atoms, for example.
732
733 while ($s =~ s/^((?:[^"\\]|\\.)*
734 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
735 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
736 return $s;
737}
738
739# valid: returns true if the parameter is an RFC822 valid address
740#
741sub rfc822_valid ($) {
742 my $s = rfc822_strip_comments(shift);
743
744 if (!$rfc822re) {
745 $rfc822re = make_rfc822re();
746 }
747
748 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
749}
750
751# validlist: In scalar context, returns true if the parameter is an RFC822
752# valid list of addresses.
753#
754# In list context, returns an empty list on failure (an invalid
755# address was found); otherwise a list whose first element is the
756# number of addresses found and whose remaining elements are the
757# addresses. This is needed to disambiguate failure (invalid)
758# from success with no addresses found, because an empty string is
759# a valid list.
760
761sub rfc822_validlist ($) {
762 my $s = rfc822_strip_comments(shift);
763
764 if (!$rfc822re) {
765 $rfc822re = make_rfc822re();
766 }
767 # * null list items are valid according to the RFC
768 # * the '1' business is to aid in distinguishing failure from no results
769
770 my @r;
771 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
772 $s =~ m/^$rfc822_char*$/) {
Joe Perches5f2441e2009-06-16 15:34:02 -0700773 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700774 push @r, $1;
775 }
776 return wantarray ? (scalar(@r), @r) : 1;
777 }
778 else {
779 return wantarray ? () : 0;
780 }
781}