blob: 6f97a13bcee4131aedbbf97a4f9db49c50e519e5 [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,
Joe Perches64f77f32010-03-05 13:43:04 -0800129 'h|help|usage' => \$help,
Joe Perchescb7301c2009-04-07 20:40:12 -0700130 )) {
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 Perches64f77f32010-03-05 13:43:04 -0800144if (-t STDIN && !@ARGV) {
145 # We're talking to a terminal, but have no command line arguments.
146 die "$P: missing patchfile or -f file - use --help if necessary\n";
Joe Perchescb7301c2009-04-07 20:40:12 -0700147}
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) {
Joe Perches4b76c9d2010-03-05 13:43:03 -0800168 die "$P: Missing required option: email, scm, status, subsystem or web\n";
169 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700170}
171
Joe Perchesf5492662009-09-21 17:04:13 -0700172if ($email &&
173 ($email_maintainer + $email_list + $email_subscriber_list +
174 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700175 die "$P: Please select at least 1 email option\n";
176}
177
178if (!top_of_kernel_tree($lk_path)) {
179 die "$P: The current directory does not appear to be "
180 . "a linux kernel source tree.\n";
181}
182
183## Read MAINTAINERS for type/value pairs
184
185my @typevalue = ();
Joe Perchesdcf36a92009-10-26 16:49:47 -0700186my %keyword_hash;
187
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800188open (my $maint, '<', "${lk_path}MAINTAINERS")
189 or die "$P: Can't open MAINTAINERS: $!\n";
190while (<$maint>) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700191 my $line = $_;
192
193 if ($line =~ m/^(\C):\s*(.*)/) {
194 my $type = $1;
195 my $value = $2;
196
197 ##Filename pattern matching
198 if ($type eq "F" || $type eq "X") {
199 $value =~ s@\.@\\\.@g; ##Convert . to \.
200 $value =~ s/\*/\.\*/g; ##Convert * to .*
201 $value =~ s/\?/\./g; ##Convert ? to .
Joe Perches870020f2009-07-29 15:04:28 -0700202 ##if pattern is a directory and it lacks a trailing slash, add one
203 if ((-d $value)) {
204 $value =~ s@([^/])$@$1/@;
205 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700206 } elsif ($type eq "K") {
207 $keyword_hash{@typevalue} = $value;
Joe Perchescb7301c2009-04-07 20:40:12 -0700208 }
209 push(@typevalue, "$type:$value");
210 } elsif (!/^(\s)*$/) {
211 $line =~ s/\n$//g;
212 push(@typevalue, $line);
213 }
214}
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800215close($maint);
Joe Perchescb7301c2009-04-07 20:40:12 -0700216
Joe Perches8cbb3a72009-09-21 17:04:21 -0700217my %mailmap;
218
Joe Perches11ecf532009-09-21 17:04:22 -0700219if ($email_remove_duplicates) {
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800220 open(my $mailmap, '<', "${lk_path}.mailmap")
221 or warn "$P: Can't open .mailmap: $!\n";
222 while (<$mailmap>) {
Joe Perches11ecf532009-09-21 17:04:22 -0700223 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 }
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800241 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 Perches64f77f32010-03-05 13:43:04 -0800251if (!@ARGV) {
252 push(@ARGV, "&STDIN");
253}
254
Joe Perches4a7fdb52009-04-10 12:28:57 -0700255foreach my $file (@ARGV) {
Joe Perches64f77f32010-03-05 13:43:04 -0800256 if ($file ne "&STDIN") {
257 ##if $file is a directory and it lacks a trailing slash, add one
258 if ((-d $file)) {
259 $file =~ s@([^/])$@$1/@;
260 } elsif (!(-f $file)) {
261 die "$P: file '${file}' not found\n";
262 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700263 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700264 if ($from_filename) {
265 push(@files, $file);
Joe Perches03372db2010-03-05 13:43:00 -0800266 if (-f $file && ($keywords || $file_emails)) {
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800267 open(my $f, '<', $file)
268 or die "$P: Can't open $file: $!\n";
269 my $text = do { local($/) ; <$f> };
270 close($f);
Joe Perches03372db2010-03-05 13:43:00 -0800271 if ($keywords) {
272 foreach my $line (keys %keyword_hash) {
273 if ($text =~ m/$keyword_hash{$line}/x) {
274 push(@keyword_tvi, $line);
275 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700276 }
277 }
Joe Perches03372db2010-03-05 13:43:00 -0800278 if ($file_emails) {
279 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;
280 push(@file_emails, clean_file_emails(@poss_addr));
281 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700282 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700283 } else {
284 my $file_cnt = @files;
Joe Perchesf5492662009-09-21 17:04:13 -0700285 my $lastfile;
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800286
Wolfram Sang3a4df132010-03-23 13:35:18 -0700287 open(my $patch, "< $file")
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800288 or die "$P: Can't open $file: $!\n";
289 while (<$patch>) {
Joe Perchesdcf36a92009-10-26 16:49:47 -0700290 my $patch_line = $_;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700291 if (m/^\+\+\+\s+(\S+)/) {
292 my $filename = $1;
293 $filename =~ s@^[^/]*/@@;
294 $filename =~ s@\n@@;
Joe Perchesf5492662009-09-21 17:04:13 -0700295 $lastfile = $filename;
Joe Perches4a7fdb52009-04-10 12:28:57 -0700296 push(@files, $filename);
Joe Perchesf5492662009-09-21 17:04:13 -0700297 } elsif (m/^\@\@ -(\d+),(\d+)/) {
298 if ($email_git_blame) {
299 push(@range, "$lastfile:$1:$2");
300 }
Joe Perchesdcf36a92009-10-26 16:49:47 -0700301 } elsif ($keywords) {
302 foreach my $line (keys %keyword_hash) {
303 if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
304 push(@keyword_tvi, $line);
305 }
306 }
Joe Perches4a7fdb52009-04-10 12:28:57 -0700307 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700308 }
Stephen Hemminger22dd5b02010-03-05 13:43:06 -0800309 close($patch);
310
Joe Perches4a7fdb52009-04-10 12:28:57 -0700311 if ($file_cnt == @files) {
Joe Perches7f29fd272009-06-16 15:34:04 -0700312 warn "$P: file '${file}' doesn't appear to be a patch. "
Joe Perches4a7fdb52009-04-10 12:28:57 -0700313 . "Add -f to options?\n";
314 }
315 @files = sort_and_uniq(@files);
Joe Perchescb7301c2009-04-07 20:40:12 -0700316 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700317}
318
Joe Perches03372db2010-03-05 13:43:00 -0800319@file_emails = uniq(@file_emails);
320
Joe Perchescb7301c2009-04-07 20:40:12 -0700321my @email_to = ();
Joe Perches290603c2009-06-16 15:33:58 -0700322my @list_to = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700323my @scm = ();
324my @web = ();
325my @subsystem = ();
326my @status = ();
327
328# Find responsible parties
329
330foreach my $file (@files) {
331
Joe Perches272a8972010-01-08 14:42:48 -0800332 my %hash;
333 my $tvi = find_first_section();
334 while ($tvi < @typevalue) {
335 my $start = find_starting_index($tvi);
336 my $end = find_ending_index($tvi);
337 my $exclude = 0;
338 my $i;
Joe Perchescb7301c2009-04-07 20:40:12 -0700339
Joe Perches272a8972010-01-08 14:42:48 -0800340 #Do not match excluded file patterns
Joe Perchescb7301c2009-04-07 20:40:12 -0700341
Joe Perches272a8972010-01-08 14:42:48 -0800342 for ($i = $start; $i < $end; $i++) {
343 my $line = $typevalue[$i];
Joe Perches290603c2009-06-16 15:33:58 -0700344 if ($line =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700345 my $type = $1;
346 my $value = $2;
Joe Perches272a8972010-01-08 14:42:48 -0800347 if ($type eq 'X') {
Joe Perchescb7301c2009-04-07 20:40:12 -0700348 if (file_match_pattern($file, $value)) {
Joe Perches272a8972010-01-08 14:42:48 -0800349 $exclude = 1;
Joe Perches3c840c12010-03-05 13:43:07 -0800350 last;
Joe Perches272a8972010-01-08 14:42:48 -0800351 }
352 }
353 }
354 }
355
356 if (!$exclude) {
357 for ($i = $start; $i < $end; $i++) {
358 my $line = $typevalue[$i];
359 if ($line =~ m/^(\C):\s*(.*)/) {
360 my $type = $1;
361 my $value = $2;
362 if ($type eq 'F') {
363 if (file_match_pattern($file, $value)) {
364 my $value_pd = ($value =~ tr@/@@);
365 my $file_pd = ($file =~ tr@/@@);
366 $value_pd++ if (substr($value,-1,1) ne "/");
367 if ($pattern_depth == 0 ||
368 (($file_pd - $value_pd) < $pattern_depth)) {
369 $hash{$tvi} = $value_pd;
370 }
Joe Perches3fb55652009-09-21 17:04:17 -0700371 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700372 }
373 }
374 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700375 }
Joe Perches272a8972010-01-08 14:42:48 -0800376
Joe Perches3c840c12010-03-05 13:43:07 -0800377 $tvi = $end + 1;
Joe Perches272a8972010-01-08 14:42:48 -0800378 }
379
380 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
381 add_categories($line);
Joe Perches4b76c9d2010-03-05 13:43:03 -0800382 if ($sections) {
383 my $i;
384 my $start = find_starting_index($line);
385 my $end = find_ending_index($line);
386 for ($i = $start; $i < $end; $i++) {
387 my $line = $typevalue[$i];
388 if ($line =~ /^[FX]:/) { ##Restore file patterns
389 $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
390 $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
391 $line =~ s/\\\./\./g; ##Convert \. to .
392 $line =~ s/\.\*/\*/g; ##Convert .* to *
393 }
Joe Perchesf11e9a12010-03-05 13:43:03 -0800394 $line =~ s/^([A-Z]):/$1:\t/g;
Joe Perches4b76c9d2010-03-05 13:43:03 -0800395 print("$line\n");
396 }
Joe Perchesf11e9a12010-03-05 13:43:03 -0800397 print("\n");
Joe Perches4b76c9d2010-03-05 13:43:03 -0800398 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700399 }
400
Joe Perches4a7fdb52009-04-10 12:28:57 -0700401 if ($email && $email_git) {
Joe Perches60db31a2009-12-14 18:00:50 -0800402 vcs_file_signoffs($file);
Joe Perchescb7301c2009-04-07 20:40:12 -0700403 }
404
Joe Perchesf5492662009-09-21 17:04:13 -0700405 if ($email && $email_git_blame) {
Joe Perches60db31a2009-12-14 18:00:50 -0800406 vcs_file_blame($file);
Joe Perchesf5492662009-09-21 17:04:13 -0700407 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700408}
409
Joe Perchesdcf36a92009-10-26 16:49:47 -0700410if ($keywords) {
411 @keyword_tvi = sort_and_uniq(@keyword_tvi);
412 foreach my $line (@keyword_tvi) {
413 add_categories($line);
414 }
415}
416
Joe Perchesf5f50782009-06-16 15:34:00 -0700417if ($email) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700418 foreach my $chief (@penguin_chief) {
419 if ($chief =~ m/^(.*):(.*)/) {
Joe Perchesf5f50782009-06-16 15:34:00 -0700420 my $email_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700421
Joe Perchesa8af2432009-12-14 18:00:49 -0800422 $email_address = format_email($1, $2, $email_usename);
Joe Perchesf5f50782009-06-16 15:34:00 -0700423 if ($email_git_penguin_chiefs) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800424 push(@email_to, [$email_address, 'chief penguin']);
Joe Perchesf5f50782009-06-16 15:34:00 -0700425 } else {
Joe Perches3c7385b2009-12-14 18:00:46 -0800426 @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700427 }
428 }
429 }
Joe Perches03372db2010-03-05 13:43:00 -0800430
431 foreach my $email (@file_emails) {
432 my ($name, $address) = parse_email($email);
433
434 my $tmp_email = format_email($name, $address, $email_usename);
435 push_email_address($tmp_email, '');
436 add_role($tmp_email, 'in file');
437 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700438}
439
Joe Perches290603c2009-06-16 15:33:58 -0700440if ($email || $email_list) {
441 my @to = ();
442 if ($email) {
443 @to = (@to, @email_to);
Joe Perchescb7301c2009-04-07 20:40:12 -0700444 }
Joe Perches290603c2009-06-16 15:33:58 -0700445 if ($email_list) {
Joe Perches290603c2009-06-16 15:33:58 -0700446 @to = (@to, @list_to);
Joe Perches290603c2009-06-16 15:33:58 -0700447 }
Joe Perches3c7385b2009-12-14 18:00:46 -0800448 output(merge_email(@to));
Joe Perchescb7301c2009-04-07 20:40:12 -0700449}
450
451if ($scm) {
Joe Perchesb7816552009-09-21 17:04:24 -0700452 @scm = uniq(@scm);
Joe Perchescb7301c2009-04-07 20:40:12 -0700453 output(@scm);
454}
455
456if ($status) {
Joe Perchesb7816552009-09-21 17:04:24 -0700457 @status = uniq(@status);
Joe Perchescb7301c2009-04-07 20:40:12 -0700458 output(@status);
459}
460
461if ($subsystem) {
Joe Perchesb7816552009-09-21 17:04:24 -0700462 @subsystem = uniq(@subsystem);
Joe Perchescb7301c2009-04-07 20:40:12 -0700463 output(@subsystem);
464}
465
466if ($web) {
Joe Perchesb7816552009-09-21 17:04:24 -0700467 @web = uniq(@web);
Joe Perchescb7301c2009-04-07 20:40:12 -0700468 output(@web);
469}
470
471exit($exit);
472
473sub file_match_pattern {
474 my ($file, $pattern) = @_;
475 if (substr($pattern, -1) eq "/") {
476 if ($file =~ m@^$pattern@) {
477 return 1;
478 }
479 } else {
480 if ($file =~ m@^$pattern@) {
481 my $s1 = ($file =~ tr@/@@);
482 my $s2 = ($pattern =~ tr@/@@);
483 if ($s1 == $s2) {
484 return 1;
485 }
486 }
487 }
488 return 0;
489}
490
491sub usage {
492 print <<EOT;
493usage: $P [options] patchfile
Joe Perches870020f2009-07-29 15:04:28 -0700494 $P [options] -f file|directory
Joe Perchescb7301c2009-04-07 20:40:12 -0700495version: $V
496
497MAINTAINER field selection options:
498 --email => print email address(es) if any
499 --git => include recent git \*-by: signers
500 --git-chief-penguins => include ${penguin_chiefs}
501 --git-min-signatures => number of signatures required (default: 1)
502 --git-max-maintainers => maximum maintainers to add (default: 5)
Joe Perches3d202ae2009-07-29 15:04:29 -0700503 --git-min-percent => minimum percentage of commits required (default: 5)
Joe Perchesf5492662009-09-21 17:04:13 -0700504 --git-blame => use git blame to find modified commits for patch or file
Joe Perches60db31a2009-12-14 18:00:50 -0800505 --git-since => git history to use (default: 1-year-ago)
506 --hg-since => hg history to use (default: -365)
Joe Perchescb7301c2009-04-07 20:40:12 -0700507 --m => include maintainer(s) if any
508 --n => include name 'Full Name <addr\@domain.tld>'
509 --l => include list(s) if any
510 --s => include subscriber only list(s) if any
Joe Perches11ecf532009-09-21 17:04:22 -0700511 --remove-duplicates => minimize duplicate email names/addresses
Joe Perches3c7385b2009-12-14 18:00:46 -0800512 --roles => show roles (status:subsystem, git-signer, list, etc...)
513 --rolestats => show roles and statistics (commits/total_commits, %)
Joe Perches03372db2010-03-05 13:43:00 -0800514 --file-emails => add email addresses found in -f file (default: 0 (off))
Joe Perchescb7301c2009-04-07 20:40:12 -0700515 --scm => print SCM tree(s) if any
516 --status => print status if any
517 --subsystem => print subsystem name if any
518 --web => print website(s) if any
519
520Output type options:
521 --separator [, ] => separator for multiple entries on 1 line
Joe Perches42498312009-09-21 17:04:21 -0700522 using --separator also sets --nomultiline if --separator is not [, ]
Joe Perchescb7301c2009-04-07 20:40:12 -0700523 --multiline => print 1 entry per line
524
Joe Perchescb7301c2009-04-07 20:40:12 -0700525Other options:
Joe Perches3fb55652009-09-21 17:04:17 -0700526 --pattern-depth => Number of pattern directory traversals (default: 0 (all))
Joe Perchesdcf36a92009-10-26 16:49:47 -0700527 --keywords => scan patch for keywords (default: 1 (on))
Joe Perches4b76c9d2010-03-05 13:43:03 -0800528 --sections => print the entire subsystem sections with pattern matches
Joe Perchesf5f50782009-06-16 15:34:00 -0700529 --version => show version
Joe Perchescb7301c2009-04-07 20:40:12 -0700530 --help => show this help information
531
Joe Perches3fb55652009-09-21 17:04:17 -0700532Default options:
Joe Perches11ecf532009-09-21 17:04:22 -0700533 [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
Joe Perches3fb55652009-09-21 17:04:17 -0700534
Joe Perches870020f2009-07-29 15:04:28 -0700535Notes:
536 Using "-f directory" may give unexpected results:
Joe Perchesf5492662009-09-21 17:04:13 -0700537 Used with "--git", git signators for _all_ files in and below
538 directory are examined as git recurses directories.
539 Any specified X: (exclude) pattern matches are _not_ ignored.
540 Used with "--nogit", directory is used as a pattern match,
Joe Perches60db31a2009-12-14 18:00:50 -0800541 no individual file within the directory or subdirectory
542 is matched.
Joe Perchesf5492662009-09-21 17:04:13 -0700543 Used with "--git-blame", does not iterate all files in directory
544 Using "--git-blame" is slow and may add old committers and authors
545 that are no longer active maintainers to the output.
Joe Perches3c7385b2009-12-14 18:00:46 -0800546 Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
547 other automated tools that expect only ["name"] <email address>
548 may not work because of additional output after <email address>.
549 Using "--rolestats" and "--git-blame" shows the #/total=% commits,
550 not the percentage of the entire file authored. # of commits is
551 not a good measure of amount of code authored. 1 major commit may
552 contain a thousand lines, 5 trivial commits may modify a single line.
Joe Perches60db31a2009-12-14 18:00:50 -0800553 If git is not installed, but mercurial (hg) is installed and an .hg
554 repository exists, the following options apply to mercurial:
555 --git,
556 --git-min-signatures, --git-max-maintainers, --git-min-percent, and
557 --git-blame
558 Use --hg-since not --git-since to control date selection
Joe Perchescb7301c2009-04-07 20:40:12 -0700559EOT
560}
561
562sub top_of_kernel_tree {
563 my ($lk_path) = @_;
564
565 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
566 $lk_path .= "/";
567 }
568 if ( (-f "${lk_path}COPYING")
569 && (-f "${lk_path}CREDITS")
570 && (-f "${lk_path}Kbuild")
571 && (-f "${lk_path}MAINTAINERS")
572 && (-f "${lk_path}Makefile")
573 && (-f "${lk_path}README")
574 && (-d "${lk_path}Documentation")
575 && (-d "${lk_path}arch")
576 && (-d "${lk_path}include")
577 && (-d "${lk_path}drivers")
578 && (-d "${lk_path}fs")
579 && (-d "${lk_path}init")
580 && (-d "${lk_path}ipc")
581 && (-d "${lk_path}kernel")
582 && (-d "${lk_path}lib")
583 && (-d "${lk_path}scripts")) {
584 return 1;
585 }
586 return 0;
587}
588
Joe Perches0e70e832009-09-21 17:04:20 -0700589sub parse_email {
590 my ($formatted_email) = @_;
591
592 my $name = "";
593 my $address = "";
594
Joe Perches11ecf532009-09-21 17:04:22 -0700595 if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700596 $name = $1;
597 $address = $2;
Joe Perches11ecf532009-09-21 17:04:22 -0700598 } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700599 $address = $1;
Joe Perchesb7816552009-09-21 17:04:24 -0700600 } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
Joe Perches0e70e832009-09-21 17:04:20 -0700601 $address = $1;
602 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700603
604 $name =~ s/^\s+|\s+$//g;
Joe Perchesd7895042009-06-16 15:34:02 -0700605 $name =~ s/^\"|\"$//g;
Joe Perches0e70e832009-09-21 17:04:20 -0700606 $address =~ s/^\s+|\s+$//g;
Joe Perchescb7301c2009-04-07 20:40:12 -0700607
Stephen Hemmingera63ceb42010-03-05 13:43:06 -0800608 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
Joe Perchescb7301c2009-04-07 20:40:12 -0700609 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
Joe Perches0e70e832009-09-21 17:04:20 -0700610 $name = "\"$name\"";
Joe Perchescb7301c2009-04-07 20:40:12 -0700611 }
Joe Perches0e70e832009-09-21 17:04:20 -0700612
613 return ($name, $address);
614}
615
616sub format_email {
Joe Perchesa8af2432009-12-14 18:00:49 -0800617 my ($name, $address, $usename) = @_;
Joe Perches0e70e832009-09-21 17:04:20 -0700618
619 my $formatted_email;
620
621 $name =~ s/^\s+|\s+$//g;
622 $name =~ s/^\"|\"$//g;
623 $address =~ s/^\s+|\s+$//g;
624
Stephen Hemmingera63ceb42010-03-05 13:43:06 -0800625 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
Joe Perches0e70e832009-09-21 17:04:20 -0700626 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
627 $name = "\"$name\"";
628 }
629
Joe Perchesa8af2432009-12-14 18:00:49 -0800630 if ($usename) {
Joe Perches0e70e832009-09-21 17:04:20 -0700631 if ("$name" eq "") {
632 $formatted_email = "$address";
633 } else {
Joe Perchesa8af2432009-12-14 18:00:49 -0800634 $formatted_email = "$name <$address>";
Joe Perches0e70e832009-09-21 17:04:20 -0700635 }
636 } else {
637 $formatted_email = $address;
638 }
639
Joe Perchescb7301c2009-04-07 20:40:12 -0700640 return $formatted_email;
641}
642
Joe Perches272a8972010-01-08 14:42:48 -0800643sub find_first_section {
644 my $index = 0;
645
646 while ($index < @typevalue) {
647 my $tv = $typevalue[$index];
648 if (($tv =~ m/^(\C):\s*(.*)/)) {
649 last;
650 }
651 $index++;
652 }
653
654 return $index;
655}
656
Joe Perchesb7816552009-09-21 17:04:24 -0700657sub find_starting_index {
Joe Perchesb7816552009-09-21 17:04:24 -0700658 my ($index) = @_;
659
660 while ($index > 0) {
661 my $tv = $typevalue[$index];
662 if (!($tv =~ m/^(\C):\s*(.*)/)) {
663 last;
664 }
665 $index--;
666 }
667
668 return $index;
669}
670
671sub find_ending_index {
672 my ($index) = @_;
673
674 while ($index < @typevalue) {
675 my $tv = $typevalue[$index];
676 if (!($tv =~ m/^(\C):\s*(.*)/)) {
677 last;
678 }
679 $index++;
680 }
681
682 return $index;
683}
684
Joe Perches3c7385b2009-12-14 18:00:46 -0800685sub get_maintainer_role {
686 my ($index) = @_;
687
688 my $i;
689 my $start = find_starting_index($index);
690 my $end = find_ending_index($index);
691
692 my $role;
693 my $subsystem = $typevalue[$start];
694 if (length($subsystem) > 20) {
695 $subsystem = substr($subsystem, 0, 17);
696 $subsystem =~ s/\s*$//;
697 $subsystem = $subsystem . "...";
698 }
699
700 for ($i = $start + 1; $i < $end; $i++) {
701 my $tv = $typevalue[$i];
702 if ($tv =~ m/^(\C):\s*(.*)/) {
703 my $ptype = $1;
704 my $pvalue = $2;
705 if ($ptype eq "S") {
706 $role = $pvalue;
707 }
708 }
709 }
710
711 $role = lc($role);
712 if ($role eq "supported") {
713 $role = "supporter";
714 } elsif ($role eq "maintained") {
715 $role = "maintainer";
716 } elsif ($role eq "odd fixes") {
717 $role = "odd fixer";
718 } elsif ($role eq "orphan") {
719 $role = "orphan minder";
720 } elsif ($role eq "obsolete") {
721 $role = "obsolete minder";
722 } elsif ($role eq "buried alive in reporters") {
723 $role = "chief penguin";
724 }
725
726 return $role . ":" . $subsystem;
727}
728
729sub get_list_role {
730 my ($index) = @_;
731
732 my $i;
733 my $start = find_starting_index($index);
734 my $end = find_ending_index($index);
735
736 my $subsystem = $typevalue[$start];
737 if (length($subsystem) > 20) {
738 $subsystem = substr($subsystem, 0, 17);
739 $subsystem =~ s/\s*$//;
740 $subsystem = $subsystem . "...";
741 }
742
743 if ($subsystem eq "THE REST") {
744 $subsystem = "";
745 }
746
747 return $subsystem;
748}
749
Joe Perchescb7301c2009-04-07 20:40:12 -0700750sub add_categories {
751 my ($index) = @_;
752
Joe Perchesb7816552009-09-21 17:04:24 -0700753 my $i;
754 my $start = find_starting_index($index);
755 my $end = find_ending_index($index);
756
757 push(@subsystem, $typevalue[$start]);
758
759 for ($i = $start + 1; $i < $end; $i++) {
760 my $tv = $typevalue[$i];
Joe Perches290603c2009-06-16 15:33:58 -0700761 if ($tv =~ m/^(\C):\s*(.*)/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700762 my $ptype = $1;
763 my $pvalue = $2;
764 if ($ptype eq "L") {
Joe Perches290603c2009-06-16 15:33:58 -0700765 my $list_address = $pvalue;
766 my $list_additional = "";
Joe Perches3c7385b2009-12-14 18:00:46 -0800767 my $list_role = get_list_role($i);
768
769 if ($list_role ne "") {
770 $list_role = ":" . $list_role;
771 }
Joe Perches290603c2009-06-16 15:33:58 -0700772 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
773 $list_address = $1;
774 $list_additional = $2;
775 }
Joe Perchesbdf7c682009-06-16 15:33:59 -0700776 if ($list_additional =~ m/subscribers-only/) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700777 if ($email_subscriber_list) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800778 push(@list_to, [$list_address, "subscriber list${list_role}"]);
Joe Perchescb7301c2009-04-07 20:40:12 -0700779 }
780 } else {
781 if ($email_list) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800782 push(@list_to, [$list_address, "open list${list_role}"]);
Joe Perchescb7301c2009-04-07 20:40:12 -0700783 }
784 }
785 } elsif ($ptype eq "M") {
Joe Perches0e70e832009-09-21 17:04:20 -0700786 my ($name, $address) = parse_email($pvalue);
787 if ($name eq "") {
Joe Perchesb7816552009-09-21 17:04:24 -0700788 if ($i > 0) {
789 my $tv = $typevalue[$i - 1];
Joe Perches0e70e832009-09-21 17:04:20 -0700790 if ($tv =~ m/^(\C):\s*(.*)/) {
791 if ($1 eq "P") {
792 $name = $2;
Joe Perchesa8af2432009-12-14 18:00:49 -0800793 $pvalue = format_email($name, $address, $email_usename);
Joe Perches5f2441e2009-06-16 15:34:02 -0700794 }
795 }
796 }
797 }
Joe Perches0e70e832009-09-21 17:04:20 -0700798 if ($email_maintainer) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800799 my $role = get_maintainer_role($i);
800 push_email_addresses($pvalue, $role);
Joe Perchescb7301c2009-04-07 20:40:12 -0700801 }
802 } elsif ($ptype eq "T") {
803 push(@scm, $pvalue);
804 } elsif ($ptype eq "W") {
805 push(@web, $pvalue);
806 } elsif ($ptype eq "S") {
807 push(@status, $pvalue);
808 }
Joe Perchescb7301c2009-04-07 20:40:12 -0700809 }
810 }
811}
812
Joe Perches11ecf532009-09-21 17:04:22 -0700813my %email_hash_name;
814my %email_hash_address;
Joe Perches0e70e832009-09-21 17:04:20 -0700815
Joe Perches11ecf532009-09-21 17:04:22 -0700816sub email_inuse {
817 my ($name, $address) = @_;
Joe Perches0e70e832009-09-21 17:04:20 -0700818
Joe Perches11ecf532009-09-21 17:04:22 -0700819 return 1 if (($name eq "") && ($address eq ""));
820 return 1 if (($name ne "") && exists($email_hash_name{$name}));
821 return 1 if (($address ne "") && exists($email_hash_address{$address}));
822
Joe Perches0e70e832009-09-21 17:04:20 -0700823 return 0;
824}
825
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700826sub push_email_address {
Joe Perches3c7385b2009-12-14 18:00:46 -0800827 my ($line, $role) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700828
Joe Perches0e70e832009-09-21 17:04:20 -0700829 my ($name, $address) = parse_email($line);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700830
Joe Perchesb7816552009-09-21 17:04:24 -0700831 if ($address eq "") {
832 return 0;
833 }
834
Joe Perches11ecf532009-09-21 17:04:22 -0700835 if (!$email_remove_duplicates) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800836 push(@email_to, [format_email($name, $address, $email_usename), $role]);
Joe Perches11ecf532009-09-21 17:04:22 -0700837 } elsif (!email_inuse($name, $address)) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800838 push(@email_to, [format_email($name, $address, $email_usename), $role]);
Joe Perches11ecf532009-09-21 17:04:22 -0700839 $email_hash_name{$name}++;
840 $email_hash_address{$address}++;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700841 }
Joe Perchesb7816552009-09-21 17:04:24 -0700842
843 return 1;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700844}
845
846sub push_email_addresses {
Joe Perches3c7385b2009-12-14 18:00:46 -0800847 my ($address, $role) = @_;
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700848
849 my @address_list = ();
850
Joe Perches5f2441e2009-06-16 15:34:02 -0700851 if (rfc822_valid($address)) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800852 push_email_address($address, $role);
Joe Perches5f2441e2009-06-16 15:34:02 -0700853 } elsif (@address_list = rfc822_validlist($address)) {
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700854 my $array_count = shift(@address_list);
855 while (my $entry = shift(@address_list)) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800856 push_email_address($entry, $role);
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700857 }
Joe Perches5f2441e2009-06-16 15:34:02 -0700858 } else {
Joe Perches3c7385b2009-12-14 18:00:46 -0800859 if (!push_email_address($address, $role)) {
Joe Perchesb7816552009-09-21 17:04:24 -0700860 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
861 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700862 }
Joe Perches1b5e1cf2009-06-16 15:34:01 -0700863}
864
Joe Perches3c7385b2009-12-14 18:00:46 -0800865sub add_role {
866 my ($line, $role) = @_;
867
868 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -0800869 my $email = format_email($name, $address, $email_usename);
Joe Perches3c7385b2009-12-14 18:00:46 -0800870
871 foreach my $entry (@email_to) {
872 if ($email_remove_duplicates) {
873 my ($entry_name, $entry_address) = parse_email($entry->[0]);
Joe Perches03372db2010-03-05 13:43:00 -0800874 if (($name eq $entry_name || $address eq $entry_address)
875 && ($role eq "" || !($entry->[1] =~ m/$role/))
876 ) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800877 if ($entry->[1] eq "") {
878 $entry->[1] = "$role";
879 } else {
880 $entry->[1] = "$entry->[1],$role";
881 }
882 }
883 } else {
Joe Perches03372db2010-03-05 13:43:00 -0800884 if ($email eq $entry->[0]
885 && ($role eq "" || !($entry->[1] =~ m/$role/))
886 ) {
Joe Perches3c7385b2009-12-14 18:00:46 -0800887 if ($entry->[1] eq "") {
888 $entry->[1] = "$role";
889 } else {
890 $entry->[1] = "$entry->[1],$role";
891 }
892 }
893 }
894 }
895}
896
Joe Perchescb7301c2009-04-07 20:40:12 -0700897sub which {
898 my ($bin) = @_;
899
Joe Perchesf5f50782009-06-16 15:34:00 -0700900 foreach my $path (split(/:/, $ENV{PATH})) {
Joe Perchescb7301c2009-04-07 20:40:12 -0700901 if (-e "$path/$bin") {
902 return "$path/$bin";
903 }
904 }
905
906 return "";
907}
908
Joe Perches8cbb3a72009-09-21 17:04:21 -0700909sub mailmap {
Joe Perchesa8af2432009-12-14 18:00:49 -0800910 my (@lines) = @_;
Joe Perches8cbb3a72009-09-21 17:04:21 -0700911 my %hash;
912
913 foreach my $line (@lines) {
914 my ($name, $address) = parse_email($line);
915 if (!exists($hash{$name})) {
916 $hash{$name} = $address;
Joe Perches11ecf532009-09-21 17:04:22 -0700917 } elsif ($address ne $hash{$name}) {
918 $address = $hash{$name};
Joe Perchesa8af2432009-12-14 18:00:49 -0800919 $line = format_email($name, $address, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700920 }
921 if (exists($mailmap{$name})) {
922 my $obj = $mailmap{$name};
923 foreach my $map_address (@$obj) {
924 if (($map_address eq $address) &&
925 ($map_address ne $hash{$name})) {
Joe Perchesa8af2432009-12-14 18:00:49 -0800926 $line = format_email($name, $hash{$name}, $email_usename);
Joe Perches8cbb3a72009-09-21 17:04:21 -0700927 }
928 }
929 }
930 }
931
932 return @lines;
933}
934
Joe Perches60db31a2009-12-14 18:00:50 -0800935sub git_execute_cmd {
936 my ($cmd) = @_;
937 my @lines = ();
Joe Perchescb7301c2009-04-07 20:40:12 -0700938
Joe Perches60db31a2009-12-14 18:00:50 -0800939 my $output = `$cmd`;
940 $output =~ s/^\s*//gm;
941 @lines = split("\n", $output);
942
943 return @lines;
Joe Perchesa8af2432009-12-14 18:00:49 -0800944}
945
Joe Perches60db31a2009-12-14 18:00:50 -0800946sub hg_execute_cmd {
Joe Perchesa8af2432009-12-14 18:00:49 -0800947 my ($cmd) = @_;
Joe Perches60db31a2009-12-14 18:00:50 -0800948 my @lines = ();
Joe Perchesa8af2432009-12-14 18:00:49 -0800949
Joe Perches60db31a2009-12-14 18:00:50 -0800950 my $output = `$cmd`;
951 @lines = split("\n", $output);
952
953 return @lines;
954}
955
956sub vcs_find_signers {
957 my ($cmd) = @_;
Joe Perchesa8af2432009-12-14 18:00:49 -0800958 my @lines = ();
959 my $commits;
960
Joe Perches60db31a2009-12-14 18:00:50 -0800961 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
Joe Perchescb7301c2009-04-07 20:40:12 -0700962
Joe Perches60db31a2009-12-14 18:00:50 -0800963 my $pattern = $VCS_cmds{"commit_pattern"};
Joe Perchescb7301c2009-04-07 20:40:12 -0700964
Joe Perches60db31a2009-12-14 18:00:50 -0800965 $commits = grep(/$pattern/, @lines); # of commits
Joe Perchesafa81ee2009-07-29 15:04:28 -0700966
Joe Perches0e70e832009-09-21 17:04:20 -0700967 @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
968 if (!$email_git_penguin_chiefs) {
969 @lines = grep(!/${penguin_chiefs}/i, @lines);
970 }
971 # cut -f2- -d":"
972 s/.*:\s*(.+)\s*/$1/ for (@lines);
973
Joe Perchesa8af2432009-12-14 18:00:49 -0800974## Reformat email addresses (with names) to avoid badly written signatures
975
Joe Perches3c7385b2009-12-14 18:00:46 -0800976 foreach my $line (@lines) {
977 my ($name, $address) = parse_email($line);
Joe Perchesa8af2432009-12-14 18:00:49 -0800978 $line = format_email($name, $address, 1);
979 }
980
981 return ($commits, @lines);
982}
983
Joe Perches60db31a2009-12-14 18:00:50 -0800984sub vcs_save_commits {
985 my ($cmd) = @_;
986 my @lines = ();
987 my @commits = ();
988
989 @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
990
991 foreach my $line (@lines) {
992 if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
993 push(@commits, $1);
994 }
995 }
996
997 return @commits;
998}
999
1000sub vcs_blame {
1001 my ($file) = @_;
1002 my $cmd;
1003 my @commits = ();
1004
1005 return @commits if (!(-f $file));
1006
1007 if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
1008 my @all_commits = ();
1009
1010 $cmd = $VCS_cmds{"blame_file_cmd"};
1011 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1012 @all_commits = vcs_save_commits($cmd);
1013
1014 foreach my $file_range_diff (@range) {
1015 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1016 my $diff_file = $1;
1017 my $diff_start = $2;
1018 my $diff_length = $3;
1019 next if ("$file" ne "$diff_file");
1020 for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
1021 push(@commits, $all_commits[$i]);
1022 }
1023 }
1024 } elsif (@range) {
1025 foreach my $file_range_diff (@range) {
1026 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
1027 my $diff_file = $1;
1028 my $diff_start = $2;
1029 my $diff_length = $3;
1030 next if ("$file" ne "$diff_file");
1031 $cmd = $VCS_cmds{"blame_range_cmd"};
1032 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1033 push(@commits, vcs_save_commits($cmd));
1034 }
1035 } else {
1036 $cmd = $VCS_cmds{"blame_file_cmd"};
1037 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1038 @commits = vcs_save_commits($cmd);
1039 }
1040
1041 return @commits;
1042}
1043
1044my $printed_novcs = 0;
1045sub vcs_exists {
1046 %VCS_cmds = %VCS_cmds_git;
1047 return 1 if eval $VCS_cmds{"available"};
1048 %VCS_cmds = %VCS_cmds_hg;
1049 return 1 if eval $VCS_cmds{"available"};
1050 %VCS_cmds = ();
1051 if (!$printed_novcs) {
1052 warn("$P: No supported VCS found. Add --nogit to options?\n");
1053 warn("Using a git repository produces better results.\n");
1054 warn("Try Linus Torvalds' latest git repository using:\n");
1055 warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
1056 $printed_novcs = 1;
1057 }
1058 return 0;
1059}
1060
1061sub vcs_assign {
Joe Perchesa8af2432009-12-14 18:00:49 -08001062 my ($role, $divisor, @lines) = @_;
1063
1064 my %hash;
1065 my $count = 0;
1066
Joe Perchesa8af2432009-12-14 18:00:49 -08001067 return if (@lines <= 0);
1068
1069 if ($divisor <= 0) {
Joe Perches60db31a2009-12-14 18:00:50 -08001070 warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
Joe Perchesa8af2432009-12-14 18:00:49 -08001071 $divisor = 1;
Joe Perches3c7385b2009-12-14 18:00:46 -08001072 }
Joe Perches8cbb3a72009-09-21 17:04:21 -07001073
Joe Perches11ecf532009-09-21 17:04:22 -07001074 if ($email_remove_duplicates) {
1075 @lines = mailmap(@lines);
1076 }
Joe Perches0e70e832009-09-21 17:04:20 -07001077
Joe Perches0e70e832009-09-21 17:04:20 -07001078 @lines = sort(@lines);
Joe Perchesafa81ee2009-07-29 15:04:28 -07001079
Joe Perches11ecf532009-09-21 17:04:22 -07001080 # uniq -c
1081 $hash{$_}++ for @lines;
1082
1083 # sort -rn
1084 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
1085 my $sign_offs = $hash{$line};
Joe Perchesa8af2432009-12-14 18:00:49 -08001086 my $percent = $sign_offs * 100 / $divisor;
Joe Perches3c7385b2009-12-14 18:00:46 -08001087
Joe Perchesa8af2432009-12-14 18:00:49 -08001088 $percent = 100 if ($percent > 100);
Joe Perches11ecf532009-09-21 17:04:22 -07001089 $count++;
1090 last if ($sign_offs < $email_git_min_signatures ||
1091 $count > $email_git_max_maintainers ||
Joe Perchesa8af2432009-12-14 18:00:49 -08001092 $percent < $email_git_min_percent);
Joe Perches3c7385b2009-12-14 18:00:46 -08001093 push_email_address($line, '');
Joe Perches3c7385b2009-12-14 18:00:46 -08001094 if ($output_rolestats) {
Joe Perchesa8af2432009-12-14 18:00:49 -08001095 my $fmt_percent = sprintf("%.0f", $percent);
1096 add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
1097 } else {
1098 add_role($line, $role);
Joe Perches3c7385b2009-12-14 18:00:46 -08001099 }
Joe Perchesf5492662009-09-21 17:04:13 -07001100 }
1101}
1102
Joe Perches60db31a2009-12-14 18:00:50 -08001103sub vcs_file_signoffs {
Joe Perchesa8af2432009-12-14 18:00:49 -08001104 my ($file) = @_;
1105
1106 my @signers = ();
Joe Perches60db31a2009-12-14 18:00:50 -08001107 my $commits;
Joe Perchesa8af2432009-12-14 18:00:49 -08001108
Joe Perches60db31a2009-12-14 18:00:50 -08001109 return if (!vcs_exists());
Joe Perchesa8af2432009-12-14 18:00:49 -08001110
Joe Perches60db31a2009-12-14 18:00:50 -08001111 my $cmd = $VCS_cmds{"find_signers_cmd"};
1112 $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
1113
1114 ($commits, @signers) = vcs_find_signers($cmd);
1115 vcs_assign("commit_signer", $commits, @signers);
Joe Perchesa8af2432009-12-14 18:00:49 -08001116}
1117
Joe Perches60db31a2009-12-14 18:00:50 -08001118sub vcs_file_blame {
Joe Perchesf5492662009-09-21 17:04:13 -07001119 my ($file) = @_;
1120
Joe Perches60db31a2009-12-14 18:00:50 -08001121 my @signers = ();
Joe Perchesa8af2432009-12-14 18:00:49 -08001122 my @commits = ();
Joe Perchesa8af2432009-12-14 18:00:49 -08001123 my $total_commits;
Joe Perchesf5492662009-09-21 17:04:13 -07001124
Joe Perches60db31a2009-12-14 18:00:50 -08001125 return if (!vcs_exists());
Joe Perchesf5492662009-09-21 17:04:13 -07001126
Joe Perches60db31a2009-12-14 18:00:50 -08001127 @commits = vcs_blame($file);
Joe Perchesf5492662009-09-21 17:04:13 -07001128 @commits = uniq(@commits);
Joe Perchesa8af2432009-12-14 18:00:49 -08001129 $total_commits = @commits;
1130
Joe Perchesf5492662009-09-21 17:04:13 -07001131 foreach my $commit (@commits) {
Joe Perchesa8af2432009-12-14 18:00:49 -08001132 my $commit_count;
1133 my @commit_signers = ();
Joe Perchesf5492662009-09-21 17:04:13 -07001134
Joe Perches60db31a2009-12-14 18:00:50 -08001135 my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
1136 $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
1137
1138 ($commit_count, @commit_signers) = vcs_find_signers($cmd);
1139 push(@signers, @commit_signers);
Joe Perchesf5492662009-09-21 17:04:13 -07001140 }
1141
Joe Perchesa8af2432009-12-14 18:00:49 -08001142 if ($from_filename) {
Joe Perches60db31a2009-12-14 18:00:50 -08001143 vcs_assign("commits", $total_commits, @signers);
Joe Perchesa8af2432009-12-14 18:00:49 -08001144 } else {
Joe Perches60db31a2009-12-14 18:00:50 -08001145 vcs_assign("modified commits", $total_commits, @signers);
Joe Perchesf5492662009-09-21 17:04:13 -07001146 }
Joe Perchescb7301c2009-04-07 20:40:12 -07001147}
1148
1149sub uniq {
Joe Perchesa8af2432009-12-14 18:00:49 -08001150 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001151
1152 my %saw;
1153 @parms = grep(!$saw{$_}++, @parms);
1154 return @parms;
1155}
1156
1157sub sort_and_uniq {
Joe Perchesa8af2432009-12-14 18:00:49 -08001158 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001159
1160 my %saw;
1161 @parms = sort @parms;
1162 @parms = grep(!$saw{$_}++, @parms);
1163 return @parms;
1164}
1165
Joe Perches03372db2010-03-05 13:43:00 -08001166sub clean_file_emails {
1167 my (@file_emails) = @_;
1168 my @fmt_emails = ();
1169
1170 foreach my $email (@file_emails) {
1171 $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
1172 my ($name, $address) = parse_email($email);
1173 if ($name eq '"[,\.]"') {
1174 $name = "";
1175 }
1176
1177 my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
1178 if (@nw > 2) {
1179 my $first = $nw[@nw - 3];
1180 my $middle = $nw[@nw - 2];
1181 my $last = $nw[@nw - 1];
1182
1183 if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
1184 (length($first) == 2 && substr($first, -1) eq ".")) ||
1185 (length($middle) == 1 ||
1186 (length($middle) == 2 && substr($middle, -1) eq "."))) {
1187 $name = "$first $middle $last";
1188 } else {
1189 $name = "$middle $last";
1190 }
1191 }
1192
1193 if (substr($name, -1) =~ /[,\.]/) {
1194 $name = substr($name, 0, length($name) - 1);
1195 } elsif (substr($name, -2) =~ /[,\.]"/) {
1196 $name = substr($name, 0, length($name) - 2) . '"';
1197 }
1198
1199 if (substr($name, 0, 1) =~ /[,\.]/) {
1200 $name = substr($name, 1, length($name) - 1);
1201 } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
1202 $name = '"' . substr($name, 2, length($name) - 2);
1203 }
1204
1205 my $fmt_email = format_email($name, $address, $email_usename);
1206 push(@fmt_emails, $fmt_email);
1207 }
1208 return @fmt_emails;
1209}
1210
Joe Perches3c7385b2009-12-14 18:00:46 -08001211sub merge_email {
1212 my @lines;
1213 my %saw;
1214
1215 for (@_) {
1216 my ($address, $role) = @$_;
1217 if (!$saw{$address}) {
1218 if ($output_roles) {
Joe Perches60db31a2009-12-14 18:00:50 -08001219 push(@lines, "$address ($role)");
Joe Perches3c7385b2009-12-14 18:00:46 -08001220 } else {
Joe Perches60db31a2009-12-14 18:00:50 -08001221 push(@lines, $address);
Joe Perches3c7385b2009-12-14 18:00:46 -08001222 }
1223 $saw{$address} = 1;
1224 }
1225 }
1226
1227 return @lines;
1228}
1229
Joe Perchescb7301c2009-04-07 20:40:12 -07001230sub output {
Joe Perchesa8af2432009-12-14 18:00:49 -08001231 my (@parms) = @_;
Joe Perchescb7301c2009-04-07 20:40:12 -07001232
1233 if ($output_multiline) {
1234 foreach my $line (@parms) {
1235 print("${line}\n");
1236 }
1237 } else {
1238 print(join($output_separator, @parms));
1239 print("\n");
1240 }
1241}
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001242
1243my $rfc822re;
1244
1245sub make_rfc822re {
1246# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
1247# comment. We must allow for rfc822_lwsp (or comments) after each of these.
1248# This regexp will only work on addresses which have had comments stripped
1249# and replaced with rfc822_lwsp.
1250
1251 my $specials = '()<>@,;:\\\\".\\[\\]';
1252 my $controls = '\\000-\\037\\177';
1253
1254 my $dtext = "[^\\[\\]\\r\\\\]";
1255 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
1256
1257 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
1258
1259# Use zero-width assertion to spot the limit of an atom. A simple
1260# $rfc822_lwsp* causes the regexp engine to hang occasionally.
1261 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
1262 my $word = "(?:$atom|$quoted_string)";
1263 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
1264
1265 my $sub_domain = "(?:$atom|$domain_literal)";
1266 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
1267
1268 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
1269
1270 my $phrase = "$word*";
1271 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
1272 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
1273 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
1274
1275 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
1276 my $address = "(?:$mailbox|$group)";
1277
1278 return "$rfc822_lwsp*$address";
1279}
1280
1281sub rfc822_strip_comments {
1282 my $s = shift;
1283# Recursively remove comments, and replace with a single space. The simpler
1284# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
1285# chars in atoms, for example.
1286
1287 while ($s =~ s/^((?:[^"\\]|\\.)*
1288 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
1289 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
1290 return $s;
1291}
1292
1293# valid: returns true if the parameter is an RFC822 valid address
1294#
Stephen Hemminger22dd5b02010-03-05 13:43:06 -08001295sub rfc822_valid {
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001296 my $s = rfc822_strip_comments(shift);
1297
1298 if (!$rfc822re) {
1299 $rfc822re = make_rfc822re();
1300 }
1301
1302 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
1303}
1304
1305# validlist: In scalar context, returns true if the parameter is an RFC822
1306# valid list of addresses.
1307#
1308# In list context, returns an empty list on failure (an invalid
1309# address was found); otherwise a list whose first element is the
1310# number of addresses found and whose remaining elements are the
1311# addresses. This is needed to disambiguate failure (invalid)
1312# from success with no addresses found, because an empty string is
1313# a valid list.
1314
Stephen Hemminger22dd5b02010-03-05 13:43:06 -08001315sub rfc822_validlist {
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001316 my $s = rfc822_strip_comments(shift);
1317
1318 if (!$rfc822re) {
1319 $rfc822re = make_rfc822re();
1320 }
1321 # * null list items are valid according to the RFC
1322 # * the '1' business is to aid in distinguishing failure from no results
1323
1324 my @r;
1325 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
1326 $s =~ m/^$rfc822_char*$/) {
Joe Perches5f2441e2009-06-16 15:34:02 -07001327 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
Joe Perches60db31a2009-12-14 18:00:50 -08001328 push(@r, $1);
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001329 }
1330 return wantarray ? (scalar(@r), @r) : 1;
1331 }
Joe Perches60db31a2009-12-14 18:00:50 -08001332 return wantarray ? () : 0;
Joe Perches1b5e1cf2009-06-16 15:34:01 -07001333}