get_maintainer.pl 33 KB

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