]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - scripts/get_maintainer.pl
scripts/get_maintainer.pl: better email routines, use perl not shell where possible
[mirror_ubuntu-bionic-kernel.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;
0e70e83d 16my $V = '0.20';
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;
afa81ee1 30my $email_git_min_percent = 5;
cb7301c7 31my $email_git_since = "1-year-ago";
f5492666 32my $email_git_blame = 0;
cb7301c7
JP
33my $output_multiline = 1;
34my $output_separator = ", ";
35my $scm = 0;
36my $web = 0;
37my $subsystem = 0;
38my $status = 0;
4a7fdb5f 39my $from_filename = 0;
3fb55652 40my $pattern_depth = 0;
cb7301c7
JP
41my $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
5f2441e9 61# rfc822 email address - preloaded methods go here.
1b5e1cf6 62my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
df4cc036 63my $rfc822_char = '[\\000-\\377]';
1b5e1cf6 64
cb7301c7
JP
65if (!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,
afa81ee1 71 'git-min-percent=i' => \$email_git_min_percent,
cb7301c7 72 'git-since=s' => \$email_git_since,
f5492666 73 'git-blame!' => \$email_git_blame,
cb7301c7
JP
74 '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,
3fb55652 84 'pattern-depth=i' => \$pattern_depth,
4a7fdb5f 85 'f|file' => \$from_filename,
cb7301c7
JP
86 '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
cb7301c7
JP
103if ($#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
f5492666
JP
114if ($email &&
115 ($email_maintainer + $email_list + $email_subscriber_list +
116 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
cb7301c7
JP
117 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 .
870020f9
JP
142 ##if pattern is a directory and it lacks a trailing slash, add one
143 if ((-d $value)) {
144 $value =~ s@([^/])$@$1/@;
145 }
cb7301c7
JP
146 }
147 push(@typevalue, "$type:$value");
148 } elsif (!/^(\s)*$/) {
149 $line =~ s/\n$//g;
150 push(@typevalue, $line);
151 }
152}
153close(MAINT);
154
4a7fdb5f 155## use the filenames on the command line or find the filenames in the patchfiles
cb7301c7
JP
156
157my @files = ();
f5492666 158my @range = ();
cb7301c7 159
4a7fdb5f 160foreach my $file (@ARGV) {
870020f9
JP
161 ##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)) {
4a7fdb5f 165 die "$P: file '${file}' not found\n";
cb7301c7 166 }
4a7fdb5f
JP
167 if ($from_filename) {
168 push(@files, $file);
169 } else {
170 my $file_cnt = @files;
f5492666 171 my $lastfile;
4a7fdb5f
JP
172 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@@;
f5492666 178 $lastfile = $filename;
4a7fdb5f 179 push(@files, $filename);
f5492666
JP
180 } elsif (m/^\@\@ -(\d+),(\d+)/) {
181 if ($email_git_blame) {
182 push(@range, "$lastfile:$1:$2");
183 }
4a7fdb5f 184 }
cb7301c7 185 }
4a7fdb5f
JP
186 close(PATCH);
187 if ($file_cnt == @files) {
7f29fd27 188 warn "$P: file '${file}' doesn't appear to be a patch. "
4a7fdb5f
JP
189 . "Add -f to options?\n";
190 }
191 @files = sort_and_uniq(@files);
cb7301c7 192 }
cb7301c7
JP
193}
194
195my @email_to = ();
290603c1 196my @list_to = ();
cb7301c7
JP
197my @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) {
290603c1 210 if ($line =~ m/^(\C):\s*(.*)/) {
cb7301c7
JP
211 my $type = $1;
212 my $value = $2;
213 if ($type eq 'X') {
214 if (file_match_pattern($file, $value)) {
215 $exclude = 1;
1d606b4e 216 last;
cb7301c7
JP
217 }
218 }
219 }
220 }
221
222 if (!$exclude) {
223 my $tvi = 0;
1d606b4e 224 my %hash;
cb7301c7 225 foreach my $line (@typevalue) {
290603c1 226 if ($line =~ m/^(\C):\s*(.*)/) {
cb7301c7
JP
227 my $type = $1;
228 my $value = $2;
229 if ($type eq 'F') {
230 if (file_match_pattern($file, $value)) {
3fb55652
JP
231 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 }
cb7301c7
JP
238 }
239 }
240 }
241 $tvi++;
242 }
1d606b4e
JP
243 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
244 add_categories($line);
245 }
cb7301c7
JP
246 }
247
4a7fdb5f 248 if ($email && $email_git) {
cb7301c7
JP
249 recent_git_signoffs($file);
250 }
251
f5492666
JP
252 if ($email && $email_git_blame) {
253 git_assign_blame($file);
254 }
cb7301c7
JP
255}
256
f5f5078d 257if ($email) {
cb7301c7
JP
258 foreach my $chief (@penguin_chief) {
259 if ($chief =~ m/^(.*):(.*)/) {
f5f5078d 260 my $email_address;
0e70e83d
JP
261
262 $email_address = format_email($1, $2);
f5f5078d
JP
263 if ($email_git_penguin_chiefs) {
264 push(@email_to, $email_address);
265 } else {
266 @email_to = grep(!/${email_address}/, @email_to);
cb7301c7
JP
267 }
268 }
269 }
270}
271
290603c1
JP
272if ($email || $email_list) {
273 my @to = ();
274 if ($email) {
275 @to = (@to, @email_to);
cb7301c7 276 }
290603c1 277 if ($email_list) {
290603c1 278 @to = (@to, @list_to);
290603c1
JP
279 }
280 output(uniq(@to));
cb7301c7
JP
281}
282
283if ($scm) {
4a7fdb5f 284 @scm = sort_and_uniq(@scm);
cb7301c7
JP
285 output(@scm);
286}
287
288if ($status) {
4a7fdb5f 289 @status = sort_and_uniq(@status);
cb7301c7
JP
290 output(@status);
291}
292
293if ($subsystem) {
4a7fdb5f 294 @subsystem = sort_and_uniq(@subsystem);
cb7301c7
JP
295 output(@subsystem);
296}
297
298if ($web) {
4a7fdb5f 299 @web = sort_and_uniq(@web);
cb7301c7
JP
300 output(@web);
301}
302
303exit($exit);
304
305sub file_match_pattern {
306 my ($file, $pattern) = @_;
307 if (substr($pattern, -1) eq "/") {
308 if ($file =~ m@^$pattern@) {
309 return 1;
310 }
311 } else {
312 if ($file =~ m@^$pattern@) {
313 my $s1 = ($file =~ tr@/@@);
314 my $s2 = ($pattern =~ tr@/@@);
315 if ($s1 == $s2) {
316 return 1;
317 }
318 }
319 }
320 return 0;
321}
322
323sub usage {
324 print <<EOT;
325usage: $P [options] patchfile
870020f9 326 $P [options] -f file|directory
cb7301c7
JP
327version: $V
328
329MAINTAINER field selection options:
330 --email => print email address(es) if any
331 --git => include recent git \*-by: signers
332 --git-chief-penguins => include ${penguin_chiefs}
333 --git-min-signatures => number of signatures required (default: 1)
334 --git-max-maintainers => maximum maintainers to add (default: 5)
3d202aeb 335 --git-min-percent => minimum percentage of commits required (default: 5)
cb7301c7 336 --git-since => git history to use (default: 1-year-ago)
f5492666 337 --git-blame => use git blame to find modified commits for patch or file
cb7301c7
JP
338 --m => include maintainer(s) if any
339 --n => include name 'Full Name <addr\@domain.tld>'
340 --l => include list(s) if any
341 --s => include subscriber only list(s) if any
342 --scm => print SCM tree(s) if any
343 --status => print status if any
344 --subsystem => print subsystem name if any
345 --web => print website(s) if any
346
347Output type options:
348 --separator [, ] => separator for multiple entries on 1 line
349 --multiline => print 1 entry per line
350
cb7301c7 351Other options:
3fb55652 352 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
f5f5078d 353 --version => show version
cb7301c7
JP
354 --help => show this help information
355
3fb55652
JP
356Default options:
357 [--email --git --m --n --l --multiline --pattern-depth=0]
358
870020f9
JP
359Notes:
360 Using "-f directory" may give unexpected results:
f5492666
JP
361 Used with "--git", git signators for _all_ files in and below
362 directory are examined as git recurses directories.
363 Any specified X: (exclude) pattern matches are _not_ ignored.
364 Used with "--nogit", directory is used as a pattern match,
365 no individual file within the directory or subdirectory
366 is matched.
367 Used with "--git-blame", does not iterate all files in directory
368 Using "--git-blame" is slow and may add old committers and authors
369 that are no longer active maintainers to the output.
cb7301c7
JP
370EOT
371}
372
373sub top_of_kernel_tree {
374 my ($lk_path) = @_;
375
376 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
377 $lk_path .= "/";
378 }
379 if ( (-f "${lk_path}COPYING")
380 && (-f "${lk_path}CREDITS")
381 && (-f "${lk_path}Kbuild")
382 && (-f "${lk_path}MAINTAINERS")
383 && (-f "${lk_path}Makefile")
384 && (-f "${lk_path}README")
385 && (-d "${lk_path}Documentation")
386 && (-d "${lk_path}arch")
387 && (-d "${lk_path}include")
388 && (-d "${lk_path}drivers")
389 && (-d "${lk_path}fs")
390 && (-d "${lk_path}init")
391 && (-d "${lk_path}ipc")
392 && (-d "${lk_path}kernel")
393 && (-d "${lk_path}lib")
394 && (-d "${lk_path}scripts")) {
395 return 1;
396 }
397 return 0;
398}
399
0e70e83d
JP
400sub parse_email {
401 my ($formatted_email) = @_;
402
403 my $name = "";
404 my $address = "";
405
406 if ($formatted_email =~ /^([^<]+)<(.*\@.*)>$/) {
407 $name = $1;
408 $address = $2;
409 } elsif ($formatted_email =~ /^<(.*\@.*)>$/) {
410 $address = $1;
411 } elsif ($formatted_email =~ /^(.*\@.*)$/) {
412 $address = $1;
413 }
cb7301c7
JP
414
415 $name =~ s/^\s+|\s+$//g;
d789504a 416 $name =~ s/^\"|\"$//g;
0e70e83d 417 $address =~ s/^\s+|\s+$//g;
cb7301c7 418
0e70e83d
JP
419 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
420 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
421 $name = "\"$name\"";
422 }
423
424 return ($name, $address);
425}
426
427sub format_email {
428 my ($name, $address) = @_;
429
430 my $formatted_email;
431
432 $name =~ s/^\s+|\s+$//g;
433 $name =~ s/^\"|\"$//g;
434 $address =~ s/^\s+|\s+$//g;
cb7301c7
JP
435
436 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
437 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
0e70e83d
JP
438 $name = "\"$name\"";
439 }
440
441 if ($email_usename) {
442 if ("$name" eq "") {
443 $formatted_email = "$address";
444 } else {
445 $formatted_email = "$name <${address}>";
446 }
cb7301c7 447 } else {
0e70e83d 448 $formatted_email = $address;
cb7301c7 449 }
0e70e83d 450
cb7301c7
JP
451 return $formatted_email;
452}
453
454sub add_categories {
455 my ($index) = @_;
456
457 $index = $index - 1;
458 while ($index >= 0) {
459 my $tv = $typevalue[$index];
290603c1 460 if ($tv =~ m/^(\C):\s*(.*)/) {
cb7301c7
JP
461 my $ptype = $1;
462 my $pvalue = $2;
463 if ($ptype eq "L") {
290603c1
JP
464 my $list_address = $pvalue;
465 my $list_additional = "";
466 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
467 $list_address = $1;
468 $list_additional = $2;
469 }
bdf7c685 470 if ($list_additional =~ m/subscribers-only/) {
cb7301c7 471 if ($email_subscriber_list) {
290603c1 472 push(@list_to, $list_address);
cb7301c7
JP
473 }
474 } else {
475 if ($email_list) {
290603c1 476 push(@list_to, $list_address);
cb7301c7
JP
477 }
478 }
479 } elsif ($ptype eq "M") {
0e70e83d
JP
480 my ($name, $address) = parse_email($pvalue);
481 if ($name eq "") {
482 if ($index >= 0) {
483 my $tv = $typevalue[$index - 1];
484 if ($tv =~ m/^(\C):\s*(.*)/) {
485 if ($1 eq "P") {
486 $name = $2;
5f2441e9
JP
487 }
488 }
489 }
490 }
0e70e83d 491 if ($email_maintainer) {
1b5e1cf6 492 push_email_addresses($pvalue);
cb7301c7
JP
493 }
494 } elsif ($ptype eq "T") {
495 push(@scm, $pvalue);
496 } elsif ($ptype eq "W") {
497 push(@web, $pvalue);
498 } elsif ($ptype eq "S") {
499 push(@status, $pvalue);
500 }
501
502 $index--;
503 } else {
504 push(@subsystem,$tv);
505 $index = -1;
506 }
507 }
508}
509
0e70e83d
JP
510sub email_address_inuse {
511 my ($test_address) = @_;
512
513 foreach my $line (@email_to) {
514 my ($name, $address) = parse_email($line);
515
516 return 1 if ($address eq $test_address);
517 }
518 return 0;
519}
520
1b5e1cf6 521sub push_email_address {
0e70e83d 522 my ($line) = @_;
1b5e1cf6 523
0e70e83d 524 my ($name, $address) = parse_email($line);
1b5e1cf6 525
0e70e83d
JP
526 if (!email_address_inuse($address)) {
527 push(@email_to, format_email($name, $address));
1b5e1cf6
JP
528 }
529}
530
531sub push_email_addresses {
532 my ($address) = @_;
533
534 my @address_list = ();
535
5f2441e9
JP
536 if (rfc822_valid($address)) {
537 push_email_address($address);
538 } elsif (@address_list = rfc822_validlist($address)) {
1b5e1cf6
JP
539 my $array_count = shift(@address_list);
540 while (my $entry = shift(@address_list)) {
541 push_email_address($entry);
542 }
5f2441e9
JP
543 } else {
544 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
1b5e1cf6 545 }
1b5e1cf6
JP
546}
547
cb7301c7
JP
548sub which {
549 my ($bin) = @_;
550
f5f5078d 551 foreach my $path (split(/:/, $ENV{PATH})) {
cb7301c7
JP
552 if (-e "$path/$bin") {
553 return "$path/$bin";
554 }
555 }
556
557 return "";
558}
559
560sub recent_git_signoffs {
561 my ($file) = @_;
562
563 my $sign_offs = "";
564 my $cmd = "";
565 my $output = "";
566 my $count = 0;
567 my @lines = ();
0e70e83d 568 my %hash;
afa81ee1 569 my $total_sign_offs;
cb7301c7
JP
570
571 if (which("git") eq "") {
de2fc492
JP
572 warn("$P: git not found. Add --nogit to options?\n");
573 return;
574 }
575 if (!(-d ".git")) {
5f2441e9
JP
576 warn("$P: .git directory not found. Use a git repository for better results.\n");
577 warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
de2fc492 578 return;
cb7301c7
JP
579 }
580
581 $cmd = "git log --since=${email_git_since} -- ${file}";
cb7301c7
JP
582
583 $output = `${cmd}`;
584 $output =~ s/^\s*//gm;
585
586 @lines = split("\n", $output);
afa81ee1 587
0e70e83d
JP
588 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
589 if (!$email_git_penguin_chiefs) {
590 @lines = grep(!/${penguin_chiefs}/i, @lines);
591 }
592 # cut -f2- -d":"
593 s/.*:\s*(.+)\s*/$1/ for (@lines);
594
595 @lines = mailmap(@lines);
596
597 $total_sign_offs = @lines;
598 @lines = sort(@lines);
599 # uniq -c
afa81ee1 600 foreach my $line (@lines) {
0e70e83d
JP
601 $hash{$line}++;
602 }
603 # sort -rn
604 @lines = ();
605 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
606 push(@lines,"$hash{$line} $line");
afa81ee1
JP
607 }
608
cb7301c7 609 foreach my $line (@lines) {
4a7fdb5f 610 if ($line =~ m/([0-9]+)\s+(.*)/) {
cb7301c7 611 my $sign_offs = $1;
4a7fdb5f 612 $line = $2;
cb7301c7
JP
613 $count++;
614 if ($sign_offs < $email_git_min_signatures ||
afa81ee1
JP
615 $count > $email_git_max_maintainers ||
616 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) {
cb7301c7
JP
617 last;
618 }
0e70e83d 619 push_email_address($line);
cb7301c7 620 }
f5492666
JP
621 }
622}
623
624sub save_commits {
625 my ($cmd, @commits) = @_;
626 my $output;
627 my @lines = ();
628
629 $output = `${cmd}`;
630
631 @lines = split("\n", $output);
632 foreach my $line (@lines) {
633 if ($line =~ m/^(\w+) /) {
634 push (@commits, $1);
635 }
636 }
637 return @commits;
638}
639
640sub git_assign_blame {
641 my ($file) = @_;
642
643 my @lines = ();
644 my @commits = ();
645 my $cmd;
646 my $output;
647 my %hash;
648 my $total_sign_offs;
649 my $count;
650
651 if (@range) {
652 foreach my $file_range_diff (@range) {
653 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
654 my $diff_file = $1;
655 my $diff_start = $2;
656 my $diff_length = $3;
657 next if (!("$file" eq "$diff_file"));
658 $cmd = "git blame -l -L $diff_start,+$diff_length $file\n";
659 @commits = save_commits($cmd, @commits);
cb7301c7 660 }
f5492666
JP
661 } else {
662 if (-f $file) {
663 $cmd = "git blame -l $file\n";
664 @commits = save_commits($cmd, @commits);
665 }
666 }
667
668 $total_sign_offs = 0;
669 @commits = uniq(@commits);
670 foreach my $commit (@commits) {
671 $cmd = "git log -1 ${commit}";
f5492666
JP
672
673 $output = `${cmd}`;
674 $output =~ s/^\s*//gm;
675 @lines = split("\n", $output);
0e70e83d
JP
676
677 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
678 if (!$email_git_penguin_chiefs) {
679 @lines = grep(!/${penguin_chiefs}/i, @lines);
680 }
681 # cut -f2- -d":"
682 s/.*:\s*(.+)\s*/$1/ for (@lines);
683
f5492666
JP
684 $hash{$_}++ for @lines;
685 $total_sign_offs += @lines;
686 }
687
688 $count = 0;
689 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
690 my $sign_offs = $hash{$line};
691 $count++;
692 last if ($sign_offs < $email_git_min_signatures ||
693 $count > $email_git_max_maintainers ||
694 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
695 push_email_address($line);
cb7301c7 696 }
cb7301c7
JP
697}
698
699sub uniq {
700 my @parms = @_;
701
702 my %saw;
703 @parms = grep(!$saw{$_}++, @parms);
704 return @parms;
705}
706
707sub sort_and_uniq {
708 my @parms = @_;
709
710 my %saw;
711 @parms = sort @parms;
712 @parms = grep(!$saw{$_}++, @parms);
713 return @parms;
714}
715
716sub output {
717 my @parms = @_;
718
719 if ($output_multiline) {
720 foreach my $line (@parms) {
721 print("${line}\n");
722 }
723 } else {
724 print(join($output_separator, @parms));
725 print("\n");
726 }
727}
1b5e1cf6
JP
728
729my $rfc822re;
730
731sub make_rfc822re {
732# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
733# comment. We must allow for rfc822_lwsp (or comments) after each of these.
734# This regexp will only work on addresses which have had comments stripped
735# and replaced with rfc822_lwsp.
736
737 my $specials = '()<>@,;:\\\\".\\[\\]';
738 my $controls = '\\000-\\037\\177';
739
740 my $dtext = "[^\\[\\]\\r\\\\]";
741 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
742
743 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
744
745# Use zero-width assertion to spot the limit of an atom. A simple
746# $rfc822_lwsp* causes the regexp engine to hang occasionally.
747 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
748 my $word = "(?:$atom|$quoted_string)";
749 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
750
751 my $sub_domain = "(?:$atom|$domain_literal)";
752 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
753
754 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
755
756 my $phrase = "$word*";
757 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
758 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
759 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
760
761 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
762 my $address = "(?:$mailbox|$group)";
763
764 return "$rfc822_lwsp*$address";
765}
766
767sub rfc822_strip_comments {
768 my $s = shift;
769# Recursively remove comments, and replace with a single space. The simpler
770# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
771# chars in atoms, for example.
772
773 while ($s =~ s/^((?:[^"\\]|\\.)*
774 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
775 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
776 return $s;
777}
778
779# valid: returns true if the parameter is an RFC822 valid address
780#
781sub rfc822_valid ($) {
782 my $s = rfc822_strip_comments(shift);
783
784 if (!$rfc822re) {
785 $rfc822re = make_rfc822re();
786 }
787
788 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
789}
790
791# validlist: In scalar context, returns true if the parameter is an RFC822
792# valid list of addresses.
793#
794# In list context, returns an empty list on failure (an invalid
795# address was found); otherwise a list whose first element is the
796# number of addresses found and whose remaining elements are the
797# addresses. This is needed to disambiguate failure (invalid)
798# from success with no addresses found, because an empty string is
799# a valid list.
800
801sub rfc822_validlist ($) {
802 my $s = rfc822_strip_comments(shift);
803
804 if (!$rfc822re) {
805 $rfc822re = make_rfc822re();
806 }
807 # * null list items are valid according to the RFC
808 # * the '1' business is to aid in distinguishing failure from no results
809
810 my @r;
811 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
812 $s =~ m/^$rfc822_char*$/) {
5f2441e9 813 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
1b5e1cf6
JP
814 push @r, $1;
815 }
816 return wantarray ? (scalar(@r), @r) : 1;
817 }
818 else {
819 return wantarray ? () : 0;
820 }
821}