get_maintainer.pl 54 KB

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