get_maintainer.pl 59 KB

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