get_maintainer.pl 60 KB

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