blob: fb446e0f8bbf7554b5ba3f926e56e59429a7b013 [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 Perchesf5492662009-09-21 17:04:13 -070016my $V = '0.18beta2';
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 Perchescb7301c2009-04-07 20:40:12 -070040my $version = 0;
41my $help = 0;
42
43my $exit = 0;
44
45my @penguin_chief = ();
46push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
47#Andrew wants in on most everything - 2009/01/14
48#push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
49
50my @penguin_chief_names = ();
51foreach my $chief (@penguin_chief) {
52 if ($chief =~ m/^(.*):(.*)/) {
53 my $chief_name = $1;
54 my $chief_addr = $2;
55 push(@penguin_chief_names, $chief_name);
56 }
57}
58my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
59
Joe Perches5f2441e2009-06-16 15:34:02 -070060# rfc822 email address - preloaded methods go here.
Joe Perches1b5e1cf2009-06-16 15:34:01 -070061my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
Joe Perchesdf4cc032009-06-16 15:34:04 -070062my $rfc822_char = '[\\000-\\377]';
Joe Perches1b5e1cf2009-06-16 15:34:01 -070063
Joe Perchescb7301c2009-04-07 20:40:12 -070064if (!GetOptions(
65 'email!' => \$email,
66 'git!' => \$email_git,
67 'git-chief-penguins!' => \$email_git_penguin_chiefs,
68 'git-min-signatures=i' => \$email_git_min_signatures,
69 'git-max-maintainers=i' => \$email_git_max_maintainers,
Joe Perchesafa81ee2009-07-29 15:04:28 -070070 'git-min-percent=i' => \$email_git_min_percent,
Joe Perchescb7301c2009-04-07 20:40:12 -070071 'git-since=s' => \$email_git_since,
Joe Perchesf5492662009-09-21 17:04:13 -070072 'git-blame!' => \$email_git_blame,
Joe Perchescb7301c2009-04-07 20:40:12 -070073 'm!' => \$email_maintainer,
74 'n!' => \$email_usename,
75 'l!' => \$email_list,
76 's!' => \$email_subscriber_list,
77 'multiline!' => \$output_multiline,
78 'separator=s' => \$output_separator,
79 'subsystem!' => \$subsystem,
80 'status!' => \$status,
81 'scm!' => \$scm,
82 'web!' => \$web,
Joe Perches4a7fdb52009-04-10 12:28:57 -070083 'f|file' => \$from_filename,
Joe Perchescb7301c2009-04-07 20:40:12 -070084 'v|version' => \$version,
85 'h|help' => \$help,
86 )) {
87 usage();
88 die "$P: invalid argument\n";
89}
90
91if ($help != 0) {
92 usage();
93 exit 0;
94}
95
96if ($version != 0) {
97 print("${P} ${V}\n");
98 exit 0;
99}
100
Joe Perchescb7301c2009-04-07 20:40:12 -0700101if ($#ARGV < 0) {
102 usage();
103 die "$P: argument missing: patchfile or -f file please\n";
104}
105
106my $selections = $email + $scm + $status + $subsystem + $web;
107if ($selections == 0) {
108 usage();
109 die "$P: Missing required option: email, scm, status, subsystem or web\n";
110}
111
Joe Perchesf5492662009-09-21 17:04:13 -0700112if ($email &&
113 ($email_maintainer + $email_list + $email_subscriber_list +
114 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700115 usage();
116 die "$P: Please select at least 1 email option\n";
117}
118
119if (!top_of_kernel_tree($lk_path)) {
120 die "$P: The current directory does not appear to be "
121 . "a linux kernel source tree.\n";
122}
123
124## Read MAINTAINERS for type/value pairs
125
126my @typevalue = ();
127open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
128while (<MAINT>) {
129 my $line = $_;
130
131 if ($line =~ m/^(\C):\s*(.*)/) {
132 my $type = $1;
133 my $value = $2;
134
135 ##Filename pattern matching
136 if ($type eq "F" || $type eq "X") {
137 $value =~ s@\.@\\\.@g; ##Convert . to \.
138 $value =~ s/\*/\.\*/g; ##Convert * to .*
139 $value =~ s/\?/\./g; ##Convert ? to .
Joe Perches870020f2009-07-29 15:04:28 -0700140 ##if pattern is a directory and it lacks a trailing slash, add one
141 if ((-d $value)) {
142 $value =~ s@([^/])$@$1/@;
143 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700144 }
145 push(@typevalue, "$type:$value");
146 } elsif (!/^(\s)*$/) {
147 $line =~ s/\n$//g;
148 push(@typevalue, $line);
149 }
150}
151close(MAINT);
152
Joe Perches4a7fdb52009-04-10 12:28:57 -0700153## use the filenames on the command line or find the filenames in the patchfiles
Joe Perchescb7301c2009-04-07 20:40:12 -0700154
155my @files = ();
Joe Perchesf5492662009-09-21 17:04:13 -0700156my @range = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700157
Joe Perches4a7fdb52009-04-10 12:28:57 -0700158foreach my $file (@ARGV) {
Joe Perches870020f2009-07-29 15:04:28 -0700159 ##if $file is a directory and it lacks a trailing slash, add one
160 if ((-d $file)) {
161 $file =~ s@([^/])$@$1/@;
162 } elsif (!(-f $file)) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700163 die "$P: file '${file}' not found\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700164 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700165 if ($from_filename) {
166 push(@files, $file);
167 } else {
168 my $file_cnt = @files;
Joe Perchesf5492662009-09-21 17:04:13 -0700169 my $lastfile;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700170 open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
171 while (<PATCH>) {
172 if (m/^\+\+\+\s+(\S+)/) {
173 my $filename = $1;
174 $filename =~ s@^[^/]*/@@;
175 $filename =~ s@\n@@;
Joe Perchesf5492662009-09-21 17:04:13 -0700176 $lastfile = $filename;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700177 push(@files, $filename);
Joe Perchesf5492662009-09-21 17:04:13 -0700178 } elsif (m/^\@\@ -(\d+),(\d+)/) {
179 if ($email_git_blame) {
180 push(@range, "$lastfile:$1:$2");
181 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700182 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700183 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700184 close(PATCH);
185 if ($file_cnt == @files) {
Joe Perches7f29fd272009-06-16 15:34:04 -0700186 warn "$P: file '${file}' doesn't appear to be a patch. "
Joe Perches4a7fdb52009-04-10 12:28:57 -0700187 . "Add -f to options?\n";
188 }
189 @files = sort_and_uniq(@files);
Joe Perchescb7301c2009-04-07 20:40:12 -0700190 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700191}
192
193my @email_to = ();
Joe Perches290603c12009-06-16 15:33:58 -0700194my @list_to = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700195my @scm = ();
196my @web = ();
197my @subsystem = ();
198my @status = ();
199
200# Find responsible parties
201
202foreach my $file (@files) {
203
204#Do not match excluded file patterns
205
206 my $exclude = 0;
207 foreach my $line (@typevalue) {
Joe Perches290603c12009-06-16 15:33:58 -0700208 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700209 my $type = $1;
210 my $value = $2;
211 if ($type eq 'X') {
212 if (file_match_pattern($file, $value)) {
213 $exclude = 1;
Joe Perches1d606b42009-09-21 17:04:14 -0700214 last;
Joe Perchescb7301c2009-04-07 20:40:12 -0700215 }
216 }
217 }
218 }
219
220 if (!$exclude) {
221 my $tvi = 0;
Joe Perches1d606b42009-09-21 17:04:14 -0700222 my %hash;
Joe Perchescb7301c2009-04-07 20:40:12 -0700223 foreach my $line (@typevalue) {
Joe Perches290603c12009-06-16 15:33:58 -0700224 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700225 my $type = $1;
226 my $value = $2;
227 if ($type eq 'F') {
228 if (file_match_pattern($file, $value)) {
Joe Perches1d606b42009-09-21 17:04:14 -0700229 my $pattern_depth = ($value =~ tr@/@@);
230 $pattern_depth++ if (!(substr($value,-1,1) eq "/"));
231 $hash{$tvi} = $pattern_depth;
Joe Perchescb7301c2009-04-07 20:40:12 -0700232 }
233 }
234 }
235 $tvi++;
236 }
Joe Perches1d606b42009-09-21 17:04:14 -0700237 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
238 add_categories($line);
239 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700240 }
241
Joe Perches4a7fdb52009-04-10 12:28:57 -0700242 if ($email && $email_git) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700243 recent_git_signoffs($file);
244 }
245
Joe Perchesf5492662009-09-21 17:04:13 -0700246 if ($email && $email_git_blame) {
247 git_assign_blame($file);
248 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700249}
250
Joe Perchesf5f50782009-06-16 15:34:00 -0700251if ($email) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700252 foreach my $chief (@penguin_chief) {
253 if ($chief =~ m/^(.*):(.*)/) {
Joe Perchesf5f50782009-06-16 15:34:00 -0700254 my $email_address;
Joe Perchescb7301c2009-04-07 20:40:12 -0700255 if ($email_usename) {
Joe Perchesf5f50782009-06-16 15:34:00 -0700256 $email_address = format_email($1, $2);
Joe Perchescb7301c2009-04-07 20:40:12 -0700257 } else {
Joe Perchesf5f50782009-06-16 15:34:00 -0700258 $email_address = $2;
259 }
260 if ($email_git_penguin_chiefs) {
261 push(@email_to, $email_address);
262 } else {
263 @email_to = grep(!/${email_address}/, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700264 }
265 }
266 }
267}
268
Joe Perches290603c12009-06-16 15:33:58 -0700269if ($email || $email_list) {
270 my @to = ();
271 if ($email) {
272 @to = (@to, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700273 }
Joe Perches290603c12009-06-16 15:33:58 -0700274 if ($email_list) {
Joe Perches290603c12009-06-16 15:33:58 -0700275 @to = (@to, @list_to);
Joe Perches290603c12009-06-16 15:33:58 -0700276 }
277 output(uniq(@to));
Joe Perchescb7301c2009-04-07 20:40:12 -0700278}
279
280if ($scm) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700281 @scm = sort_and_uniq(@scm);
Joe Perchescb7301c2009-04-07 20:40:12 -0700282 output(@scm);
283}
284
285if ($status) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700286 @status = sort_and_uniq(@status);
Joe Perchescb7301c2009-04-07 20:40:12 -0700287 output(@status);
288}
289
290if ($subsystem) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700291 @subsystem = sort_and_uniq(@subsystem);
Joe Perchescb7301c2009-04-07 20:40:12 -0700292 output(@subsystem);
293}
294
295if ($web) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700296 @web = sort_and_uniq(@web);
Joe Perchescb7301c2009-04-07 20:40:12 -0700297 output(@web);
298}
299
300exit($exit);
301
302sub file_match_pattern {
303 my ($file, $pattern) = @_;
304 if (substr($pattern, -1) eq "/") {
305 if ($file =~ m@^$pattern@) {
306 return 1;
307 }
308 } else {
309 if ($file =~ m@^$pattern@) {
310 my $s1 = ($file =~ tr@/@@);
311 my $s2 = ($pattern =~ tr@/@@);
312 if ($s1 == $s2) {
313 return 1;
314 }
315 }
316 }
317 return 0;
318}
319
320sub usage {
321 print <<EOT;
322usage: $P [options] patchfile
Joe Perches870020f2009-07-29 15:04:28 -0700323 $P [options] -f file|directory
Joe Perchescb7301c2009-04-07 20:40:12 -0700324version: $V
325
326MAINTAINER field selection options:
327 --email => print email address(es) if any
328 --git => include recent git \*-by: signers
329 --git-chief-penguins => include ${penguin_chiefs}
330 --git-min-signatures => number of signatures required (default: 1)
331 --git-max-maintainers => maximum maintainers to add (default: 5)
Joe Perches3d202ae2009-07-29 15:04:29 -0700332 --git-min-percent => minimum percentage of commits required (default: 5)
Joe Perchescb7301c2009-04-07 20:40:12 -0700333 --git-since => git history to use (default: 1-year-ago)
Joe Perchesf5492662009-09-21 17:04:13 -0700334 --git-blame => use git blame to find modified commits for patch or file
Joe Perchescb7301c2009-04-07 20:40:12 -0700335 --m => include maintainer(s) if any
336 --n => include name 'Full Name <addr\@domain.tld>'
337 --l => include list(s) if any
338 --s => include subscriber only list(s) if any
339 --scm => print SCM tree(s) if any
340 --status => print status if any
341 --subsystem => print subsystem name if any
342 --web => print website(s) if any
343
344Output type options:
345 --separator [, ] => separator for multiple entries on 1 line
346 --multiline => print 1 entry per line
347
348Default options:
Joe Perches290603c12009-06-16 15:33:58 -0700349 [--email --git --m --n --l --multiline]
Joe Perchescb7301c2009-04-07 20:40:12 -0700350
351Other options:
Joe Perchesf5f50782009-06-16 15:34:00 -0700352 --version => show version
Joe Perchescb7301c2009-04-07 20:40:12 -0700353 --help => show this help information
354
Joe Perches870020f2009-07-29 15:04:28 -0700355Notes:
356 Using "-f directory" may give unexpected results:
Joe Perchesf5492662009-09-21 17:04:13 -0700357 Used with "--git", git signators for _all_ files in and below
358 directory are examined as git recurses directories.
359 Any specified X: (exclude) pattern matches are _not_ ignored.
360 Used with "--nogit", directory is used as a pattern match,
361 no individual file within the directory or subdirectory
362 is matched.
363 Used with "--git-blame", does not iterate all files in directory
364 Using "--git-blame" is slow and may add old committers and authors
365 that are no longer active maintainers to the output.
Joe Perchescb7301c2009-04-07 20:40:12 -0700366EOT
367}
368
369sub top_of_kernel_tree {
370 my ($lk_path) = @_;
371
372 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
373 $lk_path .= "/";
374 }
375 if ( (-f "${lk_path}COPYING")
376 && (-f "${lk_path}CREDITS")
377 && (-f "${lk_path}Kbuild")
378 && (-f "${lk_path}MAINTAINERS")
379 && (-f "${lk_path}Makefile")
380 && (-f "${lk_path}README")
381 && (-d "${lk_path}Documentation")
382 && (-d "${lk_path}arch")
383 && (-d "${lk_path}include")
384 && (-d "${lk_path}drivers")
385 && (-d "${lk_path}fs")
386 && (-d "${lk_path}init")
387 && (-d "${lk_path}ipc")
388 && (-d "${lk_path}kernel")
389 && (-d "${lk_path}lib")
390 && (-d "${lk_path}scripts")) {
391 return 1;
392 }
393 return 0;
394}
395
396sub format_email {
397 my ($name, $email) = @_;
398
399 $name =~ s/^\s+|\s+$//g;
Joe Perchesd7895042009-06-16 15:34:02 -0700400 $name =~ s/^\"|\"$//g;
Joe Perchescb7301c2009-04-07 20:40:12 -0700401 $email =~ s/^\s+|\s+$//g;
402
403 my $formatted_email = "";
404
405 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
406 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
407 $formatted_email = "\"${name}\"\ \<${email}\>";
408 } else {
409 $formatted_email = "${name} \<${email}\>";
410 }
411 return $formatted_email;
412}
413
414sub add_categories {
415 my ($index) = @_;
416
417 $index = $index - 1;
418 while ($index >= 0) {
419 my $tv = $typevalue[$index];
Joe Perches290603c12009-06-16 15:33:58 -0700420 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700421 my $ptype = $1;
422 my $pvalue = $2;
423 if ($ptype eq "L") {
Joe Perches290603c12009-06-16 15:33:58 -0700424 my $list_address = $pvalue;
425 my $list_additional = "";
426 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
427 $list_address = $1;
428 $list_additional = $2;
429 }
Joe Perchesbdf7c682009-06-16 15:33:59 -0700430 if ($list_additional =~ m/subscribers-only/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700431 if ($email_subscriber_list) {
Joe Perches290603c12009-06-16 15:33:58 -0700432 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700433 }
434 } else {
435 if ($email_list) {
Joe Perches290603c12009-06-16 15:33:58 -0700436 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700437 }
438 }
439 } elsif ($ptype eq "M") {
Joe Perches5f2441e2009-06-16 15:34:02 -0700440 my $p_used = 0;
441 if ($index >= 0) {
442 my $tv = $typevalue[$index - 1];
443 if ($tv =~ m/^(\C):\s*(.*)/) {
444 if ($1 eq "P") {
445 if ($email_usename) {
446 push_email_address(format_email($2, $pvalue));
447 $p_used = 1;
448 }
449 }
450 }
451 }
452 if (!$p_used) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700453 push_email_addresses($pvalue);
Joe Perchescb7301c2009-04-07 20:40:12 -0700454 }
455 } elsif ($ptype eq "T") {
456 push(@scm, $pvalue);
457 } elsif ($ptype eq "W") {
458 push(@web, $pvalue);
459 } elsif ($ptype eq "S") {
460 push(@status, $pvalue);
461 }
462
463 $index--;
464 } else {
465 push(@subsystem,$tv);
466 $index = -1;
467 }
468 }
469}
470
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700471sub push_email_address {
472 my ($email_address) = @_;
473
474 my $email_name = "";
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700475
Joe Perches0a79c492009-06-16 15:34:03 -0700476 if ($email_maintainer) {
Joe Perchesf5492662009-09-21 17:04:13 -0700477 if ($email_address =~ m/([^<]+)<(.*\@.*)>$/) {
478 $email_name = $1;
479 $email_address = $2;
480 if ($email_usename) {
481 push(@email_to, format_email($email_name, $email_address));
482 } else {
483 push(@email_to, $email_address);
484 }
485 } elsif ($email_address =~ m/<(.+)>/) {
486 $email_address = $1;
487 push(@email_to, $email_address);
Joe Perches0a79c492009-06-16 15:34:03 -0700488 } else {
489 push(@email_to, $email_address);
490 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700491 }
492}
493
494sub push_email_addresses {
495 my ($address) = @_;
496
497 my @address_list = ();
498
Joe Perches5f2441e2009-06-16 15:34:02 -0700499 if (rfc822_valid($address)) {
500 push_email_address($address);
501 } elsif (@address_list = rfc822_validlist($address)) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700502 my $array_count = shift(@address_list);
503 while (my $entry = shift(@address_list)) {
504 push_email_address($entry);
505 }
Joe Perches5f2441e2009-06-16 15:34:02 -0700506 } else {
507 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700508 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700509}
510
Joe Perchescb7301c2009-04-07 20:40:12 -0700511sub which {
512 my ($bin) = @_;
513
Joe Perchesf5f50782009-06-16 15:34:00 -0700514 foreach my $path (split(/:/, $ENV{PATH})) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700515 if (-e "$path/$bin") {
516 return "$path/$bin";
517 }
518 }
519
520 return "";
521}
522
523sub recent_git_signoffs {
524 my ($file) = @_;
525
526 my $sign_offs = "";
527 my $cmd = "";
528 my $output = "";
529 my $count = 0;
530 my @lines = ();
Joe Perchesafa81ee2009-07-29 15:04:28 -0700531 my $total_sign_offs;
Joe Perchescb7301c2009-04-07 20:40:12 -0700532
533 if (which("git") eq "") {
Joe Perchesde2fc492009-06-16 15:34:01 -0700534 warn("$P: git not found. Add --nogit to options?\n");
535 return;
536 }
537 if (!(-d ".git")) {
Joe Perches5f2441e2009-06-16 15:34:02 -0700538 warn("$P: .git directory not found. Use a git repository for better results.\n");
539 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 -0700540 return;
Joe Perchescb7301c2009-04-07 20:40:12 -0700541 }
542
543 $cmd = "git log --since=${email_git_since} -- ${file}";
Joe Perchesde2fc492009-06-16 15:34:01 -0700544 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
545 if (!$email_git_penguin_chiefs) {
546 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
547 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700548 $cmd .= " | cut -f2- -d\":\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700549 $cmd .= " | sort | uniq -c | sort -rn";
550
551 $output = `${cmd}`;
552 $output =~ s/^\s*//gm;
553
554 @lines = split("\n", $output);
Joe Perchesafa81ee2009-07-29 15:04:28 -0700555
556 $total_sign_offs = 0;
557 foreach my $line (@lines) {
558 if ($line =~ m/([0-9]+)\s+(.*)/) {
559 $total_sign_offs += $1;
560 } else {
561 die("$P: Unexpected git output: ${line}\n");
562 }
563 }
564
Joe Perchescb7301c2009-04-07 20:40:12 -0700565 foreach my $line (@lines) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700566 if ($line =~ m/([0-9]+)\s+(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700567 my $sign_offs = $1;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700568 $line = $2;
Joe Perchescb7301c2009-04-07 20:40:12 -0700569 $count++;
570 if ($sign_offs < $email_git_min_signatures ||
Joe Perchesafa81ee2009-07-29 15:04:28 -0700571 $count > $email_git_max_maintainers ||
572 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700573 last;
574 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700575 }
Joe Perchesf5492662009-09-21 17:04:13 -0700576 push_email_address($line);
577 }
578}
579
580sub save_commits {
581 my ($cmd, @commits) = @_;
582 my $output;
583 my @lines = ();
584
585 $output = `${cmd}`;
586
587 @lines = split("\n", $output);
588 foreach my $line (@lines) {
589 if ($line =~ m/^(\w+) /) {
590 push (@commits, $1);
Joe Perchescb7301c2009-04-07 20:40:12 -0700591 }
592 }
Joe Perchesf5492662009-09-21 17:04:13 -0700593 return @commits;
594}
595
596sub git_assign_blame {
597 my ($file) = @_;
598
599 my @lines = ();
600 my @commits = ();
601 my $cmd;
602 my $output;
603 my %hash;
604 my $total_sign_offs;
605 my $count;
606
607 if (@range) {
608 foreach my $file_range_diff (@range) {
609 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
610 my $diff_file = $1;
611 my $diff_start = $2;
612 my $diff_length = $3;
613 next if (!("$file" eq "$diff_file"));
614 $cmd = "git blame -l -L $diff_start,+$diff_length $file\n";
615 @commits = save_commits($cmd, @commits);
616 }
617 } else {
618 if (-f $file) {
619 $cmd = "git blame -l $file\n";
620 @commits = save_commits($cmd, @commits);
621 }
622 }
623
624 $total_sign_offs = 0;
625 @commits = uniq(@commits);
626 foreach my $commit (@commits) {
627 $cmd = "git log -1 ${commit}";
628 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
629 if (!$email_git_penguin_chiefs) {
630 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
631 }
632 $cmd .= " | cut -f2- -d\":\"";
633
634 $output = `${cmd}`;
635 $output =~ s/^\s*//gm;
636 @lines = split("\n", $output);
637 $hash{$_}++ for @lines;
638 $total_sign_offs += @lines;
639 }
640
641 $count = 0;
642 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
643 my $sign_offs = $hash{$line};
644 $count++;
645 last if ($sign_offs < $email_git_min_signatures ||
646 $count > $email_git_max_maintainers ||
647 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
648 push_email_address($line);
649 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700650}
651
652sub uniq {
653 my @parms = @_;
654
655 my %saw;
656 @parms = grep(!$saw{$_}++, @parms);
657 return @parms;
658}
659
660sub sort_and_uniq {
661 my @parms = @_;
662
663 my %saw;
664 @parms = sort @parms;
665 @parms = grep(!$saw{$_}++, @parms);
666 return @parms;
667}
668
669sub output {
670 my @parms = @_;
671
672 if ($output_multiline) {
673 foreach my $line (@parms) {
674 print("${line}\n");
675 }
676 } else {
677 print(join($output_separator, @parms));
678 print("\n");
679 }
680}
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700681
682my $rfc822re;
683
684sub make_rfc822re {
685# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
686# comment. We must allow for rfc822_lwsp (or comments) after each of these.
687# This regexp will only work on addresses which have had comments stripped
688# and replaced with rfc822_lwsp.
689
690 my $specials = '()<>@,;:\\\\".\\[\\]';
691 my $controls = '\\000-\\037\\177';
692
693 my $dtext = "[^\\[\\]\\r\\\\]";
694 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
695
696 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
697
698# Use zero-width assertion to spot the limit of an atom. A simple
699# $rfc822_lwsp* causes the regexp engine to hang occasionally.
700 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
701 my $word = "(?:$atom|$quoted_string)";
702 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
703
704 my $sub_domain = "(?:$atom|$domain_literal)";
705 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
706
707 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
708
709 my $phrase = "$word*";
710 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
711 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
712 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
713
714 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
715 my $address = "(?:$mailbox|$group)";
716
717 return "$rfc822_lwsp*$address";
718}
719
720sub rfc822_strip_comments {
721 my $s = shift;
722# Recursively remove comments, and replace with a single space. The simpler
723# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
724# chars in atoms, for example.
725
726 while ($s =~ s/^((?:[^"\\]|\\.)*
727 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
728 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
729 return $s;
730}
731
732# valid: returns true if the parameter is an RFC822 valid address
733#
734sub rfc822_valid ($) {
735 my $s = rfc822_strip_comments(shift);
736
737 if (!$rfc822re) {
738 $rfc822re = make_rfc822re();
739 }
740
741 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
742}
743
744# validlist: In scalar context, returns true if the parameter is an RFC822
745# valid list of addresses.
746#
747# In list context, returns an empty list on failure (an invalid
748# address was found); otherwise a list whose first element is the
749# number of addresses found and whose remaining elements are the
750# addresses. This is needed to disambiguate failure (invalid)
751# from success with no addresses found, because an empty string is
752# a valid list.
753
754sub rfc822_validlist ($) {
755 my $s = rfc822_strip_comments(shift);
756
757 if (!$rfc822re) {
758 $rfc822re = make_rfc822re();
759 }
760 # * null list items are valid according to the RFC
761 # * the '1' business is to aid in distinguishing failure from no results
762
763 my @r;
764 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
765 $s =~ m/^$rfc822_char*$/) {
Joe Perches5f2441e2009-06-16 15:34:02 -0700766 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700767 push @r, $1;
768 }
769 return wantarray ? (scalar(@r), @r) : 1;
770 }
771 else {
772 return wantarray ? () : 0;
773 }
774}