get_maintainer.pl 22 KB

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