get_maintainer.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 - preloaded methods go here.
  51. my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
  52. my $rfc822_char = '[\\000-\\177]';
  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. die "$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. $email =~ s/^\s+|\s+$//g;
  312. my $formatted_email = "";
  313. if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
  314. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  315. $formatted_email = "\"${name}\"\ \<${email}\>";
  316. } else {
  317. $formatted_email = "${name} \<${email}\>";
  318. }
  319. return $formatted_email;
  320. }
  321. sub add_categories {
  322. my ($index) = @_;
  323. $index = $index - 1;
  324. while ($index >= 0) {
  325. my $tv = $typevalue[$index];
  326. if ($tv =~ m/^(\C):\s*(.*)/) {
  327. my $ptype = $1;
  328. my $pvalue = $2;
  329. if ($ptype eq "L") {
  330. my $list_address = $pvalue;
  331. my $list_additional = "";
  332. if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
  333. $list_address = $1;
  334. $list_additional = $2;
  335. }
  336. if ($list_additional =~ m/subscribers-only/) {
  337. if ($email_subscriber_list) {
  338. push(@list_to, $list_address);
  339. }
  340. } else {
  341. if ($email_list) {
  342. push(@list_to, $list_address);
  343. }
  344. }
  345. } elsif ($ptype eq "M") {
  346. if ($email_maintainer) {
  347. push_email_addresses($pvalue);
  348. }
  349. } elsif ($ptype eq "T") {
  350. push(@scm, $pvalue);
  351. } elsif ($ptype eq "W") {
  352. push(@web, $pvalue);
  353. } elsif ($ptype eq "S") {
  354. push(@status, $pvalue);
  355. }
  356. $index--;
  357. } else {
  358. push(@subsystem,$tv);
  359. $index = -1;
  360. }
  361. }
  362. }
  363. sub push_email_address {
  364. my ($email_address) = @_;
  365. my $email_name = "";
  366. if ($email_address =~ m/([^<]+)<(.*\@.*)>$/) {
  367. $email_name = $1;
  368. $email_address = $2;
  369. }
  370. if ($email_usename && $email_name) {
  371. push(@email_to, format_email($email_name, $email_address));
  372. } else {
  373. push(@email_to, $email_address);
  374. }
  375. }
  376. sub push_email_addresses {
  377. my ($address) = @_;
  378. my @address_list = ();
  379. if (@address_list = rfc822_validlist($address)) {
  380. my $array_count = shift(@address_list);
  381. while (my $entry = shift(@address_list)) {
  382. push_email_address($entry);
  383. }
  384. }
  385. }
  386. sub which {
  387. my ($bin) = @_;
  388. foreach my $path (split(/:/, $ENV{PATH})) {
  389. if (-e "$path/$bin") {
  390. return "$path/$bin";
  391. }
  392. }
  393. return "";
  394. }
  395. sub recent_git_signoffs {
  396. my ($file) = @_;
  397. my $sign_offs = "";
  398. my $cmd = "";
  399. my $output = "";
  400. my $count = 0;
  401. my @lines = ();
  402. if (which("git") eq "") {
  403. warn("$P: git not found. Add --nogit to options?\n");
  404. return;
  405. }
  406. if (!(-d ".git")) {
  407. warn("$P: .git repository not found.\n");
  408. warn("Use a .git repository for better results.\n");
  409. warn("ie: git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
  410. return;
  411. }
  412. $cmd = "git log --since=${email_git_since} -- ${file}";
  413. $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
  414. if (!$email_git_penguin_chiefs) {
  415. $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
  416. }
  417. $cmd .= " | cut -f2- -d\":\"";
  418. $cmd .= " | sort | uniq -c | sort -rn";
  419. $output = `${cmd}`;
  420. $output =~ s/^\s*//gm;
  421. @lines = split("\n", $output);
  422. foreach my $line (@lines) {
  423. if ($line =~ m/([0-9]+)\s+(.*)/) {
  424. my $sign_offs = $1;
  425. $line = $2;
  426. $count++;
  427. if ($sign_offs < $email_git_min_signatures ||
  428. $count > $email_git_max_maintainers) {
  429. last;
  430. }
  431. } else {
  432. die("$P: Unexpected git output: ${line}\n");
  433. }
  434. if ($line =~ m/(.+)<(.+)>/) {
  435. my $git_name = $1;
  436. my $git_addr = $2;
  437. if ($email_usename) {
  438. push(@email_to, format_email($git_name, $git_addr));
  439. } else {
  440. push(@email_to, $git_addr);
  441. }
  442. } elsif ($line =~ m/<(.+)>/) {
  443. my $git_addr = $1;
  444. push(@email_to, $git_addr);
  445. } else {
  446. push(@email_to, $line);
  447. }
  448. }
  449. }
  450. sub uniq {
  451. my @parms = @_;
  452. my %saw;
  453. @parms = grep(!$saw{$_}++, @parms);
  454. return @parms;
  455. }
  456. sub sort_and_uniq {
  457. my @parms = @_;
  458. my %saw;
  459. @parms = sort @parms;
  460. @parms = grep(!$saw{$_}++, @parms);
  461. return @parms;
  462. }
  463. sub output {
  464. my @parms = @_;
  465. if ($output_multiline) {
  466. foreach my $line (@parms) {
  467. print("${line}\n");
  468. }
  469. } else {
  470. print(join($output_separator, @parms));
  471. print("\n");
  472. }
  473. }
  474. my $rfc822re;
  475. sub make_rfc822re {
  476. # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
  477. # comment. We must allow for rfc822_lwsp (or comments) after each of these.
  478. # This regexp will only work on addresses which have had comments stripped
  479. # and replaced with rfc822_lwsp.
  480. my $specials = '()<>@,;:\\\\".\\[\\]';
  481. my $controls = '\\000-\\037\\177';
  482. my $dtext = "[^\\[\\]\\r\\\\]";
  483. my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
  484. my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
  485. # Use zero-width assertion to spot the limit of an atom. A simple
  486. # $rfc822_lwsp* causes the regexp engine to hang occasionally.
  487. my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
  488. my $word = "(?:$atom|$quoted_string)";
  489. my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
  490. my $sub_domain = "(?:$atom|$domain_literal)";
  491. my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
  492. my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
  493. my $phrase = "$word*";
  494. my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
  495. my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
  496. my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
  497. my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
  498. my $address = "(?:$mailbox|$group)";
  499. return "$rfc822_lwsp*$address";
  500. }
  501. sub rfc822_strip_comments {
  502. my $s = shift;
  503. # Recursively remove comments, and replace with a single space. The simpler
  504. # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
  505. # chars in atoms, for example.
  506. while ($s =~ s/^((?:[^"\\]|\\.)*
  507. (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
  508. \((?:[^()\\]|\\.)*\)/$1 /osx) {}
  509. return $s;
  510. }
  511. # valid: returns true if the parameter is an RFC822 valid address
  512. #
  513. sub rfc822_valid ($) {
  514. my $s = rfc822_strip_comments(shift);
  515. if (!$rfc822re) {
  516. $rfc822re = make_rfc822re();
  517. }
  518. return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
  519. }
  520. # validlist: In scalar context, returns true if the parameter is an RFC822
  521. # valid list of addresses.
  522. #
  523. # In list context, returns an empty list on failure (an invalid
  524. # address was found); otherwise a list whose first element is the
  525. # number of addresses found and whose remaining elements are the
  526. # addresses. This is needed to disambiguate failure (invalid)
  527. # from success with no addresses found, because an empty string is
  528. # a valid list.
  529. sub rfc822_validlist ($) {
  530. my $s = rfc822_strip_comments(shift);
  531. if (!$rfc822re) {
  532. $rfc822re = make_rfc822re();
  533. }
  534. # * null list items are valid according to the RFC
  535. # * the '1' business is to aid in distinguishing failure from no results
  536. my @r;
  537. if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
  538. $s =~ m/^$rfc822_char*$/) {
  539. while($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
  540. push @r, $1;
  541. }
  542. return wantarray ? (scalar(@r), @r) : 1;
  543. }
  544. else {
  545. return wantarray ? () : 0;
  546. }
  547. }