get_maintainer.pl 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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.25';
  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_fallback = 1;
  27. my $email_git_min_signatures = 1;
  28. my $email_git_max_maintainers = 5;
  29. my $email_git_min_percent = 5;
  30. my $email_git_since = "1-year-ago";
  31. my $email_hg_since = "-365";
  32. my $interactive = 0;
  33. my $email_remove_duplicates = 1;
  34. my $output_multiline = 1;
  35. my $output_separator = ", ";
  36. my $output_roles = 0;
  37. my $output_rolestats = 0;
  38. my $scm = 0;
  39. my $web = 0;
  40. my $subsystem = 0;
  41. my $status = 0;
  42. my $keywords = 1;
  43. my $sections = 0;
  44. my $file_emails = 0;
  45. my $from_filename = 0;
  46. my $pattern_depth = 0;
  47. my $version = 0;
  48. my $help = 0;
  49. my $exit = 0;
  50. my %shortlog_buffer;
  51. my @penguin_chief = ();
  52. push(@penguin_chief, "Linus Torvalds:torvalds\@linux-foundation.org");
  53. #Andrew wants in on most everything - 2009/01/14
  54. #push(@penguin_chief, "Andrew Morton:akpm\@linux-foundation.org");
  55. my @penguin_chief_names = ();
  56. foreach my $chief (@penguin_chief) {
  57. if ($chief =~ m/^(.*):(.*)/) {
  58. my $chief_name = $1;
  59. my $chief_addr = $2;
  60. push(@penguin_chief_names, $chief_name);
  61. }
  62. }
  63. my $penguin_chiefs = "\(" . join("|", @penguin_chief_names) . "\)";
  64. # Signature types of people who are either
  65. # a) responsible for the code in question, or
  66. # b) familiar enough with it to give relevant feedback
  67. my @signature_tags = ();
  68. push(@signature_tags, "Signed-off-by:");
  69. push(@signature_tags, "Reviewed-by:");
  70. push(@signature_tags, "Acked-by:");
  71. my $signaturePattern = "\(" . join("|", @signature_tags) . "\)";
  72. # rfc822 email address - preloaded methods go here.
  73. my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
  74. my $rfc822_char = '[\\000-\\377]';
  75. # VCS command support: class-like functions and strings
  76. my %VCS_cmds;
  77. my %VCS_cmds_git = (
  78. "execute_cmd" => \&git_execute_cmd,
  79. "available" => '(which("git") ne "") && (-d ".git")',
  80. "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file",
  81. "find_commit_signers_cmd" => "git log --no-color -1 \$commit",
  82. "find_commit_author_cmd" => "git log -1 --format=\"%an <%ae>\" \$commit",
  83. "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
  84. "blame_file_cmd" => "git blame -l \$file",
  85. "commit_pattern" => "^commit [0-9a-f]{40,40}",
  86. "blame_commit_pattern" => "^([0-9a-f]+) ",
  87. "shortlog_cmd" => "git log --no-color --oneline --since=\$email_git_since --author=\"\$email\" -- \$file"
  88. );
  89. my %VCS_cmds_hg = (
  90. "execute_cmd" => \&hg_execute_cmd,
  91. "available" => '(which("hg") ne "") && (-d ".hg")',
  92. "find_signers_cmd" =>
  93. "hg log --date=\$email_hg_since" .
  94. " --template='commit {node}\\n{desc}\\n' -- \$file",
  95. "find_commit_signers_cmd" => "hg log --template='{desc}\\n' -r \$commit",
  96. "find_commit_author_cmd" => "hg log -l 1 --template='{author}\\n' -r \$commit",
  97. "blame_range_cmd" => "", # not supported
  98. "blame_file_cmd" => "hg blame -c \$file",
  99. "commit_pattern" => "^commit [0-9a-f]{40,40}",
  100. "blame_commit_pattern" => "^([0-9a-f]+):",
  101. "shortlog_cmd" => "ht log --date=\$email_hg_since"
  102. );
  103. my $conf = which_conf(".get_maintainer.conf");
  104. if (-f $conf) {
  105. my @conf_args;
  106. open(my $conffile, '<', "$conf")
  107. or warn "$P: Can't find a readable .get_maintainer.conf file $!\n";
  108. while (<$conffile>) {
  109. my $line = $_;
  110. $line =~ s/\s*\n?$//g;
  111. $line =~ s/^\s*//g;
  112. $line =~ s/\s+/ /g;
  113. next if ($line =~ m/^\s*#/);
  114. next if ($line =~ m/^\s*$/);
  115. my @words = split(" ", $line);
  116. foreach my $word (@words) {
  117. last if ($word =~ m/^#/);
  118. push (@conf_args, $word);
  119. }
  120. }
  121. close($conffile);
  122. unshift(@ARGV, @conf_args) if @conf_args;
  123. }
  124. if (!GetOptions(
  125. 'email!' => \$email,
  126. 'git!' => \$email_git,
  127. 'git-all-signature-types!' => \$email_git_all_signature_types,
  128. 'git-blame!' => \$email_git_blame,
  129. 'git-fallback!' => \$email_git_fallback,
  130. 'git-chief-penguins!' => \$email_git_penguin_chiefs,
  131. 'git-min-signatures=i' => \$email_git_min_signatures,
  132. 'git-max-maintainers=i' => \$email_git_max_maintainers,
  133. 'git-min-percent=i' => \$email_git_min_percent,
  134. 'git-since=s' => \$email_git_since,
  135. 'hg-since=s' => \$email_hg_since,
  136. 'i|interactive!' => \$interactive,
  137. 'remove-duplicates!' => \$email_remove_duplicates,
  138. 'm!' => \$email_maintainer,
  139. 'n!' => \$email_usename,
  140. 'l!' => \$email_list,
  141. 's!' => \$email_subscriber_list,
  142. 'multiline!' => \$output_multiline,
  143. 'roles!' => \$output_roles,
  144. 'rolestats!' => \$output_rolestats,
  145. 'separator=s' => \$output_separator,
  146. 'subsystem!' => \$subsystem,
  147. 'status!' => \$status,
  148. 'scm!' => \$scm,
  149. 'web!' => \$web,
  150. 'pattern-depth=i' => \$pattern_depth,
  151. 'k|keywords!' => \$keywords,
  152. 'sections!' => \$sections,
  153. 'fe|file-emails!' => \$file_emails,
  154. 'f|file' => \$from_filename,
  155. 'v|version' => \$version,
  156. 'h|help|usage' => \$help,
  157. )) {
  158. die "$P: invalid argument - use --help if necessary\n";
  159. }
  160. if ($help != 0) {
  161. usage();
  162. exit 0;
  163. }
  164. if ($version != 0) {
  165. print("${P} ${V}\n");
  166. exit 0;
  167. }
  168. if (-t STDIN && !@ARGV) {
  169. # We're talking to a terminal, but have no command line arguments.
  170. die "$P: missing patchfile or -f file - use --help if necessary\n";
  171. }
  172. if ($output_separator ne ", ") {
  173. $output_multiline = 0;
  174. }
  175. if ($output_rolestats) {
  176. $output_roles = 1;
  177. }
  178. if ($sections) {
  179. $email = 0;
  180. $email_list = 0;
  181. $scm = 0;
  182. $status = 0;
  183. $subsystem = 0;
  184. $web = 0;
  185. $keywords = 0;
  186. } else {
  187. my $selections = $email + $scm + $status + $subsystem + $web;
  188. if ($selections == 0) {
  189. die "$P: Missing required option: email, scm, status, subsystem or web\n";
  190. }
  191. }
  192. if ($email &&
  193. ($email_maintainer + $email_list + $email_subscriber_list +
  194. $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
  195. die "$P: Please select at least 1 email option\n";
  196. }
  197. if (!top_of_kernel_tree($lk_path)) {
  198. die "$P: The current directory does not appear to be "
  199. . "a linux kernel source tree.\n";
  200. }
  201. if ($email_git_all_signature_types) {
  202. $signaturePattern = "(.+?)[Bb][Yy]:";
  203. }
  204. ## Read MAINTAINERS for type/value pairs
  205. my @typevalue = ();
  206. my %keyword_hash;
  207. open (my $maint, '<', "${lk_path}MAINTAINERS")
  208. or die "$P: Can't open MAINTAINERS: $!\n";
  209. while (<$maint>) {
  210. my $line = $_;
  211. if ($line =~ m/^(\C):\s*(.*)/) {
  212. my $type = $1;
  213. my $value = $2;
  214. ##Filename pattern matching
  215. if ($type eq "F" || $type eq "X") {
  216. $value =~ s@\.@\\\.@g; ##Convert . to \.
  217. $value =~ s/\*/\.\*/g; ##Convert * to .*
  218. $value =~ s/\?/\./g; ##Convert ? to .
  219. ##if pattern is a directory and it lacks a trailing slash, add one
  220. if ((-d $value)) {
  221. $value =~ s@([^/])$@$1/@;
  222. }
  223. } elsif ($type eq "K") {
  224. $keyword_hash{@typevalue} = $value;
  225. }
  226. push(@typevalue, "$type:$value");
  227. } elsif (!/^(\s)*$/) {
  228. $line =~ s/\n$//g;
  229. push(@typevalue, $line);
  230. }
  231. }
  232. close($maint);
  233. my %mailmap;
  234. if ($email_remove_duplicates) {
  235. open(my $mailmap, '<', "${lk_path}.mailmap")
  236. or warn "$P: Can't open .mailmap: $!\n";
  237. while (<$mailmap>) {
  238. my $line = $_;
  239. next if ($line =~ m/^\s*#/);
  240. next if ($line =~ m/^\s*$/);
  241. my ($name, $address) = parse_email($line);
  242. $line = format_email($name, $address, $email_usename);
  243. next if ($line =~ m/^\s*$/);
  244. if (exists($mailmap{$name})) {
  245. my $obj = $mailmap{$name};
  246. push(@$obj, $address);
  247. } else {
  248. my @arr = ($address);
  249. $mailmap{$name} = \@arr;
  250. }
  251. }
  252. close($mailmap);
  253. }
  254. ## use the filenames on the command line or find the filenames in the patchfiles
  255. my @files = ();
  256. my @range = ();
  257. my @keyword_tvi = ();
  258. my @file_emails = ();
  259. if (!@ARGV) {
  260. push(@ARGV, "&STDIN");
  261. }
  262. foreach my $file (@ARGV) {
  263. if ($file ne "&STDIN") {
  264. ##if $file is a directory and it lacks a trailing slash, add one
  265. if ((-d $file)) {
  266. $file =~ s@([^/])$@$1/@;
  267. } elsif (!(-f $file)) {
  268. die "$P: file '${file}' not found\n";
  269. }
  270. }
  271. if ($from_filename) {
  272. push(@files, $file);
  273. if ($file ne "MAINTAINERS" && -f $file && ($keywords || $file_emails)) {
  274. open(my $f, '<', $file)
  275. or die "$P: Can't open $file: $!\n";
  276. my $text = do { local($/) ; <$f> };
  277. close($f);
  278. if ($keywords) {
  279. foreach my $line (keys %keyword_hash) {
  280. if ($text =~ m/$keyword_hash{$line}/x) {
  281. push(@keyword_tvi, $line);
  282. }
  283. }
  284. }
  285. if ($file_emails) {
  286. 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;
  287. push(@file_emails, clean_file_emails(@poss_addr));
  288. }
  289. }
  290. } else {
  291. my $file_cnt = @files;
  292. my $lastfile;
  293. open(my $patch, "< $file")
  294. or die "$P: Can't open $file: $!\n";
  295. while (<$patch>) {
  296. my $patch_line = $_;
  297. if (m/^\+\+\+\s+(\S+)/) {
  298. my $filename = $1;
  299. $filename =~ s@^[^/]*/@@;
  300. $filename =~ s@\n@@;
  301. $lastfile = $filename;
  302. push(@files, $filename);
  303. } elsif (m/^\@\@ -(\d+),(\d+)/) {
  304. if ($email_git_blame) {
  305. push(@range, "$lastfile:$1:$2");
  306. }
  307. } elsif ($keywords) {
  308. foreach my $line (keys %keyword_hash) {
  309. if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
  310. push(@keyword_tvi, $line);
  311. }
  312. }
  313. }
  314. }
  315. close($patch);
  316. if ($file_cnt == @files) {
  317. warn "$P: file '${file}' doesn't appear to be a patch. "
  318. . "Add -f to options?\n";
  319. }
  320. @files = sort_and_uniq(@files);
  321. }
  322. }
  323. @file_emails = uniq(@file_emails);
  324. my @email_to = ();
  325. my @list_to = ();
  326. my @scm = ();
  327. my @web = ();
  328. my @subsystem = ();
  329. my @status = ();
  330. # Find responsible parties
  331. foreach my $file (@files) {
  332. my %hash;
  333. my $exact_pattern_match = 0;
  334. my $tvi = find_first_section();
  335. while ($tvi < @typevalue) {
  336. my $start = find_starting_index($tvi);
  337. my $end = find_ending_index($tvi);
  338. my $exclude = 0;
  339. my $i;
  340. #Do not match excluded file patterns
  341. for ($i = $start; $i < $end; $i++) {
  342. my $line = $typevalue[$i];
  343. if ($line =~ m/^(\C):\s*(.*)/) {
  344. my $type = $1;
  345. my $value = $2;
  346. if ($type eq 'X') {
  347. if (file_match_pattern($file, $value)) {
  348. $exclude = 1;
  349. last;
  350. }
  351. }
  352. }
  353. }
  354. if (!$exclude) {
  355. for ($i = $start; $i < $end; $i++) {
  356. my $line = $typevalue[$i];
  357. if ($line =~ m/^(\C):\s*(.*)/) {
  358. my $type = $1;
  359. my $value = $2;
  360. if ($type eq 'F') {
  361. if (file_match_pattern($file, $value)) {
  362. my $value_pd = ($value =~ tr@/@@);
  363. my $file_pd = ($file =~ tr@/@@);
  364. $value_pd++ if (substr($value,-1,1) ne "/");
  365. $value_pd = -1 if ($value =~ /^\.\*/);
  366. $exact_pattern_match = 1 if ($value_pd >= $file_pd);
  367. if ($pattern_depth == 0 ||
  368. (($file_pd - $value_pd) < $pattern_depth)) {
  369. $hash{$tvi} = $value_pd;
  370. }
  371. }
  372. }
  373. }
  374. }
  375. }
  376. $tvi = $end + 1;
  377. }
  378. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  379. add_categories($line);
  380. if ($sections) {
  381. my $i;
  382. my $start = find_starting_index($line);
  383. my $end = find_ending_index($line);
  384. for ($i = $start; $i < $end; $i++) {
  385. my $line = $typevalue[$i];
  386. if ($line =~ /^[FX]:/) { ##Restore file patterns
  387. $line =~ s/([^\\])\.([^\*])/$1\?$2/g;
  388. $line =~ s/([^\\])\.$/$1\?/g; ##Convert . back to ?
  389. $line =~ s/\\\./\./g; ##Convert \. to .
  390. $line =~ s/\.\*/\*/g; ##Convert .* to *
  391. }
  392. $line =~ s/^([A-Z]):/$1:\t/g;
  393. print("$line\n");
  394. }
  395. print("\n");
  396. }
  397. }
  398. if ($email &&
  399. ($email_git || ($email_git_fallback && !$exact_pattern_match))) {
  400. vcs_file_signoffs($file);
  401. }
  402. if ($email && $email_git_blame) {
  403. vcs_file_blame($file);
  404. }
  405. if ($email && $interactive){
  406. vcs_file_shortlogs($file);
  407. }
  408. }
  409. if ($keywords) {
  410. @keyword_tvi = sort_and_uniq(@keyword_tvi);
  411. foreach my $line (@keyword_tvi) {
  412. add_categories($line);
  413. }
  414. }
  415. if ($email) {
  416. foreach my $chief (@penguin_chief) {
  417. if ($chief =~ m/^(.*):(.*)/) {
  418. my $email_address;
  419. $email_address = format_email($1, $2, $email_usename);
  420. if ($email_git_penguin_chiefs) {
  421. push(@email_to, [$email_address, 'chief penguin']);
  422. } else {
  423. @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
  424. }
  425. }
  426. }
  427. foreach my $email (@file_emails) {
  428. my ($name, $address) = parse_email($email);
  429. my $tmp_email = format_email($name, $address, $email_usename);
  430. push_email_address($tmp_email, '');
  431. add_role($tmp_email, 'in file');
  432. }
  433. }
  434. if ($email || $email_list) {
  435. my @to = ();
  436. if ($email) {
  437. if ($interactive) {
  438. @email_to = @{vcs_interactive_menu(\@email_to)};
  439. }
  440. @to = (@to, @email_to);
  441. }
  442. if ($email_list) {
  443. @to = (@to, @list_to);
  444. }
  445. output(merge_email(@to));
  446. }
  447. if ($scm) {
  448. @scm = uniq(@scm);
  449. output(@scm);
  450. }
  451. if ($status) {
  452. @status = uniq(@status);
  453. output(@status);
  454. }
  455. if ($subsystem) {
  456. @subsystem = uniq(@subsystem);
  457. output(@subsystem);
  458. }
  459. if ($web) {
  460. @web = uniq(@web);
  461. output(@web);
  462. }
  463. exit($exit);
  464. sub file_match_pattern {
  465. my ($file, $pattern) = @_;
  466. if (substr($pattern, -1) eq "/") {
  467. if ($file =~ m@^$pattern@) {
  468. return 1;
  469. }
  470. } else {
  471. if ($file =~ m@^$pattern@) {
  472. my $s1 = ($file =~ tr@/@@);
  473. my $s2 = ($pattern =~ tr@/@@);
  474. if ($s1 == $s2) {
  475. return 1;
  476. }
  477. }
  478. }
  479. return 0;
  480. }
  481. sub usage {
  482. print <<EOT;
  483. usage: $P [options] patchfile
  484. $P [options] -f file|directory
  485. version: $V
  486. MAINTAINER field selection options:
  487. --email => print email address(es) if any
  488. --git => include recent git \*-by: signers
  489. --git-all-signature-types => include signers regardless of signature type
  490. or use only ${signaturePattern} signers (default: $email_git_all_signature_types)
  491. --git-fallback => use git when no exact MAINTAINERS pattern (default: $email_git_fallback)
  492. --git-chief-penguins => include ${penguin_chiefs}
  493. --git-min-signatures => number of signatures required (default: $email_git_min_signatures)
  494. --git-max-maintainers => maximum maintainers to add (default: $email_git_max_maintainers)
  495. --git-min-percent => minimum percentage of commits required (default: $email_git_min_percent)
  496. --git-blame => use git blame to find modified commits for patch or file
  497. --git-since => git history to use (default: $email_git_since)
  498. --hg-since => hg history to use (default: $email_hg_since)
  499. --interactive => display a menu (mostly useful if used with the --git option)
  500. --m => include maintainer(s) if any
  501. --n => include name 'Full Name <addr\@domain.tld>'
  502. --l => include list(s) if any
  503. --s => include subscriber only list(s) if any
  504. --remove-duplicates => minimize duplicate email names/addresses
  505. --roles => show roles (status:subsystem, git-signer, list, etc...)
  506. --rolestats => show roles and statistics (commits/total_commits, %)
  507. --file-emails => add email addresses found in -f file (default: 0 (off))
  508. --scm => print SCM tree(s) if any
  509. --status => print status if any
  510. --subsystem => print subsystem name if any
  511. --web => print website(s) if any
  512. Output type options:
  513. --separator [, ] => separator for multiple entries on 1 line
  514. using --separator also sets --nomultiline if --separator is not [, ]
  515. --multiline => print 1 entry per line
  516. Other options:
  517. --pattern-depth => Number of pattern directory traversals (default: 0 (all))
  518. --keywords => scan patch for keywords (default: 1 (on))
  519. --sections => print the entire subsystem sections with pattern matches
  520. --version => show version
  521. --help => show this help information
  522. Default options:
  523. [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
  524. Notes:
  525. Using "-f directory" may give unexpected results:
  526. Used with "--git", git signators for _all_ files in and below
  527. directory are examined as git recurses directories.
  528. Any specified X: (exclude) pattern matches are _not_ ignored.
  529. Used with "--nogit", directory is used as a pattern match,
  530. no individual file within the directory or subdirectory
  531. is matched.
  532. Used with "--git-blame", does not iterate all files in directory
  533. Using "--git-blame" is slow and may add old committers and authors
  534. that are no longer active maintainers to the output.
  535. Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
  536. other automated tools that expect only ["name"] <email address>
  537. may not work because of additional output after <email address>.
  538. Using "--rolestats" and "--git-blame" shows the #/total=% commits,
  539. not the percentage of the entire file authored. # of commits is
  540. not a good measure of amount of code authored. 1 major commit may
  541. contain a thousand lines, 5 trivial commits may modify a single line.
  542. If git is not installed, but mercurial (hg) is installed and an .hg
  543. repository exists, the following options apply to mercurial:
  544. --git,
  545. --git-min-signatures, --git-max-maintainers, --git-min-percent, and
  546. --git-blame
  547. Use --hg-since not --git-since to control date selection
  548. File ".get_maintainer.conf", if it exists in the linux kernel source root
  549. directory, can change whatever get_maintainer defaults are desired.
  550. Entries in this file can be any command line argument.
  551. This file is prepended to any additional command line arguments.
  552. Multiple lines and # comments are allowed.
  553. EOT
  554. }
  555. sub top_of_kernel_tree {
  556. my ($lk_path) = @_;
  557. if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
  558. $lk_path .= "/";
  559. }
  560. if ( (-f "${lk_path}COPYING")
  561. && (-f "${lk_path}CREDITS")
  562. && (-f "${lk_path}Kbuild")
  563. && (-f "${lk_path}MAINTAINERS")
  564. && (-f "${lk_path}Makefile")
  565. && (-f "${lk_path}README")
  566. && (-d "${lk_path}Documentation")
  567. && (-d "${lk_path}arch")
  568. && (-d "${lk_path}include")
  569. && (-d "${lk_path}drivers")
  570. && (-d "${lk_path}fs")
  571. && (-d "${lk_path}init")
  572. && (-d "${lk_path}ipc")
  573. && (-d "${lk_path}kernel")
  574. && (-d "${lk_path}lib")
  575. && (-d "${lk_path}scripts")) {
  576. return 1;
  577. }
  578. return 0;
  579. }
  580. sub parse_email {
  581. my ($formatted_email) = @_;
  582. my $name = "";
  583. my $address = "";
  584. if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
  585. $name = $1;
  586. $address = $2;
  587. } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
  588. $address = $1;
  589. } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
  590. $address = $1;
  591. }
  592. $name =~ s/^\s+|\s+$//g;
  593. $name =~ s/^\"|\"$//g;
  594. $address =~ s/^\s+|\s+$//g;
  595. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  596. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  597. $name = "\"$name\"";
  598. }
  599. return ($name, $address);
  600. }
  601. sub format_email {
  602. my ($name, $address, $usename) = @_;
  603. my $formatted_email;
  604. $name =~ s/^\s+|\s+$//g;
  605. $name =~ s/^\"|\"$//g;
  606. $address =~ s/^\s+|\s+$//g;
  607. if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
  608. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  609. $name = "\"$name\"";
  610. }
  611. if ($usename) {
  612. if ("$name" eq "") {
  613. $formatted_email = "$address";
  614. } else {
  615. $formatted_email = "$name <$address>";
  616. }
  617. } else {
  618. $formatted_email = $address;
  619. }
  620. return $formatted_email;
  621. }
  622. sub find_first_section {
  623. my $index = 0;
  624. while ($index < @typevalue) {
  625. my $tv = $typevalue[$index];
  626. if (($tv =~ m/^(\C):\s*(.*)/)) {
  627. last;
  628. }
  629. $index++;
  630. }
  631. return $index;
  632. }
  633. sub find_starting_index {
  634. my ($index) = @_;
  635. while ($index > 0) {
  636. my $tv = $typevalue[$index];
  637. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  638. last;
  639. }
  640. $index--;
  641. }
  642. return $index;
  643. }
  644. sub find_ending_index {
  645. my ($index) = @_;
  646. while ($index < @typevalue) {
  647. my $tv = $typevalue[$index];
  648. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  649. last;
  650. }
  651. $index++;
  652. }
  653. return $index;
  654. }
  655. sub get_maintainer_role {
  656. my ($index) = @_;
  657. my $i;
  658. my $start = find_starting_index($index);
  659. my $end = find_ending_index($index);
  660. my $role;
  661. my $subsystem = $typevalue[$start];
  662. if (length($subsystem) > 20) {
  663. $subsystem = substr($subsystem, 0, 17);
  664. $subsystem =~ s/\s*$//;
  665. $subsystem = $subsystem . "...";
  666. }
  667. for ($i = $start + 1; $i < $end; $i++) {
  668. my $tv = $typevalue[$i];
  669. if ($tv =~ m/^(\C):\s*(.*)/) {
  670. my $ptype = $1;
  671. my $pvalue = $2;
  672. if ($ptype eq "S") {
  673. $role = $pvalue;
  674. }
  675. }
  676. }
  677. $role = lc($role);
  678. if ($role eq "supported") {
  679. $role = "supporter";
  680. } elsif ($role eq "maintained") {
  681. $role = "maintainer";
  682. } elsif ($role eq "odd fixes") {
  683. $role = "odd fixer";
  684. } elsif ($role eq "orphan") {
  685. $role = "orphan minder";
  686. } elsif ($role eq "obsolete") {
  687. $role = "obsolete minder";
  688. } elsif ($role eq "buried alive in reporters") {
  689. $role = "chief penguin";
  690. }
  691. return $role . ":" . $subsystem;
  692. }
  693. sub get_list_role {
  694. my ($index) = @_;
  695. my $i;
  696. my $start = find_starting_index($index);
  697. my $end = find_ending_index($index);
  698. my $subsystem = $typevalue[$start];
  699. if (length($subsystem) > 20) {
  700. $subsystem = substr($subsystem, 0, 17);
  701. $subsystem =~ s/\s*$//;
  702. $subsystem = $subsystem . "...";
  703. }
  704. if ($subsystem eq "THE REST") {
  705. $subsystem = "";
  706. }
  707. return $subsystem;
  708. }
  709. sub add_categories {
  710. my ($index) = @_;
  711. my $i;
  712. my $start = find_starting_index($index);
  713. my $end = find_ending_index($index);
  714. push(@subsystem, $typevalue[$start]);
  715. for ($i = $start + 1; $i < $end; $i++) {
  716. my $tv = $typevalue[$i];
  717. if ($tv =~ m/^(\C):\s*(.*)/) {
  718. my $ptype = $1;
  719. my $pvalue = $2;
  720. if ($ptype eq "L") {
  721. my $list_address = $pvalue;
  722. my $list_additional = "";
  723. my $list_role = get_list_role($i);
  724. if ($list_role ne "") {
  725. $list_role = ":" . $list_role;
  726. }
  727. if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
  728. $list_address = $1;
  729. $list_additional = $2;
  730. }
  731. if ($list_additional =~ m/subscribers-only/) {
  732. if ($email_subscriber_list) {
  733. push(@list_to, [$list_address, "subscriber list${list_role}"]);
  734. }
  735. } else {
  736. if ($email_list) {
  737. push(@list_to, [$list_address, "open list${list_role}"]);
  738. }
  739. }
  740. } elsif ($ptype eq "M") {
  741. my ($name, $address) = parse_email($pvalue);
  742. if ($name eq "") {
  743. if ($i > 0) {
  744. my $tv = $typevalue[$i - 1];
  745. if ($tv =~ m/^(\C):\s*(.*)/) {
  746. if ($1 eq "P") {
  747. $name = $2;
  748. $pvalue = format_email($name, $address, $email_usename);
  749. }
  750. }
  751. }
  752. }
  753. if ($email_maintainer) {
  754. my $role = get_maintainer_role($i);
  755. push_email_addresses($pvalue, $role);
  756. }
  757. } elsif ($ptype eq "T") {
  758. push(@scm, $pvalue);
  759. } elsif ($ptype eq "W") {
  760. push(@web, $pvalue);
  761. } elsif ($ptype eq "S") {
  762. push(@status, $pvalue);
  763. }
  764. }
  765. }
  766. }
  767. my %email_hash_name;
  768. my %email_hash_address;
  769. sub email_inuse {
  770. my ($name, $address) = @_;
  771. return 1 if (($name eq "") && ($address eq ""));
  772. return 1 if (($name ne "") && exists($email_hash_name{$name}));
  773. return 1 if (($address ne "") && exists($email_hash_address{$address}));
  774. return 0;
  775. }
  776. sub push_email_address {
  777. my ($line, $role) = @_;
  778. my ($name, $address) = parse_email($line);
  779. if ($address eq "") {
  780. return 0;
  781. }
  782. if (!$email_remove_duplicates) {
  783. push(@email_to, [format_email($name, $address, $email_usename), $role]);
  784. } elsif (!email_inuse($name, $address)) {
  785. push(@email_to, [format_email($name, $address, $email_usename), $role]);
  786. $email_hash_name{$name}++;
  787. $email_hash_address{$address}++;
  788. }
  789. return 1;
  790. }
  791. sub push_email_addresses {
  792. my ($address, $role) = @_;
  793. my @address_list = ();
  794. if (rfc822_valid($address)) {
  795. push_email_address($address, $role);
  796. } elsif (@address_list = rfc822_validlist($address)) {
  797. my $array_count = shift(@address_list);
  798. while (my $entry = shift(@address_list)) {
  799. push_email_address($entry, $role);
  800. }
  801. } else {
  802. if (!push_email_address($address, $role)) {
  803. warn("Invalid MAINTAINERS address: '" . $address . "'\n");
  804. }
  805. }
  806. }
  807. sub add_role {
  808. my ($line, $role) = @_;
  809. my ($name, $address) = parse_email($line);
  810. my $email = format_email($name, $address, $email_usename);
  811. foreach my $entry (@email_to) {
  812. if ($email_remove_duplicates) {
  813. my ($entry_name, $entry_address) = parse_email($entry->[0]);
  814. if (($name eq $entry_name || $address eq $entry_address)
  815. && ($role eq "" || !($entry->[1] =~ m/$role/))
  816. ) {
  817. if ($entry->[1] eq "") {
  818. $entry->[1] = "$role";
  819. } else {
  820. $entry->[1] = "$entry->[1],$role";
  821. }
  822. }
  823. } else {
  824. if ($email eq $entry->[0]
  825. && ($role eq "" || !($entry->[1] =~ m/$role/))
  826. ) {
  827. if ($entry->[1] eq "") {
  828. $entry->[1] = "$role";
  829. } else {
  830. $entry->[1] = "$entry->[1],$role";
  831. }
  832. }
  833. }
  834. }
  835. }
  836. sub which {
  837. my ($bin) = @_;
  838. foreach my $path (split(/:/, $ENV{PATH})) {
  839. if (-e "$path/$bin") {
  840. return "$path/$bin";
  841. }
  842. }
  843. return "";
  844. }
  845. sub which_conf {
  846. my ($conf) = @_;
  847. foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
  848. if (-e "$path/$conf") {
  849. return "$path/$conf";
  850. }
  851. }
  852. return "";
  853. }
  854. sub mailmap {
  855. my (@lines) = @_;
  856. my %hash;
  857. foreach my $line (@lines) {
  858. my ($name, $address) = parse_email($line);
  859. if (!exists($hash{$name})) {
  860. $hash{$name} = $address;
  861. } elsif ($address ne $hash{$name}) {
  862. $address = $hash{$name};
  863. $line = format_email($name, $address, $email_usename);
  864. }
  865. if (exists($mailmap{$name})) {
  866. my $obj = $mailmap{$name};
  867. foreach my $map_address (@$obj) {
  868. if (($map_address eq $address) &&
  869. ($map_address ne $hash{$name})) {
  870. $line = format_email($name, $hash{$name}, $email_usename);
  871. }
  872. }
  873. }
  874. }
  875. return @lines;
  876. }
  877. sub git_execute_cmd {
  878. my ($cmd) = @_;
  879. my @lines = ();
  880. my $output = `$cmd`;
  881. $output =~ s/^\s*//gm;
  882. @lines = split("\n", $output);
  883. return @lines;
  884. }
  885. sub hg_execute_cmd {
  886. my ($cmd) = @_;
  887. my @lines = ();
  888. my $output = `$cmd`;
  889. @lines = split("\n", $output);
  890. return @lines;
  891. }
  892. sub vcs_find_signers {
  893. my ($cmd) = @_;
  894. my @lines = ();
  895. my $commits;
  896. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  897. my $pattern = $VCS_cmds{"commit_pattern"};
  898. $commits = grep(/$pattern/, @lines); # of commits
  899. @lines = grep(/^[ \t]*${signaturePattern}.*\@.*$/, @lines);
  900. if (!$email_git_penguin_chiefs) {
  901. @lines = grep(!/${penguin_chiefs}/i, @lines);
  902. }
  903. return (0, @lines) if !@lines;
  904. # cut -f2- -d":"
  905. s/.*:\s*(.+)\s*/$1/ for (@lines);
  906. ## Reformat email addresses (with names) to avoid badly written signatures
  907. foreach my $line (@lines) {
  908. my ($name, $address) = parse_email($line);
  909. $line = format_email($name, $address, 1);
  910. }
  911. return ($commits, @lines);
  912. }
  913. sub vcs_find_author {
  914. my ($cmd) = @_;
  915. my @lines = ();
  916. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  917. if (!$email_git_penguin_chiefs) {
  918. @lines = grep(!/${penguin_chiefs}/i, @lines);
  919. }
  920. return @lines if !@lines;
  921. ## Reformat email addresses (with names) to avoid badly written signatures
  922. foreach my $line (@lines) {
  923. my ($name, $address) = parse_email($line);
  924. $line = format_email($name, $address, 1);
  925. }
  926. return @lines;
  927. }
  928. sub vcs_save_commits {
  929. my ($cmd) = @_;
  930. my @lines = ();
  931. my @commits = ();
  932. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  933. foreach my $line (@lines) {
  934. if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
  935. push(@commits, $1);
  936. }
  937. }
  938. return @commits;
  939. }
  940. sub vcs_blame {
  941. my ($file) = @_;
  942. my $cmd;
  943. my @commits = ();
  944. return @commits if (!(-f $file));
  945. if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
  946. my @all_commits = ();
  947. $cmd = $VCS_cmds{"blame_file_cmd"};
  948. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  949. @all_commits = vcs_save_commits($cmd);
  950. foreach my $file_range_diff (@range) {
  951. next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
  952. my $diff_file = $1;
  953. my $diff_start = $2;
  954. my $diff_length = $3;
  955. next if ("$file" ne "$diff_file");
  956. for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
  957. push(@commits, $all_commits[$i]);
  958. }
  959. }
  960. } elsif (@range) {
  961. foreach my $file_range_diff (@range) {
  962. next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
  963. my $diff_file = $1;
  964. my $diff_start = $2;
  965. my $diff_length = $3;
  966. next if ("$file" ne "$diff_file");
  967. $cmd = $VCS_cmds{"blame_range_cmd"};
  968. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  969. push(@commits, vcs_save_commits($cmd));
  970. }
  971. } else {
  972. $cmd = $VCS_cmds{"blame_file_cmd"};
  973. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  974. @commits = vcs_save_commits($cmd);
  975. }
  976. foreach my $commit (@commits) {
  977. $commit =~ s/^\^//g;
  978. }
  979. return @commits;
  980. }
  981. my $printed_novcs = 0;
  982. sub vcs_exists {
  983. %VCS_cmds = %VCS_cmds_git;
  984. return 1 if eval $VCS_cmds{"available"};
  985. %VCS_cmds = %VCS_cmds_hg;
  986. return 1 if eval $VCS_cmds{"available"};
  987. %VCS_cmds = ();
  988. if (!$printed_novcs) {
  989. warn("$P: No supported VCS found. Add --nogit to options?\n");
  990. warn("Using a git repository produces better results.\n");
  991. warn("Try Linus Torvalds' latest git repository using:\n");
  992. warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
  993. $printed_novcs = 1;
  994. }
  995. return 0;
  996. }
  997. sub vcs_interactive_menu {
  998. my $list_ref = shift;
  999. my @list = @$list_ref;
  1000. return if (!vcs_exists());
  1001. my %selected;
  1002. my %shortlog;
  1003. my $input;
  1004. my $count = 0;
  1005. #select maintainers by default
  1006. foreach my $entry (@list){
  1007. my $role = $entry->[1];
  1008. $selected{$count} = ($role =~ /maintainer:|supporter:/);
  1009. $count++;
  1010. }
  1011. #menu loop
  1012. do {
  1013. my $count = 0;
  1014. foreach my $entry (@list){
  1015. my $email = $entry->[0];
  1016. my $role = $entry->[1];
  1017. if ($selected{$count}){
  1018. print STDERR "* ";
  1019. } else {
  1020. print STDERR " ";
  1021. }
  1022. print STDERR "$count: $email,\t\t $role";
  1023. print STDERR "\n";
  1024. if ($shortlog{$count}){
  1025. my $entries_ref = vcs_get_shortlog($email);
  1026. foreach my $entry_ref (@{$entries_ref}){
  1027. my $filename = @{$entry_ref}[0];
  1028. my @shortlog = @{@{$entry_ref}[1]};
  1029. print STDERR "\tshortlog for $filename (authored commits: " . @shortlog . ").\n";
  1030. foreach my $commit (@shortlog){
  1031. print STDERR "\t $commit\n";
  1032. }
  1033. print STDERR "\n";
  1034. }
  1035. }
  1036. $count++;
  1037. }
  1038. print STDERR "\n";
  1039. print STDERR "Choose whom to cc by entering a commaseperated list of numbers and hitting enter.\n";
  1040. print STDERR "To show a short list of commits, precede the number by a '?',\n";
  1041. print STDERR "A blank line indicates that you are satisfied with your choice.\n";
  1042. $input = <STDIN>;
  1043. chomp($input);
  1044. my @wish = split(/[, ]+/,$input);
  1045. foreach my $nr (@wish){
  1046. my $logtoggle = 0;
  1047. if ($nr =~ /\?/){
  1048. $nr =~ s/\?//;
  1049. $logtoggle = 1;
  1050. }
  1051. #skip out of bounds numbers
  1052. next unless ($nr <= $count && $nr >= 0);
  1053. if ($logtoggle){
  1054. $shortlog{$nr} = !$shortlog{$nr};
  1055. } else {
  1056. $selected{$nr} = !$selected{$nr};
  1057. #switch shortlog on if an entry get's selected
  1058. if ($selected{$nr}){
  1059. $shortlog{$nr}=1;
  1060. }
  1061. }
  1062. };
  1063. } while(length($input) > 0);
  1064. #drop not selected entries
  1065. $count = 0;
  1066. my @new_emailto;
  1067. foreach my $entry (@list){
  1068. if ($selected{$count}){
  1069. push(@new_emailto,$list[$count]);
  1070. print STDERR "$count: ";
  1071. print STDERR $email_to[$count]->[0];
  1072. print STDERR ",\t\t ";
  1073. print STDERR $email_to[$count]->[1];
  1074. print STDERR "\n";
  1075. }
  1076. $count++;
  1077. }
  1078. return \@new_emailto;
  1079. }
  1080. sub vcs_get_shortlog {
  1081. my $arg = shift;
  1082. my ($name, $address) = parse_email($arg);
  1083. return $shortlog_buffer{$address};
  1084. }
  1085. sub vcs_file_shortlogs {
  1086. my ($file) = @_;
  1087. print STDERR "shortlog processing $file:";
  1088. foreach my $entry (@email_to){
  1089. my ($name, $address) = parse_email($entry->[0]);
  1090. print STDERR ".";
  1091. my $commits_ref = vcs_email_shortlog($address, $file);
  1092. push(@{$shortlog_buffer{$address}}, [ $file, $commits_ref ]);
  1093. }
  1094. print STDERR "\n";
  1095. }
  1096. sub vcs_email_shortlog {
  1097. my $email = shift;
  1098. my ($file) = @_;
  1099. my $cmd = $VCS_cmds{"shortlog_cmd"};
  1100. $cmd =~ s/(\$\w+)/$1/eeg; #substitute variables
  1101. my @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  1102. return \@lines;
  1103. }
  1104. sub vcs_assign {
  1105. my ($role, $divisor, @lines) = @_;
  1106. my %hash;
  1107. my $count = 0;
  1108. return if (@lines <= 0);
  1109. if ($divisor <= 0) {
  1110. warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
  1111. $divisor = 1;
  1112. }
  1113. if ($email_remove_duplicates) {
  1114. @lines = mailmap(@lines);
  1115. }
  1116. return if (@lines <= 0);
  1117. @lines = sort(@lines);
  1118. # uniq -c
  1119. $hash{$_}++ for @lines;
  1120. # sort -rn
  1121. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  1122. my $sign_offs = $hash{$line};
  1123. my $percent = $sign_offs * 100 / $divisor;
  1124. $percent = 100 if ($percent > 100);
  1125. $count++;
  1126. last if ($sign_offs < $email_git_min_signatures ||
  1127. $count > $email_git_max_maintainers ||
  1128. $percent < $email_git_min_percent);
  1129. push_email_address($line, '');
  1130. if ($output_rolestats) {
  1131. my $fmt_percent = sprintf("%.0f", $percent);
  1132. add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
  1133. } else {
  1134. add_role($line, $role);
  1135. }
  1136. }
  1137. }
  1138. sub vcs_file_signoffs {
  1139. my ($file) = @_;
  1140. my @signers = ();
  1141. my $commits;
  1142. return if (!vcs_exists());
  1143. my $cmd = $VCS_cmds{"find_signers_cmd"};
  1144. $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
  1145. ($commits, @signers) = vcs_find_signers($cmd);
  1146. vcs_assign("commit_signer", $commits, @signers);
  1147. }
  1148. sub vcs_file_blame {
  1149. my ($file) = @_;
  1150. my @signers = ();
  1151. my @all_commits = ();
  1152. my @commits = ();
  1153. my $total_commits;
  1154. my $total_lines;
  1155. return if (!vcs_exists());
  1156. @all_commits = vcs_blame($file);
  1157. @commits = uniq(@all_commits);
  1158. $total_commits = @commits;
  1159. $total_lines = @all_commits;
  1160. foreach my $commit (@commits) {
  1161. my $commit_count;
  1162. my @commit_signers = ();
  1163. my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
  1164. $cmd =~ s/(\$\w+)/$1/eeg; #substitute variables in $cmd
  1165. ($commit_count, @commit_signers) = vcs_find_signers($cmd);
  1166. push(@signers, @commit_signers);
  1167. }
  1168. if ($from_filename) {
  1169. if ($output_rolestats) {
  1170. my @blame_signers;
  1171. foreach my $commit (@commits) {
  1172. my $i;
  1173. my $cmd = $VCS_cmds{"find_commit_author_cmd"};
  1174. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  1175. my @author = vcs_find_author($cmd);
  1176. next if !@author;
  1177. my $count = grep(/$commit/, @all_commits);
  1178. for ($i = 0; $i < $count ; $i++) {
  1179. push(@blame_signers, $author[0]);
  1180. }
  1181. }
  1182. if (@blame_signers) {
  1183. vcs_assign("authored lines", $total_lines, @blame_signers);
  1184. }
  1185. }
  1186. vcs_assign("commits", $total_commits, @signers);
  1187. } else {
  1188. vcs_assign("modified commits", $total_commits, @signers);
  1189. }
  1190. }
  1191. sub uniq {
  1192. my (@parms) = @_;
  1193. my %saw;
  1194. @parms = grep(!$saw{$_}++, @parms);
  1195. return @parms;
  1196. }
  1197. sub sort_and_uniq {
  1198. my (@parms) = @_;
  1199. my %saw;
  1200. @parms = sort @parms;
  1201. @parms = grep(!$saw{$_}++, @parms);
  1202. return @parms;
  1203. }
  1204. sub clean_file_emails {
  1205. my (@file_emails) = @_;
  1206. my @fmt_emails = ();
  1207. foreach my $email (@file_emails) {
  1208. $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
  1209. my ($name, $address) = parse_email($email);
  1210. if ($name eq '"[,\.]"') {
  1211. $name = "";
  1212. }
  1213. my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
  1214. if (@nw > 2) {
  1215. my $first = $nw[@nw - 3];
  1216. my $middle = $nw[@nw - 2];
  1217. my $last = $nw[@nw - 1];
  1218. if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
  1219. (length($first) == 2 && substr($first, -1) eq ".")) ||
  1220. (length($middle) == 1 ||
  1221. (length($middle) == 2 && substr($middle, -1) eq "."))) {
  1222. $name = "$first $middle $last";
  1223. } else {
  1224. $name = "$middle $last";
  1225. }
  1226. }
  1227. if (substr($name, -1) =~ /[,\.]/) {
  1228. $name = substr($name, 0, length($name) - 1);
  1229. } elsif (substr($name, -2) =~ /[,\.]"/) {
  1230. $name = substr($name, 0, length($name) - 2) . '"';
  1231. }
  1232. if (substr($name, 0, 1) =~ /[,\.]/) {
  1233. $name = substr($name, 1, length($name) - 1);
  1234. } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
  1235. $name = '"' . substr($name, 2, length($name) - 2);
  1236. }
  1237. my $fmt_email = format_email($name, $address, $email_usename);
  1238. push(@fmt_emails, $fmt_email);
  1239. }
  1240. return @fmt_emails;
  1241. }
  1242. sub merge_email {
  1243. my @lines;
  1244. my %saw;
  1245. for (@_) {
  1246. my ($address, $role) = @$_;
  1247. if (!$saw{$address}) {
  1248. if ($output_roles) {
  1249. push(@lines, "$address ($role)");
  1250. } else {
  1251. push(@lines, $address);
  1252. }
  1253. $saw{$address} = 1;
  1254. }
  1255. }
  1256. return @lines;
  1257. }
  1258. sub output {
  1259. my (@parms) = @_;
  1260. if ($output_multiline) {
  1261. foreach my $line (@parms) {
  1262. print("${line}\n");
  1263. }
  1264. } else {
  1265. print(join($output_separator, @parms));
  1266. print("\n");
  1267. }
  1268. }
  1269. my $rfc822re;
  1270. sub make_rfc822re {
  1271. # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
  1272. # comment. We must allow for rfc822_lwsp (or comments) after each of these.
  1273. # This regexp will only work on addresses which have had comments stripped
  1274. # and replaced with rfc822_lwsp.
  1275. my $specials = '()<>@,;:\\\\".\\[\\]';
  1276. my $controls = '\\000-\\037\\177';
  1277. my $dtext = "[^\\[\\]\\r\\\\]";
  1278. my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
  1279. my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
  1280. # Use zero-width assertion to spot the limit of an atom. A simple
  1281. # $rfc822_lwsp* causes the regexp engine to hang occasionally.
  1282. my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
  1283. my $word = "(?:$atom|$quoted_string)";
  1284. my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
  1285. my $sub_domain = "(?:$atom|$domain_literal)";
  1286. my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
  1287. my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
  1288. my $phrase = "$word*";
  1289. my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
  1290. my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
  1291. my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
  1292. my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
  1293. my $address = "(?:$mailbox|$group)";
  1294. return "$rfc822_lwsp*$address";
  1295. }
  1296. sub rfc822_strip_comments {
  1297. my $s = shift;
  1298. # Recursively remove comments, and replace with a single space. The simpler
  1299. # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
  1300. # chars in atoms, for example.
  1301. while ($s =~ s/^((?:[^"\\]|\\.)*
  1302. (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
  1303. \((?:[^()\\]|\\.)*\)/$1 /osx) {}
  1304. return $s;
  1305. }
  1306. # valid: returns true if the parameter is an RFC822 valid address
  1307. #
  1308. sub rfc822_valid {
  1309. my $s = rfc822_strip_comments(shift);
  1310. if (!$rfc822re) {
  1311. $rfc822re = make_rfc822re();
  1312. }
  1313. return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
  1314. }
  1315. # validlist: In scalar context, returns true if the parameter is an RFC822
  1316. # valid list of addresses.
  1317. #
  1318. # In list context, returns an empty list on failure (an invalid
  1319. # address was found); otherwise a list whose first element is the
  1320. # number of addresses found and whose remaining elements are the
  1321. # addresses. This is needed to disambiguate failure (invalid)
  1322. # from success with no addresses found, because an empty string is
  1323. # a valid list.
  1324. sub rfc822_validlist {
  1325. my $s = rfc822_strip_comments(shift);
  1326. if (!$rfc822re) {
  1327. $rfc822re = make_rfc822re();
  1328. }
  1329. # * null list items are valid according to the RFC
  1330. # * the '1' business is to aid in distinguishing failure from no results
  1331. my @r;
  1332. if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
  1333. $s =~ m/^$rfc822_char*$/) {
  1334. while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
  1335. push(@r, $1);
  1336. }
  1337. return wantarray ? (scalar(@r), @r) : 1;
  1338. }
  1339. return wantarray ? () : 0;
  1340. }