get_maintainer.pl 58 KB

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