get_maintainer.pl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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_maintainer.pl [OPTIONS] <patch>
  9. # perl scripts/get_maintainer.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.21';
  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_min_percent = 5;
  27. my $email_git_since = "1-year-ago";
  28. my $email_git_blame = 0;
  29. my $email_remove_duplicates = 1;
  30. my $output_multiline = 1;
  31. my $output_separator = ", ";
  32. my $scm = 0;
  33. my $web = 0;
  34. my $subsystem = 0;
  35. my $status = 0;
  36. my $keywords = 1;
  37. my $from_filename = 0;
  38. my $pattern_depth = 0;
  39. my $version = 0;
  40. my $help = 0;
  41. my $exit = 0;
  42. my @penguin_chief = ();
  43. push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
  44. #Andrew wants in on most everything - 2009/01/14
  45. #push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
  46. my @penguin_chief_names = ();
  47. foreach my $chief (@penguin_chief) {
  48. if ($chief =~ m/^(.*):(.*)/) {
  49. my $chief_name = $1;
  50. my $chief_addr = $2;
  51. push(@penguin_chief_names, $chief_name);
  52. }
  53. }
  54. my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
  55. # rfc822 email address - preloaded methods go here.
  56. my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
  57. my $rfc822_char = '[\\000-\\377]';
  58. if (!GetOptions(
  59. 'email!' => \$email,
  60. 'git!' => \$email_git,
  61. 'git-chief-penguins!' => \$email_git_penguin_chiefs,
  62. 'git-min-signatures=i' => \$email_git_min_signatures,
  63. 'git-max-maintainers=i' => \$email_git_max_maintainers,
  64. 'git-min-percent=i' => \$email_git_min_percent,
  65. 'git-since=s' => \$email_git_since,
  66. 'git-blame!' => \$email_git_blame,
  67. 'remove-duplicates!' => \$email_remove_duplicates,
  68. 'm!' => \$email_maintainer,
  69. 'n!' => \$email_usename,
  70. 'l!' => \$email_list,
  71. 's!' => \$email_subscriber_list,
  72. 'multiline!' => \$output_multiline,
  73. 'separator=s' => \$output_separator,
  74. 'subsystem!' => \$subsystem,
  75. 'status!' => \$status,
  76. 'scm!' => \$scm,
  77. 'web!' => \$web,
  78. 'pattern-depth=i' => \$pattern_depth,
  79. 'k|keywords!' => \$keywords,
  80. 'f|file' => \$from_filename,
  81. 'v|version' => \$version,
  82. 'h|help' => \$help,
  83. )) {
  84. usage();
  85. die "$P: invalid argument\n";
  86. }
  87. if ($help != 0) {
  88. usage();
  89. exit 0;
  90. }
  91. if ($version != 0) {
  92. print("${P} ${V}\n");
  93. exit 0;
  94. }
  95. if ($#ARGV < 0) {
  96. usage();
  97. die "$P: argument missing: patchfile or -f file please\n";
  98. }
  99. if ($output_separator ne ", ") {
  100. $output_multiline = 0;
  101. }
  102. my $selections = $email + $scm + $status + $subsystem + $web;
  103. if ($selections == 0) {
  104. usage();
  105. die "$P: Missing required option: email, scm, status, subsystem or web\n";
  106. }
  107. if ($email &&
  108. ($email_maintainer + $email_list + $email_subscriber_list +
  109. $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
  110. usage();
  111. die "$P: Please select at least 1 email option\n";
  112. }
  113. if (!top_of_kernel_tree($lk_path)) {
  114. die "$P: The current directory does not appear to be "
  115. . "a linux kernel source tree.\n";
  116. }
  117. ## Read MAINTAINERS for type/value pairs
  118. my @typevalue = ();
  119. my %keyword_hash;
  120. open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
  121. while (<MAINT>) {
  122. my $line = $_;
  123. if ($line =~ m/^(\C):\s*(.*)/) {
  124. my $type = $1;
  125. my $value = $2;
  126. ##Filename pattern matching
  127. if ($type eq "F" || $type eq "X") {
  128. $value =~ s@\.@\\\.@g; ##Convert . to \.
  129. $value =~ s/\*/\.\*/g; ##Convert * to .*
  130. $value =~ s/\?/\./g; ##Convert ? to .
  131. ##if pattern is a directory and it lacks a trailing slash, add one
  132. if ((-d $value)) {
  133. $value =~ s@([^/])$@$1/@;
  134. }
  135. } elsif ($type eq "K") {
  136. $keyword_hash{@typevalue} = $value;
  137. }
  138. push(@typevalue, "$type:$value");
  139. } elsif (!/^(\s)*$/) {
  140. $line =~ s/\n$//g;
  141. push(@typevalue, $line);
  142. }
  143. }
  144. close(MAINT);
  145. my %mailmap;
  146. if ($email_remove_duplicates) {
  147. open(MAILMAP, "<${lk_path}.mailmap") || warn "$P: Can't open .mailmap\n";
  148. while (<MAILMAP>) {
  149. my $line = $_;
  150. next if ($line =~ m/^\s*#/);
  151. next if ($line =~ m/^\s*$/);
  152. my ($name, $address) = parse_email($line);
  153. $line = format_email($name, $address);
  154. next if ($line =~ m/^\s*$/);
  155. if (exists($mailmap{$name})) {
  156. my $obj = $mailmap{$name};
  157. push(@$obj, $address);
  158. } else {
  159. my @arr = ($address);
  160. $mailmap{$name} = \@arr;
  161. }
  162. }
  163. close(MAILMAP);
  164. }
  165. ## use the filenames on the command line or find the filenames in the patchfiles
  166. my @files = ();
  167. my @range = ();
  168. my @keyword_tvi = ();
  169. foreach my $file (@ARGV) {
  170. ##if $file is a directory and it lacks a trailing slash, add one
  171. if ((-d $file)) {
  172. $file =~ s@([^/])$@$1/@;
  173. } elsif (!(-f $file)) {
  174. die "$P: file '${file}' not found\n";
  175. }
  176. if ($from_filename) {
  177. push(@files, $file);
  178. if (-f $file && $keywords) {
  179. open(FILE, "<$file") or die "$P: Can't open ${file}\n";
  180. while (<FILE>) {
  181. my $patch_line = $_;
  182. foreach my $line (keys %keyword_hash) {
  183. if ($patch_line =~ m/^.*$keyword_hash{$line}/x) {
  184. push(@keyword_tvi, $line);
  185. }
  186. }
  187. }
  188. close(FILE);
  189. }
  190. } else {
  191. my $file_cnt = @files;
  192. my $lastfile;
  193. open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
  194. while (<PATCH>) {
  195. my $patch_line = $_;
  196. if (m/^\+\+\+\s+(\S+)/) {
  197. my $filename = $1;
  198. $filename =~ s@^[^/]*/@@;
  199. $filename =~ s@\n@@;
  200. $lastfile = $filename;
  201. push(@files, $filename);
  202. } elsif (m/^\@\@ -(\d+),(\d+)/) {
  203. if ($email_git_blame) {
  204. push(@range, "$lastfile:$1:$2");
  205. }
  206. } elsif ($keywords) {
  207. foreach my $line (keys %keyword_hash) {
  208. if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
  209. push(@keyword_tvi, $line);
  210. }
  211. }
  212. }
  213. }
  214. close(PATCH);
  215. if ($file_cnt == @files) {
  216. warn "$P: file '${file}' doesn't appear to be a patch. "
  217. . "Add -f to options?\n";
  218. }
  219. @files = sort_and_uniq(@files);
  220. }
  221. }
  222. my @email_to = ();
  223. my @list_to = ();
  224. my @scm = ();
  225. my @web = ();
  226. my @subsystem = ();
  227. my @status = ();
  228. # Find responsible parties
  229. foreach my $file (@files) {
  230. #Do not match excluded file patterns
  231. my $exclude = 0;
  232. foreach my $line (@typevalue) {
  233. if ($line =~ m/^(\C):\s*(.*)/) {
  234. my $type = $1;
  235. my $value = $2;
  236. if ($type eq 'X') {
  237. if (file_match_pattern($file, $value)) {
  238. $exclude = 1;
  239. last;
  240. }
  241. }
  242. }
  243. }
  244. if (!$exclude) {
  245. my $tvi = 0;
  246. my %hash;
  247. foreach my $line (@typevalue) {
  248. if ($line =~ m/^(\C):\s*(.*)/) {
  249. my $type = $1;
  250. my $value = $2;
  251. if ($type eq 'F') {
  252. if (file_match_pattern($file, $value)) {
  253. my $value_pd = ($value =~ tr@/@@);
  254. my $file_pd = ($file =~ tr@/@@);
  255. $value_pd++ if (substr($value,-1,1) ne "/");
  256. if ($pattern_depth == 0 ||
  257. (($file_pd - $value_pd) < $pattern_depth)) {
  258. $hash{$tvi} = $value_pd;
  259. }
  260. }
  261. }
  262. }
  263. $tvi++;
  264. }
  265. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  266. add_categories($line);
  267. }
  268. }
  269. if ($email && $email_git) {
  270. recent_git_signoffs($file);
  271. }
  272. if ($email && $email_git_blame) {
  273. git_assign_blame($file);
  274. }
  275. }
  276. if ($keywords) {
  277. @keyword_tvi = sort_and_uniq(@keyword_tvi);
  278. foreach my $line (@keyword_tvi) {
  279. add_categories($line);
  280. }
  281. }
  282. if ($email) {
  283. foreach my $chief (@penguin_chief) {
  284. if ($chief =~ m/^(.*):(.*)/) {
  285. my $email_address;
  286. $email_address = format_email($1, $2);
  287. if ($email_git_penguin_chiefs) {
  288. push(@email_to, $email_address);
  289. } else {
  290. @email_to = grep(!/${email_address}/, @email_to);
  291. }
  292. }
  293. }
  294. }
  295. if ($email || $email_list) {
  296. my @to = ();
  297. if ($email) {
  298. @to = (@to, @email_to);
  299. }
  300. if ($email_list) {
  301. @to = (@to, @list_to);
  302. }
  303. output(uniq(@to));
  304. }
  305. if ($scm) {
  306. @scm = uniq(@scm);
  307. output(@scm);
  308. }
  309. if ($status) {
  310. @status = uniq(@status);
  311. output(@status);
  312. }
  313. if ($subsystem) {
  314. @subsystem = uniq(@subsystem);
  315. output(@subsystem);
  316. }
  317. if ($web) {
  318. @web = uniq(@web);
  319. output(@web);
  320. }
  321. exit($exit);
  322. sub file_match_pattern {
  323. my ($file, $pattern) = @_;
  324. if (substr($pattern, -1) eq "/") {
  325. if ($file =~ m@^$pattern@) {
  326. return 1;
  327. }
  328. } else {
  329. if ($file =~ m@^$pattern@) {
  330. my $s1 = ($file =~ tr@/@@);
  331. my $s2 = ($pattern =~ tr@/@@);
  332. if ($s1 == $s2) {
  333. return 1;
  334. }
  335. }
  336. }
  337. return 0;
  338. }
  339. sub usage {
  340. print <<EOT;
  341. usage: $P [options] patchfile
  342. $P [options] -f file|directory
  343. version: $V
  344. MAINTAINER field selection options:
  345. --email => print email address(es) if any
  346. --git => include recent git \*-by: signers
  347. --git-chief-penguins => include ${penguin_chiefs}
  348. --git-min-signatures => number of signatures required (default: 1)
  349. --git-max-maintainers => maximum maintainers to add (default: 5)
  350. --git-min-percent => minimum percentage of commits required (default: 5)
  351. --git-since => git history to use (default: 1-year-ago)
  352. --git-blame => use git blame to find modified commits for patch or file
  353. --m => include maintainer(s) if any
  354. --n => include name 'Full Name <addr\@domain.tld>'
  355. --l => include list(s) if any
  356. --s => include subscriber only list(s) if any
  357. --remove-duplicates => minimize duplicate email names/addresses
  358. --scm => print SCM tree(s) if any
  359. --status => print status if any
  360. --subsystem => print subsystem name if any
  361. --web => print website(s) if any
  362. Output type options:
  363. --separator [, ] => separator for multiple entries on 1 line
  364. using --separator also sets --nomultiline if --separator is not [, ]
  365. --multiline => print 1 entry per line
  366. Other options:
  367. --pattern-depth => Number of pattern directory traversals (default: 0 (all))
  368. --keywords => scan patch for keywords (default: 1 (on))
  369. --version => show version
  370. --help => show this help information
  371. Default options:
  372. [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
  373. Notes:
  374. Using "-f directory" may give unexpected results:
  375. Used with "--git", git signators for _all_ files in and below
  376. directory are examined as git recurses directories.
  377. Any specified X: (exclude) pattern matches are _not_ ignored.
  378. Used with "--nogit", directory is used as a pattern match,
  379. no individual file within the directory or subdirectory
  380. is matched.
  381. Used with "--git-blame", does not iterate all files in directory
  382. Using "--git-blame" is slow and may add old committers and authors
  383. that are no longer active maintainers to the output.
  384. EOT
  385. }
  386. sub top_of_kernel_tree {
  387. my ($lk_path) = @_;
  388. if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
  389. $lk_path .= "/";
  390. }
  391. if ( (-f "${lk_path}COPYING")
  392. && (-f "${lk_path}CREDITS")
  393. && (-f "${lk_path}Kbuild")
  394. && (-f "${lk_path}MAINTAINERS")
  395. && (-f "${lk_path}Makefile")
  396. && (-f "${lk_path}README")
  397. && (-d "${lk_path}Documentation")
  398. && (-d "${lk_path}arch")
  399. && (-d "${lk_path}include")
  400. && (-d "${lk_path}drivers")
  401. && (-d "${lk_path}fs")
  402. && (-d "${lk_path}init")
  403. && (-d "${lk_path}ipc")
  404. && (-d "${lk_path}kernel")
  405. && (-d "${lk_path}lib")
  406. && (-d "${lk_path}scripts")) {
  407. return 1;
  408. }
  409. return 0;
  410. }
  411. sub parse_email {
  412. my ($formatted_email) = @_;
  413. my $name = "";
  414. my $address = "";
  415. if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
  416. $name = $1;
  417. $address = $2;
  418. } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
  419. $address = $1;
  420. } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
  421. $address = $1;
  422. }
  423. $name =~ s/^\s+|\s+$//g;
  424. $name =~ s/^\"|\"$//g;
  425. $address =~ s/^\s+|\s+$//g;
  426. if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
  427. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  428. $name = "\"$name\"";
  429. }
  430. return ($name, $address);
  431. }
  432. sub format_email {
  433. my ($name, $address) = @_;
  434. my $formatted_email;
  435. $name =~ s/^\s+|\s+$//g;
  436. $name =~ s/^\"|\"$//g;
  437. $address =~ s/^\s+|\s+$//g;
  438. if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
  439. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  440. $name = "\"$name\"";
  441. }
  442. if ($email_usename) {
  443. if ("$name" eq "") {
  444. $formatted_email = "$address";
  445. } else {
  446. $formatted_email = "$name <${address}>";
  447. }
  448. } else {
  449. $formatted_email = $address;
  450. }
  451. return $formatted_email;
  452. }
  453. sub find_starting_index {
  454. my ($index) = @_;
  455. while ($index > 0) {
  456. my $tv = $typevalue[$index];
  457. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  458. last;
  459. }
  460. $index--;
  461. }
  462. return $index;
  463. }
  464. sub find_ending_index {
  465. my ($index) = @_;
  466. while ($index < @typevalue) {
  467. my $tv = $typevalue[$index];
  468. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  469. last;
  470. }
  471. $index++;
  472. }
  473. return $index;
  474. }
  475. sub add_categories {
  476. my ($index) = @_;
  477. my $i;
  478. my $start = find_starting_index($index);
  479. my $end = find_ending_index($index);
  480. push(@subsystem, $typevalue[$start]);
  481. for ($i = $start + 1; $i < $end; $i++) {
  482. my $tv = $typevalue[$i];
  483. if ($tv =~ m/^(\C):\s*(.*)/) {
  484. my $ptype = $1;
  485. my $pvalue = $2;
  486. if ($ptype eq "L") {
  487. my $list_address = $pvalue;
  488. my $list_additional = "";
  489. if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
  490. $list_address = $1;
  491. $list_additional = $2;
  492. }
  493. if ($list_additional =~ m/subscribers-only/) {
  494. if ($email_subscriber_list) {
  495. push(@list_to, $list_address);
  496. }
  497. } else {
  498. if ($email_list) {
  499. push(@list_to, $list_address);
  500. }
  501. }
  502. } elsif ($ptype eq "M") {
  503. my ($name, $address) = parse_email($pvalue);
  504. if ($name eq "") {
  505. if ($i > 0) {
  506. my $tv = $typevalue[$i - 1];
  507. if ($tv =~ m/^(\C):\s*(.*)/) {
  508. if ($1 eq "P") {
  509. $name = $2;
  510. $pvalue = format_email($name, $address);
  511. }
  512. }
  513. }
  514. }
  515. if ($email_maintainer) {
  516. push_email_addresses($pvalue);
  517. }
  518. } elsif ($ptype eq "T") {
  519. push(@scm, $pvalue);
  520. } elsif ($ptype eq "W") {
  521. push(@web, $pvalue);
  522. } elsif ($ptype eq "S") {
  523. push(@status, $pvalue);
  524. }
  525. }
  526. }
  527. }
  528. my %email_hash_name;
  529. my %email_hash_address;
  530. sub email_inuse {
  531. my ($name, $address) = @_;
  532. return 1 if (($name eq "") && ($address eq ""));
  533. return 1 if (($name ne "") && exists($email_hash_name{$name}));
  534. return 1 if (($address ne "") && exists($email_hash_address{$address}));
  535. return 0;
  536. }
  537. sub push_email_address {
  538. my ($line) = @_;
  539. my ($name, $address) = parse_email($line);
  540. if ($address eq "") {
  541. return 0;
  542. }
  543. if (!$email_remove_duplicates) {
  544. push(@email_to, format_email($name, $address));
  545. } elsif (!email_inuse($name, $address)) {
  546. push(@email_to, format_email($name, $address));
  547. $email_hash_name{$name}++;
  548. $email_hash_address{$address}++;
  549. }
  550. return 1;
  551. }
  552. sub push_email_addresses {
  553. my ($address) = @_;
  554. my @address_list = ();
  555. if (rfc822_valid($address)) {
  556. push_email_address($address);
  557. } elsif (@address_list = rfc822_validlist($address)) {
  558. my $array_count = shift(@address_list);
  559. while (my $entry = shift(@address_list)) {
  560. push_email_address($entry);
  561. }
  562. } else {
  563. if (!push_email_address($address)) {
  564. warn("Invalid MAINTAINERS address: '" . $address . "'\n");
  565. }
  566. }
  567. }
  568. sub which {
  569. my ($bin) = @_;
  570. foreach my $path (split(/:/, $ENV{PATH})) {
  571. if (-e "$path/$bin") {
  572. return "$path/$bin";
  573. }
  574. }
  575. return "";
  576. }
  577. sub mailmap {
  578. my @lines = @_;
  579. my %hash;
  580. foreach my $line (@lines) {
  581. my ($name, $address) = parse_email($line);
  582. if (!exists($hash{$name})) {
  583. $hash{$name} = $address;
  584. } elsif ($address ne $hash{$name}) {
  585. $address = $hash{$name};
  586. $line = format_email($name, $address);
  587. }
  588. if (exists($mailmap{$name})) {
  589. my $obj = $mailmap{$name};
  590. foreach my $map_address (@$obj) {
  591. if (($map_address eq $address) &&
  592. ($map_address ne $hash{$name})) {
  593. $line = format_email($name, $hash{$name});
  594. }
  595. }
  596. }
  597. }
  598. return @lines;
  599. }
  600. sub recent_git_signoffs {
  601. my ($file) = @_;
  602. my $sign_offs = "";
  603. my $cmd = "";
  604. my $output = "";
  605. my $count = 0;
  606. my @lines = ();
  607. my %hash;
  608. my $total_sign_offs;
  609. if (which("git") eq "") {
  610. warn("$P: git not found. Add --nogit to options?\n");
  611. return;
  612. }
  613. if (!(-d ".git")) {
  614. warn("$P: .git directory not found. Use a git repository for better results.\n");
  615. warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
  616. return;
  617. }
  618. $cmd = "git log --since=${email_git_since} -- ${file}";
  619. $output = `${cmd}`;
  620. $output =~ s/^\s*//gm;
  621. @lines = split("\n", $output);
  622. @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
  623. if (!$email_git_penguin_chiefs) {
  624. @lines = grep(!/${penguin_chiefs}/i, @lines);
  625. }
  626. # cut -f2- -d":"
  627. s/.*:\s*(.+)\s*/$1/ for (@lines);
  628. $total_sign_offs = @lines;
  629. if ($email_remove_duplicates) {
  630. @lines = mailmap(@lines);
  631. }
  632. @lines = sort(@lines);
  633. # uniq -c
  634. $hash{$_}++ for @lines;
  635. # sort -rn
  636. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  637. my $sign_offs = $hash{$line};
  638. $count++;
  639. last if ($sign_offs < $email_git_min_signatures ||
  640. $count > $email_git_max_maintainers ||
  641. $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
  642. push_email_address($line);
  643. }
  644. }
  645. sub save_commits {
  646. my ($cmd, @commits) = @_;
  647. my $output;
  648. my @lines = ();
  649. $output = `${cmd}`;
  650. @lines = split("\n", $output);
  651. foreach my $line (@lines) {
  652. if ($line =~ m/^(\w+) /) {
  653. push (@commits, $1);
  654. }
  655. }
  656. return @commits;
  657. }
  658. sub git_assign_blame {
  659. my ($file) = @_;
  660. my @lines = ();
  661. my @commits = ();
  662. my $cmd;
  663. my $output;
  664. my %hash;
  665. my $total_sign_offs;
  666. my $count;
  667. if (@range) {
  668. foreach my $file_range_diff (@range) {
  669. next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
  670. my $diff_file = $1;
  671. my $diff_start = $2;
  672. my $diff_length = $3;
  673. next if (!("$file" eq "$diff_file"));
  674. $cmd = "git blame -l -L $diff_start,+$diff_length $file";
  675. @commits = save_commits($cmd, @commits);
  676. }
  677. } else {
  678. if (-f $file) {
  679. $cmd = "git blame -l $file";
  680. @commits = save_commits($cmd, @commits);
  681. }
  682. }
  683. $total_sign_offs = 0;
  684. @commits = uniq(@commits);
  685. foreach my $commit (@commits) {
  686. $cmd = "git log -1 ${commit}";
  687. $output = `${cmd}`;
  688. $output =~ s/^\s*//gm;
  689. @lines = split("\n", $output);
  690. @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
  691. if (!$email_git_penguin_chiefs) {
  692. @lines = grep(!/${penguin_chiefs}/i, @lines);
  693. }
  694. # cut -f2- -d":"
  695. s/.*:\s*(.+)\s*/$1/ for (@lines);
  696. $total_sign_offs += @lines;
  697. if ($email_remove_duplicates) {
  698. @lines = mailmap(@lines);
  699. }
  700. $hash{$_}++ for @lines;
  701. }
  702. $count = 0;
  703. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  704. my $sign_offs = $hash{$line};
  705. $count++;
  706. last if ($sign_offs < $email_git_min_signatures ||
  707. $count > $email_git_max_maintainers ||
  708. $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
  709. push_email_address($line);
  710. }
  711. }
  712. sub uniq {
  713. my @parms = @_;
  714. my %saw;
  715. @parms = grep(!$saw{$_}++, @parms);
  716. return @parms;
  717. }
  718. sub sort_and_uniq {
  719. my @parms = @_;
  720. my %saw;
  721. @parms = sort @parms;
  722. @parms = grep(!$saw{$_}++, @parms);
  723. return @parms;
  724. }
  725. sub output {
  726. my @parms = @_;
  727. if ($output_multiline) {
  728. foreach my $line (@parms) {
  729. print("${line}\n");
  730. }
  731. } else {
  732. print(join($output_separator, @parms));
  733. print("\n");
  734. }
  735. }
  736. my $rfc822re;
  737. sub make_rfc822re {
  738. # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
  739. # comment. We must allow for rfc822_lwsp (or comments) after each of these.
  740. # This regexp will only work on addresses which have had comments stripped
  741. # and replaced with rfc822_lwsp.
  742. my $specials = '()<>@,;:\\\\".\\[\\]';
  743. my $controls = '\\000-\\037\\177';
  744. my $dtext = "[^\\[\\]\\r\\\\]";
  745. my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
  746. my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
  747. # Use zero-width assertion to spot the limit of an atom. A simple
  748. # $rfc822_lwsp* causes the regexp engine to hang occasionally.
  749. my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
  750. my $word = "(?:$atom|$quoted_string)";
  751. my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
  752. my $sub_domain = "(?:$atom|$domain_literal)";
  753. my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
  754. my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
  755. my $phrase = "$word*";
  756. my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
  757. my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
  758. my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
  759. my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
  760. my $address = "(?:$mailbox|$group)";
  761. return "$rfc822_lwsp*$address";
  762. }
  763. sub rfc822_strip_comments {
  764. my $s = shift;
  765. # Recursively remove comments, and replace with a single space. The simpler
  766. # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
  767. # chars in atoms, for example.
  768. while ($s =~ s/^((?:[^"\\]|\\.)*
  769. (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
  770. \((?:[^()\\]|\\.)*\)/$1 /osx) {}
  771. return $s;
  772. }
  773. # valid: returns true if the parameter is an RFC822 valid address
  774. #
  775. sub rfc822_valid ($) {
  776. my $s = rfc822_strip_comments(shift);
  777. if (!$rfc822re) {
  778. $rfc822re = make_rfc822re();
  779. }
  780. return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
  781. }
  782. # validlist: In scalar context, returns true if the parameter is an RFC822
  783. # valid list of addresses.
  784. #
  785. # In list context, returns an empty list on failure (an invalid
  786. # address was found); otherwise a list whose first element is the
  787. # number of addresses found and whose remaining elements are the
  788. # addresses. This is needed to disambiguate failure (invalid)
  789. # from success with no addresses found, because an empty string is
  790. # a valid list.
  791. sub rfc822_validlist ($) {
  792. my $s = rfc822_strip_comments(shift);
  793. if (!$rfc822re) {
  794. $rfc822re = make_rfc822re();
  795. }
  796. # * null list items are valid according to the RFC
  797. # * the '1' business is to aid in distinguishing failure from no results
  798. my @r;
  799. if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
  800. $s =~ m/^$rfc822_char*$/) {
  801. while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
  802. push @r, $1;
  803. }
  804. return wantarray ? (scalar(@r), @r) : 1;
  805. }
  806. else {
  807. return wantarray ? () : 0;
  808. }
  809. }