get_maintainer.pl 16 KB

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