get_maintainer.pl 15 KB

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