get_maintainer.pl 11 KB

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