blob: f66018d1d5eea6a0674ffd5ea680b2b171acf1f5 [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;
Joe Perchese4d26b02010-05-24 14:33:17 -070028my $email_git_all_signature_types = 1;
Joe Perches60db31a2009-12-14 18:00:50 -080029my $email_git_blame = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070030my $email_git_min_signatures = 1;
31my $email_git_max_maintainers = 5;
Joe Perchesafa81ee2009-07-29 15:04:28 -070032my $email_git_min_percent = 5;
Joe Perchescb7301c2009-04-07 20:40:12 -070033my $email_git_since = "1-year-ago";
Joe Perches60db31a2009-12-14 18:00:50 -080034my $email_hg_since = "-365";
Joe Perches11ecf532009-09-21 17:04:22 -070035my $email_remove_duplicates = 1;
Joe Perchescb7301c2009-04-07 20:40:12 -070036my $output_multiline = 1;
37my $output_separator = ", ";
Joe Perches3c7385b2009-12-14 18:00:46 -080038my $output_roles = 0;
39my $output_rolestats = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070040my $scm = 0;
41my $web = 0;
42my $subsystem = 0;
43my $status = 0;
Joe Perchesdcf36a92009-10-26 16:49:47 -070044my $keywords = 1;
Joe Perches4b76c9d2010-03-05 13:43:03 -080045my $sections = 0;
Joe Perches03372db2010-03-05 13:43:00 -080046my $file_emails = 0;
Joe Perches4a7fdb52009-04-10 12:28:57 -070047my $from_filename = 0;
Joe Perches3fb55652009-09-21 17:04:17 -070048my $pattern_depth = 0;
Joe Perchescb7301c2009-04-07 20:40:12 -070049my $version = 0;
50my $help = 0;
51
52my $exit = 0;
53
54my @penguin_chief = ();
Joe Perchese4d26b02010-05-24 14:33:17 -070055push(@penguin_chief, "Linus Torvalds:torvalds\@linux-foundation.org");
Joe Perchescb7301c2009-04-07 20:40:12 -070056#Andrew wants in on most everything - 2009/01/14
Joe Perchese4d26b02010-05-24 14:33:17 -070057#push(@penguin_chief, "Andrew Morton:akpm\@linux-foundation.org");
Joe Perchescb7301c2009-04-07 20:40:12 -070058
59my @penguin_chief_names = ();
60foreach my $chief (@penguin_chief) {
61 if ($chief =~ m/^(.*):(.*)/) {
62 my $chief_name = $1;
63 my $chief_addr = $2;
64 push(@penguin_chief_names, $chief_name);
65 }
66}
Joe Perchese4d26b02010-05-24 14:33:17 -070067my $penguin_chiefs = "\(" . join("|", @penguin_chief_names) . "\)";
68
69# Signature types of people who are either
70# a) responsible for the code in question, or
71# b) familiar enough with it to give relevant feedback
72my @signature_tags = ();
73push(@signature_tags, "Signed-off-by:");
74push(@signature_tags, "Reviewed-by:");
75push(@signature_tags, "Acked-by:");
76my $signaturePattern = "\(" . join("|", @signature_tags) . "\)";
Joe Perchescb7301c2009-04-07 20:40:12 -070077
Joe Perches5f2441e2009-06-16 15:34:02 -070078# rfc822 email address - preloaded methods go here.
Joe Perches1b5e1cf2009-06-16 15:34:01 -070079my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
Joe Perchesdf4cc032009-06-16 15:34:04 -070080my $rfc822_char = '[\\000-\\377]';
Joe Perches1b5e1cf2009-06-16 15:34:01 -070081
Joe Perches60db31a2009-12-14 18:00:50 -080082# VCS command support: class-like functions and strings
83
84my %VCS_cmds;
85
86my %VCS_cmds_git = (
87 "execute_cmd" => \&git_execute_cmd,
88 "available" => '(which("git") ne "") && (-d ".git")',
Richard Kennedy99cf6112010-02-02 13:44:07 -080089 "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file",
90 "find_commit_signers_cmd" => "git log --no-color -1 \$commit",
Joe Perches60db31a2009-12-14 18:00:50 -080091 "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
92 "blame_file_cmd" => "git blame -l \$file",
93 "commit_pattern" => "^commit [0-9a-f]{40,40}",
94 "blame_commit_pattern" => "^([0-9a-f]+) "
95);
96
97my %VCS_cmds_hg = (
98 "execute_cmd" => \&hg_execute_cmd,
99 "available" => '(which("hg") ne "") && (-d ".hg")',
100 "find_signers_cmd" =>
101 "hg log --date=\$email_hg_since" .
102 " --template='commit {node}\\n{desc}\\n' -- \$file",
103 "find_commit_signers_cmd" => "hg log --template='{desc}\\n' -r \$commit",
104 "blame_range_cmd" => "", # not supported
105 "blame_file_cmd" => "hg blame -c \$file",
106 "commit_pattern" => "^commit [0-9a-f]{40,40}",
107 "blame_commit_pattern" => "^([0-9a-f]+):"
108);
109
Joe Perchescb7301c2009-04-07 20:40:12 -0700110if (!GetOptions(
111 'email!' => \$email,
112 'git!' => \$email_git,
Joe Perchese4d26b02010-05-24 14:33:17 -0700113 'git-all-signature-types!' => \$email_git_all_signature_types,
Joe Perches60db31a2009-12-14 18:00:50 -0800114 'git-blame!' => \$email_git_blame,
Joe Perchescb7301c2009-04-07 20:40:12 -0700115 'git-chief-penguins!' => \$email_git_penguin_chiefs,
116 'git-min-signatures=i' => \$email_git_min_signatures,
117 'git-max-maintainers=i' => \$email_git_max_maintainers,
Joe Perchesafa81ee2009-07-29 15:04:28 -0700118 'git-min-percent=i' => \$email_git_min_percent,
Joe Perchescb7301c2009-04-07 20:40:12 -0700119 'git-since=s' => \$email_git_since,
Joe Perches60db31a2009-12-14 18:00:50 -0800120 'hg-since=s' => \$email_hg_since,
Joe Perches11ecf532009-09-21 17:04:22 -0700121 'remove-duplicates!' => \$email_remove_duplicates,
Joe Perchescb7301c2009-04-07 20:40:12 -0700122 'm!' => \$email_maintainer,
123 'n!' => \$email_usename,
124 'l!' => \$email_list,
125 's!' => \$email_subscriber_list,
126 'multiline!' => \$output_multiline,
Joe Perches3c7385b2009-12-14 18:00:46 -0800127 'roles!' => \$output_roles,
128 'rolestats!' => \$output_rolestats,
Joe Perchescb7301c2009-04-07 20:40:12 -0700129 'separator=s' => \$output_separator,
130 'subsystem!' => \$subsystem,
131 'status!' => \$status,
132 'scm!' => \$scm,
133 'web!' => \$web,
Joe Perches3fb55652009-09-21 17:04:17 -0700134 'pattern-depth=i' => \$pattern_depth,
Joe Perchesdcf36a92009-10-26 16:49:47 -0700135 'k|keywords!' => \$keywords,
Joe Perches4b76c9d2010-03-05 13:43:03 -0800136 'sections!' => \$sections,
Joe Perches03372db2010-03-05 13:43:00 -0800137 'fe|file-emails!' => \$file_emails,
Joe Perches4a7fdb52009-04-10 12:28:57 -0700138 'f|file' => \$from_filename,
Joe Perchescb7301c2009-04-07 20:40:12 -0700139 'v|version' => \$version,
Joe Perches64f77f32010-03-05 13:43:04 -0800140 'h|help|usage' => \$help,
Joe Perchescb7301c2009-04-07 20:40:12 -0700141 )) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800142 die "$P: invalid argument - use --help if necessary\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700143}
144
145if ($help != 0) {
146 usage();
147 exit 0;
148}
149
150if ($version != 0) {
151 print("${P} ${V}\n");
152 exit 0;
153}
154
Joe Perches64f77f32010-03-05 13:43:04 -0800155if (-t STDIN && !@ARGV) {
156 # We're talking to a terminal, but have no command line arguments.
157 die "$P: missing patchfile or -f file - use --help if necessary\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700158}
159
Joe Perches42498312009-09-21 17:04:21 -0700160if ($output_separator ne ", ") {
161 $output_multiline = 0;
162}
163
Joe Perches3c7385b2009-12-14 18:00:46 -0800164if ($output_rolestats) {
165 $output_roles = 1;
166}
167
Joe Perches4b76c9d2010-03-05 13:43:03 -0800168if ($sections) {
169 $email = 0;
170 $email_list = 0;
171 $scm = 0;
172 $status = 0;
173 $subsystem = 0;
174 $web = 0;
175 $keywords = 0;
176} else {
177 my $selections = $email + $scm + $status + $subsystem + $web;
178 if ($selections == 0) {
Joe Perches4b76c9d2010-03-05 13:43:03 -0800179 die "$P: Missing required option: email, scm, status, subsystem or web\n";
180 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700181}
182
Joe Perchesf5492662009-09-21 17:04:13 -0700183if ($email &&
184 ($email_maintainer + $email_list + $email_subscriber_list +
185 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700186 die "$P: Please select at least 1 email option\n";
187}
188
189if (!top_of_kernel_tree($lk_path)) {
190 die "$P: The current directory does not appear to be "
191 . "a linux kernel source tree.\n";
192}
193
Joe Perchese4d26b02010-05-24 14:33:17 -0700194if ($email_git_all_signature_types) {
195 $signaturePattern = "(.+?)[Bb][Yy]:";
196}
197
Joe Perchescb7301c2009-04-07 20:40:12 -0700198## Read MAINTAINERS for type/value pairs
199
200my @typevalue = ();
Joe Perchesdcf36a92009-10-26 16:49:47 -0700201my %keyword_hash;
202
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800203open (my $maint, '<', "${lk_path}MAINTAINERS")
204 or die "$P: Can't open MAINTAINERS: $!\n";
205while (<$maint>) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700206 my $line = $_;
207
208 if ($line =~ m/^(\C):\s*(.*)/) {
209 my $type = $1;
210 my $value = $2;
211
212 ##Filename pattern matching
213 if ($type eq "F" || $type eq "X") {
214 $value =~ s@\.@\\\.@g; ##Convert . to \.
215 $value =~ s/\*/\.\*/g; ##Convert * to .*
216 $value =~ s/\?/\./g; ##Convert ? to .
Joe Perches870020f2009-07-29 15:04:28 -0700217 ##if pattern is a directory and it lacks a trailing slash, add one
218 if ((-d $value)) {
219 $value =~ s@([^/])$@$1/@;
220 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700221 } elsif ($type eq "K") {
222 $keyword_hash{@typevalue} = $value;
Joe Perchescb7301c2009-04-07 20:40:12 -0700223 }
224 push(@typevalue, "$type:$value");
225 } elsif (!/^(\s)*$/) {
226 $line =~ s/\n$//g;
227 push(@typevalue, $line);
228 }
229}
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800230close($maint);
Joe Perchescb7301c2009-04-07 20:40:12 -0700231
Joe Perches8cbb3a72009-09-21 17:04:21 -0700232my %mailmap;
233
Joe Perches11ecf532009-09-21 17:04:22 -0700234if ($email_remove_duplicates) {
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800235 open(my $mailmap, '<', "${lk_path}.mailmap")
236 or warn "$P: Can't open .mailmap: $!\n";
237 while (<$mailmap>) {
Joe Perches11ecf532009-09-21 17:04:22 -0700238 my $line = $_;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700239
Joe Perches11ecf532009-09-21 17:04:22 -0700240 next if ($line =~ m/^\s*#/);
241 next if ($line =~ m/^\s*$/);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700242
Joe Perches11ecf532009-09-21 17:04:22 -0700243 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -0800244 $line = format_email($name, $address, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700245
Joe Perches11ecf532009-09-21 17:04:22 -0700246 next if ($line =~ m/^\s*$/);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700247
Joe Perches11ecf532009-09-21 17:04:22 -0700248 if (exists($mailmap{$name})) {
249 my $obj = $mailmap{$name};
250 push(@$obj, $address);
251 } else {
252 my @arr = ($address);
253 $mailmap{$name} = \@arr;
254 }
Joe Perches8cbb3a72009-09-21 17:04:21 -0700255 }
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800256 close($mailmap);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700257}
258
Joe Perches4a7fdb52009-04-10 12:28:57 -0700259## use the filenames on the command line or find the filenames in the patchfiles
Joe Perchescb7301c2009-04-07 20:40:12 -0700260
261my @files = ();
Joe Perchesf5492662009-09-21 17:04:13 -0700262my @range = ();
Joe Perchesdcf36a92009-10-26 16:49:47 -0700263my @keyword_tvi = ();
Joe Perches03372db2010-03-05 13:43:00 -0800264my @file_emails = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700265
Joe Perches64f77f32010-03-05 13:43:04 -0800266if (!@ARGV) {
267 push(@ARGV, "&STDIN");
268}
269
Joe Perches4a7fdb52009-04-10 12:28:57 -0700270foreach my $file (@ARGV) {
Joe Perches64f77f32010-03-05 13:43:04 -0800271 if ($file ne "&STDIN") {
272 ##if $file is a directory and it lacks a trailing slash, add one
273 if ((-d $file)) {
274 $file =~ s@([^/])$@$1/@;
275 } elsif (!(-f $file)) {
276 die "$P: file '${file}' not found\n";
277 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700278 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700279 if ($from_filename) {
280 push(@files, $file);
Joe Perches03372db2010-03-05 13:43:00 -0800281 if (-f $file && ($keywords || $file_emails)) {
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800282 open(my $f, '<', $file)
283 or die "$P: Can't open $file: $!\n";
284 my $text = do { local($/) ; <$f> };
285 close($f);
Joe Perches03372db2010-03-05 13:43:00 -0800286 if ($keywords) {
287 foreach my $line (keys %keyword_hash) {
288 if ($text =~ m/$keyword_hash{$line}/x) {
289 push(@keyword_tvi, $line);
290 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700291 }
292 }
Joe Perches03372db2010-03-05 13:43:00 -0800293 if ($file_emails) {
294 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;
295 push(@file_emails, clean_file_emails(@poss_addr));
296 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700297 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700298 } else {
299 my $file_cnt = @files;
Joe Perchesf5492662009-09-21 17:04:13 -0700300 my $lastfile;
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800301
Wolfram Sang3a4df132010-03-23 13:35:18 -0700302 open(my $patch, "< $file")
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800303 or die "$P: Can't open $file: $!\n";
304 while (<$patch>) {
Joe Perchesdcf36a92009-10-26 16:49:47 -0700305 my $patch_line = $_;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700306 if (m/^\+\+\+\s+(\S+)/) {
307 my $filename = $1;
308 $filename =~ s@^[^/]*/@@;
309 $filename =~ s@\n@@;
Joe Perchesf5492662009-09-21 17:04:13 -0700310 $lastfile = $filename;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700311 push(@files, $filename);
Joe Perchesf5492662009-09-21 17:04:13 -0700312 } elsif (m/^\@\@ -(\d+),(\d+)/) {
313 if ($email_git_blame) {
314 push(@range, "$lastfile:$1:$2");
315 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700316 } elsif ($keywords) {
317 foreach my $line (keys %keyword_hash) {
318 if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
319 push(@keyword_tvi, $line);
320 }
321 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700322 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700323 }
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800324 close($patch);
325
Joe Perches4a7fdb52009-04-10 12:28:57 -0700326 if ($file_cnt == @files) {
Joe Perches7f29fd272009-06-16 15:34:04 -0700327 warn "$P: file '${file}' doesn't appear to be a patch. "
Joe Perches4a7fdb52009-04-10 12:28:57 -0700328 . "Add -f to options?\n";
329 }
330 @files = sort_and_uniq(@files);
Joe Perchescb7301c2009-04-07 20:40:12 -0700331 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700332}
333
Joe Perches03372db2010-03-05 13:43:00 -0800334@file_emails = uniq(@file_emails);
335
Joe Perchescb7301c2009-04-07 20:40:12 -0700336my @email_to = ();
Joe Perches290603c2009-06-16 15:33:58 -0700337my @list_to = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700338my @scm = ();
339my @web = ();
340my @subsystem = ();
341my @status = ();
342
343# Find responsible parties
344
345foreach my $file (@files) {
346
Joe Perches272a8972010-01-08 14:42:48 -0800347 my %hash;
348 my $tvi = find_first_section();
349 while ($tvi < @typevalue) {
350 my $start = find_starting_index($tvi);
351 my $end = find_ending_index($tvi);
352 my $exclude = 0;
353 my $i;
Joe Perchescb7301c2009-04-07 20:40:12 -0700354
Joe Perches272a8972010-01-08 14:42:48 -0800355 #Do not match excluded file patterns
Joe Perchescb7301c2009-04-07 20:40:12 -0700356
Joe Perches272a8972010-01-08 14:42:48 -0800357 for ($i = $start; $i < $end; $i++) {
358 my $line = $typevalue[$i];
Joe Perches290603c2009-06-16 15:33:58 -0700359 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700360 my $type = $1;
361 my $value = $2;
Joe Perches272a8972010-01-08 14:42:48 -0800362 if ($type eq 'X') {
Joe Perchescb7301c2009-04-07 20:40:12 -0700363 if (file_match_pattern($file, $value)) {
Joe Perches272a8972010-01-08 14:42:48 -0800364 $exclude = 1;
Joe Perches3c840c12010-03-05 13:43:07 -0800365 last;
Joe Perches272a8972010-01-08 14:42:48 -0800366 }
367 }
368 }
369 }
370
371 if (!$exclude) {
372 for ($i = $start; $i < $end; $i++) {
373 my $line = $typevalue[$i];
374 if ($line =~ m/^(\C):\s*(.*)/) {
375 my $type = $1;
376 my $value = $2;
377 if ($type eq 'F') {
378 if (file_match_pattern($file, $value)) {
379 my $value_pd = ($value =~ tr@/@@);
380 my $file_pd = ($file =~ tr@/@@);
381 $value_pd++ if (substr($value,-1,1) ne "/");
382 if ($pattern_depth == 0 ||
383 (($file_pd - $value_pd) < $pattern_depth)) {
384 $hash{$tvi} = $value_pd;
385 }
Joe Perches3fb55652009-09-21 17:04:17 -0700386 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700387 }
388 }
389 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700390 }
Joe Perches272a8972010-01-08 14:42:48 -0800391
Joe Perches3c840c12010-03-05 13:43:07 -0800392 $tvi = $end + 1;
Joe Perches272a8972010-01-08 14:42:48 -0800393 }
394
395 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
396 add_categories($line);
Joe Perches4b76c9d2010-03-05 13:43:03 -0800397 if ($sections) {
398 my $i;
399 my $start = find_starting_index($line);
400 my $end = find_ending_index($line);
401 for ($i = $start; $i < $end; $i++) {
402 my $line = $typevalue[$i];
403 if ($line =~ /^[FX]:/) { ##Restore file patterns
404 $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
405 $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
406 $line =~ s/\\\./\./g; ##Convert \. to .
407 $line =~ s/\.\*/\*/g; ##Convert .* to *
408 }
Joe Perchesf11e9a12010-03-05 13:43:03 -0800409 $line =~ s/^([A-Z]):/$1:\t/g;
Joe Perches4b76c9d2010-03-05 13:43:03 -0800410 print("$line\n");
411 }
Joe Perchesf11e9a12010-03-05 13:43:03 -0800412 print("\n");
Joe Perches4b76c9d2010-03-05 13:43:03 -0800413 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700414 }
415
Joe Perches4a7fdb52009-04-10 12:28:57 -0700416 if ($email && $email_git) {
Joe Perches60db31a2009-12-14 18:00:50 -0800417 vcs_file_signoffs($file);
Joe Perchescb7301c2009-04-07 20:40:12 -0700418 }
419
Joe Perchesf5492662009-09-21 17:04:13 -0700420 if ($email && $email_git_blame) {
Joe Perches60db31a2009-12-14 18:00:50 -0800421 vcs_file_blame($file);
Joe Perchesf5492662009-09-21 17:04:13 -0700422 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700423}
424
Joe Perchesdcf36a92009-10-26 16:49:47 -0700425if ($keywords) {
426 @keyword_tvi = sort_and_uniq(@keyword_tvi);
427 foreach my $line (@keyword_tvi) {
428 add_categories($line);
429 }
430}
431
Joe Perchesf5f50782009-06-16 15:34:00 -0700432if ($email) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700433 foreach my $chief (@penguin_chief) {
434 if ($chief =~ m/^(.*):(.*)/) {
Joe Perchesf5f50782009-06-16 15:34:00 -0700435 my $email_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700436
Joe Perchesa8af2432009-12-14 18:00:49 -0800437 $email_address = format_email($1, $2, $email_usename);
Joe Perchesf5f50782009-06-16 15:34:00 -0700438 if ($email_git_penguin_chiefs) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800439 push(@email_to, [$email_address, 'chief penguin']);
Joe Perchesf5f50782009-06-16 15:34:00 -0700440 } else {
Joe Perches3c7385b2009-12-14 18:00:46 -0800441 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700442 }
443 }
444 }
Joe Perches03372db2010-03-05 13:43:00 -0800445
446 foreach my $email (@file_emails) {
447 my ($name, $address) = parse_email($email);
448
449 my $tmp_email = format_email($name, $address, $email_usename);
450 push_email_address($tmp_email, '');
451 add_role($tmp_email, 'in file');
452 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700453}
454
Joe Perches290603c2009-06-16 15:33:58 -0700455if ($email || $email_list) {
456 my @to = ();
457 if ($email) {
458 @to = (@to, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700459 }
Joe Perches290603c2009-06-16 15:33:58 -0700460 if ($email_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700461 @to = (@to, @list_to);
Joe Perches290603c2009-06-16 15:33:58 -0700462 }
Joe Perches3c7385b2009-12-14 18:00:46 -0800463 output(merge_email(@to));
Joe Perchescb7301c2009-04-07 20:40:12 -0700464}
465
466if ($scm) {
Joe Perchesb7816552009-09-21 17:04:24 -0700467 @scm = uniq(@scm);
Joe Perchescb7301c2009-04-07 20:40:12 -0700468 output(@scm);
469}
470
471if ($status) {
Joe Perchesb7816552009-09-21 17:04:24 -0700472 @status = uniq(@status);
Joe Perchescb7301c2009-04-07 20:40:12 -0700473 output(@status);
474}
475
476if ($subsystem) {
Joe Perchesb7816552009-09-21 17:04:24 -0700477 @subsystem = uniq(@subsystem);
Joe Perchescb7301c2009-04-07 20:40:12 -0700478 output(@subsystem);
479}
480
481if ($web) {
Joe Perchesb7816552009-09-21 17:04:24 -0700482 @web = uniq(@web);
Joe Perchescb7301c2009-04-07 20:40:12 -0700483 output(@web);
484}
485
486exit($exit);
487
488sub file_match_pattern {
489 my ($file, $pattern) = @_;
490 if (substr($pattern, -1) eq "/") {
491 if ($file =~ m@^$pattern@) {
492 return 1;
493 }
494 } else {
495 if ($file =~ m@^$pattern@) {
496 my $s1 = ($file =~ tr@/@@);
497 my $s2 = ($pattern =~ tr@/@@);
498 if ($s1 == $s2) {
499 return 1;
500 }
501 }
502 }
503 return 0;
504}
505
506sub usage {
507 print <<EOT;
508usage: $P [options] patchfile
Joe Perches870020f2009-07-29 15:04:28 -0700509 $P [options] -f file|directory
Joe Perchescb7301c2009-04-07 20:40:12 -0700510version: $V
511
512MAINTAINER field selection options:
513 --email => print email address(es) if any
514 --git => include recent git \*-by: signers
Joe Perchese4d26b02010-05-24 14:33:17 -0700515 --git-all-signature-types => include signers regardless of signature type
516 or use only ${signaturePattern} signers (default: $email_git_all_signature_types)
Joe Perchescb7301c2009-04-07 20:40:12 -0700517 --git-chief-penguins => include ${penguin_chiefs}
Joe Perchese4d26b02010-05-24 14:33:17 -0700518 --git-min-signatures => number of signatures required (default: $email_git_min_signatures)
519 --git-max-maintainers => maximum maintainers to add (default: $email_git_max_maintainers)
520 --git-min-percent => minimum percentage of commits required (default: $email_git_min_percent)
Joe Perchesf5492662009-09-21 17:04:13 -0700521 --git-blame => use git blame to find modified commits for patch or file
Joe Perchese4d26b02010-05-24 14:33:17 -0700522 --git-since => git history to use (default: $email_git_since)
523 --hg-since => hg history to use (default: $email_hg_since)
Joe Perchescb7301c2009-04-07 20:40:12 -0700524 --m => include maintainer(s) if any
525 --n => include name 'Full Name <addr\@domain.tld>'
526 --l => include list(s) if any
527 --s => include subscriber only list(s) if any
Joe Perches11ecf532009-09-21 17:04:22 -0700528 --remove-duplicates => minimize duplicate email names/addresses
Joe Perches3c7385b2009-12-14 18:00:46 -0800529 --roles => show roles (status:subsystem, git-signer, list, etc...)
530 --rolestats => show roles and statistics (commits/total_commits, %)
Joe Perches03372db2010-03-05 13:43:00 -0800531 --file-emails => add email addresses found in -f file (default: 0 (off))
Joe Perchescb7301c2009-04-07 20:40:12 -0700532 --scm => print SCM tree(s) if any
533 --status => print status if any
534 --subsystem => print subsystem name if any
535 --web => print website(s) if any
536
537Output type options:
538 --separator [, ] => separator for multiple entries on 1 line
Joe Perches42498312009-09-21 17:04:21 -0700539 using --separator also sets --nomultiline if --separator is not [, ]
Joe Perchescb7301c2009-04-07 20:40:12 -0700540 --multiline => print 1 entry per line
541
Joe Perchescb7301c2009-04-07 20:40:12 -0700542Other options:
Joe Perches3fb55652009-09-21 17:04:17 -0700543 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
Joe Perchesdcf36a92009-10-26 16:49:47 -0700544 --keywords => scan patch for keywords (default: 1 (on))
Joe Perches4b76c9d2010-03-05 13:43:03 -0800545 --sections => print the entire subsystem sections with pattern matches
Joe Perchesf5f50782009-06-16 15:34:00 -0700546 --version => show version
Joe Perchescb7301c2009-04-07 20:40:12 -0700547 --help => show this help information
548
Joe Perches3fb55652009-09-21 17:04:17 -0700549Default options:
Joe Perches11ecf532009-09-21 17:04:22 -0700550 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
Joe Perches3fb55652009-09-21 17:04:17 -0700551
Joe Perches870020f2009-07-29 15:04:28 -0700552Notes:
553 Using "-f directory" may give unexpected results:
Joe Perchesf5492662009-09-21 17:04:13 -0700554 Used with "--git", git signators for _all_ files in and below
555 directory are examined as git recurses directories.
556 Any specified X: (exclude) pattern matches are _not_ ignored.
557 Used with "--nogit", directory is used as a pattern match,
Joe Perches60db31a2009-12-14 18:00:50 -0800558 no individual file within the directory or subdirectory
559 is matched.
Joe Perchesf5492662009-09-21 17:04:13 -0700560 Used with "--git-blame", does not iterate all files in directory
561 Using "--git-blame" is slow and may add old committers and authors
562 that are no longer active maintainers to the output.
Joe Perches3c7385b2009-12-14 18:00:46 -0800563 Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
564 other automated tools that expect only ["name"] <email address>
565 may not work because of additional output after <email address>.
566 Using "--rolestats" and "--git-blame" shows the #/total=% commits,
567 not the percentage of the entire file authored. # of commits is
568 not a good measure of amount of code authored. 1 major commit may
569 contain a thousand lines, 5 trivial commits may modify a single line.
Joe Perches60db31a2009-12-14 18:00:50 -0800570 If git is not installed, but mercurial (hg) is installed and an .hg
571 repository exists, the following options apply to mercurial:
572 --git,
573 --git-min-signatures, --git-max-maintainers, --git-min-percent, and
574 --git-blame
575 Use --hg-since not --git-since to control date selection
Joe Perchescb7301c2009-04-07 20:40:12 -0700576EOT
577}
578
579sub top_of_kernel_tree {
580 my ($lk_path) = @_;
581
582 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
583 $lk_path .= "/";
584 }
585 if ( (-f "${lk_path}COPYING")
586 && (-f "${lk_path}CREDITS")
587 && (-f "${lk_path}Kbuild")
588 && (-f "${lk_path}MAINTAINERS")
589 && (-f "${lk_path}Makefile")
590 && (-f "${lk_path}README")
591 && (-d "${lk_path}Documentation")
592 && (-d "${lk_path}arch")
593 && (-d "${lk_path}include")
594 && (-d "${lk_path}drivers")
595 && (-d "${lk_path}fs")
596 && (-d "${lk_path}init")
597 && (-d "${lk_path}ipc")
598 && (-d "${lk_path}kernel")
599 && (-d "${lk_path}lib")
600 && (-d "${lk_path}scripts")) {
601 return 1;
602 }
603 return 0;
604}
605
Joe Perches0e70e832009-09-21 17:04:20 -0700606sub parse_email {
607 my ($formatted_email) = @_;
608
609 my $name = "";
610 my $address = "";
611
Joe Perches11ecf532009-09-21 17:04:22 -0700612 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700613 $name = $1;
614 $address = $2;
Joe Perches11ecf532009-09-21 17:04:22 -0700615 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700616 $address = $1;
Joe Perchesb7816552009-09-21 17:04:24 -0700617 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700618 $address = $1;
619 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700620
621 $name =~ s/^\s+|\s+$//g;
Joe Perchesd7895042009-06-16 15:34:02 -0700622 $name =~ s/^\"|\"$//g;
Joe Perches0e70e832009-09-21 17:04:20 -0700623 $address =~ s/^\s+|\s+$//g;
Joe Perchescb7301c2009-04-07 20:40:12 -0700624
Stephen Hemmingera63ceb42010-03-05 13:43:06 -0800625 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
Joe Perchescb7301c2009-04-07 20:40:12 -0700626 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
Joe Perches0e70e832009-09-21 17:04:20 -0700627 $name = "\"$name\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700628 }
Joe Perches0e70e832009-09-21 17:04:20 -0700629
630 return ($name, $address);
631}
632
633sub format_email {
Joe Perchesa8af2432009-12-14 18:00:49 -0800634 my ($name, $address, $usename) = @_;
Joe Perches0e70e832009-09-21 17:04:20 -0700635
636 my $formatted_email;
637
638 $name =~ s/^\s+|\s+$//g;
639 $name =~ s/^\"|\"$//g;
640 $address =~ s/^\s+|\s+$//g;
641
Stephen Hemmingera63ceb42010-03-05 13:43:06 -0800642 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
Joe Perches0e70e832009-09-21 17:04:20 -0700643 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
644 $name = "\"$name\"";
645 }
646
Joe Perchesa8af2432009-12-14 18:00:49 -0800647 if ($usename) {
Joe Perches0e70e832009-09-21 17:04:20 -0700648 if ("$name" eq "") {
649 $formatted_email = "$address";
650 } else {
Joe Perchesa8af2432009-12-14 18:00:49 -0800651 $formatted_email = "$name <$address>";
Joe Perches0e70e832009-09-21 17:04:20 -0700652 }
653 } else {
654 $formatted_email = $address;
655 }
656
Joe Perchescb7301c2009-04-07 20:40:12 -0700657 return $formatted_email;
658}
659
Joe Perches272a8972010-01-08 14:42:48 -0800660sub find_first_section {
661 my $index = 0;
662
663 while ($index < @typevalue) {
664 my $tv = $typevalue[$index];
665 if (($tv =~ m/^(\C):\s*(.*)/)) {
666 last;
667 }
668 $index++;
669 }
670
671 return $index;
672}
673
Joe Perchesb7816552009-09-21 17:04:24 -0700674sub find_starting_index {
Joe Perchesb7816552009-09-21 17:04:24 -0700675 my ($index) = @_;
676
677 while ($index > 0) {
678 my $tv = $typevalue[$index];
679 if (!($tv =~ m/^(\C):\s*(.*)/)) {
680 last;
681 }
682 $index--;
683 }
684
685 return $index;
686}
687
688sub find_ending_index {
689 my ($index) = @_;
690
691 while ($index < @typevalue) {
692 my $tv = $typevalue[$index];
693 if (!($tv =~ m/^(\C):\s*(.*)/)) {
694 last;
695 }
696 $index++;
697 }
698
699 return $index;
700}
701
Joe Perches3c7385b2009-12-14 18:00:46 -0800702sub get_maintainer_role {
703 my ($index) = @_;
704
705 my $i;
706 my $start = find_starting_index($index);
707 my $end = find_ending_index($index);
708
709 my $role;
710 my $subsystem = $typevalue[$start];
711 if (length($subsystem) > 20) {
712 $subsystem = substr($subsystem, 0, 17);
713 $subsystem =~ s/\s*$//;
714 $subsystem = $subsystem . "...";
715 }
716
717 for ($i = $start + 1; $i < $end; $i++) {
718 my $tv = $typevalue[$i];
719 if ($tv =~ m/^(\C):\s*(.*)/) {
720 my $ptype = $1;
721 my $pvalue = $2;
722 if ($ptype eq "S") {
723 $role = $pvalue;
724 }
725 }
726 }
727
728 $role = lc($role);
729 if ($role eq "supported") {
730 $role = "supporter";
731 } elsif ($role eq "maintained") {
732 $role = "maintainer";
733 } elsif ($role eq "odd fixes") {
734 $role = "odd fixer";
735 } elsif ($role eq "orphan") {
736 $role = "orphan minder";
737 } elsif ($role eq "obsolete") {
738 $role = "obsolete minder";
739 } elsif ($role eq "buried alive in reporters") {
740 $role = "chief penguin";
741 }
742
743 return $role . ":" . $subsystem;
744}
745
746sub get_list_role {
747 my ($index) = @_;
748
749 my $i;
750 my $start = find_starting_index($index);
751 my $end = find_ending_index($index);
752
753 my $subsystem = $typevalue[$start];
754 if (length($subsystem) > 20) {
755 $subsystem = substr($subsystem, 0, 17);
756 $subsystem =~ s/\s*$//;
757 $subsystem = $subsystem . "...";
758 }
759
760 if ($subsystem eq "THE REST") {
761 $subsystem = "";
762 }
763
764 return $subsystem;
765}
766
Joe Perchescb7301c2009-04-07 20:40:12 -0700767sub add_categories {
768 my ($index) = @_;
769
Joe Perchesb7816552009-09-21 17:04:24 -0700770 my $i;
771 my $start = find_starting_index($index);
772 my $end = find_ending_index($index);
773
774 push(@subsystem, $typevalue[$start]);
775
776 for ($i = $start + 1; $i < $end; $i++) {
777 my $tv = $typevalue[$i];
Joe Perches290603c2009-06-16 15:33:58 -0700778 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700779 my $ptype = $1;
780 my $pvalue = $2;
781 if ($ptype eq "L") {
Joe Perches290603c2009-06-16 15:33:58 -0700782 my $list_address = $pvalue;
783 my $list_additional = "";
Joe Perches3c7385b2009-12-14 18:00:46 -0800784 my $list_role = get_list_role($i);
785
786 if ($list_role ne "") {
787 $list_role = ":" . $list_role;
788 }
Joe Perches290603c2009-06-16 15:33:58 -0700789 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
790 $list_address = $1;
791 $list_additional = $2;
792 }
Joe Perchesbdf7c682009-06-16 15:33:59 -0700793 if ($list_additional =~ m/subscribers-only/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700794 if ($email_subscriber_list) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800795 push(@list_to, [$list_address, "subscriber list${list_role}"]);
Joe Perchescb7301c2009-04-07 20:40:12 -0700796 }
797 } else {
798 if ($email_list) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800799 push(@list_to, [$list_address, "open list${list_role}"]);
Joe Perchescb7301c2009-04-07 20:40:12 -0700800 }
801 }
802 } elsif ($ptype eq "M") {
Joe Perches0e70e832009-09-21 17:04:20 -0700803 my ($name, $address) = parse_email($pvalue);
804 if ($name eq "") {
Joe Perchesb7816552009-09-21 17:04:24 -0700805 if ($i > 0) {
806 my $tv = $typevalue[$i - 1];
Joe Perches0e70e832009-09-21 17:04:20 -0700807 if ($tv =~ m/^(\C):\s*(.*)/) {
808 if ($1 eq "P") {
809 $name = $2;
Joe Perchesa8af2432009-12-14 18:00:49 -0800810 $pvalue = format_email($name, $address, $email_usename);
Joe Perches5f2441e2009-06-16 15:34:02 -0700811 }
812 }
813 }
814 }
Joe Perches0e70e832009-09-21 17:04:20 -0700815 if ($email_maintainer) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800816 my $role = get_maintainer_role($i);
817 push_email_addresses($pvalue, $role);
Joe Perchescb7301c2009-04-07 20:40:12 -0700818 }
819 } elsif ($ptype eq "T") {
820 push(@scm, $pvalue);
821 } elsif ($ptype eq "W") {
822 push(@web, $pvalue);
823 } elsif ($ptype eq "S") {
824 push(@status, $pvalue);
825 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700826 }
827 }
828}
829
Joe Perches11ecf532009-09-21 17:04:22 -0700830my %email_hash_name;
831my %email_hash_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700832
Joe Perches11ecf532009-09-21 17:04:22 -0700833sub email_inuse {
834 my ($name, $address) = @_;
Joe Perches0e70e832009-09-21 17:04:20 -0700835
Joe Perches11ecf532009-09-21 17:04:22 -0700836 return 1 if (($name eq "") && ($address eq ""));
837 return 1 if (($name ne "") && exists($email_hash_name{$name}));
838 return 1 if (($address ne "") && exists($email_hash_address{$address}));
839
Joe Perches0e70e832009-09-21 17:04:20 -0700840 return 0;
841}
842
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700843sub push_email_address {
Joe Perches3c7385b2009-12-14 18:00:46 -0800844 my ($line, $role) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700845
Joe Perches0e70e832009-09-21 17:04:20 -0700846 my ($name, $address) = parse_email($line);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700847
Joe Perchesb7816552009-09-21 17:04:24 -0700848 if ($address eq "") {
849 return 0;
850 }
851
Joe Perches11ecf532009-09-21 17:04:22 -0700852 if (!$email_remove_duplicates) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800853 push(@email_to, [format_email($name, $address, $email_usename), $role]);
Joe Perches11ecf532009-09-21 17:04:22 -0700854 } elsif (!email_inuse($name, $address)) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800855 push(@email_to, [format_email($name, $address, $email_usename), $role]);
Joe Perches11ecf532009-09-21 17:04:22 -0700856 $email_hash_name{$name}++;
857 $email_hash_address{$address}++;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700858 }
Joe Perchesb7816552009-09-21 17:04:24 -0700859
860 return 1;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700861}
862
863sub push_email_addresses {
Joe Perches3c7385b2009-12-14 18:00:46 -0800864 my ($address, $role) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700865
866 my @address_list = ();
867
Joe Perches5f2441e2009-06-16 15:34:02 -0700868 if (rfc822_valid($address)) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800869 push_email_address($address, $role);
Joe Perches5f2441e2009-06-16 15:34:02 -0700870 } elsif (@address_list = rfc822_validlist($address)) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700871 my $array_count = shift(@address_list);
872 while (my $entry = shift(@address_list)) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800873 push_email_address($entry, $role);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700874 }
Joe Perches5f2441e2009-06-16 15:34:02 -0700875 } else {
Joe Perches3c7385b2009-12-14 18:00:46 -0800876 if (!push_email_address($address, $role)) {
Joe Perchesb7816552009-09-21 17:04:24 -0700877 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
878 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700879 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700880}
881
Joe Perches3c7385b2009-12-14 18:00:46 -0800882sub add_role {
883 my ($line, $role) = @_;
884
885 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -0800886 my $email = format_email($name, $address, $email_usename);
Joe Perches3c7385b2009-12-14 18:00:46 -0800887
888 foreach my $entry (@email_to) {
889 if ($email_remove_duplicates) {
890 my ($entry_name, $entry_address) = parse_email($entry->[0]);
Joe Perches03372db2010-03-05 13:43:00 -0800891 if (($name eq $entry_name || $address eq $entry_address)
892 && ($role eq "" || !($entry->[1] =~ m/$role/))
893 ) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800894 if ($entry->[1] eq "") {
895 $entry->[1] = "$role";
896 } else {
897 $entry->[1] = "$entry->[1],$role";
898 }
899 }
900 } else {
Joe Perches03372db2010-03-05 13:43:00 -0800901 if ($email eq $entry->[0]
902 && ($role eq "" || !($entry->[1] =~ m/$role/))
903 ) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800904 if ($entry->[1] eq "") {
905 $entry->[1] = "$role";
906 } else {
907 $entry->[1] = "$entry->[1],$role";
908 }
909 }
910 }
911 }
912}
913
Joe Perchescb7301c2009-04-07 20:40:12 -0700914sub which {
915 my ($bin) = @_;
916
Joe Perchesf5f50782009-06-16 15:34:00 -0700917 foreach my $path (split(/:/, $ENV{PATH})) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700918 if (-e "$path/$bin") {
919 return "$path/$bin";
920 }
921 }
922
923 return "";
924}
925
Joe Perches8cbb3a72009-09-21 17:04:21 -0700926sub mailmap {
Joe Perchesa8af2432009-12-14 18:00:49 -0800927 my (@lines) = @_;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700928 my %hash;
929
930 foreach my $line (@lines) {
931 my ($name, $address) = parse_email($line);
932 if (!exists($hash{$name})) {
933 $hash{$name} = $address;
Joe Perches11ecf532009-09-21 17:04:22 -0700934 } elsif ($address ne $hash{$name}) {
935 $address = $hash{$name};
Joe Perchesa8af2432009-12-14 18:00:49 -0800936 $line = format_email($name, $address, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700937 }
938 if (exists($mailmap{$name})) {
939 my $obj = $mailmap{$name};
940 foreach my $map_address (@$obj) {
941 if (($map_address eq $address) &&
942 ($map_address ne $hash{$name})) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800943 $line = format_email($name, $hash{$name}, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700944 }
945 }
946 }
947 }
948
949 return @lines;
950}
951
Joe Perches60db31a2009-12-14 18:00:50 -0800952sub git_execute_cmd {
953 my ($cmd) = @_;
954 my @lines = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700955
Joe Perches60db31a2009-12-14 18:00:50 -0800956 my $output = `$cmd`;
957 $output =~ s/^\s*//gm;
958 @lines = split("\n", $output);
959
960 return @lines;
Joe Perchesa8af2432009-12-14 18:00:49 -0800961}
962
Joe Perches60db31a2009-12-14 18:00:50 -0800963sub hg_execute_cmd {
Joe Perchesa8af2432009-12-14 18:00:49 -0800964 my ($cmd) = @_;
Joe Perches60db31a2009-12-14 18:00:50 -0800965 my @lines = ();
Joe Perchesa8af2432009-12-14 18:00:49 -0800966
Joe Perches60db31a2009-12-14 18:00:50 -0800967 my $output = `$cmd`;
968 @lines = split("\n", $output);
969
970 return @lines;
971}
972
973sub vcs_find_signers {
974 my ($cmd) = @_;
Joe Perchesa8af2432009-12-14 18:00:49 -0800975 my @lines = ();
976 my $commits;
977
Joe Perches60db31a2009-12-14 18:00:50 -0800978 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
Joe Perchescb7301c2009-04-07 20:40:12 -0700979
Joe Perches60db31a2009-12-14 18:00:50 -0800980 my $pattern = $VCS_cmds{"commit_pattern"};
Joe Perchescb7301c2009-04-07 20:40:12 -0700981
Joe Perches60db31a2009-12-14 18:00:50 -0800982 $commits = grep(/$pattern/, @lines); # of commits
Joe Perchesafa81ee2009-07-29 15:04:28 -0700983
Joe Perchese4d26b02010-05-24 14:33:17 -0700984 @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines);
Joe Perches0e70e832009-09-21 17:04:20 -0700985 if (!$email_git_penguin_chiefs) {
986 @lines = grep(!/${penguin_chiefs}/i, @lines);
987 }
988 # cut -f2- -d":"
989 s/.*:\s*(.+)\s*/$1/ for (@lines);
990
Joe Perchesa8af2432009-12-14 18:00:49 -0800991## Reformat email addresses (with names) to avoid badly written signatures
992
Joe Perches3c7385b2009-12-14 18:00:46 -0800993 foreach my $line (@lines) {
994 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -0800995 $line = format_email($name, $address, 1);
996 }
997
998 return ($commits, @lines);
999}
1000
Joe Perches60db31a2009-12-14 18:00:50 -08001001sub vcs_save_commits {
1002 my ($cmd) = @_;
1003 my @lines = ();
1004 my @commits = ();
1005
1006 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
1007
1008 foreach my $line (@lines) {
1009 if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
1010 push(@commits, $1);
1011 }
1012 }
1013
1014 return @commits;
1015}
1016
1017sub vcs_blame {
1018 my ($file) = @_;
1019 my $cmd;
1020 my @commits = ();
1021
1022 return @commits if (!(-f $file));
1023
1024 if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
1025 my @all_commits = ();
1026
1027 $cmd = $VCS_cmds{"blame_file_cmd"};
1028 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1029 @all_commits = vcs_save_commits($cmd);
1030
1031 foreach my $file_range_diff (@range) {
1032 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1033 my $diff_file = $1;
1034 my $diff_start = $2;
1035 my $diff_length = $3;
1036 next if ("$file" ne "$diff_file");
1037 for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
1038 push(@commits, $all_commits[$i]);
1039 }
1040 }
1041 } elsif (@range) {
1042 foreach my $file_range_diff (@range) {
1043 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1044 my $diff_file = $1;
1045 my $diff_start = $2;
1046 my $diff_length = $3;
1047 next if ("$file" ne "$diff_file");
1048 $cmd = $VCS_cmds{"blame_range_cmd"};
1049 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1050 push(@commits, vcs_save_commits($cmd));
1051 }
1052 } else {
1053 $cmd = $VCS_cmds{"blame_file_cmd"};
1054 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1055 @commits = vcs_save_commits($cmd);
1056 }
1057
1058 return @commits;
1059}
1060
1061my $printed_novcs = 0;
1062sub vcs_exists {
1063 %VCS_cmds = %VCS_cmds_git;
1064 return 1 if eval $VCS_cmds{"available"};
1065 %VCS_cmds = %VCS_cmds_hg;
1066 return 1 if eval $VCS_cmds{"available"};
1067 %VCS_cmds = ();
1068 if (!$printed_novcs) {
1069 warn("$P: No supported VCS found. Add --nogit to options?\n");
1070 warn("Using a git repository produces better results.\n");
1071 warn("Try Linus Torvalds' latest git repository using:\n");
1072 warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
1073 $printed_novcs = 1;
1074 }
1075 return 0;
1076}
1077
1078sub vcs_assign {
Joe Perchesa8af2432009-12-14 18:00:49 -08001079 my ($role, $divisor, @lines) = @_;
1080
1081 my %hash;
1082 my $count = 0;
1083
Joe Perchesa8af2432009-12-14 18:00:49 -08001084 return if (@lines <= 0);
1085
1086 if ($divisor <= 0) {
Joe Perches60db31a2009-12-14 18:00:50 -08001087 warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
Joe Perchesa8af2432009-12-14 18:00:49 -08001088 $divisor = 1;
Joe Perches3c7385b2009-12-14 18:00:46 -08001089 }
Joe Perches8cbb3a72009-09-21 17:04:21 -07001090
Joe Perches11ecf532009-09-21 17:04:22 -07001091 if ($email_remove_duplicates) {
1092 @lines = mailmap(@lines);
1093 }
Joe Perches0e70e832009-09-21 17:04:20 -07001094
Joe Perches0e70e832009-09-21 17:04:20 -07001095 @lines = sort(@lines);
Joe Perchesafa81ee2009-07-29 15:04:28 -07001096
Joe Perches11ecf532009-09-21 17:04:22 -07001097 # uniq -c
1098 $hash{$_}++ for @lines;
1099
1100 # sort -rn
1101 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
1102 my $sign_offs = $hash{$line};
Joe Perchesa8af2432009-12-14 18:00:49 -08001103 my $percent = $sign_offs * 100 / $divisor;
Joe Perches3c7385b2009-12-14 18:00:46 -08001104
Joe Perchesa8af2432009-12-14 18:00:49 -08001105 $percent = 100 if ($percent > 100);
Joe Perches11ecf532009-09-21 17:04:22 -07001106 $count++;
1107 last if ($sign_offs < $email_git_min_signatures ||
1108 $count > $email_git_max_maintainers ||
Joe Perchesa8af2432009-12-14 18:00:49 -08001109 $percent < $email_git_min_percent);
Joe Perches3c7385b2009-12-14 18:00:46 -08001110 push_email_address($line, '');
Joe Perches3c7385b2009-12-14 18:00:46 -08001111 if ($output_rolestats) {
Joe Perchesa8af2432009-12-14 18:00:49 -08001112 my $fmt_percent = sprintf("%.0f", $percent);
1113 add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
1114 } else {
1115 add_role($line, $role);
Joe Perches3c7385b2009-12-14 18:00:46 -08001116 }
Joe Perchesf5492662009-09-21 17:04:13 -07001117 }
1118}
1119
Joe Perches60db31a2009-12-14 18:00:50 -08001120sub vcs_file_signoffs {
Joe Perchesa8af2432009-12-14 18:00:49 -08001121 my ($file) = @_;
1122
1123 my @signers = ();
Joe Perches60db31a2009-12-14 18:00:50 -08001124 my $commits;
Joe Perchesa8af2432009-12-14 18:00:49 -08001125
Joe Perches60db31a2009-12-14 18:00:50 -08001126 return if (!vcs_exists());
Joe Perchesa8af2432009-12-14 18:00:49 -08001127
Joe Perches60db31a2009-12-14 18:00:50 -08001128 my $cmd = $VCS_cmds{"find_signers_cmd"};
1129 $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
1130
1131 ($commits, @signers) = vcs_find_signers($cmd);
1132 vcs_assign("commit_signer", $commits, @signers);
Joe Perchesa8af2432009-12-14 18:00:49 -08001133}
1134
Joe Perches60db31a2009-12-14 18:00:50 -08001135sub vcs_file_blame {
Joe Perchesf5492662009-09-21 17:04:13 -07001136 my ($file) = @_;
1137
Joe Perches60db31a2009-12-14 18:00:50 -08001138 my @signers = ();
Joe Perchesa8af2432009-12-14 18:00:49 -08001139 my @commits = ();
Joe Perchesa8af2432009-12-14 18:00:49 -08001140 my $total_commits;
Joe Perchesf5492662009-09-21 17:04:13 -07001141
Joe Perches60db31a2009-12-14 18:00:50 -08001142 return if (!vcs_exists());
Joe Perchesf5492662009-09-21 17:04:13 -07001143
Joe Perches60db31a2009-12-14 18:00:50 -08001144 @commits = vcs_blame($file);
Joe Perchesf5492662009-09-21 17:04:13 -07001145 @commits = uniq(@commits);
Joe Perchesa8af2432009-12-14 18:00:49 -08001146 $total_commits = @commits;
1147
Joe Perchesf5492662009-09-21 17:04:13 -07001148 foreach my $commit (@commits) {
Joe Perchesa8af2432009-12-14 18:00:49 -08001149 my $commit_count;
1150 my @commit_signers = ();
Joe Perchesf5492662009-09-21 17:04:13 -07001151
Joe Perches60db31a2009-12-14 18:00:50 -08001152 my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1153 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1154
1155 ($commit_count, @commit_signers) = vcs_find_signers($cmd);
1156 push(@signers, @commit_signers);
Joe Perchesf5492662009-09-21 17:04:13 -07001157 }
1158
Joe Perchesa8af2432009-12-14 18:00:49 -08001159 if ($from_filename) {
Joe Perches60db31a2009-12-14 18:00:50 -08001160 vcs_assign("commits", $total_commits, @signers);
Joe Perchesa8af2432009-12-14 18:00:49 -08001161 } else {
Joe Perches60db31a2009-12-14 18:00:50 -08001162 vcs_assign("modified commits", $total_commits, @signers);
Joe Perchesf5492662009-09-21 17:04:13 -07001163 }
Joe Perchescb7301c2009-04-07 20:40:12 -07001164}
1165
1166sub uniq {
Joe Perchesa8af2432009-12-14 18:00:49 -08001167 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001168
1169 my %saw;
1170 @parms = grep(!$saw{$_}++, @parms);
1171 return @parms;
1172}
1173
1174sub sort_and_uniq {
Joe Perchesa8af2432009-12-14 18:00:49 -08001175 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001176
1177 my %saw;
1178 @parms = sort @parms;
1179 @parms = grep(!$saw{$_}++, @parms);
1180 return @parms;
1181}
1182
Joe Perches03372db2010-03-05 13:43:00 -08001183sub clean_file_emails {
1184 my (@file_emails) = @_;
1185 my @fmt_emails = ();
1186
1187 foreach my $email (@file_emails) {
1188 $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
1189 my ($name, $address) = parse_email($email);
1190 if ($name eq '"[,\.]"') {
1191 $name = "";
1192 }
1193
1194 my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
1195 if (@nw > 2) {
1196 my $first = $nw[@nw - 3];
1197 my $middle = $nw[@nw - 2];
1198 my $last = $nw[@nw - 1];
1199
1200 if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
1201 (length($first) == 2 && substr($first, -1) eq ".")) ||
1202 (length($middle) == 1 ||
1203 (length($middle) == 2 && substr($middle, -1) eq "."))) {
1204 $name = "$first $middle $last";
1205 } else {
1206 $name = "$middle $last";
1207 }
1208 }
1209
1210 if (substr($name, -1) =~ /[,\.]/) {
1211 $name = substr($name, 0, length($name) - 1);
1212 } elsif (substr($name, -2) =~ /[,\.]"/) {
1213 $name = substr($name, 0, length($name) - 2) . '"';
1214 }
1215
1216 if (substr($name, 0, 1) =~ /[,\.]/) {
1217 $name = substr($name, 1, length($name) - 1);
1218 } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
1219 $name = '"' . substr($name, 2, length($name) - 2);
1220 }
1221
1222 my $fmt_email = format_email($name, $address, $email_usename);
1223 push(@fmt_emails, $fmt_email);
1224 }
1225 return @fmt_emails;
1226}
1227
Joe Perches3c7385b2009-12-14 18:00:46 -08001228sub merge_email {
1229 my @lines;
1230 my %saw;
1231
1232 for (@_) {
1233 my ($address, $role) = @$_;
1234 if (!$saw{$address}) {
1235 if ($output_roles) {
Joe Perches60db31a2009-12-14 18:00:50 -08001236 push(@lines, "$address ($role)");
Joe Perches3c7385b2009-12-14 18:00:46 -08001237 } else {
Joe Perches60db31a2009-12-14 18:00:50 -08001238 push(@lines, $address);
Joe Perches3c7385b2009-12-14 18:00:46 -08001239 }
1240 $saw{$address} = 1;
1241 }
1242 }
1243
1244 return @lines;
1245}
1246
Joe Perchescb7301c2009-04-07 20:40:12 -07001247sub output {
Joe Perchesa8af2432009-12-14 18:00:49 -08001248 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001249
1250 if ($output_multiline) {
1251 foreach my $line (@parms) {
1252 print("${line}\n");
1253 }
1254 } else {
1255 print(join($output_separator, @parms));
1256 print("\n");
1257 }
1258}
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001259
1260my $rfc822re;
1261
1262sub make_rfc822re {
1263# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
1264# comment. We must allow for rfc822_lwsp (or comments) after each of these.
1265# This regexp will only work on addresses which have had comments stripped
1266# and replaced with rfc822_lwsp.
1267
1268 my $specials = '()<>@,;:\\\\".\\[\\]';
1269 my $controls = '\\000-\\037\\177';
1270
1271 my $dtext = "[^\\[\\]\\r\\\\]";
1272 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1273
1274 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1275
1276# Use zero-width assertion to spot the limit of an atom. A simple
1277# $rfc822_lwsp* causes the regexp engine to hang occasionally.
1278 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
1279 my $word = "(?:$atom|$quoted_string)";
1280 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
1281
1282 my $sub_domain = "(?:$atom|$domain_literal)";
1283 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1284
1285 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1286
1287 my $phrase = "$word*";
1288 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
1289 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
1290 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
1291
1292 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1293 my $address = "(?:$mailbox|$group)";
1294
1295 return "$rfc822_lwsp*$address";
1296}
1297
1298sub rfc822_strip_comments {
1299 my $s = shift;
1300# Recursively remove comments, and replace with a single space. The simpler
1301# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
1302# chars in atoms, for example.
1303
1304 while ($s =~ s/^((?:[^"\\]|\\.)*
1305 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1306 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1307 return $s;
1308}
1309
1310# valid: returns true if the parameter is an RFC822 valid address
1311#
Stephen Hemminger22dd5b02010-03-05 13:43:06 -08001312sub rfc822_valid {
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001313 my $s = rfc822_strip_comments(shift);
1314
1315 if (!$rfc822re) {
1316 $rfc822re = make_rfc822re();
1317 }
1318
1319 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1320}
1321
1322# validlist: In scalar context, returns true if the parameter is an RFC822
1323# valid list of addresses.
1324#
1325# In list context, returns an empty list on failure (an invalid
1326# address was found); otherwise a list whose first element is the
1327# number of addresses found and whose remaining elements are the
1328# addresses. This is needed to disambiguate failure (invalid)
1329# from success with no addresses found, because an empty string is
1330# a valid list.
1331
Stephen Hemminger22dd5b02010-03-05 13:43:06 -08001332sub rfc822_validlist {
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001333 my $s = rfc822_strip_comments(shift);
1334
1335 if (!$rfc822re) {
1336 $rfc822re = make_rfc822re();
1337 }
1338 # * null list items are valid according to the RFC
1339 # * the '1' business is to aid in distinguishing failure from no results
1340
1341 my @r;
1342 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
1343 $s =~ m/^$rfc822_char*$/) {
Joe Perches5f2441e2009-06-16 15:34:02 -07001344 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
Joe Perches60db31a2009-12-14 18:00:50 -08001345 push(@r, $1);
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001346 }
1347 return wantarray ? (scalar(@r), @r) : 1;
1348 }
Joe Perches60db31a2009-12-14 18:00:50 -08001349 return wantarray ? () : 0;
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001350}