get_maintainer.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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.14';
  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 $onefile = 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. if (!GetOptions(
  51. 'email!' => \$email,
  52. 'git!' => \$email_git,
  53. 'git-chief-penguins!' => \$email_git_penguin_chiefs,
  54. 'git-min-signatures=i' => \$email_git_min_signatures,
  55. 'git-max-maintainers=i' => \$email_git_max_maintainers,
  56. 'git-since=s' => \$email_git_since,
  57. 'm!' => \$email_maintainer,
  58. 'n!' => \$email_usename,
  59. 'l!' => \$email_list,
  60. 's!' => \$email_subscriber_list,
  61. 'multiline!' => \$output_multiline,
  62. 'separator=s' => \$output_separator,
  63. 'subsystem!' => \$subsystem,
  64. 'status!' => \$status,
  65. 'scm!' => \$scm,
  66. 'web!' => \$web,
  67. 'f|file' => \$onefile,
  68. 'v|version' => \$version,
  69. 'h|help' => \$help,
  70. )) {
  71. usage();
  72. die "$P: invalid argument\n";
  73. }
  74. if ($help != 0) {
  75. usage();
  76. exit 0;
  77. }
  78. if ($version != 0) {
  79. print("${P} ${V}\n");
  80. exit 0;
  81. }
  82. my $infile = $ARGV[0];
  83. if ($#ARGV < 0) {
  84. usage();
  85. die "$P: argument missing: patchfile or -f file please\n";
  86. }
  87. my $selections = $email + $scm + $status + $subsystem + $web;
  88. if ($selections == 0) {
  89. usage();
  90. die "$P: Missing required option: email, scm, status, subsystem or web\n";
  91. }
  92. if ($email && ($email_maintainer + $email_list + $email_subscriber_list
  93. + $email_git + $email_git_penguin_chiefs) == 0) {
  94. usage();
  95. die "$P: Please select at least 1 email option\n";
  96. }
  97. if (!top_of_kernel_tree($lk_path)) {
  98. die "$P: The current directory does not appear to be "
  99. . "a linux kernel source tree.\n";
  100. }
  101. ## Read MAINTAINERS for type/value pairs
  102. my @typevalue = ();
  103. open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
  104. while (<MAINT>) {
  105. my $line = $_;
  106. if ($line =~ m/^(\C):\s*(.*)/) {
  107. my $type = $1;
  108. my $value = $2;
  109. ##Filename pattern matching
  110. if ($type eq "F" || $type eq "X") {
  111. $value =~ s@\.@\\\.@g; ##Convert . to \.
  112. $value =~ s/\*/\.\*/g; ##Convert * to .*
  113. $value =~ s/\?/\./g; ##Convert ? to .
  114. }
  115. push(@typevalue, "$type:$value");
  116. } elsif (!/^(\s)*$/) {
  117. $line =~ s/\n$//g;
  118. push(@typevalue, $line);
  119. }
  120. }
  121. close(MAINT);
  122. ## use the filename on the command line or find the filenames in the patchfile
  123. my @files = ();
  124. if ($onefile) {
  125. if (!(-f $infile)) {
  126. die "$P: file '${infile}' not found\n";
  127. }
  128. push(@files, $infile);
  129. } else {
  130. open(PATCH, "<$infile") or die "$P: Can't open ${infile}\n";
  131. while (<PATCH>) {
  132. if (m/^\+\+\+\s+(\S+)/) {
  133. my $file = $1;
  134. $file =~ s@^[^/]*/@@;
  135. $file =~ s@\n@@;
  136. push(@files, $file);
  137. }
  138. }
  139. close(PATCH);
  140. my $file_cnt = @files;
  141. if ($file_cnt == 0) {
  142. print STDERR "$P: file '${infile}' doesn't appear to be a patch. "
  143. . "Add -f to options?\n";
  144. }
  145. @files = sort_and_uniq(@files);
  146. }
  147. my @email_to = ();
  148. my @scm = ();
  149. my @web = ();
  150. my @subsystem = ();
  151. my @status = ();
  152. # Find responsible parties
  153. foreach my $file (@files) {
  154. #Do not match excluded file patterns
  155. my $exclude = 0;
  156. foreach my $line (@typevalue) {
  157. if ($line =~ m/^(\C):(.*)/) {
  158. my $type = $1;
  159. my $value = $2;
  160. if ($type eq 'X') {
  161. if (file_match_pattern($file, $value)) {
  162. $exclude = 1;
  163. }
  164. }
  165. }
  166. }
  167. if (!$exclude) {
  168. my $tvi = 0;
  169. foreach my $line (@typevalue) {
  170. if ($line =~ m/^(\C):(.*)/) {
  171. my $type = $1;
  172. my $value = $2;
  173. if ($type eq 'F') {
  174. if (file_match_pattern($file, $value)) {
  175. add_categories($tvi);
  176. }
  177. }
  178. }
  179. $tvi++;
  180. }
  181. }
  182. if ($email_git) {
  183. recent_git_signoffs($file);
  184. }
  185. }
  186. if ($email_git_penguin_chiefs) {
  187. foreach my $chief (@penguin_chief) {
  188. if ($chief =~ m/^(.*):(.*)/) {
  189. my $chief_name = $1;
  190. my $chief_addr = $2;
  191. if ($email_usename) {
  192. push(@email_to, format_email($chief_name, $chief_addr));
  193. } else {
  194. push(@email_to, $chief_addr);
  195. }
  196. }
  197. }
  198. }
  199. if ($email) {
  200. my $address_cnt = @email_to;
  201. if ($address_cnt == 0 && $email_list) {
  202. push(@email_to, "linux-kernel\@vger.kernel.org");
  203. }
  204. #Don't sort email address list, but do remove duplicates
  205. @email_to = uniq(@email_to);
  206. output(@email_to);
  207. }
  208. if ($scm) {
  209. if (!$onefile) {
  210. @scm = sort_and_uniq(@scm);
  211. }
  212. output(@scm);
  213. }
  214. if ($status) {
  215. if (!$onefile) {
  216. @status = sort_and_uniq(@status);
  217. }
  218. output(@status);
  219. }
  220. if ($subsystem) {
  221. if (!$onefile) {
  222. @subsystem = sort_and_uniq(@subsystem);
  223. }
  224. output(@subsystem);
  225. }
  226. if ($web) {
  227. if (!$onefile) {
  228. @web = sort_and_uniq(@web);
  229. }
  230. output(@web);
  231. }
  232. exit($exit);
  233. sub file_match_pattern {
  234. my ($file, $pattern) = @_;
  235. if (substr($pattern, -1) eq "/") {
  236. if ($file =~ m@^$pattern@) {
  237. return 1;
  238. }
  239. } else {
  240. if ($file =~ m@^$pattern@) {
  241. my $s1 = ($file =~ tr@/@@);
  242. my $s2 = ($pattern =~ tr@/@@);
  243. if ($s1 == $s2) {
  244. return 1;
  245. }
  246. }
  247. }
  248. return 0;
  249. }
  250. sub usage {
  251. print <<EOT;
  252. usage: $P [options] patchfile
  253. $P [options] -f file
  254. version: $V
  255. MAINTAINER field selection options:
  256. --email => print email address(es) if any
  257. --git => include recent git \*-by: signers
  258. --git-chief-penguins => include ${penguin_chiefs}
  259. --git-min-signatures => number of signatures required (default: 1)
  260. --git-max-maintainers => maximum maintainers to add (default: 5)
  261. --git-since => git history to use (default: 1-year-ago)
  262. --m => include maintainer(s) if any
  263. --n => include name 'Full Name <addr\@domain.tld>'
  264. --l => include list(s) if any
  265. --s => include subscriber only list(s) if any
  266. --scm => print SCM tree(s) if any
  267. --status => print status if any
  268. --subsystem => print subsystem name if any
  269. --web => print website(s) if any
  270. Output type options:
  271. --separator [, ] => separator for multiple entries on 1 line
  272. --multiline => print 1 entry per line
  273. Default options:
  274. [--email --git --m --l --multiline]
  275. Other options:
  276. --version -> show version
  277. --help => show this help information
  278. EOT
  279. }
  280. sub top_of_kernel_tree {
  281. my ($lk_path) = @_;
  282. if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
  283. $lk_path .= "/";
  284. }
  285. if ( (-f "${lk_path}COPYING")
  286. && (-f "${lk_path}CREDITS")
  287. && (-f "${lk_path}Kbuild")
  288. && (-f "${lk_path}MAINTAINERS")
  289. && (-f "${lk_path}Makefile")
  290. && (-f "${lk_path}README")
  291. && (-d "${lk_path}Documentation")
  292. && (-d "${lk_path}arch")
  293. && (-d "${lk_path}include")
  294. && (-d "${lk_path}drivers")
  295. && (-d "${lk_path}fs")
  296. && (-d "${lk_path}init")
  297. && (-d "${lk_path}ipc")
  298. && (-d "${lk_path}kernel")
  299. && (-d "${lk_path}lib")
  300. && (-d "${lk_path}scripts")) {
  301. return 1;
  302. }
  303. return 0;
  304. }
  305. sub format_email {
  306. my ($name, $email) = @_;
  307. $name =~ s/^\s+|\s+$//g;
  308. $email =~ s/^\s+|\s+$//g;
  309. my $formatted_email = "";
  310. if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
  311. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  312. $formatted_email = "\"${name}\"\ \<${email}\>";
  313. } else {
  314. $formatted_email = "${name} \<${email}\>";
  315. }
  316. return $formatted_email;
  317. }
  318. sub add_categories {
  319. my ($index) = @_;
  320. $index = $index - 1;
  321. while ($index >= 0) {
  322. my $tv = $typevalue[$index];
  323. if ($tv =~ m/^(\C):(.*)/) {
  324. my $ptype = $1;
  325. my $pvalue = $2;
  326. if ($ptype eq "L") {
  327. my $subscr = $pvalue;
  328. if ($subscr =~ m/\s*\(subscribers-only\)/) {
  329. if ($email_subscriber_list) {
  330. $subscr =~ s/\s*\(subscribers-only\)//g;
  331. push(@email_to, $subscr);
  332. }
  333. } else {
  334. if ($email_list) {
  335. push(@email_to, $pvalue);
  336. }
  337. }
  338. } elsif ($ptype eq "M") {
  339. if ($email_maintainer) {
  340. if ($index >= 0) {
  341. my $tv = $typevalue[$index - 1];
  342. if ($tv =~ m/^(\C):(.*)/) {
  343. if ($1 eq "P" && $email_usename) {
  344. push(@email_to, format_email($2, $pvalue));
  345. } else {
  346. push(@email_to, $pvalue);
  347. }
  348. }
  349. } else {
  350. push(@email_to, $pvalue);
  351. }
  352. }
  353. } elsif ($ptype eq "T") {
  354. push(@scm, $pvalue);
  355. } elsif ($ptype eq "W") {
  356. push(@web, $pvalue);
  357. } elsif ($ptype eq "S") {
  358. push(@status, $pvalue);
  359. }
  360. $index--;
  361. } else {
  362. push(@subsystem,$tv);
  363. $index = -1;
  364. }
  365. }
  366. }
  367. sub which {
  368. my ($bin) = @_;
  369. foreach my $path (split /:/, $ENV{PATH}) {
  370. if (-e "$path/$bin") {
  371. return "$path/$bin";
  372. }
  373. }
  374. return "";
  375. }
  376. sub recent_git_signoffs {
  377. my ($file) = @_;
  378. my $sign_offs = "";
  379. my $cmd = "";
  380. my $output = "";
  381. my $count = 0;
  382. my @lines = ();
  383. if (which("git") eq "") {
  384. die("$P: git not found. Add --nogit to options?\n");
  385. }
  386. $cmd = "git log --since=${email_git_since} -- ${file}";
  387. $cmd .= " | grep -P '^ [-A-Za-z]+by:.*\\\@'";
  388. if (!$email_git_penguin_chiefs) {
  389. $cmd .= " | grep -E -v \"${penguin_chiefs}\"";
  390. }
  391. $cmd .= " | sort | uniq -c | sort -rn";
  392. $output = `${cmd}`;
  393. $output =~ s/^\s*//gm;
  394. @lines = split("\n", $output);
  395. foreach my $line (@lines) {
  396. if ($line =~ m/([0-9]+)\s+([-A-Za-z]+by:)\s+(.*)/) {
  397. my $sign_offs = $1;
  398. $line = $3;
  399. $count++;
  400. if ($sign_offs < $email_git_min_signatures ||
  401. $count > $email_git_max_maintainers) {
  402. last;
  403. }
  404. } else {
  405. die("$P: Unexpected git output: ${line}\n");
  406. }
  407. if ($line =~ m/(.*) <(.*)>/) {
  408. my $git_name = $1;
  409. my $git_addr = $2;
  410. $git_name =~ tr/^\"//;
  411. $git_name =~ tr/\"$//;
  412. if ($email_usename) {
  413. push(@email_to, format_email($git_name, $git_addr));
  414. } else {
  415. push(@email_to, $git_addr);
  416. }
  417. } elsif ($line =~ m/<(.*)>/) {
  418. my $git_addr = $1;
  419. push(@email_to, $git_addr);
  420. } else {
  421. push(@email_to, $line);
  422. }
  423. }
  424. return $output;
  425. }
  426. sub uniq {
  427. my @parms = @_;
  428. my %saw;
  429. @parms = grep(!$saw{$_}++, @parms);
  430. return @parms;
  431. }
  432. sub sort_and_uniq {
  433. my @parms = @_;
  434. my %saw;
  435. @parms = sort @parms;
  436. @parms = grep(!$saw{$_}++, @parms);
  437. return @parms;
  438. }
  439. sub output {
  440. my @parms = @_;
  441. if ($output_multiline) {
  442. foreach my $line (@parms) {
  443. print("${line}\n");
  444. }
  445. } else {
  446. print(join($output_separator, @parms));
  447. print("\n");
  448. }
  449. }