blob: 35781e0d43e6e245cfbe285ef48a02d1898e6c1e [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;
214 }
215 }
216 }
217 }
218
219 if (!$exclude) {
220 my $tvi = 0;
221 foreach my $line (@typevalue) {
Joe Perches290603c12009-06-16 15:33:58 -0700222 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700223 my $type = $1;
224 my $value = $2;
225 if ($type eq 'F') {
226 if (file_match_pattern($file, $value)) {
227 add_categories($tvi);
228 }
229 }
230 }
231 $tvi++;
232 }
233 }
234
Joe Perches4a7fdb52009-04-10 12:28:57 -0700235 if ($email && $email_git) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700236 recent_git_signoffs($file);
237 }
238
Joe Perchesf5492662009-09-21 17:04:13 -0700239 if ($email && $email_git_blame) {
240 git_assign_blame($file);
241 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700242}
243
Joe Perchesf5f5078d2009-06-16 15:34:00 -0700244if ($email) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700245 foreach my $chief (@penguin_chief) {
246 if ($chief =~ m/^(.*):(.*)/) {
Joe Perchesf5f5078d2009-06-16 15:34:00 -0700247 my $email_address;
Joe Perchescb7301c2009-04-07 20:40:12 -0700248 if ($email_usename) {
Joe Perchesf5f5078d2009-06-16 15:34:00 -0700249 $email_address = format_email($1, $2);
Joe Perchescb7301c2009-04-07 20:40:12 -0700250 } else {
Joe Perchesf5f5078d2009-06-16 15:34:00 -0700251 $email_address = $2;
252 }
253 if ($email_git_penguin_chiefs) {
254 push(@email_to, $email_address);
255 } else {
256 @email_to = grep(!/${email_address}/, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700257 }
258 }
259 }
260}
261
Joe Perches290603c12009-06-16 15:33:58 -0700262if ($email || $email_list) {
263 my @to = ();
264 if ($email) {
265 @to = (@to, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700266 }
Joe Perches290603c12009-06-16 15:33:58 -0700267 if ($email_list) {
Joe Perches290603c12009-06-16 15:33:58 -0700268 @to = (@to, @list_to);
Joe Perches290603c12009-06-16 15:33:58 -0700269 }
270 output(uniq(@to));
Joe Perchescb7301c2009-04-07 20:40:12 -0700271}
272
273if ($scm) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700274 @scm = sort_and_uniq(@scm);
Joe Perchescb7301c2009-04-07 20:40:12 -0700275 output(@scm);
276}
277
278if ($status) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700279 @status = sort_and_uniq(@status);
Joe Perchescb7301c2009-04-07 20:40:12 -0700280 output(@status);
281}
282
283if ($subsystem) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700284 @subsystem = sort_and_uniq(@subsystem);
Joe Perchescb7301c2009-04-07 20:40:12 -0700285 output(@subsystem);
286}
287
288if ($web) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700289 @web = sort_and_uniq(@web);
Joe Perchescb7301c2009-04-07 20:40:12 -0700290 output(@web);
291}
292
293exit($exit);
294
295sub file_match_pattern {
296 my ($file, $pattern) = @_;
297 if (substr($pattern, -1) eq "/") {
298 if ($file =~ m@^$pattern@) {
299 return 1;
300 }
301 } else {
302 if ($file =~ m@^$pattern@) {
303 my $s1 = ($file =~ tr@/@@);
304 my $s2 = ($pattern =~ tr@/@@);
305 if ($s1 == $s2) {
306 return 1;
307 }
308 }
309 }
310 return 0;
311}
312
313sub usage {
314 print <<EOT;
315usage: $P [options] patchfile
Joe Perches870020f2009-07-29 15:04:28 -0700316 $P [options] -f file|directory
Joe Perchescb7301c2009-04-07 20:40:12 -0700317version: $V
318
319MAINTAINER field selection options:
320 --email => print email address(es) if any
321 --git => include recent git \*-by: signers
322 --git-chief-penguins => include ${penguin_chiefs}
323 --git-min-signatures => number of signatures required (default: 1)
324 --git-max-maintainers => maximum maintainers to add (default: 5)
Joe Perches3d202ae2009-07-29 15:04:29 -0700325 --git-min-percent => minimum percentage of commits required (default: 5)
Joe Perchescb7301c2009-04-07 20:40:12 -0700326 --git-since => git history to use (default: 1-year-ago)
Joe Perchesf5492662009-09-21 17:04:13 -0700327 --git-blame => use git blame to find modified commits for patch or file
Joe Perchescb7301c2009-04-07 20:40:12 -0700328 --m => include maintainer(s) if any
329 --n => include name 'Full Name <addr\@domain.tld>'
330 --l => include list(s) if any
331 --s => include subscriber only list(s) if any
332 --scm => print SCM tree(s) if any
333 --status => print status if any
334 --subsystem => print subsystem name if any
335 --web => print website(s) if any
336
337Output type options:
338 --separator [, ] => separator for multiple entries on 1 line
339 --multiline => print 1 entry per line
340
341Default options:
Joe Perches290603c12009-06-16 15:33:58 -0700342 [--email --git --m --n --l --multiline]
Joe Perchescb7301c2009-04-07 20:40:12 -0700343
344Other options:
Joe Perchesf5f5078d2009-06-16 15:34:00 -0700345 --version => show version
Joe Perchescb7301c2009-04-07 20:40:12 -0700346 --help => show this help information
347
Joe Perches870020f2009-07-29 15:04:28 -0700348Notes:
349 Using "-f directory" may give unexpected results:
Joe Perchesf5492662009-09-21 17:04:13 -0700350 Used with "--git", git signators for _all_ files in and below
351 directory are examined as git recurses directories.
352 Any specified X: (exclude) pattern matches are _not_ ignored.
353 Used with "--nogit", directory is used as a pattern match,
354 no individual file within the directory or subdirectory
355 is matched.
356 Used with "--git-blame", does not iterate all files in directory
357 Using "--git-blame" is slow and may add old committers and authors
358 that are no longer active maintainers to the output.
Joe Perchescb7301c2009-04-07 20:40:12 -0700359EOT
360}
361
362sub top_of_kernel_tree {
363 my ($lk_path) = @_;
364
365 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
366 $lk_path .= "/";
367 }
368 if ( (-f "${lk_path}COPYING")
369 && (-f "${lk_path}CREDITS")
370 && (-f "${lk_path}Kbuild")
371 && (-f "${lk_path}MAINTAINERS")
372 && (-f "${lk_path}Makefile")
373 && (-f "${lk_path}README")
374 && (-d "${lk_path}Documentation")
375 && (-d "${lk_path}arch")
376 && (-d "${lk_path}include")
377 && (-d "${lk_path}drivers")
378 && (-d "${lk_path}fs")
379 && (-d "${lk_path}init")
380 && (-d "${lk_path}ipc")
381 && (-d "${lk_path}kernel")
382 && (-d "${lk_path}lib")
383 && (-d "${lk_path}scripts")) {
384 return 1;
385 }
386 return 0;
387}
388
389sub format_email {
390 my ($name, $email) = @_;
391
392 $name =~ s/^\s+|\s+$//g;
Joe Perchesd7895042009-06-16 15:34:02 -0700393 $name =~ s/^\"|\"$//g;
Joe Perchescb7301c2009-04-07 20:40:12 -0700394 $email =~ s/^\s+|\s+$//g;
395
396 my $formatted_email = "";
397
398 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
399 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
400 $formatted_email = "\"${name}\"\ \<${email}\>";
401 } else {
402 $formatted_email = "${name} \<${email}\>";
403 }
404 return $formatted_email;
405}
406
407sub add_categories {
408 my ($index) = @_;
409
410 $index = $index - 1;
411 while ($index >= 0) {
412 my $tv = $typevalue[$index];
Joe Perches290603c12009-06-16 15:33:58 -0700413 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700414 my $ptype = $1;
415 my $pvalue = $2;
416 if ($ptype eq "L") {
Joe Perches290603c12009-06-16 15:33:58 -0700417 my $list_address = $pvalue;
418 my $list_additional = "";
419 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
420 $list_address = $1;
421 $list_additional = $2;
422 }
Joe Perchesbdf7c682009-06-16 15:33:59 -0700423 if ($list_additional =~ m/subscribers-only/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700424 if ($email_subscriber_list) {
Joe Perches290603c12009-06-16 15:33:58 -0700425 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700426 }
427 } else {
428 if ($email_list) {
Joe Perches290603c12009-06-16 15:33:58 -0700429 push(@list_to, $list_address);
Joe Perchescb7301c2009-04-07 20:40:12 -0700430 }
431 }
432 } elsif ($ptype eq "M") {
Joe Perches5f2441e2009-06-16 15:34:02 -0700433 my $p_used = 0;
434 if ($index >= 0) {
435 my $tv = $typevalue[$index - 1];
436 if ($tv =~ m/^(\C):\s*(.*)/) {
437 if ($1 eq "P") {
438 if ($email_usename) {
439 push_email_address(format_email($2, $pvalue));
440 $p_used = 1;
441 }
442 }
443 }
444 }
445 if (!$p_used) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700446 push_email_addresses($pvalue);
Joe Perchescb7301c2009-04-07 20:40:12 -0700447 }
448 } elsif ($ptype eq "T") {
449 push(@scm, $pvalue);
450 } elsif ($ptype eq "W") {
451 push(@web, $pvalue);
452 } elsif ($ptype eq "S") {
453 push(@status, $pvalue);
454 }
455
456 $index--;
457 } else {
458 push(@subsystem,$tv);
459 $index = -1;
460 }
461 }
462}
463
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700464sub push_email_address {
465 my ($email_address) = @_;
466
467 my $email_name = "";
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700468
Joe Perches0a79c492009-06-16 15:34:03 -0700469 if ($email_maintainer) {
Joe Perchesf5492662009-09-21 17:04:13 -0700470 if ($email_address =~ m/([^<]+)<(.*\@.*)>$/) {
471 $email_name = $1;
472 $email_address = $2;
473 if ($email_usename) {
474 push(@email_to, format_email($email_name, $email_address));
475 } else {
476 push(@email_to, $email_address);
477 }
478 } elsif ($email_address =~ m/<(.+)>/) {
479 $email_address = $1;
480 push(@email_to, $email_address);
Joe Perches0a79c492009-06-16 15:34:03 -0700481 } else {
482 push(@email_to, $email_address);
483 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700484 }
485}
486
487sub push_email_addresses {
488 my ($address) = @_;
489
490 my @address_list = ();
491
Joe Perches5f2441e2009-06-16 15:34:02 -0700492 if (rfc822_valid($address)) {
493 push_email_address($address);
494 } elsif (@address_list = rfc822_validlist($address)) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700495 my $array_count = shift(@address_list);
496 while (my $entry = shift(@address_list)) {
497 push_email_address($entry);
498 }
Joe Perches5f2441e2009-06-16 15:34:02 -0700499 } else {
500 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700501 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700502}
503
Joe Perchescb7301c2009-04-07 20:40:12 -0700504sub which {
505 my ($bin) = @_;
506
Joe Perchesf5f5078d2009-06-16 15:34:00 -0700507 foreach my $path (split(/:/, $ENV{PATH})) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700508 if (-e "$path/$bin") {
509 return "$path/$bin";
510 }
511 }
512
513 return "";
514}
515
516sub recent_git_signoffs {
517 my ($file) = @_;
518
519 my $sign_offs = "";
520 my $cmd = "";
521 my $output = "";
522 my $count = 0;
523 my @lines = ();
Joe Perchesafa81ee2009-07-29 15:04:28 -0700524 my $total_sign_offs;
Joe Perchescb7301c2009-04-07 20:40:12 -0700525
526 if (which("git") eq "") {
Joe Perchesde2fc492009-06-16 15:34:01 -0700527 warn("$P: git not found. Add --nogit to options?\n");
528 return;
529 }
530 if (!(-d ".git")) {
Joe Perches5f2441e2009-06-16 15:34:02 -0700531 warn("$P: .git directory not found. Use a git repository for better results.\n");
532 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 -0700533 return;
Joe Perchescb7301c2009-04-07 20:40:12 -0700534 }
535
536 $cmd = "git log --since=${email_git_since} -- ${file}";
Joe Perchesde2fc492009-06-16 15:34:01 -0700537 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
538 if (!$email_git_penguin_chiefs) {
539 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
540 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700541 $cmd .= " | cut -f2- -d\":\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700542 $cmd .= " | sort | uniq -c | sort -rn";
543
544 $output = `${cmd}`;
545 $output =~ s/^\s*//gm;
546
547 @lines = split("\n", $output);
Joe Perchesafa81ee2009-07-29 15:04:28 -0700548
549 $total_sign_offs = 0;
550 foreach my $line (@lines) {
551 if ($line =~ m/([0-9]+)\s+(.*)/) {
552 $total_sign_offs += $1;
553 } else {
554 die("$P: Unexpected git output: ${line}\n");
555 }
556 }
557
Joe Perchescb7301c2009-04-07 20:40:12 -0700558 foreach my $line (@lines) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700559 if ($line =~ m/([0-9]+)\s+(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700560 my $sign_offs = $1;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700561 $line = $2;
Joe Perchescb7301c2009-04-07 20:40:12 -0700562 $count++;
563 if ($sign_offs < $email_git_min_signatures ||
Joe Perchesafa81ee2009-07-29 15:04:28 -0700564 $count > $email_git_max_maintainers ||
565 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700566 last;
567 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700568 }
Joe Perchesf5492662009-09-21 17:04:13 -0700569 push_email_address($line);
570 }
571}
572
573sub save_commits {
574 my ($cmd, @commits) = @_;
575 my $output;
576 my @lines = ();
577
578 $output = `${cmd}`;
579
580 @lines = split("\n", $output);
581 foreach my $line (@lines) {
582 if ($line =~ m/^(\w+) /) {
583 push (@commits, $1);
Joe Perchescb7301c2009-04-07 20:40:12 -0700584 }
585 }
Joe Perchesf5492662009-09-21 17:04:13 -0700586 return @commits;
587}
588
589sub git_assign_blame {
590 my ($file) = @_;
591
592 my @lines = ();
593 my @commits = ();
594 my $cmd;
595 my $output;
596 my %hash;
597 my $total_sign_offs;
598 my $count;
599
600 if (@range) {
601 foreach my $file_range_diff (@range) {
602 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
603 my $diff_file = $1;
604 my $diff_start = $2;
605 my $diff_length = $3;
606 next if (!("$file" eq "$diff_file"));
607 $cmd = "git blame -l -L $diff_start,+$diff_length $file\n";
608 @commits = save_commits($cmd, @commits);
609 }
610 } else {
611 if (-f $file) {
612 $cmd = "git blame -l $file\n";
613 @commits = save_commits($cmd, @commits);
614 }
615 }
616
617 $total_sign_offs = 0;
618 @commits = uniq(@commits);
619 foreach my $commit (@commits) {
620 $cmd = "git log -1 ${commit}";
621 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
622 if (!$email_git_penguin_chiefs) {
623 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
624 }
625 $cmd .= " | cut -f2- -d\":\"";
626
627 $output = `${cmd}`;
628 $output =~ s/^\s*//gm;
629 @lines = split("\n", $output);
630 $hash{$_}++ for @lines;
631 $total_sign_offs += @lines;
632 }
633
634 $count = 0;
635 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
636 my $sign_offs = $hash{$line};
637 $count++;
638 last if ($sign_offs < $email_git_min_signatures ||
639 $count > $email_git_max_maintainers ||
640 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
641 push_email_address($line);
642 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700643}
644
645sub uniq {
646 my @parms = @_;
647
648 my %saw;
649 @parms = grep(!$saw{$_}++, @parms);
650 return @parms;
651}
652
653sub sort_and_uniq {
654 my @parms = @_;
655
656 my %saw;
657 @parms = sort @parms;
658 @parms = grep(!$saw{$_}++, @parms);
659 return @parms;
660}
661
662sub output {
663 my @parms = @_;
664
665 if ($output_multiline) {
666 foreach my $line (@parms) {
667 print("${line}\n");
668 }
669 } else {
670 print(join($output_separator, @parms));
671 print("\n");
672 }
673}
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700674
675my $rfc822re;
676
677sub make_rfc822re {
678# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
679# comment. We must allow for rfc822_lwsp (or comments) after each of these.
680# This regexp will only work on addresses which have had comments stripped
681# and replaced with rfc822_lwsp.
682
683 my $specials = '()<>@,;:\\\\".\\[\\]';
684 my $controls = '\\000-\\037\\177';
685
686 my $dtext = "[^\\[\\]\\r\\\\]";
687 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
688
689 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
690
691# Use zero-width assertion to spot the limit of an atom. A simple
692# $rfc822_lwsp* causes the regexp engine to hang occasionally.
693 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
694 my $word = "(?:$atom|$quoted_string)";
695 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
696
697 my $sub_domain = "(?:$atom|$domain_literal)";
698 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
699
700 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
701
702 my $phrase = "$word*";
703 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
704 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
705 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
706
707 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
708 my $address = "(?:$mailbox|$group)";
709
710 return "$rfc822_lwsp*$address";
711}
712
713sub rfc822_strip_comments {
714 my $s = shift;
715# Recursively remove comments, and replace with a single space. The simpler
716# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
717# chars in atoms, for example.
718
719 while ($s =~ s/^((?:[^"\\]|\\.)*
720 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
721 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
722 return $s;
723}
724
725# valid: returns true if the parameter is an RFC822 valid address
726#
727sub rfc822_valid ($) {
728 my $s = rfc822_strip_comments(shift);
729
730 if (!$rfc822re) {
731 $rfc822re = make_rfc822re();
732 }
733
734 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
735}
736
737# validlist: In scalar context, returns true if the parameter is an RFC822
738# valid list of addresses.
739#
740# In list context, returns an empty list on failure (an invalid
741# address was found); otherwise a list whose first element is the
742# number of addresses found and whose remaining elements are the
743# addresses. This is needed to disambiguate failure (invalid)
744# from success with no addresses found, because an empty string is
745# a valid list.
746
747sub rfc822_validlist ($) {
748 my $s = rfc822_strip_comments(shift);
749
750 if (!$rfc822re) {
751 $rfc822re = make_rfc822re();
752 }
753 # * null list items are valid according to the RFC
754 # * the '1' business is to aid in distinguishing failure from no results
755
756 my @r;
757 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
758 $s =~ m/^$rfc822_char*$/) {
Joe Perches5f2441e2009-06-16 15:34:02 -0700759 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700760 push @r, $1;
761 }
762 return wantarray ? (scalar(@r), @r) : 1;
763 }
764 else {
765 return wantarray ? () : 0;
766 }
767}