get_maintainer.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. 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 @list_to = ();
  151. my @scm = ();
  152. my @web = ();
  153. my @subsystem = ();
  154. my @status = ();
  155. # Find responsible parties
  156. foreach my $file (@files) {
  157. #Do not match excluded file patterns
  158. my $exclude = 0;
  159. foreach my $line (@typevalue) {
  160. if ($line =~ m/^(\C):\s*(.*)/) {
  161. my $type = $1;
  162. my $value = $2;
  163. if ($type eq 'X') {
  164. if (file_match_pattern($file, $value)) {
  165. $exclude = 1;
  166. }
  167. }
  168. }
  169. }
  170. if (!$exclude) {
  171. my $tvi = 0;
  172. foreach my $line (@typevalue) {
  173. if ($line =~ m/^(\C):\s*(.*)/) {
  174. my $type = $1;
  175. my $value = $2;
  176. if ($type eq 'F') {
  177. if (file_match_pattern($file, $value)) {
  178. add_categories($tvi);
  179. }
  180. }
  181. }
  182. $tvi++;
  183. }
  184. }
  185. if ($email && $email_git) {
  186. recent_git_signoffs($file);
  187. }
  188. }
  189. if ($email) {
  190. foreach my $chief (@penguin_chief) {
  191. if ($chief =~ m/^(.*):(.*)/) {
  192. my $email_address;
  193. if ($email_usename) {
  194. $email_address = format_email($1, $2);
  195. } else {
  196. $email_address = $2;
  197. }
  198. if ($email_git_penguin_chiefs) {
  199. push(@email_to, $email_address);
  200. } else {
  201. @email_to = grep(!/${email_address}/, @email_to);
  202. }
  203. }
  204. }
  205. }
  206. if ($email || $email_list) {
  207. my @to = ();
  208. if ($email) {
  209. @to = (@to, @email_to);
  210. }
  211. if ($email_list) {
  212. @to = (@to, @list_to);
  213. }
  214. output(uniq(@to));
  215. }
  216. if ($scm) {
  217. @scm = sort_and_uniq(@scm);
  218. output(@scm);
  219. }
  220. if ($status) {
  221. @status = sort_and_uniq(@status);
  222. output(@status);
  223. }
  224. if ($subsystem) {
  225. @subsystem = sort_and_uniq(@subsystem);
  226. output(@subsystem);
  227. }
  228. if ($web) {
  229. @web = sort_and_uniq(@web);
  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 --n --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):\s*(.*)/) {
  324. my $ptype = $1;
  325. my $pvalue = $2;
  326. if ($ptype eq "L") {
  327. my $list_address = $pvalue;
  328. my $list_additional = "";
  329. if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
  330. $list_address = $1;
  331. $list_additional = $2;
  332. }
  333. if ($list_additional =~ m/subscribers-only/) {
  334. if ($email_subscriber_list) {
  335. push(@list_to, $list_address);
  336. }
  337. } else {
  338. if ($email_list) {
  339. push(@list_to, $list_address);
  340. }
  341. }
  342. } elsif ($ptype eq "M") {
  343. if ($email_maintainer) {
  344. if ($index >= 0) {
  345. my $tv = $typevalue[$index - 1];
  346. if ($tv =~ m/^(\C):\s*(.*)/) {
  347. if ($1 eq "P" && $email_usename) {
  348. push(@email_to, format_email($2, $pvalue));
  349. } else {
  350. push(@email_to, $pvalue);
  351. }
  352. }
  353. } else {
  354. push(@email_to, $pvalue);
  355. }
  356. }
  357. } elsif ($ptype eq "T") {
  358. push(@scm, $pvalue);
  359. } elsif ($ptype eq "W") {
  360. push(@web, $pvalue);
  361. } elsif ($ptype eq "S") {
  362. push(@status, $pvalue);
  363. }
  364. $index--;
  365. } else {
  366. push(@subsystem,$tv);
  367. $index = -1;
  368. }
  369. }
  370. }
  371. sub which {
  372. my ($bin) = @_;
  373. foreach my $path (split(/:/, $ENV{PATH})) {
  374. if (-e "$path/$bin") {
  375. return "$path/$bin";
  376. }
  377. }
  378. return "";
  379. }
  380. sub recent_git_signoffs {
  381. my ($file) = @_;
  382. my $sign_offs = "";
  383. my $cmd = "";
  384. my $output = "";
  385. my $count = 0;
  386. my @lines = ();
  387. if (which("git") eq "") {
  388. warn("$P: git not found. Add --nogit to options?\n");
  389. return;
  390. }
  391. if (!(-d ".git")) {
  392. warn("$P: .git repository not found.\n");
  393. warn("Use a .git repository for better results.\n");
  394. warn("ie: git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
  395. return;
  396. }
  397. $cmd = "git log --since=${email_git_since} -- ${file}";
  398. $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
  399. if (!$email_git_penguin_chiefs) {
  400. $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
  401. }
  402. $cmd .= " | cut -f2- -d\":\"";
  403. $cmd .= " | sort | uniq -c | sort -rn";
  404. $output = `${cmd}`;
  405. $output =~ s/^\s*//gm;
  406. @lines = split("\n", $output);
  407. foreach my $line (@lines) {
  408. if ($line =~ m/([0-9]+)\s+(.*)/) {
  409. my $sign_offs = $1;
  410. $line = $2;
  411. $count++;
  412. if ($sign_offs < $email_git_min_signatures ||
  413. $count > $email_git_max_maintainers) {
  414. last;
  415. }
  416. } else {
  417. die("$P: Unexpected git output: ${line}\n");
  418. }
  419. if ($line =~ m/(.+)<(.+)>/) {
  420. my $git_name = $1;
  421. my $git_addr = $2;
  422. $git_name =~ tr/^\"//;
  423. $git_name =~ tr/^\\s*//;
  424. $git_name =~ tr/\"$//;
  425. $git_name =~ tr/\\s*$//;
  426. if ($email_usename) {
  427. push(@email_to, format_email($git_name, $git_addr));
  428. } else {
  429. push(@email_to, $git_addr);
  430. }
  431. } elsif ($line =~ m/<(.+)>/) {
  432. my $git_addr = $1;
  433. push(@email_to, $git_addr);
  434. } else {
  435. push(@email_to, $line);
  436. }
  437. }
  438. }
  439. sub uniq {
  440. my @parms = @_;
  441. my %saw;
  442. @parms = grep(!$saw{$_}++, @parms);
  443. return @parms;
  444. }
  445. sub sort_and_uniq {
  446. my @parms = @_;
  447. my %saw;
  448. @parms = sort @parms;
  449. @parms = grep(!$saw{$_}++, @parms);
  450. return @parms;
  451. }
  452. sub output {
  453. my @parms = @_;
  454. if ($output_multiline) {
  455. foreach my $line (@parms) {
  456. print("${line}\n");
  457. }
  458. } else {
  459. print(join($output_separator, @parms));
  460. print("\n");
  461. }
  462. }