get_maintainer.pl 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. #!/usr/bin/perl -w
  2. # (c) 2007, Joe Perches <joe@perches.com>
  3. # created from checkpatch.pl
  4. #
  5. # Print selected MAINTAINERS information for
  6. # the files modified in a patch or for a file
  7. #
  8. # usage: perl scripts/get_maintainer.pl [OPTIONS] <patch>
  9. # perl scripts/get_maintainer.pl [OPTIONS] -f <file>
  10. #
  11. # Licensed under the terms of the GNU GPL License version 2
  12. use strict;
  13. my $P = $0;
  14. my $V = '0.23';
  15. use Getopt::Long qw(:config no_auto_abbrev);
  16. my $lk_path = "./";
  17. my $email = 1;
  18. my $email_usename = 1;
  19. my $email_maintainer = 1;
  20. my $email_list = 1;
  21. my $email_subscriber_list = 0;
  22. my $email_git_penguin_chiefs = 0;
  23. my $email_git = 1;
  24. my $email_git_blame = 0;
  25. my $email_git_min_signatures = 1;
  26. my $email_git_max_maintainers = 5;
  27. my $email_git_min_percent = 5;
  28. my $email_git_since = "1-year-ago";
  29. my $email_hg_since = "-365";
  30. my $email_remove_duplicates = 1;
  31. my $output_multiline = 1;
  32. my $output_separator = ", ";
  33. my $output_roles = 0;
  34. my $output_rolestats = 0;
  35. my $scm = 0;
  36. my $web = 0;
  37. my $subsystem = 0;
  38. my $status = 0;
  39. my $keywords = 1;
  40. my $sections = 0;
  41. my $file_emails = 0;
  42. my $from_filename = 0;
  43. my $pattern_depth = 0;
  44. my $version = 0;
  45. my $help = 0;
  46. my $exit = 0;
  47. my @penguin_chief = ();
  48. push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
  49. #Andrew wants in on most everything - 2009/01/14
  50. #push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
  51. my @penguin_chief_names = ();
  52. foreach my $chief (@penguin_chief) {
  53. if ($chief =~ m/^(.*):(.*)/) {
  54. my $chief_name = $1;
  55. my $chief_addr = $2;
  56. push(@penguin_chief_names, $chief_name);
  57. }
  58. }
  59. my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
  60. # rfc822 email address - preloaded methods go here.
  61. my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
  62. my $rfc822_char = '[\\000-\\377]';
  63. # VCS command support: class-like functions and strings
  64. my %VCS_cmds;
  65. my %VCS_cmds_git = (
  66. "execute_cmd" => \&git_execute_cmd,
  67. "available" => '(which("git") ne "") && (-d ".git")',
  68. "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file",
  69. "find_commit_signers_cmd" => "git log --no-color -1 \$commit",
  70. "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
  71. "blame_file_cmd" => "git blame -l \$file",
  72. "commit_pattern" => "^commit [0-9a-f]{40,40}",
  73. "blame_commit_pattern" => "^([0-9a-f]+) "
  74. );
  75. my %VCS_cmds_hg = (
  76. "execute_cmd" => \&hg_execute_cmd,
  77. "available" => '(which("hg") ne "") && (-d ".hg")',
  78. "find_signers_cmd" =>
  79. "hg log --date=\$email_hg_since" .
  80. " --template='commit {node}\\n{desc}\\n' -- \$file",
  81. "find_commit_signers_cmd" => "hg log --template='{desc}\\n' -r \$commit",
  82. "blame_range_cmd" => "", # not supported
  83. "blame_file_cmd" => "hg blame -c \$file",
  84. "commit_pattern" => "^commit [0-9a-f]{40,40}",
  85. "blame_commit_pattern" => "^([0-9a-f]+):"
  86. );
  87. if (!GetOptions(
  88. 'email!' => \$email,
  89. 'git!' => \$email_git,
  90. 'git-blame!' => \$email_git_blame,
  91. 'git-chief-penguins!' => \$email_git_penguin_chiefs,
  92. 'git-min-signatures=i' => \$email_git_min_signatures,
  93. 'git-max-maintainers=i' => \$email_git_max_maintainers,
  94. 'git-min-percent=i' => \$email_git_min_percent,
  95. 'git-since=s' => \$email_git_since,
  96. 'hg-since=s' => \$email_hg_since,
  97. 'remove-duplicates!' => \$email_remove_duplicates,
  98. 'm!' => \$email_maintainer,
  99. 'n!' => \$email_usename,
  100. 'l!' => \$email_list,
  101. 's!' => \$email_subscriber_list,
  102. 'multiline!' => \$output_multiline,
  103. 'roles!' => \$output_roles,
  104. 'rolestats!' => \$output_rolestats,
  105. 'separator=s' => \$output_separator,
  106. 'subsystem!' => \$subsystem,
  107. 'status!' => \$status,
  108. 'scm!' => \$scm,
  109. 'web!' => \$web,
  110. 'pattern-depth=i' => \$pattern_depth,
  111. 'k|keywords!' => \$keywords,
  112. 'sections!' => \$sections,
  113. 'fe|file-emails!' => \$file_emails,
  114. 'f|file' => \$from_filename,
  115. 'v|version' => \$version,
  116. 'h|help|usage' => \$help,
  117. )) {
  118. die "$P: invalid argument - use --help if necessary\n";
  119. }
  120. if ($help != 0) {
  121. usage();
  122. exit 0;
  123. }
  124. if ($version != 0) {
  125. print("${P} ${V}\n");
  126. exit 0;
  127. }
  128. if (-t STDIN && !@ARGV) {
  129. # We're talking to a terminal, but have no command line arguments.
  130. die "$P: missing patchfile or -f file - use --help if necessary\n";
  131. }
  132. if ($output_separator ne ", ") {
  133. $output_multiline = 0;
  134. }
  135. if ($output_rolestats) {
  136. $output_roles = 1;
  137. }
  138. if ($sections) {
  139. $email = 0;
  140. $email_list = 0;
  141. $scm = 0;
  142. $status = 0;
  143. $subsystem = 0;
  144. $web = 0;
  145. $keywords = 0;
  146. } else {
  147. my $selections = $email + $scm + $status + $subsystem + $web;
  148. if ($selections == 0) {
  149. die "$P: Missing required option: email, scm, status, subsystem or web\n";
  150. }
  151. }
  152. if ($email &&
  153. ($email_maintainer + $email_list + $email_subscriber_list +
  154. $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
  155. die "$P: Please select at least 1 email option\n";
  156. }
  157. if (!top_of_kernel_tree($lk_path)) {
  158. die "$P: The current directory does not appear to be "
  159. . "a linux kernel source tree.\n";
  160. }
  161. ## Read MAINTAINERS for type/value pairs
  162. my @typevalue = ();
  163. my %keyword_hash;
  164. open (my $maint, '<', "${lk_path}MAINTAINERS")
  165. or die "$P: Can't open MAINTAINERS: $!\n";
  166. while (<$maint>) {
  167. my $line = $_;
  168. if ($line =~ m/^(\C):\s*(.*)/) {
  169. my $type = $1;
  170. my $value = $2;
  171. ##Filename pattern matching
  172. if ($type eq "F" || $type eq "X") {
  173. $value =~ s@\.@\\\.@g; ##Convert . to \.
  174. $value =~ s/\*/\.\*/g; ##Convert * to .*
  175. $value =~ s/\?/\./g; ##Convert ? to .
  176. ##if pattern is a directory and it lacks a trailing slash, add one
  177. if ((-d $value)) {
  178. $value =~ s@([^/])$@$1/@;
  179. }
  180. } elsif ($type eq "K") {
  181. $keyword_hash{@typevalue} = $value;
  182. }
  183. push(@typevalue, "$type:$value");
  184. } elsif (!/^(\s)*$/) {
  185. $line =~ s/\n$//g;
  186. push(@typevalue, $line);
  187. }
  188. }
  189. close($maint);
  190. my %mailmap;
  191. if ($email_remove_duplicates) {
  192. open(my $mailmap, '<', "${lk_path}.mailmap")
  193. or warn "$P: Can't open .mailmap: $!\n";
  194. while (<$mailmap>) {
  195. my $line = $_;
  196. next if ($line =~ m/^\s*#/);
  197. next if ($line =~ m/^\s*$/);
  198. my ($name, $address) = parse_email($line);
  199. $line = format_email($name, $address, $email_usename);
  200. next if ($line =~ m/^\s*$/);
  201. if (exists($mailmap{$name})) {
  202. my $obj = $mailmap{$name};
  203. push(@$obj, $address);
  204. } else {
  205. my @arr = ($address);
  206. $mailmap{$name} = \@arr;
  207. }
  208. }
  209. close($mailmap);
  210. }
  211. ## use the filenames on the command line or find the filenames in the patchfiles
  212. my @files = ();
  213. my @range = ();
  214. my @keyword_tvi = ();
  215. my @file_emails = ();
  216. if (!@ARGV) {
  217. push(@ARGV, "&STDIN");
  218. }
  219. foreach my $file (@ARGV) {
  220. if ($file ne "&STDIN") {
  221. ##if $file is a directory and it lacks a trailing slash, add one
  222. if ((-d $file)) {
  223. $file =~ s@([^/])$@$1/@;
  224. } elsif (!(-f $file)) {
  225. die "$P: file '${file}' not found\n";
  226. }
  227. }
  228. if ($from_filename) {
  229. push(@files, $file);
  230. if (-f $file && ($keywords || $file_emails)) {
  231. open(my $f, '<', $file)
  232. or die "$P: Can't open $file: $!\n";
  233. my $text = do { local($/) ; <$f> };
  234. close($f);
  235. if ($keywords) {
  236. foreach my $line (keys %keyword_hash) {
  237. if ($text =~ m/$keyword_hash{$line}/x) {
  238. push(@keyword_tvi, $line);
  239. }
  240. }
  241. }
  242. if ($file_emails) {
  243. 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;
  244. push(@file_emails, clean_file_emails(@poss_addr));
  245. }
  246. }
  247. } else {
  248. my $file_cnt = @files;
  249. my $lastfile;
  250. open(my $patch, "< $file")
  251. or die "$P: Can't open $file: $!\n";
  252. while (<$patch>) {
  253. my $patch_line = $_;
  254. if (m/^\+\+\+\s+(\S+)/) {
  255. my $filename = $1;
  256. $filename =~ s@^[^/]*/@@;
  257. $filename =~ s@\n@@;
  258. $lastfile = $filename;
  259. push(@files, $filename);
  260. } elsif (m/^\@\@ -(\d+),(\d+)/) {
  261. if ($email_git_blame) {
  262. push(@range, "$lastfile:$1:$2");
  263. }
  264. } elsif ($keywords) {
  265. foreach my $line (keys %keyword_hash) {
  266. if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
  267. push(@keyword_tvi, $line);
  268. }
  269. }
  270. }
  271. }
  272. close($patch);
  273. if ($file_cnt == @files) {
  274. warn "$P: file '${file}' doesn't appear to be a patch. "
  275. . "Add -f to options?\n";
  276. }
  277. @files = sort_and_uniq(@files);
  278. }
  279. }
  280. @file_emails = uniq(@file_emails);
  281. my @email_to = ();
  282. my @list_to = ();
  283. my @scm = ();
  284. my @web = ();
  285. my @subsystem = ();
  286. my @status = ();
  287. # Find responsible parties
  288. foreach my $file (@files) {
  289. my %hash;
  290. my $tvi = find_first_section();
  291. while ($tvi < @typevalue) {
  292. my $start = find_starting_index($tvi);
  293. my $end = find_ending_index($tvi);
  294. my $exclude = 0;
  295. my $i;
  296. #Do not match excluded file patterns
  297. for ($i = $start; $i < $end; $i++) {
  298. my $line = $typevalue[$i];
  299. if ($line =~ m/^(\C):\s*(.*)/) {
  300. my $type = $1;
  301. my $value = $2;
  302. if ($type eq 'X') {
  303. if (file_match_pattern($file, $value)) {
  304. $exclude = 1;
  305. last;
  306. }
  307. }
  308. }
  309. }
  310. if (!$exclude) {
  311. for ($i = $start; $i < $end; $i++) {
  312. my $line = $typevalue[$i];
  313. if ($line =~ m/^(\C):\s*(.*)/) {
  314. my $type = $1;
  315. my $value = $2;
  316. if ($type eq 'F') {
  317. if (file_match_pattern($file, $value)) {
  318. my $value_pd = ($value =~ tr@/@@);
  319. my $file_pd = ($file =~ tr@/@@);
  320. $value_pd++ if (substr($value,-1,1) ne "/");
  321. if ($pattern_depth == 0 ||
  322. (($file_pd - $value_pd) < $pattern_depth)) {
  323. $hash{$tvi} = $value_pd;
  324. }
  325. }
  326. }
  327. }
  328. }
  329. }
  330. $tvi = $end + 1;
  331. }
  332. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  333. add_categories($line);
  334. if ($sections) {
  335. my $i;
  336. my $start = find_starting_index($line);
  337. my $end = find_ending_index($line);
  338. for ($i = $start; $i < $end; $i++) {
  339. my $line = $typevalue[$i];
  340. if ($line =~ /^[FX]:/) { ##Restore file patterns
  341. $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
  342. $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
  343. $line =~ s/\\\./\./g; ##Convert \. to .
  344. $line =~ s/\.\*/\*/g; ##Convert .* to *
  345. }
  346. $line =~ s/^([A-Z]):/$1:\t/g;
  347. print("$line\n");
  348. }
  349. print("\n");
  350. }
  351. }
  352. if ($email && $email_git) {
  353. vcs_file_signoffs($file);
  354. }
  355. if ($email && $email_git_blame) {
  356. vcs_file_blame($file);
  357. }
  358. }
  359. if ($keywords) {
  360. @keyword_tvi = sort_and_uniq(@keyword_tvi);
  361. foreach my $line (@keyword_tvi) {
  362. add_categories($line);
  363. }
  364. }
  365. if ($email) {
  366. foreach my $chief (@penguin_chief) {
  367. if ($chief =~ m/^(.*):(.*)/) {
  368. my $email_address;
  369. $email_address = format_email($1, $2, $email_usename);
  370. if ($email_git_penguin_chiefs) {
  371. push(@email_to, [$email_address, 'chief penguin']);
  372. } else {
  373. @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
  374. }
  375. }
  376. }
  377. foreach my $email (@file_emails) {
  378. my ($name, $address) = parse_email($email);
  379. my $tmp_email = format_email($name, $address, $email_usename);
  380. push_email_address($tmp_email, '');
  381. add_role($tmp_email, 'in file');
  382. }
  383. }
  384. if ($email || $email_list) {
  385. my @to = ();
  386. if ($email) {
  387. @to = (@to, @email_to);
  388. }
  389. if ($email_list) {
  390. @to = (@to, @list_to);
  391. }
  392. output(merge_email(@to));
  393. }
  394. if ($scm) {
  395. @scm = uniq(@scm);
  396. output(@scm);
  397. }
  398. if ($status) {
  399. @status = uniq(@status);
  400. output(@status);
  401. }
  402. if ($subsystem) {
  403. @subsystem = uniq(@subsystem);
  404. output(@subsystem);
  405. }
  406. if ($web) {
  407. @web = uniq(@web);
  408. output(@web);
  409. }
  410. exit($exit);
  411. sub file_match_pattern {
  412. my ($file, $pattern) = @_;
  413. if (substr($pattern, -1) eq "/") {
  414. if ($file =~ m@^$pattern@) {
  415. return 1;
  416. }
  417. } else {
  418. if ($file =~ m@^$pattern@) {
  419. my $s1 = ($file =~ tr@/@@);
  420. my $s2 = ($pattern =~ tr@/@@);
  421. if ($s1 == $s2) {
  422. return 1;
  423. }
  424. }
  425. }
  426. return 0;
  427. }
  428. sub usage {
  429. print <<EOT;
  430. usage: $P [options] patchfile
  431. $P [options] -f file|directory
  432. version: $V
  433. MAINTAINER field selection options:
  434. --email => print email address(es) if any
  435. --git => include recent git \*-by: signers
  436. --git-chief-penguins => include ${penguin_chiefs}
  437. --git-min-signatures => number of signatures required (default: 1)
  438. --git-max-maintainers => maximum maintainers to add (default: 5)
  439. --git-min-percent => minimum percentage of commits required (default: 5)
  440. --git-blame => use git blame to find modified commits for patch or file
  441. --git-since => git history to use (default: 1-year-ago)
  442. --hg-since => hg history to use (default: -365)
  443. --m => include maintainer(s) if any
  444. --n => include name 'Full Name <addr\@domain.tld>'
  445. --l => include list(s) if any
  446. --s => include subscriber only list(s) if any
  447. --remove-duplicates => minimize duplicate email names/addresses
  448. --roles => show roles (status:subsystem, git-signer, list, etc...)
  449. --rolestats => show roles and statistics (commits/total_commits, %)
  450. --file-emails => add email addresses found in -f file (default: 0 (off))
  451. --scm => print SCM tree(s) if any
  452. --status => print status if any
  453. --subsystem => print subsystem name if any
  454. --web => print website(s) if any
  455. Output type options:
  456. --separator [, ] => separator for multiple entries on 1 line
  457. using --separator also sets --nomultiline if --separator is not [, ]
  458. --multiline => print 1 entry per line
  459. Other options:
  460. --pattern-depth => Number of pattern directory traversals (default: 0 (all))
  461. --keywords => scan patch for keywords (default: 1 (on))
  462. --sections => print the entire subsystem sections with pattern matches
  463. --version => show version
  464. --help => show this help information
  465. Default options:
  466. [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
  467. Notes:
  468. Using "-f directory" may give unexpected results:
  469. Used with "--git", git signators for _all_ files in and below
  470. directory are examined as git recurses directories.
  471. Any specified X: (exclude) pattern matches are _not_ ignored.
  472. Used with "--nogit", directory is used as a pattern match,
  473. no individual file within the directory or subdirectory
  474. is matched.
  475. Used with "--git-blame", does not iterate all files in directory
  476. Using "--git-blame" is slow and may add old committers and authors
  477. that are no longer active maintainers to the output.
  478. Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
  479. other automated tools that expect only ["name"] <email address>
  480. may not work because of additional output after <email address>.
  481. Using "--rolestats" and "--git-blame" shows the #/total=% commits,
  482. not the percentage of the entire file authored. # of commits is
  483. not a good measure of amount of code authored. 1 major commit may
  484. contain a thousand lines, 5 trivial commits may modify a single line.
  485. If git is not installed, but mercurial (hg) is installed and an .hg
  486. repository exists, the following options apply to mercurial:
  487. --git,
  488. --git-min-signatures, --git-max-maintainers, --git-min-percent, and
  489. --git-blame
  490. Use --hg-since not --git-since to control date selection
  491. EOT
  492. }
  493. sub top_of_kernel_tree {
  494. my ($lk_path) = @_;
  495. if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
  496. $lk_path .= "/";
  497. }
  498. if ( (-f "${lk_path}COPYING")
  499. && (-f "${lk_path}CREDITS")
  500. && (-f "${lk_path}Kbuild")
  501. && (-f "${lk_path}MAINTAINERS")
  502. && (-f "${lk_path}Makefile")
  503. && (-f "${lk_path}README")
  504. && (-d "${lk_path}Documentation")
  505. && (-d "${lk_path}arch")
  506. && (-d "${lk_path}include")
  507. && (-d "${lk_path}drivers")
  508. && (-d "${lk_path}fs")
  509. && (-d "${lk_path}init")
  510. && (-d "${lk_path}ipc")
  511. && (-d "${lk_path}kernel")
  512. && (-d "${lk_path}lib")
  513. && (-d "${lk_path}scripts")) {
  514. return 1;
  515. }
  516. return 0;
  517. }
  518. sub parse_email {
  519. my ($formatted_email) = @_;
  520. my $name = "";
  521. my $address = "";
  522. if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
  523. $name = $1;
  524. $address = $2;
  525. } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
  526. $address = $1;
  527. } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
  528. $address = $1;
  529. }
  530. $name =~ s/^\s+|\s+$//g;
  531. $name =~ s/^\"|\"$//g;
  532. $address =~ s/^\s+|\s+$//g;
  533. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  534. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  535. $name = "\"$name\"";
  536. }
  537. return ($name, $address);
  538. }
  539. sub format_email {
  540. my ($name, $address, $usename) = @_;
  541. my $formatted_email;
  542. $name =~ s/^\s+|\s+$//g;
  543. $name =~ s/^\"|\"$//g;
  544. $address =~ s/^\s+|\s+$//g;
  545. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  546. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  547. $name = "\"$name\"";
  548. }
  549. if ($usename) {
  550. if ("$name" eq "") {
  551. $formatted_email = "$address";
  552. } else {
  553. $formatted_email = "$name <$address>";
  554. }
  555. } else {
  556. $formatted_email = $address;
  557. }
  558. return $formatted_email;
  559. }
  560. sub find_first_section {
  561. my $index = 0;
  562. while ($index < @typevalue) {
  563. my $tv = $typevalue[$index];
  564. if (($tv =~ m/^(\C):\s*(.*)/)) {
  565. last;
  566. }
  567. $index++;
  568. }
  569. return $index;
  570. }
  571. sub find_starting_index {
  572. my ($index) = @_;
  573. while ($index > 0) {
  574. my $tv = $typevalue[$index];
  575. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  576. last;
  577. }
  578. $index--;
  579. }
  580. return $index;
  581. }
  582. sub find_ending_index {
  583. my ($index) = @_;
  584. while ($index < @typevalue) {
  585. my $tv = $typevalue[$index];
  586. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  587. last;
  588. }
  589. $index++;
  590. }
  591. return $index;
  592. }
  593. sub get_maintainer_role {
  594. my ($index) = @_;
  595. my $i;
  596. my $start = find_starting_index($index);
  597. my $end = find_ending_index($index);
  598. my $role;
  599. my $subsystem = $typevalue[$start];
  600. if (length($subsystem) > 20) {
  601. $subsystem = substr($subsystem, 0, 17);
  602. $subsystem =~ s/\s*$//;
  603. $subsystem = $subsystem . "...";
  604. }
  605. for ($i = $start + 1; $i < $end; $i++) {
  606. my $tv = $typevalue[$i];
  607. if ($tv =~ m/^(\C):\s*(.*)/) {
  608. my $ptype = $1;
  609. my $pvalue = $2;
  610. if ($ptype eq "S") {
  611. $role = $pvalue;
  612. }
  613. }
  614. }
  615. $role = lc($role);
  616. if ($role eq "supported") {
  617. $role = "supporter";
  618. } elsif ($role eq "maintained") {
  619. $role = "maintainer";
  620. } elsif ($role eq "odd fixes") {
  621. $role = "odd fixer";
  622. } elsif ($role eq "orphan") {
  623. $role = "orphan minder";
  624. } elsif ($role eq "obsolete") {
  625. $role = "obsolete minder";
  626. } elsif ($role eq "buried alive in reporters") {
  627. $role = "chief penguin";
  628. }
  629. return $role . ":" . $subsystem;
  630. }
  631. sub get_list_role {
  632. my ($index) = @_;
  633. my $i;
  634. my $start = find_starting_index($index);
  635. my $end = find_ending_index($index);
  636. my $subsystem = $typevalue[$start];
  637. if (length($subsystem) > 20) {
  638. $subsystem = substr($subsystem, 0, 17);
  639. $subsystem =~ s/\s*$//;
  640. $subsystem = $subsystem . "...";
  641. }
  642. if ($subsystem eq "THE REST") {
  643. $subsystem = "";
  644. }
  645. return $subsystem;
  646. }
  647. sub add_categories {
  648. my ($index) = @_;
  649. my $i;
  650. my $start = find_starting_index($index);
  651. my $end = find_ending_index($index);
  652. push(@subsystem, $typevalue[$start]);
  653. for ($i = $start + 1; $i < $end; $i++) {
  654. my $tv = $typevalue[$i];
  655. if ($tv =~ m/^(\C):\s*(.*)/) {
  656. my $ptype = $1;
  657. my $pvalue = $2;
  658. if ($ptype eq "L") {
  659. my $list_address = $pvalue;
  660. my $list_additional = "";
  661. my $list_role = get_list_role($i);
  662. if ($list_role ne "") {
  663. $list_role = ":" . $list_role;
  664. }
  665. if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
  666. $list_address = $1;
  667. $list_additional = $2;
  668. }
  669. if ($list_additional =~ m/subscribers-only/) {
  670. if ($email_subscriber_list) {
  671. push(@list_to, [$list_address, "subscriber list${list_role}"]);
  672. }
  673. } else {
  674. if ($email_list) {
  675. push(@list_to, [$list_address, "open list${list_role}"]);
  676. }
  677. }
  678. } elsif ($ptype eq "M") {
  679. my ($name, $address) = parse_email($pvalue);
  680. if ($name eq "") {
  681. if ($i > 0) {
  682. my $tv = $typevalue[$i - 1];
  683. if ($tv =~ m/^(\C):\s*(.*)/) {
  684. if ($1 eq "P") {
  685. $name = $2;
  686. $pvalue = format_email($name, $address, $email_usename);
  687. }
  688. }
  689. }
  690. }
  691. if ($email_maintainer) {
  692. my $role = get_maintainer_role($i);
  693. push_email_addresses($pvalue, $role);
  694. }
  695. } elsif ($ptype eq "T") {
  696. push(@scm, $pvalue);
  697. } elsif ($ptype eq "W") {
  698. push(@web, $pvalue);
  699. } elsif ($ptype eq "S") {
  700. push(@status, $pvalue);
  701. }
  702. }
  703. }
  704. }
  705. my %email_hash_name;
  706. my %email_hash_address;
  707. sub email_inuse {
  708. my ($name, $address) = @_;
  709. return 1 if (($name eq "") && ($address eq ""));
  710. return 1 if (($name ne "") && exists($email_hash_name{$name}));
  711. return 1 if (($address ne "") && exists($email_hash_address{$address}));
  712. return 0;
  713. }
  714. sub push_email_address {
  715. my ($line, $role) = @_;
  716. my ($name, $address) = parse_email($line);
  717. if ($address eq "") {
  718. return 0;
  719. }
  720. if (!$email_remove_duplicates) {
  721. push(@email_to, [format_email($name, $address, $email_usename), $role]);
  722. } elsif (!email_inuse($name, $address)) {
  723. push(@email_to, [format_email($name, $address, $email_usename), $role]);
  724. $email_hash_name{$name}++;
  725. $email_hash_address{$address}++;
  726. }
  727. return 1;
  728. }
  729. sub push_email_addresses {
  730. my ($address, $role) = @_;
  731. my @address_list = ();
  732. if (rfc822_valid($address)) {
  733. push_email_address($address, $role);
  734. } elsif (@address_list = rfc822_validlist($address)) {
  735. my $array_count = shift(@address_list);
  736. while (my $entry = shift(@address_list)) {
  737. push_email_address($entry, $role);
  738. }
  739. } else {
  740. if (!push_email_address($address, $role)) {
  741. warn("Invalid MAINTAINERS address: '" . $address . "'\n");
  742. }
  743. }
  744. }
  745. sub add_role {
  746. my ($line, $role) = @_;
  747. my ($name, $address) = parse_email($line);
  748. my $email = format_email($name, $address, $email_usename);
  749. foreach my $entry (@email_to) {
  750. if ($email_remove_duplicates) {
  751. my ($entry_name, $entry_address) = parse_email($entry->[0]);
  752. if (($name eq $entry_name || $address eq $entry_address)
  753. && ($role eq "" || !($entry->[1] =~ m/$role/))
  754. ) {
  755. if ($entry->[1] eq "") {
  756. $entry->[1] = "$role";
  757. } else {
  758. $entry->[1] = "$entry->[1],$role";
  759. }
  760. }
  761. } else {
  762. if ($email eq $entry->[0]
  763. && ($role eq "" || !($entry->[1] =~ m/$role/))
  764. ) {
  765. if ($entry->[1] eq "") {
  766. $entry->[1] = "$role";
  767. } else {
  768. $entry->[1] = "$entry->[1],$role";
  769. }
  770. }
  771. }
  772. }
  773. }
  774. sub which {
  775. my ($bin) = @_;
  776. foreach my $path (split(/:/, $ENV{PATH})) {
  777. if (-e "$path/$bin") {
  778. return "$path/$bin";
  779. }
  780. }
  781. return "";
  782. }
  783. sub mailmap {
  784. my (@lines) = @_;
  785. my %hash;
  786. foreach my $line (@lines) {
  787. my ($name, $address) = parse_email($line);
  788. if (!exists($hash{$name})) {
  789. $hash{$name} = $address;
  790. } elsif ($address ne $hash{$name}) {
  791. $address = $hash{$name};
  792. $line = format_email($name, $address, $email_usename);
  793. }
  794. if (exists($mailmap{$name})) {
  795. my $obj = $mailmap{$name};
  796. foreach my $map_address (@$obj) {
  797. if (($map_address eq $address) &&
  798. ($map_address ne $hash{$name})) {
  799. $line = format_email($name, $hash{$name}, $email_usename);
  800. }
  801. }
  802. }
  803. }
  804. return @lines;
  805. }
  806. sub git_execute_cmd {
  807. my ($cmd) = @_;
  808. my @lines = ();
  809. my $output = `$cmd`;
  810. $output =~ s/^\s*//gm;
  811. @lines = split("\n", $output);
  812. return @lines;
  813. }
  814. sub hg_execute_cmd {
  815. my ($cmd) = @_;
  816. my @lines = ();
  817. my $output = `$cmd`;
  818. @lines = split("\n", $output);
  819. return @lines;
  820. }
  821. sub vcs_find_signers {
  822. my ($cmd) = @_;
  823. my @lines = ();
  824. my $commits;
  825. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  826. my $pattern = $VCS_cmds{"commit_pattern"};
  827. $commits = grep(/$pattern/, @lines); # of commits
  828. @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
  829. if (!$email_git_penguin_chiefs) {
  830. @lines = grep(!/${penguin_chiefs}/i, @lines);
  831. }
  832. # cut -f2- -d":"
  833. s/.*:\s*(.+)\s*/$1/ for (@lines);
  834. ## Reformat email addresses (with names) to avoid badly written signatures
  835. foreach my $line (@lines) {
  836. my ($name, $address) = parse_email($line);
  837. $line = format_email($name, $address, 1);
  838. }
  839. return ($commits, @lines);
  840. }
  841. sub vcs_save_commits {
  842. my ($cmd) = @_;
  843. my @lines = ();
  844. my @commits = ();
  845. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  846. foreach my $line (@lines) {
  847. if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
  848. push(@commits, $1);
  849. }
  850. }
  851. return @commits;
  852. }
  853. sub vcs_blame {
  854. my ($file) = @_;
  855. my $cmd;
  856. my @commits = ();
  857. return @commits if (!(-f $file));
  858. if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
  859. my @all_commits = ();
  860. $cmd = $VCS_cmds{"blame_file_cmd"};
  861. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  862. @all_commits = vcs_save_commits($cmd);
  863. foreach my $file_range_diff (@range) {
  864. next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
  865. my $diff_file = $1;
  866. my $diff_start = $2;
  867. my $diff_length = $3;
  868. next if ("$file" ne "$diff_file");
  869. for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
  870. push(@commits, $all_commits[$i]);
  871. }
  872. }
  873. } elsif (@range) {
  874. foreach my $file_range_diff (@range) {
  875. next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
  876. my $diff_file = $1;
  877. my $diff_start = $2;
  878. my $diff_length = $3;
  879. next if ("$file" ne "$diff_file");
  880. $cmd = $VCS_cmds{"blame_range_cmd"};
  881. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  882. push(@commits, vcs_save_commits($cmd));
  883. }
  884. } else {
  885. $cmd = $VCS_cmds{"blame_file_cmd"};
  886. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  887. @commits = vcs_save_commits($cmd);
  888. }
  889. return @commits;
  890. }
  891. my $printed_novcs = 0;
  892. sub vcs_exists {
  893. %VCS_cmds = %VCS_cmds_git;
  894. return 1 if eval $VCS_cmds{"available"};
  895. %VCS_cmds = %VCS_cmds_hg;
  896. return 1 if eval $VCS_cmds{"available"};
  897. %VCS_cmds = ();
  898. if (!$printed_novcs) {
  899. warn("$P: No supported VCS found. Add --nogit to options?\n");
  900. warn("Using a git repository produces better results.\n");
  901. warn("Try Linus Torvalds' latest git repository using:\n");
  902. warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
  903. $printed_novcs = 1;
  904. }
  905. return 0;
  906. }
  907. sub vcs_assign {
  908. my ($role, $divisor, @lines) = @_;
  909. my %hash;
  910. my $count = 0;
  911. return if (@lines <= 0);
  912. if ($divisor <= 0) {
  913. warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
  914. $divisor = 1;
  915. }
  916. if ($email_remove_duplicates) {
  917. @lines = mailmap(@lines);
  918. }
  919. @lines = sort(@lines);
  920. # uniq -c
  921. $hash{$_}++ for @lines;
  922. # sort -rn
  923. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  924. my $sign_offs = $hash{$line};
  925. my $percent = $sign_offs * 100 / $divisor;
  926. $percent = 100 if ($percent > 100);
  927. $count++;
  928. last if ($sign_offs < $email_git_min_signatures ||
  929. $count > $email_git_max_maintainers ||
  930. $percent < $email_git_min_percent);
  931. push_email_address($line, '');
  932. if ($output_rolestats) {
  933. my $fmt_percent = sprintf("%.0f", $percent);
  934. add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
  935. } else {
  936. add_role($line, $role);
  937. }
  938. }
  939. }
  940. sub vcs_file_signoffs {
  941. my ($file) = @_;
  942. my @signers = ();
  943. my $commits;
  944. return if (!vcs_exists());
  945. my $cmd = $VCS_cmds{"find_signers_cmd"};
  946. $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
  947. ($commits, @signers) = vcs_find_signers($cmd);
  948. vcs_assign("commit_signer", $commits, @signers);
  949. }
  950. sub vcs_file_blame {
  951. my ($file) = @_;
  952. my @signers = ();
  953. my @commits = ();
  954. my $total_commits;
  955. return if (!vcs_exists());
  956. @commits = vcs_blame($file);
  957. @commits = uniq(@commits);
  958. $total_commits = @commits;
  959. foreach my $commit (@commits) {
  960. my $commit_count;
  961. my @commit_signers = ();
  962. my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
  963. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  964. ($commit_count, @commit_signers) = vcs_find_signers($cmd);
  965. push(@signers, @commit_signers);
  966. }
  967. if ($from_filename) {
  968. vcs_assign("commits", $total_commits, @signers);
  969. } else {
  970. vcs_assign("modified commits", $total_commits, @signers);
  971. }
  972. }
  973. sub uniq {
  974. my (@parms) = @_;
  975. my %saw;
  976. @parms = grep(!$saw{$_}++, @parms);
  977. return @parms;
  978. }
  979. sub sort_and_uniq {
  980. my (@parms) = @_;
  981. my %saw;
  982. @parms = sort @parms;
  983. @parms = grep(!$saw{$_}++, @parms);
  984. return @parms;
  985. }
  986. sub clean_file_emails {
  987. my (@file_emails) = @_;
  988. my @fmt_emails = ();
  989. foreach my $email (@file_emails) {
  990. $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
  991. my ($name, $address) = parse_email($email);
  992. if ($name eq '"[,\.]"') {
  993. $name = "";
  994. }
  995. my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
  996. if (@nw > 2) {
  997. my $first = $nw[@nw - 3];
  998. my $middle = $nw[@nw - 2];
  999. my $last = $nw[@nw - 1];
  1000. if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
  1001. (length($first) == 2 && substr($first, -1) eq ".")) ||
  1002. (length($middle) == 1 ||
  1003. (length($middle) == 2 && substr($middle, -1) eq "."))) {
  1004. $name = "$first $middle $last";
  1005. } else {
  1006. $name = "$middle $last";
  1007. }
  1008. }
  1009. if (substr($name, -1) =~ /[,\.]/) {
  1010. $name = substr($name, 0, length($name) - 1);
  1011. } elsif (substr($name, -2) =~ /[,\.]"/) {
  1012. $name = substr($name, 0, length($name) - 2) . '"';
  1013. }
  1014. if (substr($name, 0, 1) =~ /[,\.]/) {
  1015. $name = substr($name, 1, length($name) - 1);
  1016. } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
  1017. $name = '"' . substr($name, 2, length($name) - 2);
  1018. }
  1019. my $fmt_email = format_email($name, $address, $email_usename);
  1020. push(@fmt_emails, $fmt_email);
  1021. }
  1022. return @fmt_emails;
  1023. }
  1024. sub merge_email {
  1025. my @lines;
  1026. my %saw;
  1027. for (@_) {
  1028. my ($address, $role) = @$_;
  1029. if (!$saw{$address}) {
  1030. if ($output_roles) {
  1031. push(@lines, "$address ($role)");
  1032. } else {
  1033. push(@lines, $address);
  1034. }
  1035. $saw{$address} = 1;
  1036. }
  1037. }
  1038. return @lines;
  1039. }
  1040. sub output {
  1041. my (@parms) = @_;
  1042. if ($output_multiline) {
  1043. foreach my $line (@parms) {
  1044. print("${line}\n");
  1045. }
  1046. } else {
  1047. print(join($output_separator, @parms));
  1048. print("\n");
  1049. }
  1050. }
  1051. my $rfc822re;
  1052. sub make_rfc822re {
  1053. # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
  1054. # comment. We must allow for rfc822_lwsp (or comments) after each of these.
  1055. # This regexp will only work on addresses which have had comments stripped
  1056. # and replaced with rfc822_lwsp.
  1057. my $specials = '()<>@,;:\\\\".\\[\\]';
  1058. my $controls = '\\000-\\037\\177';
  1059. my $dtext = "[^\\[\\]\\r\\\\]";
  1060. my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
  1061. my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
  1062. # Use zero-width assertion to spot the limit of an atom. A simple
  1063. # $rfc822_lwsp* causes the regexp engine to hang occasionally.
  1064. my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
  1065. my $word = "(?:$atom|$quoted_string)";
  1066. my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
  1067. my $sub_domain = "(?:$atom|$domain_literal)";
  1068. my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
  1069. my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
  1070. my $phrase = "$word*";
  1071. my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
  1072. my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
  1073. my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
  1074. my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
  1075. my $address = "(?:$mailbox|$group)";
  1076. return "$rfc822_lwsp*$address";
  1077. }
  1078. sub rfc822_strip_comments {
  1079. my $s = shift;
  1080. # Recursively remove comments, and replace with a single space. The simpler
  1081. # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
  1082. # chars in atoms, for example.
  1083. while ($s =~ s/^((?:[^"\\]|\\.)*
  1084. (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
  1085. \((?:[^()\\]|\\.)*\)/$1 /osx) {}
  1086. return $s;
  1087. }
  1088. # valid: returns true if the parameter is an RFC822 valid address
  1089. #
  1090. sub rfc822_valid {
  1091. my $s = rfc822_strip_comments(shift);
  1092. if (!$rfc822re) {
  1093. $rfc822re = make_rfc822re();
  1094. }
  1095. return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
  1096. }
  1097. # validlist: In scalar context, returns true if the parameter is an RFC822
  1098. # valid list of addresses.
  1099. #
  1100. # In list context, returns an empty list on failure (an invalid
  1101. # address was found); otherwise a list whose first element is the
  1102. # number of addresses found and whose remaining elements are the
  1103. # addresses. This is needed to disambiguate failure (invalid)
  1104. # from success with no addresses found, because an empty string is
  1105. # a valid list.
  1106. sub rfc822_validlist {
  1107. my $s = rfc822_strip_comments(shift);
  1108. if (!$rfc822re) {
  1109. $rfc822re = make_rfc822re();
  1110. }
  1111. # * null list items are valid according to the RFC
  1112. # * the '1' business is to aid in distinguishing failure from no results
  1113. my @r;
  1114. if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
  1115. $s =~ m/^$rfc822_char*$/) {
  1116. while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
  1117. push(@r, $1);
  1118. }
  1119. return wantarray ? (scalar(@r), @r) : 1;
  1120. }
  1121. return wantarray ? () : 0;
  1122. }