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. }
  306. }
  307. }
  308. }
  309. if (!$exclude) {
  310. for ($i = $start; $i < $end; $i++) {
  311. my $line = $typevalue[$i];
  312. if ($line =~ m/^(\C):\s*(.*)/) {
  313. my $type = $1;
  314. my $value = $2;
  315. if ($type eq 'F') {
  316. if (file_match_pattern($file, $value)) {
  317. my $value_pd = ($value =~ tr@/@@);
  318. my $file_pd = ($file =~ tr@/@@);
  319. $value_pd++ if (substr($value,-1,1) ne "/");
  320. if ($pattern_depth == 0 ||
  321. (($file_pd - $value_pd) < $pattern_depth)) {
  322. $hash{$tvi} = $value_pd;
  323. }
  324. }
  325. }
  326. }
  327. }
  328. }
  329. $tvi += ($end - $start);
  330. }
  331. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  332. add_categories($line);
  333. if ($sections) {
  334. my $i;
  335. my $start = find_starting_index($line);
  336. my $end = find_ending_index($line);
  337. for ($i = $start; $i < $end; $i++) {
  338. my $line = $typevalue[$i];
  339. if ($line =~ /^[FX]:/) { ##Restore file patterns
  340. $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
  341. $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
  342. $line =~ s/\\\./\./g; ##Convert \. to .
  343. $line =~ s/\.\*/\*/g; ##Convert .* to *
  344. }
  345. $line =~ s/^([A-Z]):/$1:\t/g;
  346. print("$line\n");
  347. }
  348. print("\n");
  349. }
  350. }
  351. if ($email && $email_git) {
  352. vcs_file_signoffs($file);
  353. }
  354. if ($email && $email_git_blame) {
  355. vcs_file_blame($file);
  356. }
  357. }
  358. if ($keywords) {
  359. @keyword_tvi = sort_and_uniq(@keyword_tvi);
  360. foreach my $line (@keyword_tvi) {
  361. add_categories($line);
  362. }
  363. }
  364. if ($email) {
  365. foreach my $chief (@penguin_chief) {
  366. if ($chief =~ m/^(.*):(.*)/) {
  367. my $email_address;
  368. $email_address = format_email($1, $2, $email_usename);
  369. if ($email_git_penguin_chiefs) {
  370. push(@email_to, [$email_address, 'chief penguin']);
  371. } else {
  372. @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
  373. }
  374. }
  375. }
  376. foreach my $email (@file_emails) {
  377. my ($name, $address) = parse_email($email);
  378. my $tmp_email = format_email($name, $address, $email_usename);
  379. push_email_address($tmp_email, '');
  380. add_role($tmp_email, 'in file');
  381. }
  382. }
  383. if ($email || $email_list) {
  384. my @to = ();
  385. if ($email) {
  386. @to = (@to, @email_to);
  387. }
  388. if ($email_list) {
  389. @to = (@to, @list_to);
  390. }
  391. output(merge_email(@to));
  392. }
  393. if ($scm) {
  394. @scm = uniq(@scm);
  395. output(@scm);
  396. }
  397. if ($status) {
  398. @status = uniq(@status);
  399. output(@status);
  400. }
  401. if ($subsystem) {
  402. @subsystem = uniq(@subsystem);
  403. output(@subsystem);
  404. }
  405. if ($web) {
  406. @web = uniq(@web);
  407. output(@web);
  408. }
  409. exit($exit);
  410. sub file_match_pattern {
  411. my ($file, $pattern) = @_;
  412. if (substr($pattern, -1) eq "/") {
  413. if ($file =~ m@^$pattern@) {
  414. return 1;
  415. }
  416. } else {
  417. if ($file =~ m@^$pattern@) {
  418. my $s1 = ($file =~ tr@/@@);
  419. my $s2 = ($pattern =~ tr@/@@);
  420. if ($s1 == $s2) {
  421. return 1;
  422. }
  423. }
  424. }
  425. return 0;
  426. }
  427. sub usage {
  428. print <<EOT;
  429. usage: $P [options] patchfile
  430. $P [options] -f file|directory
  431. version: $V
  432. MAINTAINER field selection options:
  433. --email => print email address(es) if any
  434. --git => include recent git \*-by: signers
  435. --git-chief-penguins => include ${penguin_chiefs}
  436. --git-min-signatures => number of signatures required (default: 1)
  437. --git-max-maintainers => maximum maintainers to add (default: 5)
  438. --git-min-percent => minimum percentage of commits required (default: 5)
  439. --git-blame => use git blame to find modified commits for patch or file
  440. --git-since => git history to use (default: 1-year-ago)
  441. --hg-since => hg history to use (default: -365)
  442. --m => include maintainer(s) if any
  443. --n => include name 'Full Name <addr\@domain.tld>'
  444. --l => include list(s) if any
  445. --s => include subscriber only list(s) if any
  446. --remove-duplicates => minimize duplicate email names/addresses
  447. --roles => show roles (status:subsystem, git-signer, list, etc...)
  448. --rolestats => show roles and statistics (commits/total_commits, %)
  449. --file-emails => add email addresses found in -f file (default: 0 (off))
  450. --scm => print SCM tree(s) if any
  451. --status => print status if any
  452. --subsystem => print subsystem name if any
  453. --web => print website(s) if any
  454. Output type options:
  455. --separator [, ] => separator for multiple entries on 1 line
  456. using --separator also sets --nomultiline if --separator is not [, ]
  457. --multiline => print 1 entry per line
  458. Other options:
  459. --pattern-depth => Number of pattern directory traversals (default: 0 (all))
  460. --keywords => scan patch for keywords (default: 1 (on))
  461. --sections => print the entire subsystem sections with pattern matches
  462. --version => show version
  463. --help => show this help information
  464. Default options:
  465. [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
  466. Notes:
  467. Using "-f directory" may give unexpected results:
  468. Used with "--git", git signators for _all_ files in and below
  469. directory are examined as git recurses directories.
  470. Any specified X: (exclude) pattern matches are _not_ ignored.
  471. Used with "--nogit", directory is used as a pattern match,
  472. no individual file within the directory or subdirectory
  473. is matched.
  474. Used with "--git-blame", does not iterate all files in directory
  475. Using "--git-blame" is slow and may add old committers and authors
  476. that are no longer active maintainers to the output.
  477. Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
  478. other automated tools that expect only ["name"] <email address>
  479. may not work because of additional output after <email address>.
  480. Using "--rolestats" and "--git-blame" shows the #/total=% commits,
  481. not the percentage of the entire file authored. # of commits is
  482. not a good measure of amount of code authored. 1 major commit may
  483. contain a thousand lines, 5 trivial commits may modify a single line.
  484. If git is not installed, but mercurial (hg) is installed and an .hg
  485. repository exists, the following options apply to mercurial:
  486. --git,
  487. --git-min-signatures, --git-max-maintainers, --git-min-percent, and
  488. --git-blame
  489. Use --hg-since not --git-since to control date selection
  490. EOT
  491. }
  492. sub top_of_kernel_tree {
  493. my ($lk_path) = @_;
  494. if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
  495. $lk_path .= "/";
  496. }
  497. if ( (-f "${lk_path}COPYING")
  498. && (-f "${lk_path}CREDITS")
  499. && (-f "${lk_path}Kbuild")
  500. && (-f "${lk_path}MAINTAINERS")
  501. && (-f "${lk_path}Makefile")
  502. && (-f "${lk_path}README")
  503. && (-d "${lk_path}Documentation")
  504. && (-d "${lk_path}arch")
  505. && (-d "${lk_path}include")
  506. && (-d "${lk_path}drivers")
  507. && (-d "${lk_path}fs")
  508. && (-d "${lk_path}init")
  509. && (-d "${lk_path}ipc")
  510. && (-d "${lk_path}kernel")
  511. && (-d "${lk_path}lib")
  512. && (-d "${lk_path}scripts")) {
  513. return 1;
  514. }
  515. return 0;
  516. }
  517. sub parse_email {
  518. my ($formatted_email) = @_;
  519. my $name = "";
  520. my $address = "";
  521. if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
  522. $name = $1;
  523. $address = $2;
  524. } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
  525. $address = $1;
  526. } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
  527. $address = $1;
  528. }
  529. $name =~ s/^\s+|\s+$//g;
  530. $name =~ s/^\"|\"$//g;
  531. $address =~ s/^\s+|\s+$//g;
  532. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  533. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  534. $name = "\"$name\"";
  535. }
  536. return ($name, $address);
  537. }
  538. sub format_email {
  539. my ($name, $address, $usename) = @_;
  540. my $formatted_email;
  541. $name =~ s/^\s+|\s+$//g;
  542. $name =~ s/^\"|\"$//g;
  543. $address =~ s/^\s+|\s+$//g;
  544. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  545. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  546. $name = "\"$name\"";
  547. }
  548. if ($usename) {
  549. if ("$name" eq "") {
  550. $formatted_email = "$address";
  551. } else {
  552. $formatted_email = "$name <$address>";
  553. }
  554. } else {
  555. $formatted_email = $address;
  556. }
  557. return $formatted_email;
  558. }
  559. sub find_first_section {
  560. my $index = 0;
  561. while ($index < @typevalue) {
  562. my $tv = $typevalue[$index];
  563. if (($tv =~ m/^(\C):\s*(.*)/)) {
  564. last;
  565. }
  566. $index++;
  567. }
  568. return $index;
  569. }
  570. sub find_starting_index {
  571. my ($index) = @_;
  572. while ($index > 0) {
  573. my $tv = $typevalue[$index];
  574. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  575. last;
  576. }
  577. $index--;
  578. }
  579. return $index;
  580. }
  581. sub find_ending_index {
  582. my ($index) = @_;
  583. while ($index < @typevalue) {
  584. my $tv = $typevalue[$index];
  585. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  586. last;
  587. }
  588. $index++;
  589. }
  590. return $index;
  591. }
  592. sub get_maintainer_role {
  593. my ($index) = @_;
  594. my $i;
  595. my $start = find_starting_index($index);
  596. my $end = find_ending_index($index);
  597. my $role;
  598. my $subsystem = $typevalue[$start];
  599. if (length($subsystem) > 20) {
  600. $subsystem = substr($subsystem, 0, 17);
  601. $subsystem =~ s/\s*$//;
  602. $subsystem = $subsystem . "...";
  603. }
  604. for ($i = $start + 1; $i < $end; $i++) {
  605. my $tv = $typevalue[$i];
  606. if ($tv =~ m/^(\C):\s*(.*)/) {
  607. my $ptype = $1;
  608. my $pvalue = $2;
  609. if ($ptype eq "S") {
  610. $role = $pvalue;
  611. }
  612. }
  613. }
  614. $role = lc($role);
  615. if ($role eq "supported") {
  616. $role = "supporter";
  617. } elsif ($role eq "maintained") {
  618. $role = "maintainer";
  619. } elsif ($role eq "odd fixes") {
  620. $role = "odd fixer";
  621. } elsif ($role eq "orphan") {
  622. $role = "orphan minder";
  623. } elsif ($role eq "obsolete") {
  624. $role = "obsolete minder";
  625. } elsif ($role eq "buried alive in reporters") {
  626. $role = "chief penguin";
  627. }
  628. return $role . ":" . $subsystem;
  629. }
  630. sub get_list_role {
  631. my ($index) = @_;
  632. my $i;
  633. my $start = find_starting_index($index);
  634. my $end = find_ending_index($index);
  635. my $subsystem = $typevalue[$start];
  636. if (length($subsystem) > 20) {
  637. $subsystem = substr($subsystem, 0, 17);
  638. $subsystem =~ s/\s*$//;
  639. $subsystem = $subsystem . "...";
  640. }
  641. if ($subsystem eq "THE REST") {
  642. $subsystem = "";
  643. }
  644. return $subsystem;
  645. }
  646. sub add_categories {
  647. my ($index) = @_;
  648. my $i;
  649. my $start = find_starting_index($index);
  650. my $end = find_ending_index($index);
  651. push(@subsystem, $typevalue[$start]);
  652. for ($i = $start + 1; $i < $end; $i++) {
  653. my $tv = $typevalue[$i];
  654. if ($tv =~ m/^(\C):\s*(.*)/) {
  655. my $ptype = $1;
  656. my $pvalue = $2;
  657. if ($ptype eq "L") {
  658. my $list_address = $pvalue;
  659. my $list_additional = "";
  660. my $list_role = get_list_role($i);
  661. if ($list_role ne "") {
  662. $list_role = ":" . $list_role;
  663. }
  664. if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
  665. $list_address = $1;
  666. $list_additional = $2;
  667. }
  668. if ($list_additional =~ m/subscribers-only/) {
  669. if ($email_subscriber_list) {
  670. push(@list_to, [$list_address, "subscriber list${list_role}"]);
  671. }
  672. } else {
  673. if ($email_list) {
  674. push(@list_to, [$list_address, "open list${list_role}"]);
  675. }
  676. }
  677. } elsif ($ptype eq "M") {
  678. my ($name, $address) = parse_email($pvalue);
  679. if ($name eq "") {
  680. if ($i > 0) {
  681. my $tv = $typevalue[$i - 1];
  682. if ($tv =~ m/^(\C):\s*(.*)/) {
  683. if ($1 eq "P") {
  684. $name = $2;
  685. $pvalue = format_email($name, $address, $email_usename);
  686. }
  687. }
  688. }
  689. }
  690. if ($email_maintainer) {
  691. my $role = get_maintainer_role($i);
  692. push_email_addresses($pvalue, $role);
  693. }
  694. } elsif ($ptype eq "T") {
  695. push(@scm, $pvalue);
  696. } elsif ($ptype eq "W") {
  697. push(@web, $pvalue);
  698. } elsif ($ptype eq "S") {
  699. push(@status, $pvalue);
  700. }
  701. }
  702. }
  703. }
  704. my %email_hash_name;
  705. my %email_hash_address;
  706. sub email_inuse {
  707. my ($name, $address) = @_;
  708. return 1 if (($name eq "") && ($address eq ""));
  709. return 1 if (($name ne "") && exists($email_hash_name{$name}));
  710. return 1 if (($address ne "") && exists($email_hash_address{$address}));
  711. return 0;
  712. }
  713. sub push_email_address {
  714. my ($line, $role) = @_;
  715. my ($name, $address) = parse_email($line);
  716. if ($address eq "") {
  717. return 0;
  718. }
  719. if (!$email_remove_duplicates) {
  720. push(@email_to, [format_email($name, $address, $email_usename), $role]);
  721. } elsif (!email_inuse($name, $address)) {
  722. push(@email_to, [format_email($name, $address, $email_usename), $role]);
  723. $email_hash_name{$name}++;
  724. $email_hash_address{$address}++;
  725. }
  726. return 1;
  727. }
  728. sub push_email_addresses {
  729. my ($address, $role) = @_;
  730. my @address_list = ();
  731. if (rfc822_valid($address)) {
  732. push_email_address($address, $role);
  733. } elsif (@address_list = rfc822_validlist($address)) {
  734. my $array_count = shift(@address_list);
  735. while (my $entry = shift(@address_list)) {
  736. push_email_address($entry, $role);
  737. }
  738. } else {
  739. if (!push_email_address($address, $role)) {
  740. warn("Invalid MAINTAINERS address: '" . $address . "'\n");
  741. }
  742. }
  743. }
  744. sub add_role {
  745. my ($line, $role) = @_;
  746. my ($name, $address) = parse_email($line);
  747. my $email = format_email($name, $address, $email_usename);
  748. foreach my $entry (@email_to) {
  749. if ($email_remove_duplicates) {
  750. my ($entry_name, $entry_address) = parse_email($entry->[0]);
  751. if (($name eq $entry_name || $address eq $entry_address)
  752. && ($role eq "" || !($entry->[1] =~ m/$role/))
  753. ) {
  754. if ($entry->[1] eq "") {
  755. $entry->[1] = "$role";
  756. } else {
  757. $entry->[1] = "$entry->[1],$role";
  758. }
  759. }
  760. } else {
  761. if ($email eq $entry->[0]
  762. && ($role eq "" || !($entry->[1] =~ m/$role/))
  763. ) {
  764. if ($entry->[1] eq "") {
  765. $entry->[1] = "$role";
  766. } else {
  767. $entry->[1] = "$entry->[1],$role";
  768. }
  769. }
  770. }
  771. }
  772. }
  773. sub which {
  774. my ($bin) = @_;
  775. foreach my $path (split(/:/, $ENV{PATH})) {
  776. if (-e "$path/$bin") {
  777. return "$path/$bin";
  778. }
  779. }
  780. return "";
  781. }
  782. sub mailmap {
  783. my (@lines) = @_;
  784. my %hash;
  785. foreach my $line (@lines) {
  786. my ($name, $address) = parse_email($line);
  787. if (!exists($hash{$name})) {
  788. $hash{$name} = $address;
  789. } elsif ($address ne $hash{$name}) {
  790. $address = $hash{$name};
  791. $line = format_email($name, $address, $email_usename);
  792. }
  793. if (exists($mailmap{$name})) {
  794. my $obj = $mailmap{$name};
  795. foreach my $map_address (@$obj) {
  796. if (($map_address eq $address) &&
  797. ($map_address ne $hash{$name})) {
  798. $line = format_email($name, $hash{$name}, $email_usename);
  799. }
  800. }
  801. }
  802. }
  803. return @lines;
  804. }
  805. sub git_execute_cmd {
  806. my ($cmd) = @_;
  807. my @lines = ();
  808. my $output = `$cmd`;
  809. $output =~ s/^\s*//gm;
  810. @lines = split("\n", $output);
  811. return @lines;
  812. }
  813. sub hg_execute_cmd {
  814. my ($cmd) = @_;
  815. my @lines = ();
  816. my $output = `$cmd`;
  817. @lines = split("\n", $output);
  818. return @lines;
  819. }
  820. sub vcs_find_signers {
  821. my ($cmd) = @_;
  822. my @lines = ();
  823. my $commits;
  824. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  825. my $pattern = $VCS_cmds{"commit_pattern"};
  826. $commits = grep(/$pattern/, @lines); # of commits
  827. @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
  828. if (!$email_git_penguin_chiefs) {
  829. @lines = grep(!/${penguin_chiefs}/i, @lines);
  830. }
  831. # cut -f2- -d":"
  832. s/.*:\s*(.+)\s*/$1/ for (@lines);
  833. ## Reformat email addresses (with names) to avoid badly written signatures
  834. foreach my $line (@lines) {
  835. my ($name, $address) = parse_email($line);
  836. $line = format_email($name, $address, 1);
  837. }
  838. return ($commits, @lines);
  839. }
  840. sub vcs_save_commits {
  841. my ($cmd) = @_;
  842. my @lines = ();
  843. my @commits = ();
  844. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  845. foreach my $line (@lines) {
  846. if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
  847. push(@commits, $1);
  848. }
  849. }
  850. return @commits;
  851. }
  852. sub vcs_blame {
  853. my ($file) = @_;
  854. my $cmd;
  855. my @commits = ();
  856. return @commits if (!(-f $file));
  857. if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
  858. my @all_commits = ();
  859. $cmd = $VCS_cmds{"blame_file_cmd"};
  860. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  861. @all_commits = vcs_save_commits($cmd);
  862. foreach my $file_range_diff (@range) {
  863. next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
  864. my $diff_file = $1;
  865. my $diff_start = $2;
  866. my $diff_length = $3;
  867. next if ("$file" ne "$diff_file");
  868. for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
  869. push(@commits, $all_commits[$i]);
  870. }
  871. }
  872. } elsif (@range) {
  873. foreach my $file_range_diff (@range) {
  874. next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
  875. my $diff_file = $1;
  876. my $diff_start = $2;
  877. my $diff_length = $3;
  878. next if ("$file" ne "$diff_file");
  879. $cmd = $VCS_cmds{"blame_range_cmd"};
  880. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  881. push(@commits, vcs_save_commits($cmd));
  882. }
  883. } else {
  884. $cmd = $VCS_cmds{"blame_file_cmd"};
  885. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  886. @commits = vcs_save_commits($cmd);
  887. }
  888. return @commits;
  889. }
  890. my $printed_novcs = 0;
  891. sub vcs_exists {
  892. %VCS_cmds = %VCS_cmds_git;
  893. return 1 if eval $VCS_cmds{"available"};
  894. %VCS_cmds = %VCS_cmds_hg;
  895. return 1 if eval $VCS_cmds{"available"};
  896. %VCS_cmds = ();
  897. if (!$printed_novcs) {
  898. warn("$P: No supported VCS found. Add --nogit to options?\n");
  899. warn("Using a git repository produces better results.\n");
  900. warn("Try Linus Torvalds' latest git repository using:\n");
  901. warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
  902. $printed_novcs = 1;
  903. }
  904. return 0;
  905. }
  906. sub vcs_assign {
  907. my ($role, $divisor, @lines) = @_;
  908. my %hash;
  909. my $count = 0;
  910. return if (@lines <= 0);
  911. if ($divisor <= 0) {
  912. warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
  913. $divisor = 1;
  914. }
  915. if ($email_remove_duplicates) {
  916. @lines = mailmap(@lines);
  917. }
  918. @lines = sort(@lines);
  919. # uniq -c
  920. $hash{$_}++ for @lines;
  921. # sort -rn
  922. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  923. my $sign_offs = $hash{$line};
  924. my $percent = $sign_offs * 100 / $divisor;
  925. $percent = 100 if ($percent > 100);
  926. $count++;
  927. last if ($sign_offs < $email_git_min_signatures ||
  928. $count > $email_git_max_maintainers ||
  929. $percent < $email_git_min_percent);
  930. push_email_address($line, '');
  931. if ($output_rolestats) {
  932. my $fmt_percent = sprintf("%.0f", $percent);
  933. add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
  934. } else {
  935. add_role($line, $role);
  936. }
  937. }
  938. }
  939. sub vcs_file_signoffs {
  940. my ($file) = @_;
  941. my @signers = ();
  942. my $commits;
  943. return if (!vcs_exists());
  944. my $cmd = $VCS_cmds{"find_signers_cmd"};
  945. $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
  946. ($commits, @signers) = vcs_find_signers($cmd);
  947. vcs_assign("commit_signer", $commits, @signers);
  948. }
  949. sub vcs_file_blame {
  950. my ($file) = @_;
  951. my @signers = ();
  952. my @commits = ();
  953. my $total_commits;
  954. return if (!vcs_exists());
  955. @commits = vcs_blame($file);
  956. @commits = uniq(@commits);
  957. $total_commits = @commits;
  958. foreach my $commit (@commits) {
  959. my $commit_count;
  960. my @commit_signers = ();
  961. my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
  962. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  963. ($commit_count, @commit_signers) = vcs_find_signers($cmd);
  964. push(@signers, @commit_signers);
  965. }
  966. if ($from_filename) {
  967. vcs_assign("commits", $total_commits, @signers);
  968. } else {
  969. vcs_assign("modified commits", $total_commits, @signers);
  970. }
  971. }
  972. sub uniq {
  973. my (@parms) = @_;
  974. my %saw;
  975. @parms = grep(!$saw{$_}++, @parms);
  976. return @parms;
  977. }
  978. sub sort_and_uniq {
  979. my (@parms) = @_;
  980. my %saw;
  981. @parms = sort @parms;
  982. @parms = grep(!$saw{$_}++, @parms);
  983. return @parms;
  984. }
  985. sub clean_file_emails {
  986. my (@file_emails) = @_;
  987. my @fmt_emails = ();
  988. foreach my $email (@file_emails) {
  989. $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
  990. my ($name, $address) = parse_email($email);
  991. if ($name eq '"[,\.]"') {
  992. $name = "";
  993. }
  994. my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
  995. if (@nw > 2) {
  996. my $first = $nw[@nw - 3];
  997. my $middle = $nw[@nw - 2];
  998. my $last = $nw[@nw - 1];
  999. if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
  1000. (length($first) == 2 && substr($first, -1) eq ".")) ||
  1001. (length($middle) == 1 ||
  1002. (length($middle) == 2 && substr($middle, -1) eq "."))) {
  1003. $name = "$first $middle $last";
  1004. } else {
  1005. $name = "$middle $last";
  1006. }
  1007. }
  1008. if (substr($name, -1) =~ /[,\.]/) {
  1009. $name = substr($name, 0, length($name) - 1);
  1010. } elsif (substr($name, -2) =~ /[,\.]"/) {
  1011. $name = substr($name, 0, length($name) - 2) . '"';
  1012. }
  1013. if (substr($name, 0, 1) =~ /[,\.]/) {
  1014. $name = substr($name, 1, length($name) - 1);
  1015. } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
  1016. $name = '"' . substr($name, 2, length($name) - 2);
  1017. }
  1018. my $fmt_email = format_email($name, $address, $email_usename);
  1019. push(@fmt_emails, $fmt_email);
  1020. }
  1021. return @fmt_emails;
  1022. }
  1023. sub merge_email {
  1024. my @lines;
  1025. my %saw;
  1026. for (@_) {
  1027. my ($address, $role) = @$_;
  1028. if (!$saw{$address}) {
  1029. if ($output_roles) {
  1030. push(@lines, "$address ($role)");
  1031. } else {
  1032. push(@lines, $address);
  1033. }
  1034. $saw{$address} = 1;
  1035. }
  1036. }
  1037. return @lines;
  1038. }
  1039. sub output {
  1040. my (@parms) = @_;
  1041. if ($output_multiline) {
  1042. foreach my $line (@parms) {
  1043. print("${line}\n");
  1044. }
  1045. } else {
  1046. print(join($output_separator, @parms));
  1047. print("\n");
  1048. }
  1049. }
  1050. my $rfc822re;
  1051. sub make_rfc822re {
  1052. # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
  1053. # comment. We must allow for rfc822_lwsp (or comments) after each of these.
  1054. # This regexp will only work on addresses which have had comments stripped
  1055. # and replaced with rfc822_lwsp.
  1056. my $specials = '()<>@,;:\\\\".\\[\\]';
  1057. my $controls = '\\000-\\037\\177';
  1058. my $dtext = "[^\\[\\]\\r\\\\]";
  1059. my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
  1060. my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
  1061. # Use zero-width assertion to spot the limit of an atom. A simple
  1062. # $rfc822_lwsp* causes the regexp engine to hang occasionally.
  1063. my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
  1064. my $word = "(?:$atom|$quoted_string)";
  1065. my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
  1066. my $sub_domain = "(?:$atom|$domain_literal)";
  1067. my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
  1068. my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
  1069. my $phrase = "$word*";
  1070. my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
  1071. my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
  1072. my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
  1073. my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
  1074. my $address = "(?:$mailbox|$group)";
  1075. return "$rfc822_lwsp*$address";
  1076. }
  1077. sub rfc822_strip_comments {
  1078. my $s = shift;
  1079. # Recursively remove comments, and replace with a single space. The simpler
  1080. # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
  1081. # chars in atoms, for example.
  1082. while ($s =~ s/^((?:[^"\\]|\\.)*
  1083. (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
  1084. \((?:[^()\\]|\\.)*\)/$1 /osx) {}
  1085. return $s;
  1086. }
  1087. # valid: returns true if the parameter is an RFC822 valid address
  1088. #
  1089. sub rfc822_valid {
  1090. my $s = rfc822_strip_comments(shift);
  1091. if (!$rfc822re) {
  1092. $rfc822re = make_rfc822re();
  1093. }
  1094. return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
  1095. }
  1096. # validlist: In scalar context, returns true if the parameter is an RFC822
  1097. # valid list of addresses.
  1098. #
  1099. # In list context, returns an empty list on failure (an invalid
  1100. # address was found); otherwise a list whose first element is the
  1101. # number of addresses found and whose remaining elements are the
  1102. # addresses. This is needed to disambiguate failure (invalid)
  1103. # from success with no addresses found, because an empty string is
  1104. # a valid list.
  1105. sub rfc822_validlist {
  1106. my $s = rfc822_strip_comments(shift);
  1107. if (!$rfc822re) {
  1108. $rfc822re = make_rfc822re();
  1109. }
  1110. # * null list items are valid according to the RFC
  1111. # * the '1' business is to aid in distinguishing failure from no results
  1112. my @r;
  1113. if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
  1114. $s =~ m/^$rfc822_char*$/) {
  1115. while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
  1116. push(@r, $1);
  1117. }
  1118. return wantarray ? (scalar(@r), @r) : 1;
  1119. }
  1120. return wantarray ? () : 0;
  1121. }