get_maintainer.pl 32 KB

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