get_maintainer.pl 30 KB

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