get_maintainer.pl 29 KB

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