Commit | Line | Data |
---|---|---|
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 | ||
13 | use strict; | |
14 | ||
15 | my $P = $0; | |
3fb55652 | 16 | my $V = '0.19'; |
cb7301c7 JP |
17 | |
18 | use Getopt::Long qw(:config no_auto_abbrev); | |
19 | ||
20 | my $lk_path = "./"; | |
21 | my $email = 1; | |
22 | my $email_usename = 1; | |
23 | my $email_maintainer = 1; | |
24 | my $email_list = 1; | |
25 | my $email_subscriber_list = 0; | |
26 | my $email_git = 1; | |
27 | my $email_git_penguin_chiefs = 0; | |
28 | my $email_git_min_signatures = 1; | |
29 | my $email_git_max_maintainers = 5; | |
afa81ee1 | 30 | my $email_git_min_percent = 5; |
cb7301c7 | 31 | my $email_git_since = "1-year-ago"; |
f5492666 | 32 | my $email_git_blame = 0; |
cb7301c7 JP |
33 | my $output_multiline = 1; |
34 | my $output_separator = ", "; | |
35 | my $scm = 0; | |
36 | my $web = 0; | |
37 | my $subsystem = 0; | |
38 | my $status = 0; | |
4a7fdb5f | 39 | my $from_filename = 0; |
3fb55652 | 40 | my $pattern_depth = 0; |
cb7301c7 JP |
41 | my $version = 0; |
42 | my $help = 0; | |
43 | ||
44 | my $exit = 0; | |
45 | ||
46 | my @penguin_chief = (); | |
47 | push(@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 | ||
51 | my @penguin_chief_names = (); | |
52 | foreach 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 | } | |
59 | my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)"; | |
60 | ||
5f2441e9 | 61 | # rfc822 email address - preloaded methods go here. |
1b5e1cf6 | 62 | my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])"; |
df4cc036 | 63 | my $rfc822_char = '[\\000-\\377]'; |
1b5e1cf6 | 64 | |
cb7301c7 JP |
65 | if (!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 | ||
93 | if ($help != 0) { | |
94 | usage(); | |
95 | exit 0; | |
96 | } | |
97 | ||
98 | if ($version != 0) { | |
99 | print("${P} ${V}\n"); | |
100 | exit 0; | |
101 | } | |
102 | ||
cb7301c7 JP |
103 | if ($#ARGV < 0) { |
104 | usage(); | |
105 | die "$P: argument missing: patchfile or -f file please\n"; | |
106 | } | |
107 | ||
108 | my $selections = $email + $scm + $status + $subsystem + $web; | |
109 | if ($selections == 0) { | |
110 | usage(); | |
111 | die "$P: Missing required option: email, scm, status, subsystem or web\n"; | |
112 | } | |
113 | ||
f5492666 JP |
114 | if ($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 | ||
121 | if (!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 | ||
128 | my @typevalue = (); | |
129 | open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n"; | |
130 | while (<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 | } | |
153 | close(MAINT); | |
154 | ||
4a7fdb5f | 155 | ## use the filenames on the command line or find the filenames in the patchfiles |
cb7301c7 JP |
156 | |
157 | my @files = (); | |
f5492666 | 158 | my @range = (); |
cb7301c7 | 159 | |
4a7fdb5f | 160 | foreach 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 | ||
195 | my @email_to = (); | |
290603c1 | 196 | my @list_to = (); |
cb7301c7 JP |
197 | my @scm = (); |
198 | my @web = (); | |
199 | my @subsystem = (); | |
200 | my @status = (); | |
201 | ||
202 | # Find responsible parties | |
203 | ||
204 | foreach 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 | 257 | if ($email) { |
cb7301c7 JP |
258 | foreach my $chief (@penguin_chief) { |
259 | if ($chief =~ m/^(.*):(.*)/) { | |
f5f5078d | 260 | my $email_address; |
cb7301c7 | 261 | if ($email_usename) { |
f5f5078d | 262 | $email_address = format_email($1, $2); |
cb7301c7 | 263 | } else { |
f5f5078d JP |
264 | $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); | |
cb7301c7 JP |
270 | } |
271 | } | |
272 | } | |
273 | } | |
274 | ||
290603c1 JP |
275 | if ($email || $email_list) { |
276 | my @to = (); | |
277 | if ($email) { | |
278 | @to = (@to, @email_to); | |
cb7301c7 | 279 | } |
290603c1 | 280 | if ($email_list) { |
290603c1 | 281 | @to = (@to, @list_to); |
290603c1 JP |
282 | } |
283 | output(uniq(@to)); | |
cb7301c7 JP |
284 | } |
285 | ||
286 | if ($scm) { | |
4a7fdb5f | 287 | @scm = sort_and_uniq(@scm); |
cb7301c7 JP |
288 | output(@scm); |
289 | } | |
290 | ||
291 | if ($status) { | |
4a7fdb5f | 292 | @status = sort_and_uniq(@status); |
cb7301c7 JP |
293 | output(@status); |
294 | } | |
295 | ||
296 | if ($subsystem) { | |
4a7fdb5f | 297 | @subsystem = sort_and_uniq(@subsystem); |
cb7301c7 JP |
298 | output(@subsystem); |
299 | } | |
300 | ||
301 | if ($web) { | |
4a7fdb5f | 302 | @web = sort_and_uniq(@web); |
cb7301c7 JP |
303 | output(@web); |
304 | } | |
305 | ||
306 | exit($exit); | |
307 | ||
308 | sub 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 | ||
326 | sub usage { | |
327 | print <<EOT; | |
328 | usage: $P [options] patchfile | |
870020f9 | 329 | $P [options] -f file|directory |
cb7301c7 JP |
330 | version: $V |
331 | ||
332 | MAINTAINER 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) | |
3d202aeb | 338 | --git-min-percent => minimum percentage of commits required (default: 5) |
cb7301c7 | 339 | --git-since => git history to use (default: 1-year-ago) |
f5492666 | 340 | --git-blame => use git blame to find modified commits for patch or file |
cb7301c7 JP |
341 | --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 | ||
350 | Output type options: | |
351 | --separator [, ] => separator for multiple entries on 1 line | |
352 | --multiline => print 1 entry per line | |
353 | ||
cb7301c7 | 354 | Other options: |
3fb55652 | 355 | --pattern-depth => Number of pattern directory traversals (default: 0 (all)) |
f5f5078d | 356 | --version => show version |
cb7301c7 JP |
357 | --help => show this help information |
358 | ||
3fb55652 JP |
359 | Default options: |
360 | [--email --git --m --n --l --multiline --pattern-depth=0] | |
361 | ||
870020f9 JP |
362 | Notes: |
363 | Using "-f directory" may give unexpected results: | |
f5492666 JP |
364 | 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. | |
cb7301c7 JP |
373 | EOT |
374 | } | |
375 | ||
376 | sub 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 | ||
403 | sub format_email { | |
404 | my ($name, $email) = @_; | |
405 | ||
406 | $name =~ s/^\s+|\s+$//g; | |
d789504a | 407 | $name =~ s/^\"|\"$//g; |
cb7301c7 JP |
408 | $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 | ||
421 | sub add_categories { | |
422 | my ($index) = @_; | |
423 | ||
424 | $index = $index - 1; | |
425 | while ($index >= 0) { | |
426 | my $tv = $typevalue[$index]; | |
290603c1 | 427 | if ($tv =~ m/^(\C):\s*(.*)/) { |
cb7301c7 JP |
428 | my $ptype = $1; |
429 | my $pvalue = $2; | |
430 | if ($ptype eq "L") { | |
290603c1 JP |
431 | my $list_address = $pvalue; |
432 | my $list_additional = ""; | |
433 | if ($list_address =~ m/([^\s]+)\s+(.*)$/) { | |
434 | $list_address = $1; | |
435 | $list_additional = $2; | |
436 | } | |
bdf7c685 | 437 | if ($list_additional =~ m/subscribers-only/) { |
cb7301c7 | 438 | if ($email_subscriber_list) { |
290603c1 | 439 | push(@list_to, $list_address); |
cb7301c7 JP |
440 | } |
441 | } else { | |
442 | if ($email_list) { | |
290603c1 | 443 | push(@list_to, $list_address); |
cb7301c7 JP |
444 | } |
445 | } | |
446 | } elsif ($ptype eq "M") { | |
5f2441e9 JP |
447 | 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) { | |
1b5e1cf6 | 460 | push_email_addresses($pvalue); |
cb7301c7 JP |
461 | } |
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 | ||
1b5e1cf6 JP |
478 | sub push_email_address { |
479 | my ($email_address) = @_; | |
480 | ||
481 | my $email_name = ""; | |
1b5e1cf6 | 482 | |
0a79c492 | 483 | if ($email_maintainer) { |
f5492666 JP |
484 | 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); | |
0a79c492 JP |
495 | } else { |
496 | push(@email_to, $email_address); | |
497 | } | |
1b5e1cf6 JP |
498 | } |
499 | } | |
500 | ||
501 | sub push_email_addresses { | |
502 | my ($address) = @_; | |
503 | ||
504 | my @address_list = (); | |
505 | ||
5f2441e9 JP |
506 | if (rfc822_valid($address)) { |
507 | push_email_address($address); | |
508 | } elsif (@address_list = rfc822_validlist($address)) { | |
1b5e1cf6 JP |
509 | my $array_count = shift(@address_list); |
510 | while (my $entry = shift(@address_list)) { | |
511 | push_email_address($entry); | |
512 | } | |
5f2441e9 JP |
513 | } else { |
514 | warn("Invalid MAINTAINERS address: '" . $address . "'\n"); | |
1b5e1cf6 | 515 | } |
1b5e1cf6 JP |
516 | } |
517 | ||
cb7301c7 JP |
518 | sub which { |
519 | my ($bin) = @_; | |
520 | ||
f5f5078d | 521 | foreach my $path (split(/:/, $ENV{PATH})) { |
cb7301c7 JP |
522 | if (-e "$path/$bin") { |
523 | return "$path/$bin"; | |
524 | } | |
525 | } | |
526 | ||
527 | return ""; | |
528 | } | |
529 | ||
530 | sub recent_git_signoffs { | |
531 | my ($file) = @_; | |
532 | ||
533 | my $sign_offs = ""; | |
534 | my $cmd = ""; | |
535 | my $output = ""; | |
536 | my $count = 0; | |
537 | my @lines = (); | |
afa81ee1 | 538 | my $total_sign_offs; |
cb7301c7 JP |
539 | |
540 | if (which("git") eq "") { | |
de2fc492 JP |
541 | warn("$P: git not found. Add --nogit to options?\n"); |
542 | return; | |
543 | } | |
544 | if (!(-d ".git")) { | |
5f2441e9 JP |
545 | 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"); | |
de2fc492 | 547 | return; |
cb7301c7 JP |
548 | } |
549 | ||
550 | $cmd = "git log --since=${email_git_since} -- ${file}"; | |
de2fc492 JP |
551 | $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\""; |
552 | if (!$email_git_penguin_chiefs) { | |
553 | $cmd .= " | grep -Ev \"${penguin_chiefs}\""; | |
554 | } | |
4a7fdb5f | 555 | $cmd .= " | cut -f2- -d\":\""; |
cb7301c7 JP |
556 | $cmd .= " | sort | uniq -c | sort -rn"; |
557 | ||
558 | $output = `${cmd}`; | |
559 | $output =~ s/^\s*//gm; | |
560 | ||
561 | @lines = split("\n", $output); | |
afa81ee1 JP |
562 | |
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 | ||
cb7301c7 | 572 | foreach my $line (@lines) { |
4a7fdb5f | 573 | if ($line =~ m/([0-9]+)\s+(.*)/) { |
cb7301c7 | 574 | my $sign_offs = $1; |
4a7fdb5f | 575 | $line = $2; |
cb7301c7 JP |
576 | $count++; |
577 | if ($sign_offs < $email_git_min_signatures || | |
afa81ee1 JP |
578 | $count > $email_git_max_maintainers || |
579 | $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) { | |
cb7301c7 JP |
580 | last; |
581 | } | |
cb7301c7 | 582 | } |
f5492666 JP |
583 | push_email_address($line); |
584 | } | |
585 | } | |
586 | ||
587 | sub 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); | |
598 | } | |
599 | } | |
600 | return @commits; | |
601 | } | |
602 | ||
603 | sub 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); | |
cb7301c7 | 623 | } |
f5492666 JP |
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); | |
cb7301c7 | 656 | } |
cb7301c7 JP |
657 | } |
658 | ||
659 | sub uniq { | |
660 | my @parms = @_; | |
661 | ||
662 | my %saw; | |
663 | @parms = grep(!$saw{$_}++, @parms); | |
664 | return @parms; | |
665 | } | |
666 | ||
667 | sub sort_and_uniq { | |
668 | my @parms = @_; | |
669 | ||
670 | my %saw; | |
671 | @parms = sort @parms; | |
672 | @parms = grep(!$saw{$_}++, @parms); | |
673 | return @parms; | |
674 | } | |
675 | ||
676 | sub 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 | } | |
1b5e1cf6 JP |
688 | |
689 | my $rfc822re; | |
690 | ||
691 | sub 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 | ||
727 | sub 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 | # | |
741 | sub 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 | ||
761 | sub 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*$/) { | |
5f2441e9 | 773 | while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) { |
1b5e1cf6 JP |
774 | push @r, $1; |
775 | } | |
776 | return wantarray ? (scalar(@r), @r) : 1; | |
777 | } | |
778 | else { | |
779 | return wantarray ? () : 0; | |
780 | } | |
781 | } |