get_maintainer.pl 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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.23';
  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 = 1;
  24. my $email_git_blame = 0;
  25. my $email_git_min_signatures = 1;
  26. my $email_git_max_maintainers = 5;
  27. my $email_git_min_percent = 5;
  28. my $email_git_since = "1-year-ago";
  29. my $email_hg_since = "-365";
  30. my $email_remove_duplicates = 1;
  31. my $output_multiline = 1;
  32. my $output_separator = ", ";
  33. my $output_roles = 0;
  34. my $output_rolestats = 0;
  35. my $scm = 0;
  36. my $web = 0;
  37. my $subsystem = 0;
  38. my $status = 0;
  39. my $keywords = 1;
  40. my $file_emails = 0;
  41. my $from_filename = 0;
  42. my $pattern_depth = 0;
  43. my $version = 0;
  44. my $help = 0;
  45. my $exit = 0;
  46. my @penguin_chief = ();
  47. push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
  48. #Andrew wants in on most everything - 2009/01/14
  49. #push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
  50. my @penguin_chief_names = ();
  51. foreach my $chief (@penguin_chief) {
  52. if ($chief =~ m/^(.*):(.*)/) {
  53. my $chief_name = $1;
  54. my $chief_addr = $2;
  55. push(@penguin_chief_names, $chief_name);
  56. }
  57. }
  58. my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
  59. # rfc822 email address - preloaded methods go here.
  60. my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
  61. my $rfc822_char = '[\\000-\\377]';
  62. # VCS command support: class-like functions and strings
  63. my %VCS_cmds;
  64. my %VCS_cmds_git = (
  65. "execute_cmd" => \&git_execute_cmd,
  66. "available" => '(which("git") ne "") && (-d ".git")',
  67. "find_signers_cmd" => "git log --no-color --since=\$email_git_since -- \$file",
  68. "find_commit_signers_cmd" => "git log --no-color -1 \$commit",
  69. "blame_range_cmd" => "git blame -l -L \$diff_start,+\$diff_length \$file",
  70. "blame_file_cmd" => "git blame -l \$file",
  71. "commit_pattern" => "^commit [0-9a-f]{40,40}",
  72. "blame_commit_pattern" => "^([0-9a-f]+) "
  73. );
  74. my %VCS_cmds_hg = (
  75. "execute_cmd" => \&hg_execute_cmd,
  76. "available" => '(which("hg") ne "") && (-d ".hg")',
  77. "find_signers_cmd" =>
  78. "hg log --date=\$email_hg_since" .
  79. " --template='commit {node}\\n{desc}\\n' -- \$file",
  80. "find_commit_signers_cmd" => "hg log --template='{desc}\\n' -r \$commit",
  81. "blame_range_cmd" => "", # not supported
  82. "blame_file_cmd" => "hg blame -c \$file",
  83. "commit_pattern" => "^commit [0-9a-f]{40,40}",
  84. "blame_commit_pattern" => "^([0-9a-f]+):"
  85. );
  86. if (!GetOptions(
  87. 'email!' => \$email,
  88. 'git!' => \$email_git,
  89. 'git-blame!' => \$email_git_blame,
  90. 'git-chief-penguins!' => \$email_git_penguin_chiefs,
  91. 'git-min-signatures=i' => \$email_git_min_signatures,
  92. 'git-max-maintainers=i' => \$email_git_max_maintainers,
  93. 'git-min-percent=i' => \$email_git_min_percent,
  94. 'git-since=s' => \$email_git_since,
  95. 'hg-since=s' => \$email_hg_since,
  96. 'remove-duplicates!' => \$email_remove_duplicates,
  97. 'm!' => \$email_maintainer,
  98. 'n!' => \$email_usename,
  99. 'l!' => \$email_list,
  100. 's!' => \$email_subscriber_list,
  101. 'multiline!' => \$output_multiline,
  102. 'roles!' => \$output_roles,
  103. 'rolestats!' => \$output_rolestats,
  104. 'separator=s' => \$output_separator,
  105. 'subsystem!' => \$subsystem,
  106. 'status!' => \$status,
  107. 'scm!' => \$scm,
  108. 'web!' => \$web,
  109. 'pattern-depth=i' => \$pattern_depth,
  110. 'k|keywords!' => \$keywords,
  111. 'fe|file-emails!' => \$file_emails,
  112. 'f|file' => \$from_filename,
  113. 'v|version' => \$version,
  114. 'h|help' => \$help,
  115. )) {
  116. die "$P: invalid argument - use --help if necessary\n";
  117. }
  118. if ($help != 0) {
  119. usage();
  120. exit 0;
  121. }
  122. if ($version != 0) {
  123. print("${P} ${V}\n");
  124. exit 0;
  125. }
  126. if ($#ARGV < 0) {
  127. usage();
  128. die "$P: argument missing: patchfile or -f file please\n";
  129. }
  130. if ($output_separator ne ", ") {
  131. $output_multiline = 0;
  132. }
  133. if ($output_rolestats) {
  134. $output_roles = 1;
  135. }
  136. my $selections = $email + $scm + $status + $subsystem + $web;
  137. if ($selections == 0) {
  138. usage();
  139. die "$P: Missing required option: email, scm, status, subsystem or web\n";
  140. }
  141. if ($email &&
  142. ($email_maintainer + $email_list + $email_subscriber_list +
  143. $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
  144. usage();
  145. die "$P: Please select at least 1 email option\n";
  146. }
  147. if (!top_of_kernel_tree($lk_path)) {
  148. die "$P: The current directory does not appear to be "
  149. . "a linux kernel source tree.\n";
  150. }
  151. ## Read MAINTAINERS for type/value pairs
  152. my @typevalue = ();
  153. my %keyword_hash;
  154. open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
  155. while (<MAINT>) {
  156. my $line = $_;
  157. if ($line =~ m/^(\C):\s*(.*)/) {
  158. my $type = $1;
  159. my $value = $2;
  160. ##Filename pattern matching
  161. if ($type eq "F" || $type eq "X") {
  162. $value =~ s@\.@\\\.@g; ##Convert . to \.
  163. $value =~ s/\*/\.\*/g; ##Convert * to .*
  164. $value =~ s/\?/\./g; ##Convert ? to .
  165. ##if pattern is a directory and it lacks a trailing slash, add one
  166. if ((-d $value)) {
  167. $value =~ s@([^/])$@$1/@;
  168. }
  169. } elsif ($type eq "K") {
  170. $keyword_hash{@typevalue} = $value;
  171. }
  172. push(@typevalue, "$type:$value");
  173. } elsif (!/^(\s)*$/) {
  174. $line =~ s/\n$//g;
  175. push(@typevalue, $line);
  176. }
  177. }
  178. close(MAINT);
  179. my %mailmap;
  180. if ($email_remove_duplicates) {
  181. open(MAILMAP, "<${lk_path}.mailmap") || warn "$P: Can't open .mailmap\n";
  182. while (<MAILMAP>) {
  183. my $line = $_;
  184. next if ($line =~ m/^\s*#/);
  185. next if ($line =~ m/^\s*$/);
  186. my ($name, $address) = parse_email($line);
  187. $line = format_email($name, $address, $email_usename);
  188. next if ($line =~ m/^\s*$/);
  189. if (exists($mailmap{$name})) {
  190. my $obj = $mailmap{$name};
  191. push(@$obj, $address);
  192. } else {
  193. my @arr = ($address);
  194. $mailmap{$name} = \@arr;
  195. }
  196. }
  197. close(MAILMAP);
  198. }
  199. ## use the filenames on the command line or find the filenames in the patchfiles
  200. my @files = ();
  201. my @range = ();
  202. my @keyword_tvi = ();
  203. my @file_emails = ();
  204. foreach my $file (@ARGV) {
  205. ##if $file is a directory and it lacks a trailing slash, add one
  206. if ((-d $file)) {
  207. $file =~ s@([^/])$@$1/@;
  208. } elsif (!(-f $file)) {
  209. die "$P: file '${file}' not found\n";
  210. }
  211. if ($from_filename) {
  212. push(@files, $file);
  213. if (-f $file && ($keywords || $file_emails)) {
  214. open(FILE, "<$file") or die "$P: Can't open ${file}\n";
  215. my $text = do { local($/) ; <FILE> };
  216. close(FILE);
  217. if ($keywords) {
  218. foreach my $line (keys %keyword_hash) {
  219. if ($text =~ m/$keyword_hash{$line}/x) {
  220. push(@keyword_tvi, $line);
  221. }
  222. }
  223. }
  224. if ($file_emails) {
  225. 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;
  226. push(@file_emails, clean_file_emails(@poss_addr));
  227. }
  228. }
  229. } else {
  230. my $file_cnt = @files;
  231. my $lastfile;
  232. open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
  233. while (<PATCH>) {
  234. my $patch_line = $_;
  235. if (m/^\+\+\+\s+(\S+)/) {
  236. my $filename = $1;
  237. $filename =~ s@^[^/]*/@@;
  238. $filename =~ s@\n@@;
  239. $lastfile = $filename;
  240. push(@files, $filename);
  241. } elsif (m/^\@\@ -(\d+),(\d+)/) {
  242. if ($email_git_blame) {
  243. push(@range, "$lastfile:$1:$2");
  244. }
  245. } elsif ($keywords) {
  246. foreach my $line (keys %keyword_hash) {
  247. if ($patch_line =~ m/^[+-].*$keyword_hash{$line}/x) {
  248. push(@keyword_tvi, $line);
  249. }
  250. }
  251. }
  252. }
  253. close(PATCH);
  254. if ($file_cnt == @files) {
  255. warn "$P: file '${file}' doesn't appear to be a patch. "
  256. . "Add -f to options?\n";
  257. }
  258. @files = sort_and_uniq(@files);
  259. }
  260. }
  261. @file_emails = uniq(@file_emails);
  262. my @email_to = ();
  263. my @list_to = ();
  264. my @scm = ();
  265. my @web = ();
  266. my @subsystem = ();
  267. my @status = ();
  268. # Find responsible parties
  269. foreach my $file (@files) {
  270. my %hash;
  271. my $tvi = find_first_section();
  272. while ($tvi < @typevalue) {
  273. my $start = find_starting_index($tvi);
  274. my $end = find_ending_index($tvi);
  275. my $exclude = 0;
  276. my $i;
  277. #Do not match excluded file patterns
  278. for ($i = $start; $i < $end; $i++) {
  279. my $line = $typevalue[$i];
  280. if ($line =~ m/^(\C):\s*(.*)/) {
  281. my $type = $1;
  282. my $value = $2;
  283. if ($type eq 'X') {
  284. if (file_match_pattern($file, $value)) {
  285. $exclude = 1;
  286. }
  287. }
  288. }
  289. }
  290. if (!$exclude) {
  291. for ($i = $start; $i < $end; $i++) {
  292. my $line = $typevalue[$i];
  293. if ($line =~ m/^(\C):\s*(.*)/) {
  294. my $type = $1;
  295. my $value = $2;
  296. if ($type eq 'F') {
  297. if (file_match_pattern($file, $value)) {
  298. my $value_pd = ($value =~ tr@/@@);
  299. my $file_pd = ($file =~ tr@/@@);
  300. $value_pd++ if (substr($value,-1,1) ne "/");
  301. if ($pattern_depth == 0 ||
  302. (($file_pd - $value_pd) < $pattern_depth)) {
  303. $hash{$tvi} = $value_pd;
  304. }
  305. }
  306. }
  307. }
  308. }
  309. }
  310. $tvi += ($end - $start);
  311. }
  312. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  313. add_categories($line);
  314. }
  315. if ($email && $email_git) {
  316. vcs_file_signoffs($file);
  317. }
  318. if ($email && $email_git_blame) {
  319. vcs_file_blame($file);
  320. }
  321. }
  322. if ($keywords) {
  323. @keyword_tvi = sort_and_uniq(@keyword_tvi);
  324. foreach my $line (@keyword_tvi) {
  325. add_categories($line);
  326. }
  327. }
  328. if ($email) {
  329. foreach my $chief (@penguin_chief) {
  330. if ($chief =~ m/^(.*):(.*)/) {
  331. my $email_address;
  332. $email_address = format_email($1, $2, $email_usename);
  333. if ($email_git_penguin_chiefs) {
  334. push(@email_to, [$email_address, 'chief penguin']);
  335. } else {
  336. @email_to = grep($_->[0] !~ /${email_address}/, @email_to);
  337. }
  338. }
  339. }
  340. foreach my $email (@file_emails) {
  341. my ($name, $address) = parse_email($email);
  342. my $tmp_email = format_email($name, $address, $email_usename);
  343. push_email_address($tmp_email, '');
  344. add_role($tmp_email, 'in file');
  345. }
  346. }
  347. if ($email || $email_list) {
  348. my @to = ();
  349. if ($email) {
  350. @to = (@to, @email_to);
  351. }
  352. if ($email_list) {
  353. @to = (@to, @list_to);
  354. }
  355. output(merge_email(@to));
  356. }
  357. if ($scm) {
  358. @scm = uniq(@scm);
  359. output(@scm);
  360. }
  361. if ($status) {
  362. @status = uniq(@status);
  363. output(@status);
  364. }
  365. if ($subsystem) {
  366. @subsystem = uniq(@subsystem);
  367. output(@subsystem);
  368. }
  369. if ($web) {
  370. @web = uniq(@web);
  371. output(@web);
  372. }
  373. exit($exit);
  374. sub file_match_pattern {
  375. my ($file, $pattern) = @_;
  376. if (substr($pattern, -1) eq "/") {
  377. if ($file =~ m@^$pattern@) {
  378. return 1;
  379. }
  380. } else {
  381. if ($file =~ m@^$pattern@) {
  382. my $s1 = ($file =~ tr@/@@);
  383. my $s2 = ($pattern =~ tr@/@@);
  384. if ($s1 == $s2) {
  385. return 1;
  386. }
  387. }
  388. }
  389. return 0;
  390. }
  391. sub usage {
  392. print <<EOT;
  393. usage: $P [options] patchfile
  394. $P [options] -f file|directory
  395. version: $V
  396. MAINTAINER field selection options:
  397. --email => print email address(es) if any
  398. --git => include recent git \*-by: signers
  399. --git-chief-penguins => include ${penguin_chiefs}
  400. --git-min-signatures => number of signatures required (default: 1)
  401. --git-max-maintainers => maximum maintainers to add (default: 5)
  402. --git-min-percent => minimum percentage of commits required (default: 5)
  403. --git-blame => use git blame to find modified commits for patch or file
  404. --git-since => git history to use (default: 1-year-ago)
  405. --hg-since => hg history to use (default: -365)
  406. --m => include maintainer(s) if any
  407. --n => include name 'Full Name <addr\@domain.tld>'
  408. --l => include list(s) if any
  409. --s => include subscriber only list(s) if any
  410. --remove-duplicates => minimize duplicate email names/addresses
  411. --roles => show roles (status:subsystem, git-signer, list, etc...)
  412. --rolestats => show roles and statistics (commits/total_commits, %)
  413. --file-emails => add email addresses found in -f file (default: 0 (off))
  414. --scm => print SCM tree(s) if any
  415. --status => print status if any
  416. --subsystem => print subsystem name if any
  417. --web => print website(s) if any
  418. Output type options:
  419. --separator [, ] => separator for multiple entries on 1 line
  420. using --separator also sets --nomultiline if --separator is not [, ]
  421. --multiline => print 1 entry per line
  422. Other options:
  423. --pattern-depth => Number of pattern directory traversals (default: 0 (all))
  424. --keywords => scan patch for keywords (default: 1 (on))
  425. --version => show version
  426. --help => show this help information
  427. Default options:
  428. [--email --git --m --n --l --multiline --pattern-depth=0 --remove-duplicates]
  429. Notes:
  430. Using "-f directory" may give unexpected results:
  431. Used with "--git", git signators for _all_ files in and below
  432. directory are examined as git recurses directories.
  433. Any specified X: (exclude) pattern matches are _not_ ignored.
  434. Used with "--nogit", directory is used as a pattern match,
  435. no individual file within the directory or subdirectory
  436. is matched.
  437. Used with "--git-blame", does not iterate all files in directory
  438. Using "--git-blame" is slow and may add old committers and authors
  439. that are no longer active maintainers to the output.
  440. Using "--roles" or "--rolestats" with git send-email --cc-cmd or any
  441. other automated tools that expect only ["name"] <email address>
  442. may not work because of additional output after <email address>.
  443. Using "--rolestats" and "--git-blame" shows the #/total=% commits,
  444. not the percentage of the entire file authored. # of commits is
  445. not a good measure of amount of code authored. 1 major commit may
  446. contain a thousand lines, 5 trivial commits may modify a single line.
  447. If git is not installed, but mercurial (hg) is installed and an .hg
  448. repository exists, the following options apply to mercurial:
  449. --git,
  450. --git-min-signatures, --git-max-maintainers, --git-min-percent, and
  451. --git-blame
  452. Use --hg-since not --git-since to control date selection
  453. EOT
  454. }
  455. sub top_of_kernel_tree {
  456. my ($lk_path) = @_;
  457. if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
  458. $lk_path .= "/";
  459. }
  460. if ( (-f "${lk_path}COPYING")
  461. && (-f "${lk_path}CREDITS")
  462. && (-f "${lk_path}Kbuild")
  463. && (-f "${lk_path}MAINTAINERS")
  464. && (-f "${lk_path}Makefile")
  465. && (-f "${lk_path}README")
  466. && (-d "${lk_path}Documentation")
  467. && (-d "${lk_path}arch")
  468. && (-d "${lk_path}include")
  469. && (-d "${lk_path}drivers")
  470. && (-d "${lk_path}fs")
  471. && (-d "${lk_path}init")
  472. && (-d "${lk_path}ipc")
  473. && (-d "${lk_path}kernel")
  474. && (-d "${lk_path}lib")
  475. && (-d "${lk_path}scripts")) {
  476. return 1;
  477. }
  478. return 0;
  479. }
  480. sub parse_email {
  481. my ($formatted_email) = @_;
  482. my $name = "";
  483. my $address = "";
  484. if ($formatted_email =~ /^([^<]+)<(.+\@.*)>.*$/) {
  485. $name = $1;
  486. $address = $2;
  487. } elsif ($formatted_email =~ /^\s*<(.+\@\S*)>.*$/) {
  488. $address = $1;
  489. } elsif ($formatted_email =~ /^(.+\@\S*).*$/) {
  490. $address = $1;
  491. }
  492. $name =~ s/^\s+|\s+$//g;
  493. $name =~ s/^\"|\"$//g;
  494. $address =~ s/^\s+|\s+$//g;
  495. if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
  496. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  497. $name = "\"$name\"";
  498. }
  499. return ($name, $address);
  500. }
  501. sub format_email {
  502. my ($name, $address, $usename) = @_;
  503. my $formatted_email;
  504. $name =~ s/^\s+|\s+$//g;
  505. $name =~ s/^\"|\"$//g;
  506. $address =~ s/^\s+|\s+$//g;
  507. if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
  508. $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
  509. $name = "\"$name\"";
  510. }
  511. if ($usename) {
  512. if ("$name" eq "") {
  513. $formatted_email = "$address";
  514. } else {
  515. $formatted_email = "$name <$address>";
  516. }
  517. } else {
  518. $formatted_email = $address;
  519. }
  520. return $formatted_email;
  521. }
  522. sub find_first_section {
  523. my $index = 0;
  524. while ($index < @typevalue) {
  525. my $tv = $typevalue[$index];
  526. if (($tv =~ m/^(\C):\s*(.*)/)) {
  527. last;
  528. }
  529. $index++;
  530. }
  531. return $index;
  532. }
  533. sub find_starting_index {
  534. my ($index) = @_;
  535. while ($index > 0) {
  536. my $tv = $typevalue[$index];
  537. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  538. last;
  539. }
  540. $index--;
  541. }
  542. return $index;
  543. }
  544. sub find_ending_index {
  545. my ($index) = @_;
  546. while ($index < @typevalue) {
  547. my $tv = $typevalue[$index];
  548. if (!($tv =~ m/^(\C):\s*(.*)/)) {
  549. last;
  550. }
  551. $index++;
  552. }
  553. return $index;
  554. }
  555. sub get_maintainer_role {
  556. my ($index) = @_;
  557. my $i;
  558. my $start = find_starting_index($index);
  559. my $end = find_ending_index($index);
  560. my $role;
  561. my $subsystem = $typevalue[$start];
  562. if (length($subsystem) > 20) {
  563. $subsystem = substr($subsystem, 0, 17);
  564. $subsystem =~ s/\s*$//;
  565. $subsystem = $subsystem . "...";
  566. }
  567. for ($i = $start + 1; $i < $end; $i++) {
  568. my $tv = $typevalue[$i];
  569. if ($tv =~ m/^(\C):\s*(.*)/) {
  570. my $ptype = $1;
  571. my $pvalue = $2;
  572. if ($ptype eq "S") {
  573. $role = $pvalue;
  574. }
  575. }
  576. }
  577. $role = lc($role);
  578. if ($role eq "supported") {
  579. $role = "supporter";
  580. } elsif ($role eq "maintained") {
  581. $role = "maintainer";
  582. } elsif ($role eq "odd fixes") {
  583. $role = "odd fixer";
  584. } elsif ($role eq "orphan") {
  585. $role = "orphan minder";
  586. } elsif ($role eq "obsolete") {
  587. $role = "obsolete minder";
  588. } elsif ($role eq "buried alive in reporters") {
  589. $role = "chief penguin";
  590. }
  591. return $role . ":" . $subsystem;
  592. }
  593. sub get_list_role {
  594. my ($index) = @_;
  595. my $i;
  596. my $start = find_starting_index($index);
  597. my $end = find_ending_index($index);
  598. my $subsystem = $typevalue[$start];
  599. if (length($subsystem) > 20) {
  600. $subsystem = substr($subsystem, 0, 17);
  601. $subsystem =~ s/\s*$//;
  602. $subsystem = $subsystem . "...";
  603. }
  604. if ($subsystem eq "THE REST") {
  605. $subsystem = "";
  606. }
  607. return $subsystem;
  608. }
  609. sub add_categories {
  610. my ($index) = @_;
  611. my $i;
  612. my $start = find_starting_index($index);
  613. my $end = find_ending_index($index);
  614. push(@subsystem, $typevalue[$start]);
  615. for ($i = $start + 1; $i < $end; $i++) {
  616. my $tv = $typevalue[$i];
  617. if ($tv =~ m/^(\C):\s*(.*)/) {
  618. my $ptype = $1;
  619. my $pvalue = $2;
  620. if ($ptype eq "L") {
  621. my $list_address = $pvalue;
  622. my $list_additional = "";
  623. my $list_role = get_list_role($i);
  624. if ($list_role ne "") {
  625. $list_role = ":" . $list_role;
  626. }
  627. if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
  628. $list_address = $1;
  629. $list_additional = $2;
  630. }
  631. if ($list_additional =~ m/subscribers-only/) {
  632. if ($email_subscriber_list) {
  633. push(@list_to, [$list_address, "subscriber list${list_role}"]);
  634. }
  635. } else {
  636. if ($email_list) {
  637. push(@list_to, [$list_address, "open list${list_role}"]);
  638. }
  639. }
  640. } elsif ($ptype eq "M") {
  641. my ($name, $address) = parse_email($pvalue);
  642. if ($name eq "") {
  643. if ($i > 0) {
  644. my $tv = $typevalue[$i - 1];
  645. if ($tv =~ m/^(\C):\s*(.*)/) {
  646. if ($1 eq "P") {
  647. $name = $2;
  648. $pvalue = format_email($name, $address, $email_usename);
  649. }
  650. }
  651. }
  652. }
  653. if ($email_maintainer) {
  654. my $role = get_maintainer_role($i);
  655. push_email_addresses($pvalue, $role);
  656. }
  657. } elsif ($ptype eq "T") {
  658. push(@scm, $pvalue);
  659. } elsif ($ptype eq "W") {
  660. push(@web, $pvalue);
  661. } elsif ($ptype eq "S") {
  662. push(@status, $pvalue);
  663. }
  664. }
  665. }
  666. }
  667. my %email_hash_name;
  668. my %email_hash_address;
  669. sub email_inuse {
  670. my ($name, $address) = @_;
  671. return 1 if (($name eq "") && ($address eq ""));
  672. return 1 if (($name ne "") && exists($email_hash_name{$name}));
  673. return 1 if (($address ne "") && exists($email_hash_address{$address}));
  674. return 0;
  675. }
  676. sub push_email_address {
  677. my ($line, $role) = @_;
  678. my ($name, $address) = parse_email($line);
  679. if ($address eq "") {
  680. return 0;
  681. }
  682. if (!$email_remove_duplicates) {
  683. push(@email_to, [format_email($name, $address, $email_usename), $role]);
  684. } elsif (!email_inuse($name, $address)) {
  685. push(@email_to, [format_email($name, $address, $email_usename), $role]);
  686. $email_hash_name{$name}++;
  687. $email_hash_address{$address}++;
  688. }
  689. return 1;
  690. }
  691. sub push_email_addresses {
  692. my ($address, $role) = @_;
  693. my @address_list = ();
  694. if (rfc822_valid($address)) {
  695. push_email_address($address, $role);
  696. } elsif (@address_list = rfc822_validlist($address)) {
  697. my $array_count = shift(@address_list);
  698. while (my $entry = shift(@address_list)) {
  699. push_email_address($entry, $role);
  700. }
  701. } else {
  702. if (!push_email_address($address, $role)) {
  703. warn("Invalid MAINTAINERS address: '" . $address . "'\n");
  704. }
  705. }
  706. }
  707. sub add_role {
  708. my ($line, $role) = @_;
  709. my ($name, $address) = parse_email($line);
  710. my $email = format_email($name, $address, $email_usename);
  711. foreach my $entry (@email_to) {
  712. if ($email_remove_duplicates) {
  713. my ($entry_name, $entry_address) = parse_email($entry->[0]);
  714. if (($name eq $entry_name || $address eq $entry_address)
  715. && ($role eq "" || !($entry->[1] =~ m/$role/))
  716. ) {
  717. if ($entry->[1] eq "") {
  718. $entry->[1] = "$role";
  719. } else {
  720. $entry->[1] = "$entry->[1],$role";
  721. }
  722. }
  723. } else {
  724. if ($email eq $entry->[0]
  725. && ($role eq "" || !($entry->[1] =~ m/$role/))
  726. ) {
  727. if ($entry->[1] eq "") {
  728. $entry->[1] = "$role";
  729. } else {
  730. $entry->[1] = "$entry->[1],$role";
  731. }
  732. }
  733. }
  734. }
  735. }
  736. sub which {
  737. my ($bin) = @_;
  738. foreach my $path (split(/:/, $ENV{PATH})) {
  739. if (-e "$path/$bin") {
  740. return "$path/$bin";
  741. }
  742. }
  743. return "";
  744. }
  745. sub mailmap {
  746. my (@lines) = @_;
  747. my %hash;
  748. foreach my $line (@lines) {
  749. my ($name, $address) = parse_email($line);
  750. if (!exists($hash{$name})) {
  751. $hash{$name} = $address;
  752. } elsif ($address ne $hash{$name}) {
  753. $address = $hash{$name};
  754. $line = format_email($name, $address, $email_usename);
  755. }
  756. if (exists($mailmap{$name})) {
  757. my $obj = $mailmap{$name};
  758. foreach my $map_address (@$obj) {
  759. if (($map_address eq $address) &&
  760. ($map_address ne $hash{$name})) {
  761. $line = format_email($name, $hash{$name}, $email_usename);
  762. }
  763. }
  764. }
  765. }
  766. return @lines;
  767. }
  768. sub git_execute_cmd {
  769. my ($cmd) = @_;
  770. my @lines = ();
  771. my $output = `$cmd`;
  772. $output =~ s/^\s*//gm;
  773. @lines = split("\n", $output);
  774. return @lines;
  775. }
  776. sub hg_execute_cmd {
  777. my ($cmd) = @_;
  778. my @lines = ();
  779. my $output = `$cmd`;
  780. @lines = split("\n", $output);
  781. return @lines;
  782. }
  783. sub vcs_find_signers {
  784. my ($cmd) = @_;
  785. my @lines = ();
  786. my $commits;
  787. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  788. my $pattern = $VCS_cmds{"commit_pattern"};
  789. $commits = grep(/$pattern/, @lines); # of commits
  790. @lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
  791. if (!$email_git_penguin_chiefs) {
  792. @lines = grep(!/${penguin_chiefs}/i, @lines);
  793. }
  794. # cut -f2- -d":"
  795. s/.*:\s*(.+)\s*/$1/ for (@lines);
  796. ## Reformat email addresses (with names) to avoid badly written signatures
  797. foreach my $line (@lines) {
  798. my ($name, $address) = parse_email($line);
  799. $line = format_email($name, $address, 1);
  800. }
  801. return ($commits, @lines);
  802. }
  803. sub vcs_save_commits {
  804. my ($cmd) = @_;
  805. my @lines = ();
  806. my @commits = ();
  807. @lines = &{$VCS_cmds{"execute_cmd"}}($cmd);
  808. foreach my $line (@lines) {
  809. if ($line =~ m/$VCS_cmds{"blame_commit_pattern"}/) {
  810. push(@commits, $1);
  811. }
  812. }
  813. return @commits;
  814. }
  815. sub vcs_blame {
  816. my ($file) = @_;
  817. my $cmd;
  818. my @commits = ();
  819. return @commits if (!(-f $file));
  820. if (@range && $VCS_cmds{"blame_range_cmd"} eq "") {
  821. my @all_commits = ();
  822. $cmd = $VCS_cmds{"blame_file_cmd"};
  823. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  824. @all_commits = vcs_save_commits($cmd);
  825. foreach my $file_range_diff (@range) {
  826. next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
  827. my $diff_file = $1;
  828. my $diff_start = $2;
  829. my $diff_length = $3;
  830. next if ("$file" ne "$diff_file");
  831. for (my $i = $diff_start; $i < $diff_start + $diff_length; $i++) {
  832. push(@commits, $all_commits[$i]);
  833. }
  834. }
  835. } elsif (@range) {
  836. foreach my $file_range_diff (@range) {
  837. next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
  838. my $diff_file = $1;
  839. my $diff_start = $2;
  840. my $diff_length = $3;
  841. next if ("$file" ne "$diff_file");
  842. $cmd = $VCS_cmds{"blame_range_cmd"};
  843. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  844. push(@commits, vcs_save_commits($cmd));
  845. }
  846. } else {
  847. $cmd = $VCS_cmds{"blame_file_cmd"};
  848. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  849. @commits = vcs_save_commits($cmd);
  850. }
  851. return @commits;
  852. }
  853. my $printed_novcs = 0;
  854. sub vcs_exists {
  855. %VCS_cmds = %VCS_cmds_git;
  856. return 1 if eval $VCS_cmds{"available"};
  857. %VCS_cmds = %VCS_cmds_hg;
  858. return 1 if eval $VCS_cmds{"available"};
  859. %VCS_cmds = ();
  860. if (!$printed_novcs) {
  861. warn("$P: No supported VCS found. Add --nogit to options?\n");
  862. warn("Using a git repository produces better results.\n");
  863. warn("Try Linus Torvalds' latest git repository using:\n");
  864. warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
  865. $printed_novcs = 1;
  866. }
  867. return 0;
  868. }
  869. sub vcs_assign {
  870. my ($role, $divisor, @lines) = @_;
  871. my %hash;
  872. my $count = 0;
  873. return if (@lines <= 0);
  874. if ($divisor <= 0) {
  875. warn("Bad divisor in " . (caller(0))[3] . ": $divisor\n");
  876. $divisor = 1;
  877. }
  878. if ($email_remove_duplicates) {
  879. @lines = mailmap(@lines);
  880. }
  881. @lines = sort(@lines);
  882. # uniq -c
  883. $hash{$_}++ for @lines;
  884. # sort -rn
  885. foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
  886. my $sign_offs = $hash{$line};
  887. my $percent = $sign_offs * 100 / $divisor;
  888. $percent = 100 if ($percent > 100);
  889. $count++;
  890. last if ($sign_offs < $email_git_min_signatures ||
  891. $count > $email_git_max_maintainers ||
  892. $percent < $email_git_min_percent);
  893. push_email_address($line, '');
  894. if ($output_rolestats) {
  895. my $fmt_percent = sprintf("%.0f", $percent);
  896. add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
  897. } else {
  898. add_role($line, $role);
  899. }
  900. }
  901. }
  902. sub vcs_file_signoffs {
  903. my ($file) = @_;
  904. my @signers = ();
  905. my $commits;
  906. return if (!vcs_exists());
  907. my $cmd = $VCS_cmds{"find_signers_cmd"};
  908. $cmd =~ s/(\$\w+)/$1/eeg; # interpolate $cmd
  909. ($commits, @signers) = vcs_find_signers($cmd);
  910. vcs_assign("commit_signer", $commits, @signers);
  911. }
  912. sub vcs_file_blame {
  913. my ($file) = @_;
  914. my @signers = ();
  915. my @commits = ();
  916. my $total_commits;
  917. return if (!vcs_exists());
  918. @commits = vcs_blame($file);
  919. @commits = uniq(@commits);
  920. $total_commits = @commits;
  921. foreach my $commit (@commits) {
  922. my $commit_count;
  923. my @commit_signers = ();
  924. my $cmd = $VCS_cmds{"find_commit_signers_cmd"};
  925. $cmd =~ s/(\$\w+)/$1/eeg; #interpolate $cmd
  926. ($commit_count, @commit_signers) = vcs_find_signers($cmd);
  927. push(@signers, @commit_signers);
  928. }
  929. if ($from_filename) {
  930. vcs_assign("commits", $total_commits, @signers);
  931. } else {
  932. vcs_assign("modified commits", $total_commits, @signers);
  933. }
  934. }
  935. sub uniq {
  936. my (@parms) = @_;
  937. my %saw;
  938. @parms = grep(!$saw{$_}++, @parms);
  939. return @parms;
  940. }
  941. sub sort_and_uniq {
  942. my (@parms) = @_;
  943. my %saw;
  944. @parms = sort @parms;
  945. @parms = grep(!$saw{$_}++, @parms);
  946. return @parms;
  947. }
  948. sub clean_file_emails {
  949. my (@file_emails) = @_;
  950. my @fmt_emails = ();
  951. foreach my $email (@file_emails) {
  952. $email =~ s/[\(\<\{]{0,1}([A-Za-z0-9_\.\+-]+\@[A-Za-z0-9\.-]+)[\)\>\}]{0,1}/\<$1\>/g;
  953. my ($name, $address) = parse_email($email);
  954. if ($name eq '"[,\.]"') {
  955. $name = "";
  956. }
  957. my @nw = split(/[^A-Za-zÀ-ÿ\'\,\.\+-]/, $name);
  958. if (@nw > 2) {
  959. my $first = $nw[@nw - 3];
  960. my $middle = $nw[@nw - 2];
  961. my $last = $nw[@nw - 1];
  962. if (((length($first) == 1 && $first =~ m/[A-Za-z]/) ||
  963. (length($first) == 2 && substr($first, -1) eq ".")) ||
  964. (length($middle) == 1 ||
  965. (length($middle) == 2 && substr($middle, -1) eq "."))) {
  966. $name = "$first $middle $last";
  967. } else {
  968. $name = "$middle $last";
  969. }
  970. }
  971. if (substr($name, -1) =~ /[,\.]/) {
  972. $name = substr($name, 0, length($name) - 1);
  973. } elsif (substr($name, -2) =~ /[,\.]"/) {
  974. $name = substr($name, 0, length($name) - 2) . '"';
  975. }
  976. if (substr($name, 0, 1) =~ /[,\.]/) {
  977. $name = substr($name, 1, length($name) - 1);
  978. } elsif (substr($name, 0, 2) =~ /"[,\.]/) {
  979. $name = '"' . substr($name, 2, length($name) - 2);
  980. }
  981. my $fmt_email = format_email($name, $address, $email_usename);
  982. push(@fmt_emails, $fmt_email);
  983. }
  984. return @fmt_emails;
  985. }
  986. sub merge_email {
  987. my @lines;
  988. my %saw;
  989. for (@_) {
  990. my ($address, $role) = @$_;
  991. if (!$saw{$address}) {
  992. if ($output_roles) {
  993. push(@lines, "$address ($role)");
  994. } else {
  995. push(@lines, $address);
  996. }
  997. $saw{$address} = 1;
  998. }
  999. }
  1000. return @lines;
  1001. }
  1002. sub output {
  1003. my (@parms) = @_;
  1004. if ($output_multiline) {
  1005. foreach my $line (@parms) {
  1006. print("${line}\n");
  1007. }
  1008. } else {
  1009. print(join($output_separator, @parms));
  1010. print("\n");
  1011. }
  1012. }
  1013. my $rfc822re;
  1014. sub make_rfc822re {
  1015. # Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
  1016. # comment. We must allow for rfc822_lwsp (or comments) after each of these.
  1017. # This regexp will only work on addresses which have had comments stripped
  1018. # and replaced with rfc822_lwsp.
  1019. my $specials = '()<>@,;:\\\\".\\[\\]';
  1020. my $controls = '\\000-\\037\\177';
  1021. my $dtext = "[^\\[\\]\\r\\\\]";
  1022. my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
  1023. my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
  1024. # Use zero-width assertion to spot the limit of an atom. A simple
  1025. # $rfc822_lwsp* causes the regexp engine to hang occasionally.
  1026. my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
  1027. my $word = "(?:$atom|$quoted_string)";
  1028. my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
  1029. my $sub_domain = "(?:$atom|$domain_literal)";
  1030. my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
  1031. my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
  1032. my $phrase = "$word*";
  1033. my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
  1034. my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
  1035. my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
  1036. my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
  1037. my $address = "(?:$mailbox|$group)";
  1038. return "$rfc822_lwsp*$address";
  1039. }
  1040. sub rfc822_strip_comments {
  1041. my $s = shift;
  1042. # Recursively remove comments, and replace with a single space. The simpler
  1043. # regexps in the Email Addressing FAQ are imperfect - they will miss escaped
  1044. # chars in atoms, for example.
  1045. while ($s =~ s/^((?:[^"\\]|\\.)*
  1046. (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
  1047. \((?:[^()\\]|\\.)*\)/$1 /osx) {}
  1048. return $s;
  1049. }
  1050. # valid: returns true if the parameter is an RFC822 valid address
  1051. #
  1052. sub rfc822_valid ($) {
  1053. my $s = rfc822_strip_comments(shift);
  1054. if (!$rfc822re) {
  1055. $rfc822re = make_rfc822re();
  1056. }
  1057. return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
  1058. }
  1059. # validlist: In scalar context, returns true if the parameter is an RFC822
  1060. # valid list of addresses.
  1061. #
  1062. # In list context, returns an empty list on failure (an invalid
  1063. # address was found); otherwise a list whose first element is the
  1064. # number of addresses found and whose remaining elements are the
  1065. # addresses. This is needed to disambiguate failure (invalid)
  1066. # from success with no addresses found, because an empty string is
  1067. # a valid list.
  1068. sub rfc822_validlist ($) {
  1069. my $s = rfc822_strip_comments(shift);
  1070. if (!$rfc822re) {
  1071. $rfc822re = make_rfc822re();
  1072. }
  1073. # * null list items are valid according to the RFC
  1074. # * the '1' business is to aid in distinguishing failure from no results
  1075. my @r;
  1076. if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
  1077. $s =~ m/^$rfc822_char*$/) {
  1078. while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
  1079. push(@r, $1);
  1080. }
  1081. return wantarray ? (scalar(@r), @r) : 1;
  1082. }
  1083. return wantarray ? () : 0;
  1084. }