get_maintainer.pl 67 KB

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