get_maintainer.pl 57 KB

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