2
0

get_maintainer.pl 53 KB

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