blob: e54f72f6c341667a7000c384afc89e0c3854c31f [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#
Roel Kluin3bd7bf52009-11-11 14:26:13 -08008# usage: perl scripts/get_maintainer.pl [OPTIONS] <patch>
9# perl scripts/get_maintainer.pl [OPTIONS] -f <file>
Joe Perchescb7301c2009-04-07 20:40:12 -070010#
11# Licensed under the terms of the GNU GPL License version 2
12
13use strict;
14
15my $P = $0;
Joe Perches60db31a2009-12-14 18:00:50 -080016my $V = '0.23';
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;
Joe Perchescb7301c2009-04-07 20:40:12 -070026my $email_git_penguin_chiefs = 0;
Joe Perches60db31a2009-12-14 18:00:50 -080027my $email_git = 1;
28my $email_git_blame = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070029my $email_git_min_signatures = 1;
30my $email_git_max_maintainers = 5;
Joe Perchesafa81ee2009-07-29 15:04:28 -070031my $email_git_min_percent = 5;
Joe Perchescb7301c2009-04-07 20:40:12 -070032my $email_git_since = "1-year-ago";
Joe Perches60db31a2009-12-14 18:00:50 -080033my $email_hg_since = "-365";
Joe Perches11ecf532009-09-21 17:04:22 -070034my $email_remove_duplicates = 1;
Joe Perchescb7301c2009-04-07 20:40:12 -070035my $output_multiline = 1;
36my $output_separator = ", ";
Joe Perches3c7385b2009-12-14 18:00:46 -080037my $output_roles = 0;
38my $output_rolestats = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070039my $scm = 0;
40my $web = 0;
41my $subsystem = 0;
42my $status = 0;
Joe Perchesdcf36a92009-10-26 16:49:47 -070043my $keywords = 1;
Joe Perches4b76c9d2010-03-05 13:43:03 -080044my $sections = 0;
Joe Perches03372db2010-03-05 13:43:00 -080045my $file_emails = 0;
Joe Perches4a7fdb52009-04-10 12:28:57 -070046my $from_filename = 0;
Joe Perches3fb55652009-09-21 17:04:17 -070047my $pattern_depth = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070048my $version = 0;
49my $help = 0;
50
51my $exit = 0;
52
53my @penguin_chief = ();
54push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
55#Andrew wants in on most everything - 2009/01/14
56#push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
57
58my @penguin_chief_names = ();
59foreach my $chief (@penguin_chief) {
60 if ($chief =~ m/^(.*):(.*)/) {
61 my $chief_name = $1;
62 my $chief_addr = $2;
63 push(@penguin_chief_names, $chief_name);
64 }
65}
66my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
67
Joe Perches5f2441e2009-06-16 15:34:02 -070068# rfc822 email address - preloaded methods go here.
Joe Perches1b5e1cf2009-06-16 15:34:01 -070069my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
Joe Perchesdf4cc032009-06-16 15:34:04 -070070my $rfc822_char = '[\\000-\\377]';
Joe Perches1b5e1cf2009-06-16 15:34:01 -070071
Joe Perches60db31a2009-12-14 18:00:50 -080072# VCS command support: class-like functions and strings
73
74my %VCS_cmds;
75
76my %VCS_cmds_git = (
77 "execute_cmd" => \&git_execute_cmd,
78 "available" => '(which("git") ne "") && (-d ".git")',
Richard Kennedy99cf6112010-02-02 13:44:07 -080079 "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file",
80 "find_commit_signers_cmd" => "git log --no-color -1 \$commit",
Joe Perches60db31a2009-12-14 18:00:50 -080081 "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
82 "blame_file_cmd" => "git blame -l \$file",
83 "commit_pattern" => "^commit [0-9a-f]{40,40}",
84 "blame_commit_pattern" => "^([0-9a-f]+) "
85);
86
87my %VCS_cmds_hg = (
88 "execute_cmd" => \&hg_execute_cmd,
89 "available" => '(which("hg") ne "") && (-d ".hg")',
90 "find_signers_cmd" =>
91 "hg log --date=\$email_hg_since" .
92 " --template='commit {node}\\n{desc}\\n' -- \$file",
93 "find_commit_signers_cmd" => "hg log --template='{desc}\\n' -r \$commit",
94 "blame_range_cmd" => "", # not supported
95 "blame_file_cmd" => "hg blame -c \$file",
96 "commit_pattern" => "^commit [0-9a-f]{40,40}",
97 "blame_commit_pattern" => "^([0-9a-f]+):"
98);
99
Joe Perchescb7301c2009-04-07 20:40:12 -0700100if (!GetOptions(
101 'email!' => \$email,
102 'git!' => \$email_git,
Joe Perches60db31a2009-12-14 18:00:50 -0800103 'git-blame!' => \$email_git_blame,
Joe Perchescb7301c2009-04-07 20:40:12 -0700104 'git-chief-penguins!' => \$email_git_penguin_chiefs,
105 'git-min-signatures=i' => \$email_git_min_signatures,
106 'git-max-maintainers=i' => \$email_git_max_maintainers,
Joe Perchesafa81ee2009-07-29 15:04:28 -0700107 'git-min-percent=i' => \$email_git_min_percent,
Joe Perchescb7301c2009-04-07 20:40:12 -0700108 'git-since=s' => \$email_git_since,
Joe Perches60db31a2009-12-14 18:00:50 -0800109 'hg-since=s' => \$email_hg_since,
Joe Perches11ecf532009-09-21 17:04:22 -0700110 'remove-duplicates!' => \$email_remove_duplicates,
Joe Perchescb7301c2009-04-07 20:40:12 -0700111 'm!' => \$email_maintainer,
112 'n!' => \$email_usename,
113 'l!' => \$email_list,
114 's!' => \$email_subscriber_list,
115 'multiline!' => \$output_multiline,
Joe Perches3c7385b2009-12-14 18:00:46 -0800116 'roles!' => \$output_roles,
117 'rolestats!' => \$output_rolestats,
Joe Perchescb7301c2009-04-07 20:40:12 -0700118 'separator=s' => \$output_separator,
119 'subsystem!' => \$subsystem,
120 'status!' => \$status,
121 'scm!' => \$scm,
122 'web!' => \$web,
Joe Perches3fb55652009-09-21 17:04:17 -0700123 'pattern-depth=i' => \$pattern_depth,
Joe Perchesdcf36a92009-10-26 16:49:47 -0700124 'k|keywords!' => \$keywords,
Joe Perches4b76c9d2010-03-05 13:43:03 -0800125 'sections!' => \$sections,
Joe Perches03372db2010-03-05 13:43:00 -0800126 'fe|file-emails!' => \$file_emails,
Joe Perches4a7fdb52009-04-10 12:28:57 -0700127 'f|file' => \$from_filename,
Joe Perchescb7301c2009-04-07 20:40:12 -0700128 'v|version' => \$version,
129 'h|help' => \$help,
130 )) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800131 die "$P: invalid argument - use --help if necessary\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700132}
133
134if ($help != 0) {
135 usage();
136 exit 0;
137}
138
139if ($version != 0) {
140 print("${P} ${V}\n");
141 exit 0;
142}
143
Joe Perchescb7301c2009-04-07 20:40:12 -0700144if ($#ARGV < 0) {
145 usage();
146 die "$P: argument missing: patchfile or -f file please\n";
147}
148
Joe Perches42498312009-09-21 17:04:21 -0700149if ($output_separator ne ", ") {
150 $output_multiline = 0;
151}
152
Joe Perches3c7385b2009-12-14 18:00:46 -0800153if ($output_rolestats) {
154 $output_roles = 1;
155}
156
Joe Perches4b76c9d2010-03-05 13:43:03 -0800157if ($sections) {
158 $email = 0;
159 $email_list = 0;
160 $scm = 0;
161 $status = 0;
162 $subsystem = 0;
163 $web = 0;
164 $keywords = 0;
165} else {
166 my $selections = $email + $scm + $status + $subsystem + $web;
167 if ($selections == 0) {
168 usage();
169 die "$P: Missing required option: email, scm, status, subsystem or web\n";
170 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700171}
172
Joe Perchesf5492662009-09-21 17:04:13 -0700173if ($email &&
174 ($email_maintainer + $email_list + $email_subscriber_list +
175 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700176 usage();
177 die "$P: Please select at least 1 email option\n";
178}
179
180if (!top_of_kernel_tree($lk_path)) {
181 die "$P: The current directory does not appear to be "
182 . "a linux kernel source tree.\n";
183}
184
185## Read MAINTAINERS for type/value pairs
186
187my @typevalue = ();
Joe Perchesdcf36a92009-10-26 16:49:47 -0700188my %keyword_hash;
189
Joe Perchescb7301c2009-04-07 20:40:12 -0700190open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
191while (<MAINT>) {
192 my $line = $_;
193
194 if ($line =~ m/^(\C):\s*(.*)/) {
195 my $type = $1;
196 my $value = $2;
197
198 ##Filename pattern matching
199 if ($type eq "F" || $type eq "X") {
200 $value =~ s@\.@\\\.@g; ##Convert . to \.
201 $value =~ s/\*/\.\*/g; ##Convert * to .*
202 $value =~ s/\?/\./g; ##Convert ? to .
Joe Perches870020f2009-07-29 15:04:28 -0700203 ##if pattern is a directory and it lacks a trailing slash, add one
204 if ((-d $value)) {
205 $value =~ s@([^/])$@$1/@;
206 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700207 } elsif ($type eq "K") {
208 $keyword_hash{@typevalue} = $value;
Joe Perchescb7301c2009-04-07 20:40:12 -0700209 }
210 push(@typevalue, "$type:$value");
211 } elsif (!/^(\s)*$/) {
212 $line =~ s/\n$//g;
213 push(@typevalue, $line);
214 }
215}
216close(MAINT);
217
Joe Perches8cbb3a72009-09-21 17:04:21 -0700218my %mailmap;
219
Joe Perches11ecf532009-09-21 17:04:22 -0700220if ($email_remove_duplicates) {
221 open(MAILMAP, "<${lk_path}.mailmap") || warn "$P: Can't open .mailmap\n";
222 while (<MAILMAP>) {
223 my $line = $_;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700224
Joe Perches11ecf532009-09-21 17:04:22 -0700225 next if ($line =~ m/^\s*#/);
226 next if ($line =~ m/^\s*$/);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700227
Joe Perches11ecf532009-09-21 17:04:22 -0700228 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -0800229 $line = format_email($name, $address, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700230
Joe Perches11ecf532009-09-21 17:04:22 -0700231 next if ($line =~ m/^\s*$/);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700232
Joe Perches11ecf532009-09-21 17:04:22 -0700233 if (exists($mailmap{$name})) {
234 my $obj = $mailmap{$name};
235 push(@$obj, $address);
236 } else {
237 my @arr = ($address);
238 $mailmap{$name} = \@arr;
239 }
Joe Perches8cbb3a72009-09-21 17:04:21 -0700240 }
Joe Perches11ecf532009-09-21 17:04:22 -0700241 close(MAILMAP);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700242}
243
Joe Perches4a7fdb52009-04-10 12:28:57 -0700244## use the filenames on the command line or find the filenames in the patchfiles
Joe Perchescb7301c2009-04-07 20:40:12 -0700245
246my @files = ();
Joe Perchesf5492662009-09-21 17:04:13 -0700247my @range = ();
Joe Perchesdcf36a92009-10-26 16:49:47 -0700248my @keyword_tvi = ();
Joe Perches03372db2010-03-05 13:43:00 -0800249my @file_emails = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700250
Joe Perches4a7fdb52009-04-10 12:28:57 -0700251foreach my $file (@ARGV) {
Joe Perches870020f2009-07-29 15:04:28 -0700252 ##if $file is a directory and it lacks a trailing slash, add one
253 if ((-d $file)) {
254 $file =~ s@([^/])$@$1/@;
255 } elsif (!(-f $file)) {
Joe Perches4a7fdb52009-04-10 12:28:57 -0700256 die "$P: file '${file}' not found\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700257 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700258 if ($from_filename) {
259 push(@files, $file);
Joe Perches03372db2010-03-05 13:43:00 -0800260 if (-f $file && ($keywords || $file_emails)) {
Joe Perchesdcf36a92009-10-26 16:49:47 -0700261 open(FILE, "<$file") or die "$P: Can't open ${file}\n";
Joe Perchesa8af2432009-12-14 18:00:49 -0800262 my $text = do { local($/) ; <FILE> };
Joe Perches03372db2010-03-05 13:43:00 -0800263 close(FILE);
264 if ($keywords) {
265 foreach my $line (keys %keyword_hash) {
266 if ($text =~ m/$keyword_hash{$line}/x) {
267 push(@keyword_tvi, $line);
268 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700269 }
270 }
Joe Perches03372db2010-03-05 13:43:00 -0800271 if ($file_emails) {
272 my @poss_addr = $text =~ m$[A-Za-zÀ-ÿ\"\' \,\.\+-]*\s*[\,]*\s*[\(\<\{]{0,1}[A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+\.[A-Za-z0-9]+[\)\>\}]{0,1}$g;
273 push(@file_emails, clean_file_emails(@poss_addr));
274 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700275 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700276 } else {
277 my $file_cnt = @files;
Joe Perchesf5492662009-09-21 17:04:13 -0700278 my $lastfile;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700279 open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
280 while (<PATCH>) {
Joe Perchesdcf36a92009-10-26 16:49:47 -0700281 my $patch_line = $_;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700282 if (m/^\+\+\+\s+(\S+)/) {
283 my $filename = $1;
284 $filename =~ s@^[^/]*/@@;
285 $filename =~ s@\n@@;
Joe Perchesf5492662009-09-21 17:04:13 -0700286 $lastfile = $filename;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700287 push(@files, $filename);
Joe Perchesf5492662009-09-21 17:04:13 -0700288 } elsif (m/^\@\@ -(\d+),(\d+)/) {
289 if ($email_git_blame) {
290 push(@range, "$lastfile:$1:$2");
291 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700292 } elsif ($keywords) {
293 foreach my $line (keys %keyword_hash) {
294 if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
295 push(@keyword_tvi, $line);
296 }
297 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700298 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700299 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700300 close(PATCH);
301 if ($file_cnt == @files) {
Joe Perches7f29fd272009-06-16 15:34:04 -0700302 warn "$P: file '${file}' doesn't appear to be a patch. "
Joe Perches4a7fdb52009-04-10 12:28:57 -0700303 . "Add -f to options?\n";
304 }
305 @files = sort_and_uniq(@files);
Joe Perchescb7301c2009-04-07 20:40:12 -0700306 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700307}
308
Joe Perches03372db2010-03-05 13:43:00 -0800309@file_emails = uniq(@file_emails);
310
Joe Perchescb7301c2009-04-07 20:40:12 -0700311my @email_to = ();
Joe Perches290603c2009-06-16 15:33:58 -0700312my @list_to = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700313my @scm = ();
314my @web = ();
315my @subsystem = ();
316my @status = ();
317
318# Find responsible parties
319
320foreach my $file (@files) {
321
Joe Perches272a8972010-01-08 14:42:48 -0800322 my %hash;
323 my $tvi = find_first_section();
324 while ($tvi < @typevalue) {
325 my $start = find_starting_index($tvi);
326 my $end = find_ending_index($tvi);
327 my $exclude = 0;
328 my $i;
Joe Perchescb7301c2009-04-07 20:40:12 -0700329
Joe Perches272a8972010-01-08 14:42:48 -0800330 #Do not match excluded file patterns
Joe Perchescb7301c2009-04-07 20:40:12 -0700331
Joe Perches272a8972010-01-08 14:42:48 -0800332 for ($i = $start; $i < $end; $i++) {
333 my $line = $typevalue[$i];
Joe Perches290603c2009-06-16 15:33:58 -0700334 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700335 my $type = $1;
336 my $value = $2;
Joe Perches272a8972010-01-08 14:42:48 -0800337 if ($type eq 'X') {
Joe Perchescb7301c2009-04-07 20:40:12 -0700338 if (file_match_pattern($file, $value)) {
Joe Perches272a8972010-01-08 14:42:48 -0800339 $exclude = 1;
340 }
341 }
342 }
343 }
344
345 if (!$exclude) {
346 for ($i = $start; $i < $end; $i++) {
347 my $line = $typevalue[$i];
348 if ($line =~ m/^(\C):\s*(.*)/) {
349 my $type = $1;
350 my $value = $2;
351 if ($type eq 'F') {
352 if (file_match_pattern($file, $value)) {
353 my $value_pd = ($value =~ tr@/@@);
354 my $file_pd = ($file =~ tr@/@@);
355 $value_pd++ if (substr($value,-1,1) ne "/");
356 if ($pattern_depth == 0 ||
357 (($file_pd - $value_pd) < $pattern_depth)) {
358 $hash{$tvi} = $value_pd;
359 }
Joe Perches3fb55652009-09-21 17:04:17 -0700360 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700361 }
362 }
363 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700364 }
Joe Perches272a8972010-01-08 14:42:48 -0800365
366 $tvi += ($end - $start);
367
368 }
369
370 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
371 add_categories($line);
Joe Perches4b76c9d2010-03-05 13:43:03 -0800372 if ($sections) {
373 my $i;
374 my $start = find_starting_index($line);
375 my $end = find_ending_index($line);
376 for ($i = $start; $i < $end; $i++) {
377 my $line = $typevalue[$i];
378 if ($line =~ /^[FX]:/) { ##Restore file patterns
379 $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
380 $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
381 $line =~ s/\\\./\./g; ##Convert \. to .
382 $line =~ s/\.\*/\*/g; ##Convert .* to *
383 }
384 print("$line\n");
385 }
386 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700387 }
388
Joe Perches4a7fdb52009-04-10 12:28:57 -0700389 if ($email && $email_git) {
Joe Perches60db31a2009-12-14 18:00:50 -0800390 vcs_file_signoffs($file);
Joe Perchescb7301c2009-04-07 20:40:12 -0700391 }
392
Joe Perchesf5492662009-09-21 17:04:13 -0700393 if ($email && $email_git_blame) {
Joe Perches60db31a2009-12-14 18:00:50 -0800394 vcs_file_blame($file);
Joe Perchesf5492662009-09-21 17:04:13 -0700395 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700396}
397
Joe Perchesdcf36a92009-10-26 16:49:47 -0700398if ($keywords) {
399 @keyword_tvi = sort_and_uniq(@keyword_tvi);
400 foreach my $line (@keyword_tvi) {
401 add_categories($line);
402 }
403}
404
Joe Perchesf5f50782009-06-16 15:34:00 -0700405if ($email) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700406 foreach my $chief (@penguin_chief) {
407 if ($chief =~ m/^(.*):(.*)/) {
Joe Perchesf5f50782009-06-16 15:34:00 -0700408 my $email_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700409
Joe Perchesa8af2432009-12-14 18:00:49 -0800410 $email_address = format_email($1, $2, $email_usename);
Joe Perchesf5f50782009-06-16 15:34:00 -0700411 if ($email_git_penguin_chiefs) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800412 push(@email_to, [$email_address, 'chief penguin']);
Joe Perchesf5f50782009-06-16 15:34:00 -0700413 } else {
Joe Perches3c7385b2009-12-14 18:00:46 -0800414 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700415 }
416 }
417 }
Joe Perches03372db2010-03-05 13:43:00 -0800418
419 foreach my $email (@file_emails) {
420 my ($name, $address) = parse_email($email);
421
422 my $tmp_email = format_email($name, $address, $email_usename);
423 push_email_address($tmp_email, '');
424 add_role($tmp_email, 'in file');
425 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700426}
427
Joe Perches290603c2009-06-16 15:33:58 -0700428if ($email || $email_list) {
429 my @to = ();
430 if ($email) {
431 @to = (@to, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700432 }
Joe Perches290603c2009-06-16 15:33:58 -0700433 if ($email_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700434 @to = (@to, @list_to);
Joe Perches290603c2009-06-16 15:33:58 -0700435 }
Joe Perches3c7385b2009-12-14 18:00:46 -0800436 output(merge_email(@to));
Joe Perchescb7301c2009-04-07 20:40:12 -0700437}
438
439if ($scm) {
Joe Perchesb7816552009-09-21 17:04:24 -0700440 @scm = uniq(@scm);
Joe Perchescb7301c2009-04-07 20:40:12 -0700441 output(@scm);
442}
443
444if ($status) {
Joe Perchesb7816552009-09-21 17:04:24 -0700445 @status = uniq(@status);
Joe Perchescb7301c2009-04-07 20:40:12 -0700446 output(@status);
447}
448
449if ($subsystem) {
Joe Perchesb7816552009-09-21 17:04:24 -0700450 @subsystem = uniq(@subsystem);
Joe Perchescb7301c2009-04-07 20:40:12 -0700451 output(@subsystem);
452}
453
454if ($web) {
Joe Perchesb7816552009-09-21 17:04:24 -0700455 @web = uniq(@web);
Joe Perchescb7301c2009-04-07 20:40:12 -0700456 output(@web);
457}
458
459exit($exit);
460
461sub file_match_pattern {
462 my ($file, $pattern) = @_;
463 if (substr($pattern, -1) eq "/") {
464 if ($file =~ m@^$pattern@) {
465 return 1;
466 }
467 } else {
468 if ($file =~ m@^$pattern@) {
469 my $s1 = ($file =~ tr@/@@);
470 my $s2 = ($pattern =~ tr@/@@);
471 if ($s1 == $s2) {
472 return 1;
473 }
474 }
475 }
476 return 0;
477}
478
479sub usage {
480 print <<EOT;
481usage: $P [options] patchfile
Joe Perches870020f2009-07-29 15:04:28 -0700482 $P [options] -f file|directory
Joe Perchescb7301c2009-04-07 20:40:12 -0700483version: $V
484
485MAINTAINER field selection options:
486 --email => print email address(es) if any
487 --git => include recent git \*-by: signers
488 --git-chief-penguins => include ${penguin_chiefs}
489 --git-min-signatures => number of signatures required (default: 1)
490 --git-max-maintainers => maximum maintainers to add (default: 5)
Joe Perches3d202ae2009-07-29 15:04:29 -0700491 --git-min-percent => minimum percentage of commits required (default: 5)
Joe Perchesf5492662009-09-21 17:04:13 -0700492 --git-blame => use git blame to find modified commits for patch or file
Joe Perches60db31a2009-12-14 18:00:50 -0800493 --git-since => git history to use (default: 1-year-ago)
494 --hg-since => hg history to use (default: -365)
Joe Perchescb7301c2009-04-07 20:40:12 -0700495 --m => include maintainer(s) if any
496 --n => include name 'Full Name <addr\@domain.tld>'
497 --l => include list(s) if any
498 --s => include subscriber only list(s) if any
Joe Perches11ecf532009-09-21 17:04:22 -0700499 --remove-duplicates => minimize duplicate email names/addresses
Joe Perches3c7385b2009-12-14 18:00:46 -0800500 --roles => show roles (status:subsystem, git-signer, list, etc...)
501 --rolestats => show roles and statistics (commits/total_commits, %)
Joe Perches03372db2010-03-05 13:43:00 -0800502 --file-emails => add email addresses found in -f file (default: 0 (off))
Joe Perchescb7301c2009-04-07 20:40:12 -0700503 --scm => print SCM tree(s) if any
504 --status => print status if any
505 --subsystem => print subsystem name if any
506 --web => print website(s) if any
507
508Output type options:
509 --separator [, ] => separator for multiple entries on 1 line
Joe Perches42498312009-09-21 17:04:21 -0700510 using --separator also sets --nomultiline if --separator is not [, ]
Joe Perchescb7301c2009-04-07 20:40:12 -0700511 --multiline => print 1 entry per line
512
Joe Perchescb7301c2009-04-07 20:40:12 -0700513Other options:
Joe Perches3fb55652009-09-21 17:04:17 -0700514 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
Joe Perchesdcf36a92009-10-26 16:49:47 -0700515 --keywords => scan patch for keywords (default: 1 (on))
Joe Perches4b76c9d2010-03-05 13:43:03 -0800516 --sections => print the entire subsystem sections with pattern matches
Joe Perchesf5f50782009-06-16 15:34:00 -0700517 --version => show version
Joe Perchescb7301c2009-04-07 20:40:12 -0700518 --help => show this help information
519
Joe Perches3fb55652009-09-21 17:04:17 -0700520Default options:
Joe Perches11ecf532009-09-21 17:04:22 -0700521 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
Joe Perches3fb55652009-09-21 17:04:17 -0700522
Joe Perches870020f2009-07-29 15:04:28 -0700523Notes:
524 Using "-f directory" may give unexpected results:
Joe Perchesf5492662009-09-21 17:04:13 -0700525 Used with "--git", git signators for _all_ files in and below
526 directory are examined as git recurses directories.
527 Any specified X: (exclude) pattern matches are _not_ ignored.
528 Used with "--nogit", directory is used as a pattern match,
Joe Perches60db31a2009-12-14 18:00:50 -0800529 no individual file within the directory or subdirectory
530 is matched.
Joe Perchesf5492662009-09-21 17:04:13 -0700531 Used with "--git-blame", does not iterate all files in directory
532 Using "--git-blame" is slow and may add old committers and authors
533 that are no longer active maintainers to the output.
Joe Perches3c7385b2009-12-14 18:00:46 -0800534 Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
535 other automated tools that expect only ["name"] <email address>
536 may not work because of additional output after <email address>.
537 Using "--rolestats" and "--git-blame" shows the #/total=% commits,
538 not the percentage of the entire file authored. # of commits is
539 not a good measure of amount of code authored. 1 major commit may
540 contain a thousand lines, 5 trivial commits may modify a single line.
Joe Perches60db31a2009-12-14 18:00:50 -0800541 If git is not installed, but mercurial (hg) is installed and an .hg
542 repository exists, the following options apply to mercurial:
543 --git,
544 --git-min-signatures, --git-max-maintainers, --git-min-percent, and
545 --git-blame
546 Use --hg-since not --git-since to control date selection
Joe Perchescb7301c2009-04-07 20:40:12 -0700547EOT
548}
549
550sub top_of_kernel_tree {
551 my ($lk_path) = @_;
552
553 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
554 $lk_path .= "/";
555 }
556 if ( (-f "${lk_path}COPYING")
557 && (-f "${lk_path}CREDITS")
558 && (-f "${lk_path}Kbuild")
559 && (-f "${lk_path}MAINTAINERS")
560 && (-f "${lk_path}Makefile")
561 && (-f "${lk_path}README")
562 && (-d "${lk_path}Documentation")
563 && (-d "${lk_path}arch")
564 && (-d "${lk_path}include")
565 && (-d "${lk_path}drivers")
566 && (-d "${lk_path}fs")
567 && (-d "${lk_path}init")
568 && (-d "${lk_path}ipc")
569 && (-d "${lk_path}kernel")
570 && (-d "${lk_path}lib")
571 && (-d "${lk_path}scripts")) {
572 return 1;
573 }
574 return 0;
575}
576
Joe Perches0e70e832009-09-21 17:04:20 -0700577sub parse_email {
578 my ($formatted_email) = @_;
579
580 my $name = "";
581 my $address = "";
582
Joe Perches11ecf532009-09-21 17:04:22 -0700583 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700584 $name = $1;
585 $address = $2;
Joe Perches11ecf532009-09-21 17:04:22 -0700586 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700587 $address = $1;
Joe Perchesb7816552009-09-21 17:04:24 -0700588 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700589 $address = $1;
590 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700591
592 $name =~ s/^\s+|\s+$//g;
Joe Perchesd7895042009-06-16 15:34:02 -0700593 $name =~ s/^\"|\"$//g;
Joe Perches0e70e832009-09-21 17:04:20 -0700594 $address =~ s/^\s+|\s+$//g;
Joe Perchescb7301c2009-04-07 20:40:12 -0700595
596 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
597 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
Joe Perches0e70e832009-09-21 17:04:20 -0700598 $name = "\"$name\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700599 }
Joe Perches0e70e832009-09-21 17:04:20 -0700600
601 return ($name, $address);
602}
603
604sub format_email {
Joe Perchesa8af2432009-12-14 18:00:49 -0800605 my ($name, $address, $usename) = @_;
Joe Perches0e70e832009-09-21 17:04:20 -0700606
607 my $formatted_email;
608
609 $name =~ s/^\s+|\s+$//g;
610 $name =~ s/^\"|\"$//g;
611 $address =~ s/^\s+|\s+$//g;
612
613 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
614 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
615 $name = "\"$name\"";
616 }
617
Joe Perchesa8af2432009-12-14 18:00:49 -0800618 if ($usename) {
Joe Perches0e70e832009-09-21 17:04:20 -0700619 if ("$name" eq "") {
620 $formatted_email = "$address";
621 } else {
Joe Perchesa8af2432009-12-14 18:00:49 -0800622 $formatted_email = "$name <$address>";
Joe Perches0e70e832009-09-21 17:04:20 -0700623 }
624 } else {
625 $formatted_email = $address;
626 }
627
Joe Perchescb7301c2009-04-07 20:40:12 -0700628 return $formatted_email;
629}
630
Joe Perches272a8972010-01-08 14:42:48 -0800631sub find_first_section {
632 my $index = 0;
633
634 while ($index < @typevalue) {
635 my $tv = $typevalue[$index];
636 if (($tv =~ m/^(\C):\s*(.*)/)) {
637 last;
638 }
639 $index++;
640 }
641
642 return $index;
643}
644
Joe Perchesb7816552009-09-21 17:04:24 -0700645sub find_starting_index {
Joe Perchesb7816552009-09-21 17:04:24 -0700646 my ($index) = @_;
647
648 while ($index > 0) {
649 my $tv = $typevalue[$index];
650 if (!($tv =~ m/^(\C):\s*(.*)/)) {
651 last;
652 }
653 $index--;
654 }
655
656 return $index;
657}
658
659sub find_ending_index {
660 my ($index) = @_;
661
662 while ($index < @typevalue) {
663 my $tv = $typevalue[$index];
664 if (!($tv =~ m/^(\C):\s*(.*)/)) {
665 last;
666 }
667 $index++;
668 }
669
670 return $index;
671}
672
Joe Perches3c7385b2009-12-14 18:00:46 -0800673sub get_maintainer_role {
674 my ($index) = @_;
675
676 my $i;
677 my $start = find_starting_index($index);
678 my $end = find_ending_index($index);
679
680 my $role;
681 my $subsystem = $typevalue[$start];
682 if (length($subsystem) > 20) {
683 $subsystem = substr($subsystem, 0, 17);
684 $subsystem =~ s/\s*$//;
685 $subsystem = $subsystem . "...";
686 }
687
688 for ($i = $start + 1; $i < $end; $i++) {
689 my $tv = $typevalue[$i];
690 if ($tv =~ m/^(\C):\s*(.*)/) {
691 my $ptype = $1;
692 my $pvalue = $2;
693 if ($ptype eq "S") {
694 $role = $pvalue;
695 }
696 }
697 }
698
699 $role = lc($role);
700 if ($role eq "supported") {
701 $role = "supporter";
702 } elsif ($role eq "maintained") {
703 $role = "maintainer";
704 } elsif ($role eq "odd fixes") {
705 $role = "odd fixer";
706 } elsif ($role eq "orphan") {
707 $role = "orphan minder";
708 } elsif ($role eq "obsolete") {
709 $role = "obsolete minder";
710 } elsif ($role eq "buried alive in reporters") {
711 $role = "chief penguin";
712 }
713
714 return $role . ":" . $subsystem;
715}
716
717sub get_list_role {
718 my ($index) = @_;
719
720 my $i;
721 my $start = find_starting_index($index);
722 my $end = find_ending_index($index);
723
724 my $subsystem = $typevalue[$start];
725 if (length($subsystem) > 20) {
726 $subsystem = substr($subsystem, 0, 17);
727 $subsystem =~ s/\s*$//;
728 $subsystem = $subsystem . "...";
729 }
730
731 if ($subsystem eq "THE REST") {
732 $subsystem = "";
733 }
734
735 return $subsystem;
736}
737
Joe Perchescb7301c2009-04-07 20:40:12 -0700738sub add_categories {
739 my ($index) = @_;
740
Joe Perchesb7816552009-09-21 17:04:24 -0700741 my $i;
742 my $start = find_starting_index($index);
743 my $end = find_ending_index($index);
744
745 push(@subsystem, $typevalue[$start]);
746
747 for ($i = $start + 1; $i < $end; $i++) {
748 my $tv = $typevalue[$i];
Joe Perches290603c2009-06-16 15:33:58 -0700749 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700750 my $ptype = $1;
751 my $pvalue = $2;
752 if ($ptype eq "L") {
Joe Perches290603c2009-06-16 15:33:58 -0700753 my $list_address = $pvalue;
754 my $list_additional = "";
Joe Perches3c7385b2009-12-14 18:00:46 -0800755 my $list_role = get_list_role($i);
756
757 if ($list_role ne "") {
758 $list_role = ":" . $list_role;
759 }
Joe Perches290603c2009-06-16 15:33:58 -0700760 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
761 $list_address = $1;
762 $list_additional = $2;
763 }
Joe Perchesbdf7c682009-06-16 15:33:59 -0700764 if ($list_additional =~ m/subscribers-only/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700765 if ($email_subscriber_list) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800766 push(@list_to, [$list_address, "subscriber list${list_role}"]);
Joe Perchescb7301c2009-04-07 20:40:12 -0700767 }
768 } else {
769 if ($email_list) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800770 push(@list_to, [$list_address, "open list${list_role}"]);
Joe Perchescb7301c2009-04-07 20:40:12 -0700771 }
772 }
773 } elsif ($ptype eq "M") {
Joe Perches0e70e832009-09-21 17:04:20 -0700774 my ($name, $address) = parse_email($pvalue);
775 if ($name eq "") {
Joe Perchesb7816552009-09-21 17:04:24 -0700776 if ($i > 0) {
777 my $tv = $typevalue[$i - 1];
Joe Perches0e70e832009-09-21 17:04:20 -0700778 if ($tv =~ m/^(\C):\s*(.*)/) {
779 if ($1 eq "P") {
780 $name = $2;
Joe Perchesa8af2432009-12-14 18:00:49 -0800781 $pvalue = format_email($name, $address, $email_usename);
Joe Perches5f2441e2009-06-16 15:34:02 -0700782 }
783 }
784 }
785 }
Joe Perches0e70e832009-09-21 17:04:20 -0700786 if ($email_maintainer) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800787 my $role = get_maintainer_role($i);
788 push_email_addresses($pvalue, $role);
Joe Perchescb7301c2009-04-07 20:40:12 -0700789 }
790 } elsif ($ptype eq "T") {
791 push(@scm, $pvalue);
792 } elsif ($ptype eq "W") {
793 push(@web, $pvalue);
794 } elsif ($ptype eq "S") {
795 push(@status, $pvalue);
796 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700797 }
798 }
799}
800
Joe Perches11ecf532009-09-21 17:04:22 -0700801my %email_hash_name;
802my %email_hash_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700803
Joe Perches11ecf532009-09-21 17:04:22 -0700804sub email_inuse {
805 my ($name, $address) = @_;
Joe Perches0e70e832009-09-21 17:04:20 -0700806
Joe Perches11ecf532009-09-21 17:04:22 -0700807 return 1 if (($name eq "") && ($address eq ""));
808 return 1 if (($name ne "") && exists($email_hash_name{$name}));
809 return 1 if (($address ne "") && exists($email_hash_address{$address}));
810
Joe Perches0e70e832009-09-21 17:04:20 -0700811 return 0;
812}
813
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700814sub push_email_address {
Joe Perches3c7385b2009-12-14 18:00:46 -0800815 my ($line, $role) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700816
Joe Perches0e70e832009-09-21 17:04:20 -0700817 my ($name, $address) = parse_email($line);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700818
Joe Perchesb7816552009-09-21 17:04:24 -0700819 if ($address eq "") {
820 return 0;
821 }
822
Joe Perches11ecf532009-09-21 17:04:22 -0700823 if (!$email_remove_duplicates) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800824 push(@email_to, [format_email($name, $address, $email_usename), $role]);
Joe Perches11ecf532009-09-21 17:04:22 -0700825 } elsif (!email_inuse($name, $address)) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800826 push(@email_to, [format_email($name, $address, $email_usename), $role]);
Joe Perches11ecf532009-09-21 17:04:22 -0700827 $email_hash_name{$name}++;
828 $email_hash_address{$address}++;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700829 }
Joe Perchesb7816552009-09-21 17:04:24 -0700830
831 return 1;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700832}
833
834sub push_email_addresses {
Joe Perches3c7385b2009-12-14 18:00:46 -0800835 my ($address, $role) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700836
837 my @address_list = ();
838
Joe Perches5f2441e2009-06-16 15:34:02 -0700839 if (rfc822_valid($address)) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800840 push_email_address($address, $role);
Joe Perches5f2441e2009-06-16 15:34:02 -0700841 } elsif (@address_list = rfc822_validlist($address)) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700842 my $array_count = shift(@address_list);
843 while (my $entry = shift(@address_list)) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800844 push_email_address($entry, $role);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700845 }
Joe Perches5f2441e2009-06-16 15:34:02 -0700846 } else {
Joe Perches3c7385b2009-12-14 18:00:46 -0800847 if (!push_email_address($address, $role)) {
Joe Perchesb7816552009-09-21 17:04:24 -0700848 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
849 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700850 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700851}
852
Joe Perches3c7385b2009-12-14 18:00:46 -0800853sub add_role {
854 my ($line, $role) = @_;
855
856 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -0800857 my $email = format_email($name, $address, $email_usename);
Joe Perches3c7385b2009-12-14 18:00:46 -0800858
859 foreach my $entry (@email_to) {
860 if ($email_remove_duplicates) {
861 my ($entry_name, $entry_address) = parse_email($entry->[0]);
Joe Perches03372db2010-03-05 13:43:00 -0800862 if (($name eq $entry_name || $address eq $entry_address)
863 && ($role eq "" || !($entry->[1] =~ m/$role/))
864 ) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800865 if ($entry->[1] eq "") {
866 $entry->[1] = "$role";
867 } else {
868 $entry->[1] = "$entry->[1],$role";
869 }
870 }
871 } else {
Joe Perches03372db2010-03-05 13:43:00 -0800872 if ($email eq $entry->[0]
873 && ($role eq "" || !($entry->[1] =~ m/$role/))
874 ) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800875 if ($entry->[1] eq "") {
876 $entry->[1] = "$role";
877 } else {
878 $entry->[1] = "$entry->[1],$role";
879 }
880 }
881 }
882 }
883}
884
Joe Perchescb7301c2009-04-07 20:40:12 -0700885sub which {
886 my ($bin) = @_;
887
Joe Perchesf5f50782009-06-16 15:34:00 -0700888 foreach my $path (split(/:/, $ENV{PATH})) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700889 if (-e "$path/$bin") {
890 return "$path/$bin";
891 }
892 }
893
894 return "";
895}
896
Joe Perches8cbb3a72009-09-21 17:04:21 -0700897sub mailmap {
Joe Perchesa8af2432009-12-14 18:00:49 -0800898 my (@lines) = @_;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700899 my %hash;
900
901 foreach my $line (@lines) {
902 my ($name, $address) = parse_email($line);
903 if (!exists($hash{$name})) {
904 $hash{$name} = $address;
Joe Perches11ecf532009-09-21 17:04:22 -0700905 } elsif ($address ne $hash{$name}) {
906 $address = $hash{$name};
Joe Perchesa8af2432009-12-14 18:00:49 -0800907 $line = format_email($name, $address, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700908 }
909 if (exists($mailmap{$name})) {
910 my $obj = $mailmap{$name};
911 foreach my $map_address (@$obj) {
912 if (($map_address eq $address) &&
913 ($map_address ne $hash{$name})) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800914 $line = format_email($name, $hash{$name}, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700915 }
916 }
917 }
918 }
919
920 return @lines;
921}
922
Joe Perches60db31a2009-12-14 18:00:50 -0800923sub git_execute_cmd {
924 my ($cmd) = @_;
925 my @lines = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700926
Joe Perches60db31a2009-12-14 18:00:50 -0800927 my $output = `$cmd`;
928 $output =~ s/^\s*//gm;
929 @lines = split("\n", $output);
930
931 return @lines;
Joe Perchesa8af2432009-12-14 18:00:49 -0800932}
933
Joe Perches60db31a2009-12-14 18:00:50 -0800934sub hg_execute_cmd {
Joe Perchesa8af2432009-12-14 18:00:49 -0800935 my ($cmd) = @_;
Joe Perches60db31a2009-12-14 18:00:50 -0800936 my @lines = ();
Joe Perchesa8af2432009-12-14 18:00:49 -0800937
Joe Perches60db31a2009-12-14 18:00:50 -0800938 my $output = `$cmd`;
939 @lines = split("\n", $output);
940
941 return @lines;
942}
943
944sub vcs_find_signers {
945 my ($cmd) = @_;
Joe Perchesa8af2432009-12-14 18:00:49 -0800946 my @lines = ();
947 my $commits;
948
Joe Perches60db31a2009-12-14 18:00:50 -0800949 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
Joe Perchescb7301c2009-04-07 20:40:12 -0700950
Joe Perches60db31a2009-12-14 18:00:50 -0800951 my $pattern = $VCS_cmds{"commit_pattern"};
Joe Perchescb7301c2009-04-07 20:40:12 -0700952
Joe Perches60db31a2009-12-14 18:00:50 -0800953 $commits = grep(/$pattern/, @lines); # of commits
Joe Perchesafa81ee2009-07-29 15:04:28 -0700954
Joe Perches0e70e832009-09-21 17:04:20 -0700955 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
956 if (!$email_git_penguin_chiefs) {
957 @lines = grep(!/${penguin_chiefs}/i, @lines);
958 }
959 # cut -f2- -d":"
960 s/.*:\s*(.+)\s*/$1/ for (@lines);
961
Joe Perchesa8af2432009-12-14 18:00:49 -0800962## Reformat email addresses (with names) to avoid badly written signatures
963
Joe Perches3c7385b2009-12-14 18:00:46 -0800964 foreach my $line (@lines) {
965 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -0800966 $line = format_email($name, $address, 1);
967 }
968
969 return ($commits, @lines);
970}
971
Joe Perches60db31a2009-12-14 18:00:50 -0800972sub vcs_save_commits {
973 my ($cmd) = @_;
974 my @lines = ();
975 my @commits = ();
976
977 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
978
979 foreach my $line (@lines) {
980 if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
981 push(@commits, $1);
982 }
983 }
984
985 return @commits;
986}
987
988sub vcs_blame {
989 my ($file) = @_;
990 my $cmd;
991 my @commits = ();
992
993 return @commits if (!(-f $file));
994
995 if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
996 my @all_commits = ();
997
998 $cmd = $VCS_cmds{"blame_file_cmd"};
999 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1000 @all_commits = vcs_save_commits($cmd);
1001
1002 foreach my $file_range_diff (@range) {
1003 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1004 my $diff_file = $1;
1005 my $diff_start = $2;
1006 my $diff_length = $3;
1007 next if ("$file" ne "$diff_file");
1008 for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
1009 push(@commits, $all_commits[$i]);
1010 }
1011 }
1012 } elsif (@range) {
1013 foreach my $file_range_diff (@range) {
1014 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1015 my $diff_file = $1;
1016 my $diff_start = $2;
1017 my $diff_length = $3;
1018 next if ("$file" ne "$diff_file");
1019 $cmd = $VCS_cmds{"blame_range_cmd"};
1020 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1021 push(@commits, vcs_save_commits($cmd));
1022 }
1023 } else {
1024 $cmd = $VCS_cmds{"blame_file_cmd"};
1025 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1026 @commits = vcs_save_commits($cmd);
1027 }
1028
1029 return @commits;
1030}
1031
1032my $printed_novcs = 0;
1033sub vcs_exists {
1034 %VCS_cmds = %VCS_cmds_git;
1035 return 1 if eval $VCS_cmds{"available"};
1036 %VCS_cmds = %VCS_cmds_hg;
1037 return 1 if eval $VCS_cmds{"available"};
1038 %VCS_cmds = ();
1039 if (!$printed_novcs) {
1040 warn("$P: No supported VCS found. Add --nogit to options?\n");
1041 warn("Using a git repository produces better results.\n");
1042 warn("Try Linus Torvalds' latest git repository using:\n");
1043 warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
1044 $printed_novcs = 1;
1045 }
1046 return 0;
1047}
1048
1049sub vcs_assign {
Joe Perchesa8af2432009-12-14 18:00:49 -08001050 my ($role, $divisor, @lines) = @_;
1051
1052 my %hash;
1053 my $count = 0;
1054
Joe Perchesa8af2432009-12-14 18:00:49 -08001055 return if (@lines <= 0);
1056
1057 if ($divisor <= 0) {
Joe Perches60db31a2009-12-14 18:00:50 -08001058 warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
Joe Perchesa8af2432009-12-14 18:00:49 -08001059 $divisor = 1;
Joe Perches3c7385b2009-12-14 18:00:46 -08001060 }
Joe Perches8cbb3a72009-09-21 17:04:21 -07001061
Joe Perches11ecf532009-09-21 17:04:22 -07001062 if ($email_remove_duplicates) {
1063 @lines = mailmap(@lines);
1064 }
Joe Perches0e70e832009-09-21 17:04:20 -07001065
Joe Perches0e70e832009-09-21 17:04:20 -07001066 @lines = sort(@lines);
Joe Perchesafa81ee2009-07-29 15:04:28 -07001067
Joe Perches11ecf532009-09-21 17:04:22 -07001068 # uniq -c
1069 $hash{$_}++ for @lines;
1070
1071 # sort -rn
1072 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
1073 my $sign_offs = $hash{$line};
Joe Perchesa8af2432009-12-14 18:00:49 -08001074 my $percent = $sign_offs * 100 / $divisor;
Joe Perches3c7385b2009-12-14 18:00:46 -08001075
Joe Perchesa8af2432009-12-14 18:00:49 -08001076 $percent = 100 if ($percent > 100);
Joe Perches11ecf532009-09-21 17:04:22 -07001077 $count++;
1078 last if ($sign_offs < $email_git_min_signatures ||
1079 $count > $email_git_max_maintainers ||
Joe Perchesa8af2432009-12-14 18:00:49 -08001080 $percent < $email_git_min_percent);
Joe Perches3c7385b2009-12-14 18:00:46 -08001081 push_email_address($line, '');
Joe Perches3c7385b2009-12-14 18:00:46 -08001082 if ($output_rolestats) {
Joe Perchesa8af2432009-12-14 18:00:49 -08001083 my $fmt_percent = sprintf("%.0f", $percent);
1084 add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
1085 } else {
1086 add_role($line, $role);
Joe Perches3c7385b2009-12-14 18:00:46 -08001087 }
Joe Perchesf5492662009-09-21 17:04:13 -07001088 }
1089}
1090
Joe Perches60db31a2009-12-14 18:00:50 -08001091sub vcs_file_signoffs {
Joe Perchesa8af2432009-12-14 18:00:49 -08001092 my ($file) = @_;
1093
1094 my @signers = ();
Joe Perches60db31a2009-12-14 18:00:50 -08001095 my $commits;
Joe Perchesa8af2432009-12-14 18:00:49 -08001096
Joe Perches60db31a2009-12-14 18:00:50 -08001097 return if (!vcs_exists());
Joe Perchesa8af2432009-12-14 18:00:49 -08001098
Joe Perches60db31a2009-12-14 18:00:50 -08001099 my $cmd = $VCS_cmds{"find_signers_cmd"};
1100 $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
1101
1102 ($commits, @signers) = vcs_find_signers($cmd);
1103 vcs_assign("commit_signer", $commits, @signers);
Joe Perchesa8af2432009-12-14 18:00:49 -08001104}
1105
Joe Perches60db31a2009-12-14 18:00:50 -08001106sub vcs_file_blame {
Joe Perchesf5492662009-09-21 17:04:13 -07001107 my ($file) = @_;
1108
Joe Perches60db31a2009-12-14 18:00:50 -08001109 my @signers = ();
Joe Perchesa8af2432009-12-14 18:00:49 -08001110 my @commits = ();
Joe Perchesa8af2432009-12-14 18:00:49 -08001111 my $total_commits;
Joe Perchesf5492662009-09-21 17:04:13 -07001112
Joe Perches60db31a2009-12-14 18:00:50 -08001113 return if (!vcs_exists());
Joe Perchesf5492662009-09-21 17:04:13 -07001114
Joe Perches60db31a2009-12-14 18:00:50 -08001115 @commits = vcs_blame($file);
Joe Perchesf5492662009-09-21 17:04:13 -07001116 @commits = uniq(@commits);
Joe Perchesa8af2432009-12-14 18:00:49 -08001117 $total_commits = @commits;
1118
Joe Perchesf5492662009-09-21 17:04:13 -07001119 foreach my $commit (@commits) {
Joe Perchesa8af2432009-12-14 18:00:49 -08001120 my $commit_count;
1121 my @commit_signers = ();
Joe Perchesf5492662009-09-21 17:04:13 -07001122
Joe Perches60db31a2009-12-14 18:00:50 -08001123 my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1124 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1125
1126 ($commit_count, @commit_signers) = vcs_find_signers($cmd);
1127 push(@signers, @commit_signers);
Joe Perchesf5492662009-09-21 17:04:13 -07001128 }
1129
Joe Perchesa8af2432009-12-14 18:00:49 -08001130 if ($from_filename) {
Joe Perches60db31a2009-12-14 18:00:50 -08001131 vcs_assign("commits", $total_commits, @signers);
Joe Perchesa8af2432009-12-14 18:00:49 -08001132 } else {
Joe Perches60db31a2009-12-14 18:00:50 -08001133 vcs_assign("modified commits", $total_commits, @signers);
Joe Perchesf5492662009-09-21 17:04:13 -07001134 }
Joe Perchescb7301c2009-04-07 20:40:12 -07001135}
1136
1137sub uniq {
Joe Perchesa8af2432009-12-14 18:00:49 -08001138 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001139
1140 my %saw;
1141 @parms = grep(!$saw{$_}++, @parms);
1142 return @parms;
1143}
1144
1145sub sort_and_uniq {
Joe Perchesa8af2432009-12-14 18:00:49 -08001146 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001147
1148 my %saw;
1149 @parms = sort @parms;
1150 @parms = grep(!$saw{$_}++, @parms);
1151 return @parms;
1152}
1153
Joe Perches03372db2010-03-05 13:43:00 -08001154sub clean_file_emails {
1155 my (@file_emails) = @_;
1156 my @fmt_emails = ();
1157
1158 foreach my $email (@file_emails) {
1159 $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
1160 my ($name, $address) = parse_email($email);
1161 if ($name eq '"[,\.]"') {
1162 $name = "";
1163 }
1164
1165 my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
1166 if (@nw > 2) {
1167 my $first = $nw[@nw - 3];
1168 my $middle = $nw[@nw - 2];
1169 my $last = $nw[@nw - 1];
1170
1171 if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
1172 (length($first) == 2 && substr($first, -1) eq ".")) ||
1173 (length($middle) == 1 ||
1174 (length($middle) == 2 && substr($middle, -1) eq "."))) {
1175 $name = "$first $middle $last";
1176 } else {
1177 $name = "$middle $last";
1178 }
1179 }
1180
1181 if (substr($name, -1) =~ /[,\.]/) {
1182 $name = substr($name, 0, length($name) - 1);
1183 } elsif (substr($name, -2) =~ /[,\.]"/) {
1184 $name = substr($name, 0, length($name) - 2) . '"';
1185 }
1186
1187 if (substr($name, 0, 1) =~ /[,\.]/) {
1188 $name = substr($name, 1, length($name) - 1);
1189 } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
1190 $name = '"' . substr($name, 2, length($name) - 2);
1191 }
1192
1193 my $fmt_email = format_email($name, $address, $email_usename);
1194 push(@fmt_emails, $fmt_email);
1195 }
1196 return @fmt_emails;
1197}
1198
Joe Perches3c7385b2009-12-14 18:00:46 -08001199sub merge_email {
1200 my @lines;
1201 my %saw;
1202
1203 for (@_) {
1204 my ($address, $role) = @$_;
1205 if (!$saw{$address}) {
1206 if ($output_roles) {
Joe Perches60db31a2009-12-14 18:00:50 -08001207 push(@lines, "$address ($role)");
Joe Perches3c7385b2009-12-14 18:00:46 -08001208 } else {
Joe Perches60db31a2009-12-14 18:00:50 -08001209 push(@lines, $address);
Joe Perches3c7385b2009-12-14 18:00:46 -08001210 }
1211 $saw{$address} = 1;
1212 }
1213 }
1214
1215 return @lines;
1216}
1217
Joe Perchescb7301c2009-04-07 20:40:12 -07001218sub output {
Joe Perchesa8af2432009-12-14 18:00:49 -08001219 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001220
1221 if ($output_multiline) {
1222 foreach my $line (@parms) {
1223 print("${line}\n");
1224 }
1225 } else {
1226 print(join($output_separator, @parms));
1227 print("\n");
1228 }
1229}
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001230
1231my $rfc822re;
1232
1233sub make_rfc822re {
1234# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
1235# comment. We must allow for rfc822_lwsp (or comments) after each of these.
1236# This regexp will only work on addresses which have had comments stripped
1237# and replaced with rfc822_lwsp.
1238
1239 my $specials = '()<>@,;:\\\\".\\[\\]';
1240 my $controls = '\\000-\\037\\177';
1241
1242 my $dtext = "[^\\[\\]\\r\\\\]";
1243 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1244
1245 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1246
1247# Use zero-width assertion to spot the limit of an atom. A simple
1248# $rfc822_lwsp* causes the regexp engine to hang occasionally.
1249 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
1250 my $word = "(?:$atom|$quoted_string)";
1251 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
1252
1253 my $sub_domain = "(?:$atom|$domain_literal)";
1254 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1255
1256 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1257
1258 my $phrase = "$word*";
1259 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
1260 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
1261 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
1262
1263 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1264 my $address = "(?:$mailbox|$group)";
1265
1266 return "$rfc822_lwsp*$address";
1267}
1268
1269sub rfc822_strip_comments {
1270 my $s = shift;
1271# Recursively remove comments, and replace with a single space. The simpler
1272# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
1273# chars in atoms, for example.
1274
1275 while ($s =~ s/^((?:[^"\\]|\\.)*
1276 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1277 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1278 return $s;
1279}
1280
1281# valid: returns true if the parameter is an RFC822 valid address
1282#
1283sub rfc822_valid ($) {
1284 my $s = rfc822_strip_comments(shift);
1285
1286 if (!$rfc822re) {
1287 $rfc822re = make_rfc822re();
1288 }
1289
1290 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1291}
1292
1293# validlist: In scalar context, returns true if the parameter is an RFC822
1294# valid list of addresses.
1295#
1296# In list context, returns an empty list on failure (an invalid
1297# address was found); otherwise a list whose first element is the
1298# number of addresses found and whose remaining elements are the
1299# addresses. This is needed to disambiguate failure (invalid)
1300# from success with no addresses found, because an empty string is
1301# a valid list.
1302
1303sub rfc822_validlist ($) {
1304 my $s = rfc822_strip_comments(shift);
1305
1306 if (!$rfc822re) {
1307 $rfc822re = make_rfc822re();
1308 }
1309 # * null list items are valid according to the RFC
1310 # * the '1' business is to aid in distinguishing failure from no results
1311
1312 my @r;
1313 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
1314 $s =~ m/^$rfc822_char*$/) {
Joe Perches5f2441e2009-06-16 15:34:02 -07001315 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
Joe Perches60db31a2009-12-14 18:00:50 -08001316 push(@r, $1);
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001317 }
1318 return wantarray ? (scalar(@r), @r) : 1;
1319 }
Joe Perches60db31a2009-12-14 18:00:50 -08001320 return wantarray ? () : 0;
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001321}