get_maintainer.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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_maintainers.pl [OPTIONS] <patch>
  9. # perl scripts/get_maintainers.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.17';
  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 = 1;
  23. my $email_git_penguin_chiefs = 0;
  24. my $email_git_min_signatures = 1;
  25. my $email_git_max_maintainers = 5;
  26. my $email_git_min_percent = 5;
  27. my $email_git_since = "1-year-ago";
  28. my $output_multiline = 1;
  29. my $output_separator = ", ";
  30. my $scm = 0;
  31. my $web = 0;
  32. my $subsystem = 0;
  33. my $status = 0;
  34. my $from_filename = 0;
  35. my $version = 0;
  36. my $help = 0;
  37. my $exit = 0;
  38. my @penguin_chief = ();
  39. push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
  40. #Andrew wants in on most everything - 2009/01/14
  41. #push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
  42. my @penguin_chief_names = ();
  43. foreach my $chief (@penguin_chief) {
  44. if ($chief =~ m/^(.*):(.*)/) {
  45. my $chief_name = $1;
  46. my $chief_addr = $2;
  47. push(@penguin_chief_names, $chief_name);
  48. }
  49. }
  50. my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
  51. # rfc822 email address - preloaded methods go here.
  52. my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
  53. my $rfc822_char = '[\\000-\\377]';
  54. if (!GetOptions(
  55. 'email!' => \$email,
  56. 'git!' => \$email_git,
  57. 'git-chief-penguins!' => \$email_git_penguin_chiefs,
  58. 'git-min-signatures=i' => \$email_git_min_signatures,
  59. 'git-max-maintainers=i' => \$email_git_max_maintainers,
  60. 'git-min-percent=i' => \$email_git_min_percent,
  61. 'git-since=s' => \$email_git_since,
  62. 'm!' => \$email_maintainer,
  63. 'n!' => \$email_usename,
  64. 'l!' => \$email_list,
  65. 's!' => \$email_subscriber_list,
  66. 'multiline!' => \$output_multiline,
  67. 'separator=s' => \$output_separator,
  68. 'subsystem!' => \$subsystem,
  69. 'status!' => \$status,
  70. 'scm!' => \$scm,
  71. 'web!' => \$web,
  72. 'f|file' => \$from_filename,
  73. 'v|version' => \$version,
  74. 'h|help' => \$help,
  75. )) {
  76. usage();
  77. die "$P: invalid argument\n";
  78. }
  79. if ($help != 0) {
  80. usage();
  81. exit 0;
  82. }
  83. if ($version != 0) {
  84. print("${P} ${V}\n");
  85. exit 0;
  86. }
  87. if ($#ARGV < 0) {
  88. usage();
  89. die "$P: argument missing: patchfile or -f file please\n";
  90. }
  91. my $selections = $email + $scm + $status + $subsystem + $web;
  92. if ($selections == 0) {
  93. usage();
  94. die "$P: Missing required option: email, scm, status, subsystem or web\n";
  95. }
  96. if ($email && ($email_maintainer + $email_list + $email_subscriber_list
  97. + $email_git + $email_git_penguin_chiefs) == 0) {
  98. usage();
  99. die "$P: Please select at least 1 email option\n";
  100. }
  101. if (!top_of_kernel_tree($lk_path)) {
  102. die "$P: The current directory does not appear to be "
  103. . "a linux kernel source tree.\n";
  104. }
  105. ## Read MAINTAINERS for type/value pairs
  106. my @typevalue = ();
  107. open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
  108. while (<MAINT>) {
  109. my $line = $_;
  110. if ($line =~ m/^(\C):\s*(.*)/) {
  111. my $type = $1;
  112. my $value = $2;
  113. ##Filename pattern matching
  114. if ($type eq "F" || $type eq "X") {
  115. $value =~ s@\.@\\\.@g; ##Convert . to \.
  116. $value =~ s/\*/\.\*/g; ##Convert * to .*
  117. $value =~ s/\?/\./g; ##Convert ? to .
  118. ##if pattern is a directory and it lacks a trailing slash, add one
  119. if ((-d $value)) {
  120. $value =~ s@([^/])$@$1/@;
  121. }
  122. }
  123. push(@typevalue, "$type:$value");
  124. } elsif (!/^(\s)*$/) {
  125. $line =~ s/\n$//g;
  126. push(@typevalue, $line);
  127. }
  128. }
  129. close(MAINT);
  130. ## use the filenames on the command line or find the filenames in the patchfiles
  131. my @files = ();
  132. foreach my $file (@ARGV) {
  133. ##if $file is a directory and it lacks a trailing slash, add one
  134. if ((-d $file)) {
  135. $file =~ s@([^/])$@$1/@;
  136. } elsif (!(-f $file)) {
  137. die "$P: file '${file}' not found\n";
  138. }
  139. if ($from_filename) {
  140. push(@files, $file);
  141. } else {
  142. my $file_cnt = @files;
  143. open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
  144. while (<PATCH>) {
  145. if (m/^\+\+\+\s+(\S+)/) {
  146. my $filename = $1;
  147. $filename =~ s@^[^/]*/@@;
  148. $filename =~ s@\n@@;
  149. push(@files, $filename);
  150. }
  151. }
  152. close(PATCH);
  153. if ($file_cnt == @files) {
  154. warn "$P: file '${file}' doesn't appear to be a patch. "
  155. . "Add -f to options?\n";
  156. }
  157. @files = sort_and_uniq(@files);
  158. }
  159. }
  160. my @email_to = ();
  161. my @list_to = ();
  162. my @scm = ();
  163. my @web = ();
  164. my @subsystem = ();
  165. my @status = ();
  166. # Find responsible parties
  167. foreach my $file (@files) {
  168. #Do not match excluded file patterns
  169. my $exclude = 0;
  170. foreach my $line (@typevalue) {
  171. if ($line =~ m/^(\C):\s*(.*)/) {
  172. my $type = $1;
  173. my $value = $2;
  174. if ($type eq 'X') {
  175. if (file_match_pattern($file, $value)) {
  176. $exclude = 1;
  177. }
  178. }
  179. }
  180. }
  181. if (!$exclude) {
  182. my $tvi = 0;
  183. foreach my $line (@typevalue) {
  184. if ($line =~ m/^(\C):\s*(.*)/) {
  185. my $type = $1;
  186. my $value = $2;
  187. if ($type eq 'F') {
  188. if (file_match_pattern($file, $value)) {
  189. add_categories($tvi);
  190. }
  191. }
  192. }
  193. $tvi++;
  194. }
  195. }
  196. if ($email && $email_git) {
  197. recent_git_signoffs($file);
  198. }
  199. }
  200. if ($email) {
  201. foreach my $chief (@penguin_chief) {
  202. if ($chief =~ m/^(.*):(.*)/) {
  203. my $email_address;
  204. if ($email_usename) {
  205. $email_address = format_email($1, $2);
  206. } else {
  207. $email_address = $2;
  208. }
  209. if ($email_git_penguin_chiefs) {
  210. push(@email_to, $email_address);
  211. } else {
  212. @email_to = grep(!/${email_address}/, @email_to);
  213. }
  214. }
  215. }
  216. }
  217. if ($email || $email_list) {
  218. my @to = ();
  219. if ($email) {
  220. @to = (@to, @email_to);
  221. }
  222. if ($email_list) {
  223. @to = (@to, @list_to);
  224. }
  225. output(uniq(@to));
  226. }
  227. if ($scm) {
  228. @scm = sort_and_uniq(@scm);
  229. output(@scm);
  230. }
  231. if ($status) {
  232. @status = sort_and_uniq(@status);
  233. output(@status);
  234. }
  235. if ($subsystem) {
  236. @subsystem = sort_and_uniq(@subsystem);
  237. output(@subsystem);
  238. }
  239. if ($web) {
  240. @web = sort_and_uniq(@web);
  241. output(@web);
  242. }
  243. exit($exit);
  244. sub file_match_pattern {
  245. my ($file, $pattern) = @_;
  246. if (substr($pattern, -1) eq "/") {
  247. if ($file =~ m@^$pattern@) {
  248. return 1;
  249. }
  250. } else {
  251. if ($file =~ m@^$pattern@) {
  252. my $s1 = ($file =~ tr@/@@);
  253. my $s2 = ($pattern =~ tr@/@@);
  254. if ($s1 == $s2) {
  255. return 1;
  256. }
  257. }
  258. }
  259. return 0;
  260. }
  261. sub usage {
  262. print <<EOT;
  263. usage: $P [options] patchfile
  264. $P [options] -f file|directory
  265. version: $V
  266. MAINTAINER field selection options:
  267. --email => print email address(es) if any
  268. --git => include recent git \*-by: signers
  269. --git-chief-penguins => include ${penguin_chiefs}
  270. --git-min-signatures => number of signatures required (default: 1)
  271. --git-max-maintainers => maximum maintainers to add (default: 5)
  272. --git-min-percent => minimum percentage of commits required (default: 5)
  273. --git-since => git history to use (default: 1-year-ago)
  274. --m => include maintainer(s) if any
  275. --n => include name 'Full Name <addr\@domain.tld>'
  276. --l => include list(s) if any
  277. --s => include subscriber only list(s) if any
  278. --scm => print SCM tree(s) if any
  279. --status => print status if any
  280. --subsystem => print subsystem name if any
  281. --web => print website(s) if any
  282. Output type options:
  283. --separator [, ] => separator for multiple entries on 1 line
  284. --multiline => print 1 entry per line
  285. Default options:
  286. [--email --git --m --n --l --multiline]
  287. Other options:
  288. --version => show version
  289. --help => show this help information
  290. Notes:
  291. Using "-f directory" may give unexpected results:
  292. Used with "--git", git signators for _all_ files in and below
  293. directory are examined as git recurses directories.
  294. Any specified X: (exclude) pattern matches are _not_ ignored.
  295. Used with "--nogit", directory is used as a pattern match,
  296. no individual file within the directory or subdirectory
  297. is matched.
  298. EOT
  299. }
  300. sub top_of_kernel_tree {
  301. my ($lk_path) = @_;
  302. if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
  303. $lk_path .= "/";
  304. }
  305. if ( (-f "${lk_path}COPYING")
  306. && (-f "${lk_path}CREDITS")
  307. && (-f "${lk_path}Kbuild")
  308. && (-f "${lk_path}MAINTAINERS")
  309. && (-f "${lk_path}Makefile")
  310. && (-f "${lk_path}README")
  311. && (-d "${lk_path}Documentation")
  312. && (-d "${lk_path}arch")
  313. && (-d "${lk_path}include")
  314. && (-d "${lk_path}drivers")
  315. && (-d "${lk_path}fs")
  316. && (-d "${lk_path}init")
  317. && (-d "${lk_path}ipc")
  318. && (-d "${lk_path}kernel")
  319. && (-d "${lk_path}lib")
  320. && (-d "${lk_path}scripts")) {
  321. return 1;
  322. }
  323. return 0;
  324. }
  325. sub format_email {
  326. my ($name, $email) = @_;
  327. $name =~ s/^\s+|\s+$//g;
  328. $name =~ s/^\"|\"$//g;
  329. $email =~ s/^\s+|\s+$//g;
  330. my $formatted_email = "";
  331. if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
  332. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  333. $formatted_email = "\"${name}\"\ \<${email}\>";
  334. } else {
  335. $formatted_email = "${name} \<${email}\>";
  336. }
  337. return $formatted_email;
  338. }
  339. sub add_categories {
  340. my ($index) = @_;
  341. $index = $index - 1;
  342. while ($index >= 0) {
  343. my $tv = $typevalue[$index];
  344. if ($tv =~ m/^(\C):\s*(.*)/) {
  345. my $ptype = $1;
  346. my $pvalue = $2;
  347. if ($ptype eq "L") {
  348. my $list_address = $pvalue;
  349. my $list_additional = "";
  350. if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
  351. $list_address = $1;
  352. $list_additional = $2;
  353. }
  354. if ($list_additional =~ m/subscribers-only/) {
  355. if ($email_subscriber_list) {
  356. push(@list_to, $list_address);
  357. }
  358. } else {
  359. if ($email_list) {
  360. push(@list_to, $list_address);
  361. }
  362. }
  363. } elsif ($ptype eq "M") {
  364. my $p_used = 0;
  365. if ($index >= 0) {
  366. my $tv = $typevalue[$index - 1];
  367. if ($tv =~ m/^(\C):\s*(.*)/) {
  368. if ($1 eq "P") {
  369. if ($email_usename) {
  370. push_email_address(format_email($2, $pvalue));
  371. $p_used = 1;
  372. }
  373. }
  374. }
  375. }
  376. if (!$p_used) {
  377. push_email_addresses($pvalue);
  378. }
  379. } elsif ($ptype eq "T") {
  380. push(@scm, $pvalue);
  381. } elsif ($ptype eq "W") {
  382. push(@web, $pvalue);
  383. } elsif ($ptype eq "S") {
  384. push(@status, $pvalue);
  385. }
  386. $index--;
  387. } else {
  388. push(@subsystem,$tv);
  389. $index = -1;
  390. }
  391. }
  392. }
  393. sub push_email_address {
  394. my ($email_address) = @_;
  395. my $email_name = "";
  396. if ($email_address =~ m/([^<]+)<(.*\@.*)>$/) {
  397. $email_name = $1;
  398. $email_address = $2;
  399. }
  400. if ($email_maintainer) {
  401. if ($email_usename && $email_name) {
  402. push(@email_to, format_email($email_name, $email_address));
  403. } else {
  404. push(@email_to, $email_address);
  405. }
  406. }
  407. }
  408. sub push_email_addresses {
  409. my ($address) = @_;
  410. my @address_list = ();
  411. if (rfc822_valid($address)) {
  412. push_email_address($address);
  413. } elsif (@address_list = rfc822_validlist($address)) {
  414. my $array_count = shift(@address_list);
  415. while (my $entry = shift(@address_list)) {
  416. push_email_address($entry);
  417. }
  418. } else {
  419. warn("Invalid MAINTAINERS address: '" . $address . "'\n");
  420. }
  421. }
  422. sub which {
  423. my ($bin) = @_;
  424. foreach my $path (split(/:/, $ENV{PATH})) {
  425. if (-e "$path/$bin") {
  426. return "$path/$bin";
  427. }
  428. }
  429. return "";
  430. }
  431. sub recent_git_signoffs {
  432. my ($file) = @_;
  433. my $sign_offs = "";
  434. my $cmd = "";
  435. my $output = "";
  436. my $count = 0;
  437. my @lines = ();
  438. my $total_sign_offs;
  439. if (which("git") eq "") {
  440. warn("$P: git not found. Add --nogit to options?\n");
  441. return;
  442. }
  443. if (!(-d ".git")) {
  444. warn("$P: .git directory not found. Use a git repository for better results.\n");
  445. warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
  446. return;
  447. }
  448. $cmd = "git log --since=${email_git_since} -- ${file}";
  449. $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
  450. if (!$email_git_penguin_chiefs) {
  451. $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
  452. }
  453. $cmd .= " | cut -f2- -d\":\"";
  454. $cmd .= " | sort | uniq -c | sort -rn";
  455. $output = `${cmd}`;
  456. $output =~ s/^\s*//gm;
  457. @lines = split("\n", $output);
  458. $total_sign_offs = 0;
  459. foreach my $line (@lines) {
  460. if ($line =~ m/([0-9]+)\s+(.*)/) {
  461. $total_sign_offs += $1;
  462. } else {
  463. die("$P: Unexpected git output: ${line}\n");
  464. }
  465. }
  466. foreach my $line (@lines) {
  467. if ($line =~ m/([0-9]+)\s+(.*)/) {
  468. my $sign_offs = $1;
  469. $line = $2;
  470. $count++;
  471. if ($sign_offs < $email_git_min_signatures ||
  472. $count > $email_git_max_maintainers ||
  473. $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) {
  474. last;
  475. }
  476. }
  477. if ($line =~ m/(.+)<(.+)>/) {
  478. my $git_name = $1;
  479. my $git_addr = $2;
  480. if ($email_usename) {
  481. push(@email_to, format_email($git_name, $git_addr));
  482. } else {
  483. push(@email_to, $git_addr);
  484. }
  485. } elsif ($line =~ m/<(.+)>/) {
  486. my $git_addr = $1;
  487. push(@email_to, $git_addr);
  488. } else {
  489. push(@email_to, $line);
  490. }
  491. }
  492. }
  493. sub uniq {
  494. my @parms = @_;
  495. my %saw;
  496. @parms = grep(!$saw{$_}++, @parms);
  497. return @parms;
  498. }
  499. sub sort_and_uniq {
  500. my @parms = @_;
  501. my %saw;
  502. @parms = sort @parms;
  503. @parms = grep(!$saw{$_}++, @parms);
  504. return @parms;
  505. }
  506. sub output {
  507. my @parms = @_;
  508. if ($output_multiline) {
  509. foreach my $line (@parms) {
  510. print("${line}\n");
  511. }
  512. } else {
  513. print(join($output_separator, @parms));
  514. print("\n");
  515. }
  516. }
  517. my $rfc822re;
  518. sub make_rfc822re {
  519. # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
  520. # comment. We must allow for rfc822_lwsp (or comments) after each of these.
  521. # This regexp will only work on addresses which have had comments stripped
  522. # and replaced with rfc822_lwsp.
  523. my $specials = '()<>@,;:\\\\".\\[\\]';
  524. my $controls = '\\000-\\037\\177';
  525. my $dtext = "[^\\[\\]\\r\\\\]";
  526. my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
  527. my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
  528. # Use zero-width assertion to spot the limit of an atom. A simple
  529. # $rfc822_lwsp* causes the regexp engine to hang occasionally.
  530. my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
  531. my $word = "(?:$atom|$quoted_string)";
  532. my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
  533. my $sub_domain = "(?:$atom|$domain_literal)";
  534. my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
  535. my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
  536. my $phrase = "$word*";
  537. my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
  538. my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
  539. my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
  540. my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
  541. my $address = "(?:$mailbox|$group)";
  542. return "$rfc822_lwsp*$address";
  543. }
  544. sub rfc822_strip_comments {
  545. my $s = shift;
  546. # Recursively remove comments, and replace with a single space. The simpler
  547. # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
  548. # chars in atoms, for example.
  549. while ($s =~ s/^((?:[^"\\]|\\.)*
  550. (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
  551. \((?:[^()\\]|\\.)*\)/$1 /osx) {}
  552. return $s;
  553. }
  554. # valid: returns true if the parameter is an RFC822 valid address
  555. #
  556. sub rfc822_valid ($) {
  557. my $s = rfc822_strip_comments(shift);
  558. if (!$rfc822re) {
  559. $rfc822re = make_rfc822re();
  560. }
  561. return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
  562. }
  563. # validlist: In scalar context, returns true if the parameter is an RFC822
  564. # valid list of addresses.
  565. #
  566. # In list context, returns an empty list on failure (an invalid
  567. # address was found); otherwise a list whose first element is the
  568. # number of addresses found and whose remaining elements are the
  569. # addresses. This is needed to disambiguate failure (invalid)
  570. # from success with no addresses found, because an empty string is
  571. # a valid list.
  572. sub rfc822_validlist ($) {
  573. my $s = rfc822_strip_comments(shift);
  574. if (!$rfc822re) {
  575. $rfc822re = make_rfc822re();
  576. }
  577. # * null list items are valid according to the RFC
  578. # * the '1' business is to aid in distinguishing failure from no results
  579. my @r;
  580. if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
  581. $s =~ m/^$rfc822_char*$/) {
  582. while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
  583. push @r, $1;
  584. }
  585. return wantarray ? (scalar(@r), @r) : 1;
  586. }
  587. else {
  588. return wantarray ? () : 0;
  589. }
  590. }