PageRenderTime 32ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/get_maintainer.pl

https://github.com/mstsirkin/kvm
Perl | 1984 lines | 1829 code | 120 blank | 35 comment | 134 complexity | cabb5453e8098495575f5bf67fc911ea MD5 | raw file
  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.26';
  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_penguin_chiefs = 0;
  23. my $email_git = 0;
  24. my $email_git_all_signature_types = 0;
  25. my $email_git_blame = 0;
  26. my $email_git_blame_signatures = 1;
  27. my $email_git_fallback = 1;
  28. my $email_git_min_signatures = 1;
  29. my $email_git_max_maintainers = 5;
  30. my $email_git_min_percent = 5;
  31. my $email_git_since = "1-year-ago";
  32. my $email_hg_since = "-365";
  33. my $interactive = 0;
  34. my $email_remove_duplicates = 1;
  35. my $email_use_mailmap = 1;
  36. my $output_multiline = 1;
  37. my $output_separator = ", ";
  38. my $output_roles = 0;
  39. my $output_rolestats = 1;
  40. my $scm = 0;
  41. my $web = 0;
  42. my $subsystem = 0;
  43. my $status = 0;
  44. my $keywords = 1;
  45. my $sections = 0;
  46. my $file_emails = 0;
  47. my $from_filename = 0;
  48. my $pattern_depth = 0;
  49. my $version = 0;
  50. my $help = 0;
  51. my $vcs_used = 0;
  52. my $exit = 0;
  53. my %commit_author_hash;
  54. my %commit_signer_hash;
  55. my @penguin_chief = ();
  56. push(@penguin_chief, "Linus Torvalds:torvalds\@linux-foundation.org");
  57. #Andrew wants in on most everything - 2009/01/14
  58. #push(@penguin_chief, "Andrew Morton:akpm\@linux-foundation.org");
  59. my @penguin_chief_names = ();
  60. foreach my $chief (@penguin_chief) {
  61. if ($chief =~ m/^(.*):(.*)/) {
  62. my $chief_name = $1;
  63. my $chief_addr = $2;
  64. push(@penguin_chief_names, $chief_name);
  65. }
  66. }
  67. my $penguin_chiefs = "\(" . join("|", @penguin_chief_names) . "\)";
  68. # Signature types of people who are either
  69. # a) responsible for the code in question, or
  70. # b) familiar enough with it to give relevant feedback
  71. my @signature_tags = ();
  72. push(@signature_tags, "Signed-off-by:");
  73. push(@signature_tags, "Reviewed-by:");
  74. push(@signature_tags, "Acked-by:");
  75. # rfc822 email address - preloaded methods go here.
  76. my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
  77. my $rfc822_char = '[\\000-\\377]';
  78. # VCS command support: class-like functions and strings
  79. my %VCS_cmds;
  80. my %VCS_cmds_git = (
  81. "execute_cmd" => \&git_execute_cmd,
  82. "available" => '(which("git") ne "") && (-d ".git")',
  83. "find_signers_cmd" =>
  84. "git log --no-color --since=\$email_git_since " .
  85. '--format="GitCommit: %H%n' .
  86. 'GitAuthor: %an <%ae>%n' .
  87. 'GitDate: %aD%n' .
  88. 'GitSubject: %s%n' .
  89. '%b%n"' .
  90. " -- \$file",
  91. "find_commit_signers_cmd" =>
  92. "git log --no-color " .
  93. '--format="GitCommit: %H%n' .
  94. 'GitAuthor: %an <%ae>%n' .
  95. 'GitDate: %aD%n' .
  96. 'GitSubject: %s%n' .
  97. '%b%n"' .
  98. " -1 \$commit",
  99. "find_commit_author_cmd" =>
  100. "git log --no-color " .
  101. '--format="GitCommit: %H%n' .
  102. 'GitAuthor: %an <%ae>%n' .
  103. 'GitDate: %aD%n' .
  104. 'GitSubject: %s%n"' .
  105. " -1 \$commit",
  106. "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
  107. "blame_file_cmd" => "git blame -l \$file",
  108. "commit_pattern" => "^GitCommit: ([0-9a-f]{40,40})",
  109. "blame_commit_pattern" => "^([0-9a-f]+) ",
  110. "author_pattern" => "^GitAuthor: (.*)",
  111. "subject_pattern" => "^GitSubject: (.*)",
  112. );
  113. my %VCS_cmds_hg = (
  114. "execute_cmd" => \&hg_execute_cmd,
  115. "available" => '(which("hg") ne "") && (-d ".hg")',
  116. "find_signers_cmd" =>
  117. "hg log --date=\$email_hg_since " .
  118. "--template='HgCommit: {node}\\n" .
  119. "HgAuthor: {author}\\n" .
  120. "HgSubject: {desc}\\n'" .
  121. " -- \$file",
  122. "find_commit_signers_cmd" =>
  123. "hg log " .
  124. "--template='HgSubject: {desc}\\n'" .
  125. " -r \$commit",
  126. "find_commit_author_cmd" =>
  127. "hg log " .
  128. "--template='HgCommit: {node}\\n" .
  129. "HgAuthor: {author}\\n" .
  130. "HgSubject: {desc|firstline}\\n'" .
  131. " -r \$commit",
  132. "blame_range_cmd" => "", # not supported
  133. "blame_file_cmd" => "hg blame -n \$file",
  134. "commit_pattern" => "^HgCommit: ([0-9a-f]{40,40})",
  135. "blame_commit_pattern" => "^([ 0-9a-f]+):",
  136. "author_pattern" => "^HgAuthor: (.*)",
  137. "subject_pattern" => "^HgSubject: (.*)",
  138. );
  139. my $conf = which_conf(".get_maintainer.conf");
  140. if (-f $conf) {
  141. my @conf_args;
  142. open(my $conffile, '<', "$conf")
  143. or warn "$P: Can't find a readable .get_maintainer.conf file $!\n";
  144. while (<$conffile>) {
  145. my $line = $_;
  146. $line =~ s/\s*\n?$//g;
  147. $line =~ s/^\s*//g;
  148. $line =~ s/\s+/ /g;
  149. next if ($line =~ m/^\s*#/);
  150. next if ($line =~ m/^\s*$/);
  151. my @words = split(" ", $line);
  152. foreach my $word (@words) {
  153. last if ($word =~ m/^#/);
  154. push (@conf_args, $word);
  155. }
  156. }
  157. close($conffile);
  158. unshift(@ARGV, @conf_args) if @conf_args;
  159. }
  160. if (!GetOptions(
  161. 'email!' => \$email,
  162. 'git!' => \$email_git,
  163. 'git-all-signature-types!' => \$email_git_all_signature_types,
  164. 'git-blame!' => \$email_git_blame,
  165. 'git-blame-signatures!' => \$email_git_blame_signatures,
  166. 'git-fallback!' => \$email_git_fallback,
  167. 'git-chief-penguins!' => \$email_git_penguin_chiefs,
  168. 'git-min-signatures=i' => \$email_git_min_signatures,
  169. 'git-max-maintainers=i' => \$email_git_max_maintainers,
  170. 'git-min-percent=i' => \$email_git_min_percent,
  171. 'git-since=s' => \$email_git_since,
  172. 'hg-since=s' => \$email_hg_since,
  173. 'i|interactive!' => \$interactive,
  174. 'remove-duplicates!' => \$email_remove_duplicates,
  175. 'mailmap!' => \$email_use_mailmap,
  176. 'm!' => \$email_maintainer,
  177. 'n!' => \$email_usename,
  178. 'l!' => \$email_list,
  179. 's!' => \$email_subscriber_list,
  180. 'multiline!' => \$output_multiline,
  181. 'roles!' => \$output_roles,
  182. 'rolestats!' => \$output_rolestats,
  183. 'separator=s' => \$output_separator,
  184. 'subsystem!' => \$subsystem,
  185. 'status!' => \$status,
  186. 'scm!' => \$scm,
  187. 'web!' => \$web,
  188. 'pattern-depth=i' => \$pattern_depth,
  189. 'k|keywords!' => \$keywords,
  190. 'sections!' => \$sections,
  191. 'fe|file-emails!' => \$file_emails,
  192. 'f|file' => \$from_filename,
  193. 'v|version' => \$version,
  194. 'h|help|usage' => \$help,
  195. )) {
  196. die "$P: invalid argument - use --help if necessary\n";
  197. }
  198. if ($help != 0) {
  199. usage();
  200. exit 0;
  201. }
  202. if ($version != 0) {
  203. print("${P} ${V}\n");
  204. exit 0;
  205. }
  206. if (-t STDIN && !@ARGV) {
  207. # We're talking to a terminal, but have no command line arguments.
  208. die "$P: missing patchfile or -f file - use --help if necessary\n";
  209. }
  210. $output_multiline = 0 if ($output_separator ne ", ");
  211. $output_rolestats = 1 if ($interactive);
  212. $output_roles = 1 if ($output_rolestats);
  213. if ($sections) {
  214. $email = 0;
  215. $email_list = 0;
  216. $scm = 0;
  217. $status = 0;
  218. $subsystem = 0;
  219. $web = 0;
  220. $keywords = 0;
  221. $interactive = 0;
  222. } else {
  223. my $selections = $email + $scm + $status + $subsystem + $web;
  224. if ($selections == 0) {
  225. die "$P: Missing required option: email, scm, status, subsystem or web\n";
  226. }
  227. }
  228. if ($email &&
  229. ($email_maintainer + $email_list + $email_subscriber_list +
  230. $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
  231. die "$P: Please select at least 1 email option\n";
  232. }
  233. if (!top_of_kernel_tree($lk_path)) {
  234. die "$P: The current directory does not appear to be "
  235. . "a linux kernel source tree.\n";
  236. }
  237. ## Read MAINTAINERS for type/value pairs
  238. my @typevalue = ();
  239. my %keyword_hash;
  240. open (my $maint, '<', "${lk_path}MAINTAINERS")
  241. or die "$P: Can't open MAINTAINERS: $!\n";
  242. while (<$maint>) {
  243. my $line = $_;
  244. if ($line =~ m/^(\C):\s*(.*)/) {
  245. my $type = $1;
  246. my $value = $2;
  247. ##Filename pattern matching
  248. if ($type eq "F" || $type eq "X") {
  249. $value =~ s@\.@\\\.@g; ##Convert . to \.
  250. $value =~ s/\*/\.\*/g; ##Convert * to .*
  251. $value =~ s/\?/\./g; ##Convert ? to .
  252. ##if pattern is a directory and it lacks a trailing slash, add one
  253. if ((-d $value)) {
  254. $value =~ s@([^/])$@$1/@;
  255. }
  256. } elsif ($type eq "K") {
  257. $keyword_hash{@typevalue} = $value;
  258. }
  259. push(@typevalue, "$type:$value");
  260. } elsif (!/^(\s)*$/) {
  261. $line =~ s/\n$//g;
  262. push(@typevalue, $line);
  263. }
  264. }
  265. close($maint);
  266. #
  267. # Read mail address map
  268. #
  269. my $mailmap;
  270. read_mailmap();
  271. sub read_mailmap {
  272. $mailmap = {
  273. names => {},
  274. addresses => {}
  275. };
  276. return if (!$email_use_mailmap || !(-f "${lk_path}.mailmap"));
  277. open(my $mailmap_file, '<', "${lk_path}.mailmap")
  278. or warn "$P: Can't open .mailmap: $!\n";
  279. while (<$mailmap_file>) {
  280. s/#.*$//; #strip comments
  281. s/^\s+|\s+$//g; #trim
  282. next if (/^\s*$/); #skip empty lines
  283. #entries have one of the following formats:
  284. # name1 <mail1>
  285. # <mail1> <mail2>
  286. # name1 <mail1> <mail2>
  287. # name1 <mail1> name2 <mail2>
  288. # (see man git-shortlog)
  289. if (/^([^<]+)<([^>]+)>$/) {
  290. my $real_name = $1;
  291. my $address = $2;
  292. $real_name =~ s/\s+$//;
  293. ($real_name, $address) = parse_email("$real_name <$address>");
  294. $mailmap->{names}->{$address} = $real_name;
  295. } elsif (/^<([^>]+)>\s*<([^>]+)>$/) {
  296. my $real_address = $1;
  297. my $wrong_address = $2;
  298. $mailmap->{addresses}->{$wrong_address} = $real_address;
  299. } elsif (/^(.+)<([^>]+)>\s*<([^>]+)>$/) {
  300. my $real_name = $1;
  301. my $real_address = $2;
  302. my $wrong_address = $3;
  303. $real_name =~ s/\s+$//;
  304. ($real_name, $real_address) =
  305. parse_email("$real_name <$real_address>");
  306. $mailmap->{names}->{$wrong_address} = $real_name;
  307. $mailmap->{addresses}->{$wrong_address} = $real_address;
  308. } elsif (/^(.+)<([^>]+)>\s*(.+)\s*<([^>]+)>$/) {
  309. my $real_name = $1;
  310. my $real_address = $2;
  311. my $wrong_name = $3;
  312. my $wrong_address = $4;
  313. $real_name =~ s/\s+$//;
  314. ($real_name, $real_address) =
  315. parse_email("$real_name <$real_address>");
  316. $wrong_name =~ s/\s+$//;
  317. ($wrong_name, $wrong_address) =
  318. parse_email("$wrong_name <$wrong_address>");
  319. my $wrong_email = format_email($wrong_name, $wrong_address, 1);
  320. $mailmap->{names}->{$wrong_email} = $real_name;
  321. $mailmap->{addresses}->{$wrong_email} = $real_address;
  322. }
  323. }
  324. close($mailmap_file);
  325. }
  326. ## use the filenames on the command line or find the filenames in the patchfiles
  327. my @files = ();
  328. my @range = ();
  329. my @keyword_tvi = ();
  330. my @file_emails = ();
  331. if (!@ARGV) {
  332. push(@ARGV, "&STDIN");
  333. }
  334. foreach my $file (@ARGV) {
  335. if ($file ne "&STDIN") {
  336. ##if $file is a directory and it lacks a trailing slash, add one
  337. if ((-d $file)) {
  338. $file =~ s@([^/])$@$1/@;
  339. } elsif (!(-f $file)) {
  340. die "$P: file '${file}' not found\n";
  341. }
  342. }
  343. if ($from_filename) {
  344. push(@files, $file);
  345. if ($file ne "MAINTAINERS" && -f $file && ($keywords || $file_emails)) {
  346. open(my $f, '<', $file)
  347. or die "$P: Can't open $file: $!\n";
  348. my $text = do { local($/) ; <$f> };
  349. close($f);
  350. if ($keywords) {
  351. foreach my $line (keys %keyword_hash) {
  352. if ($text =~ m/$keyword_hash{$line}/x) {
  353. push(@keyword_tvi, $line);
  354. }
  355. }
  356. }
  357. if ($file_emails) {
  358. my @poss_addr = $text =~ m$[A-Za-zÀ-ÿ\"\' \,\.\+-]*\s*[\,]*\s*[\(\<\{]{0,1}[A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+\.[A-Za-z0-9]+[\)\>\}]{0,1}$g;
  359. push(@file_emails, clean_file_emails(@poss_addr));
  360. }
  361. }
  362. } else {
  363. my $file_cnt = @files;
  364. my $lastfile;
  365. open(my $patch, "< $file")
  366. or die "$P: Can't open $file: $!\n";
  367. # We can check arbitrary information before the patch
  368. # like the commit message, mail headers, etc...
  369. # This allows us to match arbitrary keywords against any part
  370. # of a git format-patch generated file (subject tags, etc...)
  371. my $patch_prefix = ""; #Parsing the intro
  372. while (<$patch>) {
  373. my $patch_line = $_;
  374. if (m/^\+\+\+\s+(\S+)/) {
  375. my $filename = $1;
  376. $filename =~ s@^[^/]*/@@;
  377. $filename =~ s@\n@@;
  378. $lastfile = $filename;
  379. push(@files, $filename);
  380. $patch_prefix = "^[+-].*"; #Now parsing the actual patch
  381. } elsif (m/^\@\@ -(\d+),(\d+)/) {
  382. if ($email_git_blame) {
  383. push(@range, "$lastfile:$1:$2");
  384. }
  385. } elsif ($keywords) {
  386. foreach my $line (keys %keyword_hash) {
  387. if ($patch_line =~ m/${patch_prefix}$keyword_hash{$line}/x) {
  388. push(@keyword_tvi, $line);
  389. }
  390. }
  391. }
  392. }
  393. close($patch);
  394. if ($file_cnt == @files) {
  395. warn "$P: file '${file}' doesn't appear to be a patch. "
  396. . "Add -f to options?\n";
  397. }
  398. @files = sort_and_uniq(@files);
  399. }
  400. }
  401. @file_emails = uniq(@file_emails);
  402. my %email_hash_name;
  403. my %email_hash_address;
  404. my @email_to = ();
  405. my %hash_list_to;
  406. my @list_to = ();
  407. my @scm = ();
  408. my @web = ();
  409. my @subsystem = ();
  410. my @status = ();
  411. my %deduplicate_name_hash = ();
  412. my %deduplicate_address_hash = ();
  413. my $signature_pattern;
  414. my @maintainers = get_maintainers();
  415. if (@maintainers) {
  416. @maintainers = merge_email(@maintainers);
  417. output(@maintainers);
  418. }
  419. if ($scm) {
  420. @scm = uniq(@scm);
  421. output(@scm);
  422. }
  423. if ($status) {
  424. @status = uniq(@status);
  425. output(@status);
  426. }
  427. if ($subsystem) {
  428. @subsystem = uniq(@subsystem);
  429. output(@subsystem);
  430. }
  431. if ($web) {
  432. @web = uniq(@web);
  433. output(@web);
  434. }
  435. exit($exit);
  436. sub range_is_maintained {
  437. my ($start, $end) = @_;
  438. for (my $i = $start; $i < $end; $i++) {
  439. my $line = $typevalue[$i];
  440. if ($line =~ m/^(\C):\s*(.*)/) {
  441. my $type = $1;
  442. my $value = $2;
  443. if ($type eq 'S') {
  444. if ($value =~ /(maintain|support)/i) {
  445. return 1;
  446. }
  447. }
  448. }
  449. }
  450. return 0;
  451. }
  452. sub range_has_maintainer {
  453. my ($start, $end) = @_;
  454. for (my $i = $start; $i < $end; $i++) {
  455. my $line = $typevalue[$i];
  456. if ($line =~ m/^(\C):\s*(.*)/) {
  457. my $type = $1;
  458. my $value = $2;
  459. if ($type eq 'M') {
  460. return 1;
  461. }
  462. }
  463. }
  464. return 0;
  465. }
  466. sub get_maintainers {
  467. %email_hash_name = ();
  468. %email_hash_address = ();
  469. %commit_author_hash = ();
  470. %commit_signer_hash = ();
  471. @email_to = ();
  472. %hash_list_to = ();
  473. @list_to = ();
  474. @scm = ();
  475. @web = ();
  476. @subsystem = ();
  477. @status = ();
  478. %deduplicate_name_hash = ();
  479. %deduplicate_address_hash = ();
  480. if ($email_git_all_signature_types) {
  481. $signature_pattern = "(.+?)[Bb][Yy]:";
  482. } else {
  483. $signature_pattern = "\(" . join("|", @signature_tags) . "\)";
  484. }
  485. # Find responsible parties
  486. my %exact_pattern_match_hash = ();
  487. foreach my $file (@files) {
  488. my %hash;
  489. my $tvi = find_first_section();
  490. while ($tvi < @typevalue) {
  491. my $start = find_starting_index($tvi);
  492. my $end = find_ending_index($tvi);
  493. my $exclude = 0;
  494. my $i;
  495. #Do not match excluded file patterns
  496. for ($i = $start; $i < $end; $i++) {
  497. my $line = $typevalue[$i];
  498. if ($line =~ m/^(\C):\s*(.*)/) {
  499. my $type = $1;
  500. my $value = $2;
  501. if ($type eq 'X') {
  502. if (file_match_pattern($file, $value)) {
  503. $exclude = 1;
  504. last;
  505. }
  506. }
  507. }
  508. }
  509. if (!$exclude) {
  510. for ($i = $start; $i < $end; $i++) {
  511. my $line = $typevalue[$i];
  512. if ($line =~ m/^(\C):\s*(.*)/) {
  513. my $type = $1;
  514. my $value = $2;
  515. if ($type eq 'F') {
  516. if (file_match_pattern($file, $value)) {
  517. my $value_pd = ($value =~ tr@/@@);
  518. my $file_pd = ($file =~ tr@/@@);
  519. $value_pd++ if (substr($value,-1,1) ne "/");
  520. $value_pd = -1 if ($value =~ /^\.\*/);
  521. if ($value_pd >= $file_pd &&
  522. range_is_maintained($start, $end) &&
  523. range_has_maintainer($start, $end)) {
  524. $exact_pattern_match_hash{$file} = 1;
  525. }
  526. if ($pattern_depth == 0 ||
  527. (($file_pd - $value_pd) < $pattern_depth)) {
  528. $hash{$tvi} = $value_pd;
  529. }
  530. }
  531. }
  532. }
  533. }
  534. }
  535. $tvi = $end + 1;
  536. }
  537. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  538. add_categories($line);
  539. if ($sections) {
  540. my $i;
  541. my $start = find_starting_index($line);
  542. my $end = find_ending_index($line);
  543. for ($i = $start; $i < $end; $i++) {
  544. my $line = $typevalue[$i];
  545. if ($line =~ /^[FX]:/) { ##Restore file patterns
  546. $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
  547. $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
  548. $line =~ s/\\\./\./g; ##Convert \. to .
  549. $line =~ s/\.\*/\*/g; ##Convert .* to *
  550. }
  551. $line =~ s/^([A-Z]):/$1:\t/g;
  552. print("$line\n");
  553. }
  554. print("\n");
  555. }
  556. }
  557. }
  558. if ($keywords) {
  559. @keyword_tvi = sort_and_uniq(@keyword_tvi);
  560. foreach my $line (@keyword_tvi) {
  561. add_categories($line);
  562. }
  563. }
  564. foreach my $email (@email_to, @list_to) {
  565. $email->[0] = deduplicate_email($email->[0]);
  566. }
  567. foreach my $file (@files) {
  568. if ($email &&
  569. ($email_git || ($email_git_fallback &&
  570. !$exact_pattern_match_hash{$file}))) {
  571. vcs_file_signoffs($file);
  572. }
  573. if ($email && $email_git_blame) {
  574. vcs_file_blame($file);
  575. }
  576. }
  577. if ($email) {
  578. foreach my $chief (@penguin_chief) {
  579. if ($chief =~ m/^(.*):(.*)/) {
  580. my $email_address;
  581. $email_address = format_email($1, $2, $email_usename);
  582. if ($email_git_penguin_chiefs) {
  583. push(@email_to, [$email_address, 'chief penguin']);
  584. } else {
  585. @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
  586. }
  587. }
  588. }
  589. foreach my $email (@file_emails) {
  590. my ($name, $address) = parse_email($email);
  591. my $tmp_email = format_email($name, $address, $email_usename);
  592. push_email_address($tmp_email, '');
  593. add_role($tmp_email, 'in file');
  594. }
  595. }
  596. my @to = ();
  597. if ($email || $email_list) {
  598. if ($email) {
  599. @to = (@to, @email_to);
  600. }
  601. if ($email_list) {
  602. @to = (@to, @list_to);
  603. }
  604. }
  605. if ($interactive) {
  606. @to = interactive_get_maintainers(\@to);
  607. }
  608. return @to;
  609. }
  610. sub file_match_pattern {
  611. my ($file, $pattern) = @_;
  612. if (substr($pattern, -1) eq "/") {
  613. if ($file =~ m@^$pattern@) {
  614. return 1;
  615. }
  616. } else {
  617. if ($file =~ m@^$pattern@) {
  618. my $s1 = ($file =~ tr@/@@);
  619. my $s2 = ($pattern =~ tr@/@@);
  620. if ($s1 == $s2) {
  621. return 1;
  622. }
  623. }
  624. }
  625. return 0;
  626. }
  627. sub usage {
  628. print <<EOT;
  629. usage: $P [options] patchfile
  630. $P [options] -f file|directory
  631. version: $V
  632. MAINTAINER field selection options:
  633. --email => print email address(es) if any
  634. --git => include recent git \*-by: signers
  635. --git-all-signature-types => include signers regardless of signature type
  636. or use only ${signature_pattern} signers (default: $email_git_all_signature_types)
  637. --git-fallback => use git when no exact MAINTAINERS pattern (default: $email_git_fallback)
  638. --git-chief-penguins => include ${penguin_chiefs}
  639. --git-min-signatures => number of signatures required (default: $email_git_min_signatures)
  640. --git-max-maintainers => maximum maintainers to add (default: $email_git_max_maintainers)
  641. --git-min-percent => minimum percentage of commits required (default: $email_git_min_percent)
  642. --git-blame => use git blame to find modified commits for patch or file
  643. --git-since => git history to use (default: $email_git_since)
  644. --hg-since => hg history to use (default: $email_hg_since)
  645. --interactive => display a menu (mostly useful if used with the --git option)
  646. --m => include maintainer(s) if any
  647. --n => include name 'Full Name <addr\@domain.tld>'
  648. --l => include list(s) if any
  649. --s => include subscriber only list(s) if any
  650. --remove-duplicates => minimize duplicate email names/addresses
  651. --roles => show roles (status:subsystem, git-signer, list, etc...)
  652. --rolestats => show roles and statistics (commits/total_commits, %)
  653. --file-emails => add email addresses found in -f file (default: 0 (off))
  654. --scm => print SCM tree(s) if any
  655. --status => print status if any
  656. --subsystem => print subsystem name if any
  657. --web => print website(s) if any
  658. Output type options:
  659. --separator [, ] => separator for multiple entries on 1 line
  660. using --separator also sets --nomultiline if --separator is not [, ]
  661. --multiline => print 1 entry per line
  662. Other options:
  663. --pattern-depth => Number of pattern directory traversals (default: 0 (all))
  664. --keywords => scan patch for keywords (default: $keywords)
  665. --sections => print all of the subsystem sections with pattern matches
  666. --mailmap => use .mailmap file (default: $email_use_mailmap)
  667. --version => show version
  668. --help => show this help information
  669. Default options:
  670. [--email --nogit --git-fallback --m --n --l --multiline -pattern-depth=0
  671. --remove-duplicates --rolestats]
  672. Notes:
  673. Using "-f directory" may give unexpected results:
  674. Used with "--git", git signators for _all_ files in and below
  675. directory are examined as git recurses directories.
  676. Any specified X: (exclude) pattern matches are _not_ ignored.
  677. Used with "--nogit", directory is used as a pattern match,
  678. no individual file within the directory or subdirectory
  679. is matched.
  680. Used with "--git-blame", does not iterate all files in directory
  681. Using "--git-blame" is slow and may add old committers and authors
  682. that are no longer active maintainers to the output.
  683. Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
  684. other automated tools that expect only ["name"] <email address>
  685. may not work because of additional output after <email address>.
  686. Using "--rolestats" and "--git-blame" shows the #/total=% commits,
  687. not the percentage of the entire file authored. # of commits is
  688. not a good measure of amount of code authored. 1 major commit may
  689. contain a thousand lines, 5 trivial commits may modify a single line.
  690. If git is not installed, but mercurial (hg) is installed and an .hg
  691. repository exists, the following options apply to mercurial:
  692. --git,
  693. --git-min-signatures, --git-max-maintainers, --git-min-percent, and
  694. --git-blame
  695. Use --hg-since not --git-since to control date selection
  696. File ".get_maintainer.conf", if it exists in the linux kernel source root
  697. directory, can change whatever get_maintainer defaults are desired.
  698. Entries in this file can be any command line argument.
  699. This file is prepended to any additional command line arguments.
  700. Multiple lines and # comments are allowed.
  701. EOT
  702. }
  703. sub top_of_kernel_tree {
  704. my ($lk_path) = @_;
  705. if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
  706. $lk_path .= "/";
  707. }
  708. if ( (-f "${lk_path}COPYING")
  709. && (-f "${lk_path}CREDITS")
  710. && (-f "${lk_path}Kbuild")
  711. && (-f "${lk_path}MAINTAINERS")
  712. && (-f "${lk_path}Makefile")
  713. && (-f "${lk_path}README")
  714. && (-d "${lk_path}Documentation")
  715. && (-d "${lk_path}arch")
  716. && (-d "${lk_path}include")
  717. && (-d "${lk_path}drivers")
  718. && (-d "${lk_path}fs")
  719. && (-d "${lk_path}init")
  720. && (-d "${lk_path}ipc")
  721. && (-d "${lk_path}kernel")
  722. && (-d "${lk_path}lib")
  723. && (-d "${lk_path}scripts")) {
  724. return 1;
  725. }
  726. return 0;
  727. }
  728. sub parse_email {
  729. my ($formatted_email) = @_;
  730. my $name = "";
  731. my $address = "";
  732. if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
  733. $name = $1;
  734. $address = $2;
  735. } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
  736. $address = $1;
  737. } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
  738. $address = $1;
  739. }
  740. $name =~ s/^\s+|\s+$//g;
  741. $name =~ s/^\"|\"$//g;
  742. $address =~ s/^\s+|\s+$//g;
  743. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  744. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  745. $name = "\"$name\"";
  746. }
  747. return ($name, $address);
  748. }
  749. sub format_email {
  750. my ($name, $address, $usename) = @_;
  751. my $formatted_email;
  752. $name =~ s/^\s+|\s+$//g;
  753. $name =~ s/^\"|\"$//g;
  754. $address =~ s/^\s+|\s+$//g;
  755. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  756. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  757. $name = "\"$name\"";
  758. }
  759. if ($usename) {
  760. if ("$name" eq "") {
  761. $formatted_email = "$address";
  762. } else {
  763. $formatted_email = "$name <$address>";
  764. }
  765. } else {
  766. $formatted_email = $address;
  767. }
  768. return $formatted_email;
  769. }
  770. sub find_first_section {
  771. my $index = 0;
  772. while ($index < @typevalue) {
  773. my $tv = $typevalue[$index];
  774. if (($tv =~ m/^(\C):\s*(.*)/)) {
  775. last;
  776. }
  777. $index++;
  778. }
  779. return $index;
  780. }
  781. sub find_starting_index {
  782. my ($index) = @_;
  783. while ($index > 0) {
  784. my $tv = $typevalue[$index];
  785. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  786. last;
  787. }
  788. $index--;
  789. }
  790. return $index;
  791. }
  792. sub find_ending_index {
  793. my ($index) = @_;
  794. while ($index < @typevalue) {
  795. my $tv = $typevalue[$index];
  796. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  797. last;
  798. }
  799. $index++;
  800. }
  801. return $index;
  802. }
  803. sub get_maintainer_role {
  804. my ($index) = @_;
  805. my $i;
  806. my $start = find_starting_index($index);
  807. my $end = find_ending_index($index);
  808. my $role;
  809. my $subsystem = $typevalue[$start];
  810. if (length($subsystem) > 20) {
  811. $subsystem = substr($subsystem, 0, 17);
  812. $subsystem =~ s/\s*$//;
  813. $subsystem = $subsystem . "...";
  814. }
  815. for ($i = $start + 1; $i < $end; $i++) {
  816. my $tv = $typevalue[$i];
  817. if ($tv =~ m/^(\C):\s*(.*)/) {
  818. my $ptype = $1;
  819. my $pvalue = $2;
  820. if ($ptype eq "S") {
  821. $role = $pvalue;
  822. }
  823. }
  824. }
  825. $role = lc($role);
  826. if ($role eq "supported") {
  827. $role = "supporter";
  828. } elsif ($role eq "maintained") {
  829. $role = "maintainer";
  830. } elsif ($role eq "odd fixes") {
  831. $role = "odd fixer";
  832. } elsif ($role eq "orphan") {
  833. $role = "orphan minder";
  834. } elsif ($role eq "obsolete") {
  835. $role = "obsolete minder";
  836. } elsif ($role eq "buried alive in reporters") {
  837. $role = "chief penguin";
  838. }
  839. return $role . ":" . $subsystem;
  840. }
  841. sub get_list_role {
  842. my ($index) = @_;
  843. my $i;
  844. my $start = find_starting_index($index);
  845. my $end = find_ending_index($index);
  846. my $subsystem = $typevalue[$start];
  847. if (length($subsystem) > 20) {
  848. $subsystem = substr($subsystem, 0, 17);
  849. $subsystem =~ s/\s*$//;
  850. $subsystem = $subsystem . "...";
  851. }
  852. if ($subsystem eq "THE REST") {
  853. $subsystem = "";
  854. }
  855. return $subsystem;
  856. }
  857. sub add_categories {
  858. my ($index) = @_;
  859. my $i;
  860. my $start = find_starting_index($index);
  861. my $end = find_ending_index($index);
  862. push(@subsystem, $typevalue[$start]);
  863. for ($i = $start + 1; $i < $end; $i++) {
  864. my $tv = $typevalue[$i];
  865. if ($tv =~ m/^(\C):\s*(.*)/) {
  866. my $ptype = $1;
  867. my $pvalue = $2;
  868. if ($ptype eq "L") {
  869. my $list_address = $pvalue;
  870. my $list_additional = "";
  871. my $list_role = get_list_role($i);
  872. if ($list_role ne "") {
  873. $list_role = ":" . $list_role;
  874. }
  875. if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
  876. $list_address = $1;
  877. $list_additional = $2;
  878. }
  879. if ($list_additional =~ m/subscribers-only/) {
  880. if ($email_subscriber_list) {
  881. if (!$hash_list_to{lc($list_address)}) {
  882. $hash_list_to{lc($list_address)} = 1;
  883. push(@list_to, [$list_address,
  884. "subscriber list${list_role}"]);
  885. }
  886. }
  887. } else {
  888. if ($email_list) {
  889. if (!$hash_list_to{lc($list_address)}) {
  890. $hash_list_to{lc($list_address)} = 1;
  891. push(@list_to, [$list_address,
  892. "open list${list_role}"]);
  893. }
  894. }
  895. }
  896. } elsif ($ptype eq "M") {
  897. my ($name, $address) = parse_email($pvalue);
  898. if ($name eq "") {
  899. if ($i > 0) {
  900. my $tv = $typevalue[$i - 1];
  901. if ($tv =~ m/^(\C):\s*(.*)/) {
  902. if ($1 eq "P") {
  903. $name = $2;
  904. $pvalue = format_email($name, $address, $email_usename);
  905. }
  906. }
  907. }
  908. }
  909. if ($email_maintainer) {
  910. my $role = get_maintainer_role($i);
  911. push_email_addresses($pvalue, $role);
  912. }
  913. } elsif ($ptype eq "T") {
  914. push(@scm, $pvalue);
  915. } elsif ($ptype eq "W") {
  916. push(@web, $pvalue);
  917. } elsif ($ptype eq "S") {
  918. push(@status, $pvalue);
  919. }
  920. }
  921. }
  922. }
  923. sub email_inuse {
  924. my ($name, $address) = @_;
  925. return 1 if (($name eq "") && ($address eq ""));
  926. return 1 if (($name ne "") && exists($email_hash_name{lc($name)}));
  927. return 1 if (($address ne "") && exists($email_hash_address{lc($address)}));
  928. return 0;
  929. }
  930. sub push_email_address {
  931. my ($line, $role) = @_;
  932. my ($name, $address) = parse_email($line);
  933. if ($address eq "") {
  934. return 0;
  935. }
  936. if (!$email_remove_duplicates) {
  937. push(@email_to, [format_email($name, $address, $email_usename), $role]);
  938. } elsif (!email_inuse($name, $address)) {
  939. push(@email_to, [format_email($name, $address, $email_usename), $role]);
  940. $email_hash_name{lc($name)}++ if ($name ne "");
  941. $email_hash_address{lc($address)}++;
  942. }
  943. return 1;
  944. }
  945. sub push_email_addresses {
  946. my ($address, $role) = @_;
  947. my @address_list = ();
  948. if (rfc822_valid($address)) {
  949. push_email_address($address, $role);
  950. } elsif (@address_list = rfc822_validlist($address)) {
  951. my $array_count = shift(@address_list);
  952. while (my $entry = shift(@address_list)) {
  953. push_email_address($entry, $role);
  954. }
  955. } else {
  956. if (!push_email_address($address, $role)) {
  957. warn("Invalid MAINTAINERS address: '" . $address . "'\n");
  958. }
  959. }
  960. }
  961. sub add_role {
  962. my ($line, $role) = @_;
  963. my ($name, $address) = parse_email($line);
  964. my $email = format_email($name, $address, $email_usename);
  965. foreach my $entry (@email_to) {
  966. if ($email_remove_duplicates) {
  967. my ($entry_name, $entry_address) = parse_email($entry->[0]);
  968. if (($name eq $entry_name || $address eq $entry_address)
  969. && ($role eq "" || !($entry->[1] =~ m/$role/))
  970. ) {
  971. if ($entry->[1] eq "") {
  972. $entry->[1] = "$role";
  973. } else {
  974. $entry->[1] = "$entry->[1],$role";
  975. }
  976. }
  977. } else {
  978. if ($email eq $entry->[0]
  979. && ($role eq "" || !($entry->[1] =~ m/$role/))
  980. ) {
  981. if ($entry->[1] eq "") {
  982. $entry->[1] = "$role";
  983. } else {
  984. $entry->[1] = "$entry->[1],$role";
  985. }
  986. }
  987. }
  988. }
  989. }
  990. sub which {
  991. my ($bin) = @_;
  992. foreach my $path (split(/:/, $ENV{PATH})) {
  993. if (-e "$path/$bin") {
  994. return "$path/$bin";
  995. }
  996. }
  997. return "";
  998. }
  999. sub which_conf {
  1000. my ($conf) = @_;
  1001. foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
  1002. if (-e "$path/$conf") {
  1003. return "$path/$conf";
  1004. }
  1005. }
  1006. return "";
  1007. }
  1008. sub mailmap_email {
  1009. my ($line) = @_;
  1010. my ($name, $address) = parse_email($line);
  1011. my $email = format_email($name, $address, 1);
  1012. my $real_name = $name;
  1013. my $real_address = $address;
  1014. if (exists $mailmap->{names}->{$email} ||
  1015. exists $mailmap->{addresses}->{$email}) {
  1016. if (exists $mailmap->{names}->{$email}) {
  1017. $real_name = $mailmap->{names}->{$email};
  1018. }
  1019. if (exists $mailmap->{addresses}->{$email}) {
  1020. $real_address = $mailmap->{addresses}->{$email};
  1021. }
  1022. } else {
  1023. if (exists $mailmap->{names}->{$address}) {
  1024. $real_name = $mailmap->{names}->{$address};
  1025. }
  1026. if (exists $mailmap->{addresses}->{$address}) {
  1027. $real_address = $mailmap->{addresses}->{$address};
  1028. }
  1029. }
  1030. return format_email($real_name, $real_address, 1);
  1031. }
  1032. sub mailmap {
  1033. my (@addresses) = @_;
  1034. my @mapped_emails = ();
  1035. foreach my $line (@addresses) {
  1036. push(@mapped_emails, mailmap_email($line));
  1037. }
  1038. merge_by_realname(@mapped_emails) if ($email_use_mailmap);
  1039. return @mapped_emails;
  1040. }
  1041. sub merge_by_realname {
  1042. my %address_map;
  1043. my (@emails) = @_;
  1044. foreach my $email (@emails) {
  1045. my ($name, $address) = parse_email($email);
  1046. if (exists $address_map{$name}) {
  1047. $address = $address_map{$name};
  1048. $email = format_email($name, $address, 1);
  1049. } else {
  1050. $address_map{$name} = $address;
  1051. }
  1052. }
  1053. }
  1054. sub git_execute_cmd {
  1055. my ($cmd) = @_;
  1056. my @lines = ();
  1057. my $output = `$cmd`;
  1058. $output =~ s/^\s*//gm;
  1059. @lines = split("\n", $output);
  1060. return @lines;
  1061. }
  1062. sub hg_execute_cmd {
  1063. my ($cmd) = @_;
  1064. my @lines = ();
  1065. my $output = `$cmd`;
  1066. @lines = split("\n", $output);
  1067. return @lines;
  1068. }
  1069. sub extract_formatted_signatures {
  1070. my (@signature_lines) = @_;
  1071. my @type = @signature_lines;
  1072. s/\s*(.*):.*/$1/ for (@type);
  1073. # cut -f2- -d":"
  1074. s/\s*.*:\s*(.+)\s*/$1/ for (@signature_lines);
  1075. ## Reformat email addresses (with names) to avoid badly written signatures
  1076. foreach my $signer (@signature_lines) {
  1077. $signer = deduplicate_email($signer);
  1078. }
  1079. return (\@type, \@signature_lines);
  1080. }
  1081. sub vcs_find_signers {
  1082. my ($cmd) = @_;
  1083. my $commits;
  1084. my @lines = ();
  1085. my @signatures = ();
  1086. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  1087. my $pattern = $VCS_cmds{"commit_pattern"};
  1088. $commits = grep(/$pattern/, @lines); # of commits
  1089. @signatures = grep(/^[ \t]*${signature_pattern}.*\@.*$/, @lines);
  1090. return (0, @signatures) if !@signatures;
  1091. save_commits_by_author(@lines) if ($interactive);
  1092. save_commits_by_signer(@lines) if ($interactive);
  1093. if (!$email_git_penguin_chiefs) {
  1094. @signatures = grep(!/${penguin_chiefs}/i, @signatures);
  1095. }
  1096. my ($types_ref, $signers_ref) = extract_formatted_signatures(@signatures);
  1097. return ($commits, @$signers_ref);
  1098. }
  1099. sub vcs_find_author {
  1100. my ($cmd) = @_;
  1101. my @lines = ();
  1102. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  1103. if (!$email_git_penguin_chiefs) {
  1104. @lines = grep(!/${penguin_chiefs}/i, @lines);
  1105. }
  1106. return @lines if !@lines;
  1107. my @authors = ();
  1108. foreach my $line (@lines) {
  1109. if ($line =~ m/$VCS_cmds{"author_pattern"}/) {
  1110. my $author = $1;
  1111. my ($name, $address) = parse_email($author);
  1112. $author = format_email($name, $address, 1);
  1113. push(@authors, $author);
  1114. }
  1115. }
  1116. save_commits_by_author(@lines) if ($interactive);
  1117. save_commits_by_signer(@lines) if ($interactive);
  1118. return @authors;
  1119. }
  1120. sub vcs_save_commits {
  1121. my ($cmd) = @_;
  1122. my @lines = ();
  1123. my @commits = ();
  1124. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  1125. foreach my $line (@lines) {
  1126. if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
  1127. push(@commits, $1);
  1128. }
  1129. }
  1130. return @commits;
  1131. }
  1132. sub vcs_blame {
  1133. my ($file) = @_;
  1134. my $cmd;
  1135. my @commits = ();
  1136. return @commits if (!(-f $file));
  1137. if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
  1138. my @all_commits = ();
  1139. $cmd = $VCS_cmds{"blame_file_cmd"};
  1140. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  1141. @all_commits = vcs_save_commits($cmd);
  1142. foreach my $file_range_diff (@range) {
  1143. next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
  1144. my $diff_file = $1;
  1145. my $diff_start = $2;
  1146. my $diff_length = $3;
  1147. next if ("$file" ne "$diff_file");
  1148. for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
  1149. push(@commits, $all_commits[$i]);
  1150. }
  1151. }
  1152. } elsif (@range) {
  1153. foreach my $file_range_diff (@range) {
  1154. next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
  1155. my $diff_file = $1;
  1156. my $diff_start = $2;
  1157. my $diff_length = $3;
  1158. next if ("$file" ne "$diff_file");
  1159. $cmd = $VCS_cmds{"blame_range_cmd"};
  1160. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  1161. push(@commits, vcs_save_commits($cmd));
  1162. }
  1163. } else {
  1164. $cmd = $VCS_cmds{"blame_file_cmd"};
  1165. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  1166. @commits = vcs_save_commits($cmd);
  1167. }
  1168. foreach my $commit (@commits) {
  1169. $commit =~ s/^\^//g;
  1170. }
  1171. return @commits;
  1172. }
  1173. my $printed_novcs = 0;
  1174. sub vcs_exists {
  1175. %VCS_cmds = %VCS_cmds_git;
  1176. return 1 if eval $VCS_cmds{"available"};
  1177. %VCS_cmds = %VCS_cmds_hg;
  1178. return 2 if eval $VCS_cmds{"available"};
  1179. %VCS_cmds = ();
  1180. if (!$printed_novcs) {
  1181. warn("$P: No supported VCS found. Add --nogit to options?\n");
  1182. warn("Using a git repository produces better results.\n");
  1183. warn("Try Linus Torvalds' latest git repository using:\n");
  1184. warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git\n");
  1185. $printed_novcs = 1;
  1186. }
  1187. return 0;
  1188. }
  1189. sub vcs_is_git {
  1190. vcs_exists();
  1191. return $vcs_used == 1;
  1192. }
  1193. sub vcs_is_hg {
  1194. return $vcs_used == 2;
  1195. }
  1196. sub interactive_get_maintainers {
  1197. my ($list_ref) = @_;
  1198. my @list = @$list_ref;
  1199. vcs_exists();
  1200. my %selected;
  1201. my %authored;
  1202. my %signed;
  1203. my $count = 0;
  1204. my $maintained = 0;
  1205. foreach my $entry (@list) {
  1206. $maintained = 1 if ($entry->[1] =~ /^(maintainer|supporter)/i);
  1207. $selected{$count} = 1;
  1208. $authored{$count} = 0;
  1209. $signed{$count} = 0;
  1210. $count++;
  1211. }
  1212. #menu loop
  1213. my $done = 0;
  1214. my $print_options = 0;
  1215. my $redraw = 1;
  1216. while (!$done) {
  1217. $count = 0;
  1218. if ($redraw) {
  1219. printf STDERR "\n%1s %2s %-65s",
  1220. "*", "#", "email/list and role:stats";
  1221. if ($email_git ||
  1222. ($email_git_fallback && !$maintained) ||
  1223. $email_git_blame) {
  1224. print STDERR "auth sign";
  1225. }
  1226. print STDERR "\n";
  1227. foreach my $entry (@list) {
  1228. my $email = $entry->[0];
  1229. my $role = $entry->[1];
  1230. my $sel = "";
  1231. $sel = "*" if ($selected{$count});
  1232. my $commit_author = $commit_author_hash{$email};
  1233. my $commit_signer = $commit_signer_hash{$email};
  1234. my $authored = 0;
  1235. my $signed = 0;
  1236. $authored++ for (@{$commit_author});
  1237. $signed++ for (@{$commit_signer});
  1238. printf STDERR "%1s %2d %-65s", $sel, $count + 1, $email;
  1239. printf STDERR "%4d %4d", $authored, $signed
  1240. if ($authored > 0 || $signed > 0);
  1241. printf STDERR "\n %s\n", $role;
  1242. if ($authored{$count}) {
  1243. my $commit_author = $commit_author_hash{$email};
  1244. foreach my $ref (@{$commit_author}) {
  1245. print STDERR " Author: @{$ref}[1]\n";
  1246. }
  1247. }
  1248. if ($signed{$count}) {
  1249. my $commit_signer = $commit_signer_hash{$email};
  1250. foreach my $ref (@{$commit_signer}) {
  1251. print STDERR " @{$ref}[2]: @{$ref}[1]\n";
  1252. }
  1253. }
  1254. $count++;
  1255. }
  1256. }
  1257. my $date_ref = \$email_git_since;
  1258. $date_ref = \$email_hg_since if (vcs_is_hg());
  1259. if ($print_options) {
  1260. $print_options = 0;
  1261. if (vcs_exists()) {
  1262. print STDERR <<EOT
  1263. Version Control options:
  1264. g use git history [$email_git]
  1265. gf use git-fallback [$email_git_fallback]
  1266. b use git blame [$email_git_blame]
  1267. bs use blame signatures [$email_git_blame_signatures]
  1268. c# minimum commits [$email_git_min_signatures]
  1269. %# min percent [$email_git_min_percent]
  1270. d# history to use [$$date_ref]
  1271. x# max maintainers [$email_git_max_maintainers]
  1272. t all signature types [$email_git_all_signature_types]
  1273. m use .mailmap [$email_use_mailmap]
  1274. EOT
  1275. }
  1276. print STDERR <<EOT
  1277. Additional options:
  1278. 0 toggle all
  1279. tm toggle maintainers
  1280. tg toggle git entries
  1281. tl toggle open list entries
  1282. ts toggle subscriber list entries
  1283. f emails in file [$file_emails]
  1284. k keywords in file [$keywords]
  1285. r remove duplicates [$email_remove_duplicates]
  1286. p# pattern match depth [$pattern_depth]
  1287. EOT
  1288. }
  1289. print STDERR
  1290. "\n#(toggle), A#(author), S#(signed) *(all), ^(none), O(options), Y(approve): ";
  1291. my $input = <STDIN>;
  1292. chomp($input);
  1293. $redraw = 1;
  1294. my $rerun = 0;
  1295. my @wish = split(/[, ]+/, $input);
  1296. foreach my $nr (@wish) {
  1297. $nr = lc($nr);
  1298. my $sel = substr($nr, 0, 1);
  1299. my $str = substr($nr, 1);
  1300. my $val = 0;
  1301. $val = $1 if $str =~ /^(\d+)$/;
  1302. if ($sel eq "y") {
  1303. $interactive = 0;
  1304. $done = 1;
  1305. $output_rolestats = 0;
  1306. $output_roles = 0;
  1307. last;
  1308. } elsif ($nr =~ /^\d+$/ && $nr > 0 && $nr <= $count) {
  1309. $selected{$nr - 1} = !$selected{$nr - 1};
  1310. } elsif ($sel eq "*" || $sel eq '^') {
  1311. my $toggle = 0;
  1312. $toggle = 1 if ($sel eq '*');
  1313. for (my $i = 0; $i < $count; $i++) {
  1314. $selected{$i} = $toggle;
  1315. }
  1316. } elsif ($sel eq "0") {
  1317. for (my $i = 0; $i < $count; $i++) {
  1318. $selected{$i} = !$selected{$i};
  1319. }
  1320. } elsif ($sel eq "t") {
  1321. if (lc($str) eq "m") {
  1322. for (my $i = 0; $i < $count; $i++) {
  1323. $selected{$i} = !$selected{$i}
  1324. if ($list[$i]->[1] =~ /^(maintainer|supporter)/i);
  1325. }
  1326. } elsif (lc($str) eq "g") {
  1327. for (my $i = 0; $i < $count; $i++) {
  1328. $selected{$i} = !$selected{$i}
  1329. if ($list[$i]->[1] =~ /^(author|commit|signer)/i);
  1330. }
  1331. } elsif (lc($str) eq "l") {
  1332. for (my $i = 0; $i < $count; $i++) {
  1333. $selected{$i} = !$selected{$i}
  1334. if ($list[$i]->[1] =~ /^(open list)/i);
  1335. }
  1336. } elsif (lc($str) eq "s") {
  1337. for (my $i = 0; $i < $count; $i++) {
  1338. $selected{$i} = !$selected{$i}
  1339. if ($list[$i]->[1] =~ /^(subscriber list)/i);
  1340. }
  1341. }
  1342. } elsif ($sel eq "a") {
  1343. if ($val > 0 && $val <= $count) {
  1344. $authored{$val - 1} = !$authored{$val - 1};
  1345. } elsif ($str eq '*' || $str eq '^') {
  1346. my $toggle = 0;
  1347. $toggle = 1 if ($str eq '*');
  1348. for (my $i = 0; $i < $count; $i++) {
  1349. $authored{$i} = $toggle;
  1350. }
  1351. }
  1352. } elsif ($sel eq "s") {
  1353. if ($val > 0 && $val <= $count) {
  1354. $signed{$val - 1} = !$signed{$val - 1};
  1355. } elsif ($str eq '*' || $str eq '^') {
  1356. my $toggle = 0;
  1357. $toggle = 1 if ($str eq '*');
  1358. for (my $i = 0; $i < $count; $i++) {
  1359. $signed{$i} = $toggle;
  1360. }
  1361. }
  1362. } elsif ($sel eq "o") {
  1363. $print_options = 1;
  1364. $redraw = 1;
  1365. } elsif ($sel eq "g") {
  1366. if ($str eq "f") {
  1367. bool_invert(\$email_git_fallback);
  1368. } else {
  1369. bool_invert(\$email_git);
  1370. }
  1371. $rerun = 1;
  1372. } elsif ($sel eq "b") {
  1373. if ($str eq "s") {
  1374. bool_invert(\$email_git_blame_signatures);
  1375. } else {
  1376. bool_invert(\$email_git_blame);
  1377. }
  1378. $rerun = 1;
  1379. } elsif ($sel eq "c") {
  1380. if ($val > 0) {
  1381. $email_git_min_signatures = $val;
  1382. $rerun = 1;
  1383. }
  1384. } elsif ($sel eq "x") {
  1385. if ($val > 0) {
  1386. $email_git_max_maintainers = $val;
  1387. $rerun = 1;
  1388. }
  1389. } elsif ($sel eq "%") {
  1390. if ($str ne "" && $val >= 0) {
  1391. $email_git_min_percent = $val;
  1392. $rerun = 1;
  1393. }
  1394. } elsif ($sel eq "d") {
  1395. if (vcs_is_git()) {
  1396. $email_git_since = $str;
  1397. } elsif (vcs_is_hg()) {
  1398. $email_hg_since = $str;
  1399. }
  1400. $rerun = 1;
  1401. } elsif ($sel eq "t") {
  1402. bool_invert(\$email_git_all_signature_types);
  1403. $rerun = 1;
  1404. } elsif ($sel eq "f") {
  1405. bool_invert(\$file_emails);
  1406. $rerun = 1;
  1407. } elsif ($sel eq "r") {
  1408. bool_invert(\$email_remove_duplicates);
  1409. $rerun = 1;
  1410. } elsif ($sel eq "m") {
  1411. bool_invert(\$email_use_mailmap);
  1412. read_mailmap();
  1413. $rerun = 1;
  1414. } elsif ($sel eq "k") {
  1415. bool_invert(\$keywords);
  1416. $rerun = 1;
  1417. } elsif ($sel eq "p") {
  1418. if ($str ne "" && $val >= 0) {
  1419. $pattern_depth = $val;
  1420. $rerun = 1;
  1421. }
  1422. } elsif ($sel eq "h" || $sel eq "?") {
  1423. print STDERR <<EOT
  1424. Interactive mode allows you to select the various maintainers, submitters,
  1425. commit signers and mailing lists that could be CC'd on a patch.
  1426. Any *'d entry is selected.
  1427. If you have git or hg installed, you can choose to summarize the commit
  1428. history of files in the patch. Also, each line of the current file can
  1429. be matched to its commit author and that commits signers with blame.
  1430. Various knobs exist to control the length of time for active commit
  1431. tracking, the maximum number of commit authors and signers to add,
  1432. and such.
  1433. Enter selections at the prompt until you are satisfied that the selected
  1434. maintainers are appropriate. You may enter multiple selections separated
  1435. by either commas or spaces.
  1436. EOT
  1437. } else {
  1438. print STDERR "invalid option: '$nr'\n";
  1439. $redraw = 0;
  1440. }
  1441. }
  1442. if ($rerun) {
  1443. print STDERR "git-blame can be very slow, please have patience..."
  1444. if ($email_git_blame);
  1445. goto &get_maintainers;
  1446. }
  1447. }
  1448. #drop not selected entries
  1449. $count = 0;
  1450. my @new_emailto = ();
  1451. foreach my $entry (@list) {
  1452. if ($selected{$count}) {
  1453. push(@new_emailto, $list[$count]);
  1454. }
  1455. $count++;
  1456. }
  1457. return @new_emailto;
  1458. }
  1459. sub bool_invert {
  1460. my ($bool_ref) = @_;
  1461. if ($$bool_ref) {
  1462. $$bool_ref = 0;
  1463. } else {
  1464. $$bool_ref = 1;
  1465. }
  1466. }
  1467. sub deduplicate_email {
  1468. my ($email) = @_;
  1469. my $matched = 0;
  1470. my ($name, $address) = parse_email($email);
  1471. $email = format_email($name, $address, 1);
  1472. $email = mailmap_email($email);
  1473. return $email if (!$email_remove_duplicates);
  1474. ($name, $address) = parse_email($email);
  1475. if ($name ne "" && $deduplicate_name_hash{lc($name)}) {
  1476. $name = $deduplicate_name_hash{lc($name)}->[0];
  1477. $address = $deduplicate_name_hash{lc($name)}->[1];
  1478. $matched = 1;
  1479. } elsif ($deduplicate_address_hash{lc($address)}) {
  1480. $name = $deduplicate_address_hash{lc($address)}->[0];
  1481. $address = $deduplicate_address_hash{lc($address)}->[1];
  1482. $matched = 1;
  1483. }
  1484. if (!$matched) {
  1485. $deduplicate_name_hash{lc($name)} = [ $name, $address ];
  1486. $deduplicate_address_hash{lc($address)} = [ $name, $address ];
  1487. }
  1488. $email = format_email($name, $address, 1);
  1489. $email = mailmap_email($email);
  1490. return $email;
  1491. }
  1492. sub save_commits_by_author {
  1493. my (@lines) = @_;
  1494. my @authors = ();
  1495. my @commits = ();
  1496. my @subjects = ();
  1497. foreach my $line (@lines) {
  1498. if ($line =~ m/$VCS_cmds{"author_pattern"}/) {
  1499. my $author = $1;
  1500. $author = deduplicate_email($author);
  1501. push(@authors, $author);
  1502. }
  1503. push(@commits, $1) if ($line =~ m/$VCS_cmds{"commit_pattern"}/);
  1504. push(@subjects, $1) if ($line =~ m/$VCS_cmds{"subject_pattern"}/);
  1505. }
  1506. for (my $i = 0; $i < @authors; $i++) {
  1507. my $exists = 0;
  1508. foreach my $ref(@{$commit_author_hash{$authors[$i]}}) {
  1509. if (@{$ref}[0] eq $commits[$i] &&
  1510. @{$ref}[1] eq $subjects[$i]) {
  1511. $exists = 1;
  1512. last;
  1513. }
  1514. }
  1515. if (!$exists) {
  1516. push(@{$commit_author_hash{$authors[$i]}},
  1517. [ ($commits[$i], $subjects[$i]) ]);
  1518. }
  1519. }
  1520. }
  1521. sub save_commits_by_signer {
  1522. my (@lines) = @_;
  1523. my $commit = "";
  1524. my $subject = "";
  1525. foreach my $line (@lines) {
  1526. $commit = $1 if ($line =~ m/$VCS_cmds{"commit_pattern"}/);
  1527. $subject = $1 if ($line =~ m/$VCS_cmds{"subject_pattern"}/);
  1528. if ($line =~ /^[ \t]*${signature_pattern}.*\@.*$/) {
  1529. my @signatures = ($line);
  1530. my ($types_ref, $signers_ref) = extract_formatted_signatures(@signatures);
  1531. my @types = @$types_ref;
  1532. my @signers = @$signers_ref;
  1533. my $type = $types[0];
  1534. my $signer = $signers[0];
  1535. $signer = deduplicate_email($signer);
  1536. my $exists = 0;
  1537. foreach my $ref(@{$commit_signer_hash{$signer}}) {
  1538. if (@{$ref}[0] eq $commit &&
  1539. @{$ref}[1] eq $subject &&
  1540. @{$ref}[2] eq $type) {
  1541. $exists = 1;
  1542. last;
  1543. }
  1544. }
  1545. if (!$exists) {
  1546. push(@{$commit_signer_hash{$signer}},
  1547. [ ($commit, $subject, $type) ]);
  1548. }
  1549. }
  1550. }
  1551. }
  1552. sub vcs_assign {
  1553. my ($role, $divisor, @lines) = @_;
  1554. my %hash;
  1555. my $count = 0;
  1556. return if (@lines <= 0);
  1557. if ($divisor <= 0) {
  1558. warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
  1559. $divisor = 1;
  1560. }
  1561. @lines = mailmap(@lines);
  1562. return if (@lines <= 0);
  1563. @lines = sort(@lines);
  1564. # uniq -c
  1565. $hash{$_}++ for @lines;
  1566. # sort -rn
  1567. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  1568. my $sign_offs = $hash{$line};
  1569. my $percent = $sign_offs * 100 / $divisor;
  1570. $percent = 100 if ($percent > 100);
  1571. $count++;
  1572. last if ($sign_offs < $email_git_min_signatures ||
  1573. $count > $email_git_max_maintainers ||
  1574. $percent < $email_git_min_percent);
  1575. push_email_address($line, '');
  1576. if ($output_rolestats) {
  1577. my $fmt_percent = sprintf("%.0f", $percent);
  1578. add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
  1579. } else {
  1580. add_role($line, $role);
  1581. }
  1582. }
  1583. }
  1584. sub vcs_file_signoffs {
  1585. my ($file) = @_;
  1586. my @signers = ();
  1587. my $commits;
  1588. $vcs_used = vcs_exists();
  1589. return if (!$vcs_used);
  1590. my $cmd = $VCS_cmds{"find_signers_cmd"};
  1591. $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
  1592. ($commits, @signers) = vcs_find_signers($cmd);
  1593. foreach my $signer (@signers) {
  1594. $signer = deduplicate_email($signer);
  1595. }
  1596. vcs_assign("commit_signer", $commits, @signers);
  1597. }
  1598. sub vcs_file_blame {
  1599. my ($file) = @_;
  1600. my @signers = ();
  1601. my @all_commits = ();
  1602. my @commits = ();
  1603. my $total_commits;
  1604. my $total_lines;
  1605. $vcs_used = vcs_exists();
  1606. return if (!$vcs_used);
  1607. @all_commits = vcs_blame($file);
  1608. @commits = uniq(@all_commits);
  1609. $total_commits = @commits;
  1610. $total_lines = @all_commits;
  1611. if ($email_git_blame_signatures) {
  1612. if (vcs_is_hg()) {
  1613. my $commit_count;
  1614. my @commit_signers = ();
  1615. my $commit = join(" -r ", @commits);
  1616. my $cmd;
  1617. $cmd = $VCS_cmds{"find_commit_signers_cmd"};
  1618. $cmd =~ s/(\$\w+)/$1/eeg; #substitute variables in $cmd
  1619. ($commit_count, @commit_signers) = vcs_find_signers($cmd);
  1620. push(@signers, @commit_signers);
  1621. } else {
  1622. foreach my $commit (@commits) {
  1623. my $commit_count;
  1624. my @commit_signers = ();
  1625. my $cmd;
  1626. $cmd = $VCS_cmds{"find_commit_signers_cmd"};
  1627. $cmd =~ s/(\$\w+)/$1/eeg; #substitute variables in $cmd
  1628. ($commit_count, @commit_signers) = vcs_find_signers($cmd);
  1629. push(@signers, @commit_signers);
  1630. }
  1631. }
  1632. }
  1633. if ($from_filename) {
  1634. if ($output_rolestats) {
  1635. my @blame_signers;
  1636. if (vcs_is_hg()) {{ # Double brace for last exit
  1637. my $commit_count;
  1638. my @commit_signers = ();
  1639. @commits = uniq(@commits);
  1640. @commits = sort(@commits);
  1641. my $commit = join(" -r ", @commits);
  1642. my $cmd;
  1643. $cmd = $VCS_cmds{"find_commit_author_cmd"};
  1644. $cmd =~ s/(\$\w+)/$1/eeg; #substitute variables in $cmd
  1645. my @lines = ();
  1646. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  1647. if (!$email_git_penguin_chiefs) {
  1648. @lines = grep(!/${penguin_chiefs}/i, @lines);
  1649. }
  1650. last if !@lines;
  1651. my @authors = ();
  1652. foreach my $line (@lines) {
  1653. if ($line =~ m/$VCS_cmds{"author_pattern"}/) {
  1654. my $author = $1;
  1655. $author = deduplicate_email($author);
  1656. push(@authors, $author);
  1657. }
  1658. }
  1659. save_commits_by_author(@lines) if ($interactive);
  1660. save_commits_by_signer(@lines) if ($interactive);
  1661. push(@signers, @authors);
  1662. }}
  1663. else {
  1664. foreach my $commit (@commits) {
  1665. my $i;
  1666. my $cmd = $VCS_cmds{"find_commit_author_cmd"};
  1667. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  1668. my @author = vcs_find_author($cmd);
  1669. next if !@author;
  1670. my $formatted_author = deduplicate_email($author[0]);
  1671. my $count = grep(/$commit/, @all_commits);
  1672. for ($i = 0; $i < $count ; $i++) {
  1673. push(@blame_signers, $formatted_author);
  1674. }
  1675. }
  1676. }
  1677. if (@blame_signers) {
  1678. vcs_assign("authored lines", $total_lines, @blame_signers);
  1679. }
  1680. }
  1681. foreach my $signer (@signers) {
  1682. $signer = deduplicate_email($signer);
  1683. }
  1684. vcs_assign("commits", $total_commits, @signers);
  1685. } else {
  1686. foreach my $signer (@signers) {
  1687. $signer = deduplicate_email($signer);
  1688. }
  1689. vcs_assign("modified commits", $total_commits, @signers);
  1690. }
  1691. }
  1692. sub uniq {
  1693. my (@parms) = @_;
  1694. my %saw;
  1695. @parms = grep(!$saw{$_}++, @parms);
  1696. return @parms;
  1697. }
  1698. s