get_maintainer.pl 66 KB

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