Skip to content

Commit a8af243

Browse files
JoePerchestorvalds
authored andcommitted
scripts/get_maintainer.pl: fix --non with --git-blame and cleanups
Fix email matching without name --n and --git-blame Using --non and --git-blame caused maintainer signature matching to fail. Fixed that by adding 3rd argument to sub format_email to control show/hide name portion of address Slurp -f file instead of reading line-by-line for K: pattern matching. Suggested by Wolfram Sang as more efficient Refactor git command execution Break into 2 functions, execute/analyze Share code between --git and --git-blame Don't warn multiple times when git isn't installed Improve stats reporting --git-min-percent and -- rolestats now count the total number of commits for either the period of --git-since or if using --git-blame the commits used by the current file and calculate commit % as # of commits signed / total commits * 100 Code style cleaning Use consistent sub foo { my (args...) = @_; Signed-off-by: Joe Perches <joe@perches.com> Cc: Ben Hutchings <ben@decadent.org.uk> Cc: Greg KH <greg@kroah.com> Cc: Pavel Machek <pavel@ucw.cz> Cc: Wolfram Sang <w.sang@pengutronix.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 3c7385b commit a8af243

1 file changed

Lines changed: 104 additions & 94 deletions

File tree

‎scripts/get_maintainer.pl‎

Lines changed: 104 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
next if ($line =~ m/^\s*$/);
183183

184184
my ($name, $address) = parse_email($line);
185-
$line = format_email($name, $address);
185+
$line = format_email($name, $address, $email_usename);
186186

187187
next if ($line =~ m/^\s*$/);
188188

@@ -214,12 +214,10 @@
214214
push(@files, $file);
215215
if (-f $file && $keywords) {
216216
open(FILE, "<$file") or die "$P: Can't open ${file}\n";
217-
while (<FILE>) {
218-
my $patch_line = $_;
219-
foreach my $line (keys %keyword_hash) {
220-
if ($patch_line =~ m/^.*$keyword_hash{$line}/x) {
221-
push(@keyword_tvi, $line);
222-
}
217+
my $text = do { local($/) ; <FILE> };
218+
foreach my $line (keys %keyword_hash) {
219+
if ($text =~ m/$keyword_hash{$line}/x) {
220+
push(@keyword_tvi, $line);
223221
}
224222
}
225223
close(FILE);
@@ -311,7 +309,7 @@
311309
}
312310

313311
if ($email && $email_git) {
314-
recent_git_signoffs($file);
312+
git_file_signoffs($file);
315313
}
316314

317315
if ($email && $email_git_blame) {
@@ -331,7 +329,7 @@
331329
if ($chief =~ m/^(.*):(.*)/) {
332330
my $email_address;
333331

334-
$email_address = format_email($1, $2);
332+
$email_address = format_email($1, $2, $email_usename);
335333
if ($email_git_penguin_chiefs) {
336334
push(@email_to, [$email_address, 'chief penguin']);
337335
} else {
@@ -509,7 +507,7 @@ sub parse_email {
509507
}
510508

511509
sub format_email {
512-
my ($name, $address) = @_;
510+
my ($name, $address, $usename) = @_;
513511

514512
my $formatted_email;
515513

@@ -522,11 +520,11 @@ sub format_email {
522520
$name = "\"$name\"";
523521
}
524522

525-
if ($email_usename) {
523+
if ($usename) {
526524
if ("$name" eq "") {
527525
$formatted_email = "$address";
528526
} else {
529-
$formatted_email = "$name <${address}>";
527+
$formatted_email = "$name <$address>";
530528
}
531529
} else {
532530
$formatted_email = $address;
@@ -671,7 +669,7 @@ sub add_categories {
671669
if ($tv =~ m/^(\C):\s*(.*)/) {
672670
if ($1 eq "P") {
673671
$name = $2;
674-
$pvalue = format_email($name, $address);
672+
$pvalue = format_email($name, $address, $email_usename);
675673
}
676674
}
677675
}
@@ -714,9 +712,9 @@ sub push_email_address {
714712
}
715713

716714
if (!$email_remove_duplicates) {
717-
push(@email_to, [format_email($name, $address), $role]);
715+
push(@email_to, [format_email($name, $address, $email_usename), $role]);
718716
} elsif (!email_inuse($name, $address)) {
719-
push(@email_to, [format_email($name, $address), $role]);
717+
push(@email_to, [format_email($name, $address, $email_usename), $role]);
720718
$email_hash_name{$name}++;
721719
$email_hash_address{$address}++;
722720
}
@@ -747,7 +745,7 @@ sub add_role {
747745
my ($line, $role) = @_;
748746

749747
my ($name, $address) = parse_email($line);
750-
my $email = format_email($name, $address);
748+
my $email = format_email($name, $address, $email_usename);
751749

752750
foreach my $entry (@email_to) {
753751
if ($email_remove_duplicates) {
@@ -784,7 +782,7 @@ sub which {
784782
}
785783

786784
sub mailmap {
787-
my @lines = @_;
785+
my (@lines) = @_;
788786
my %hash;
789787

790788
foreach my $line (@lines) {
@@ -793,14 +791,14 @@ sub mailmap {
793791
$hash{$name} = $address;
794792
} elsif ($address ne $hash{$name}) {
795793
$address = $hash{$name};
796-
$line = format_email($name, $address);
794+
$line = format_email($name, $address, $email_usename);
797795
}
798796
if (exists($mailmap{$name})) {
799797
my $obj = $mailmap{$name};
800798
foreach my $map_address (@$obj) {
801799
if (($map_address eq $address) &&
802800
($map_address ne $hash{$name})) {
803-
$line = format_email($name, $hash{$name});
801+
$line = format_email($name, $hash{$name}, $email_usename);
804802
}
805803
}
806804
}
@@ -809,33 +807,44 @@ sub mailmap {
809807
return @lines;
810808
}
811809

812-
sub recent_git_signoffs {
813-
my ($file) = @_;
814-
815-
my $sign_offs = "";
816-
my $cmd = "";
817-
my $output = "";
818-
my $count = 0;
819-
my @lines = ();
820-
my %hash;
821-
my $total_sign_offs;
822-
810+
my $printed_nogit = 0;
811+
my $printed_nogitdir = 0;
812+
sub has_git {
823813
if (which("git") eq "") {
824-
warn("$P: git not found. Add --nogit to options?\n");
825-
return;
814+
if (!$printed_nogit) {
815+
warn("$P: git not found. Add --nogit to options?\n");
816+
$printed_nogit = 1;
817+
}
818+
return 0;
826819
}
827820
if (!(-d ".git")) {
828-
warn("$P: .git directory not found. Use a git repository for better results.\n");
829-
warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
830-
return;
821+
if (!$printed_nogitdir) {
822+
warn(".git directory not found. "
823+
. "Using a git repository produces better results.\n");
824+
warn("Try Linus Torvalds' latest git repository using:\n");
825+
warn("git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git\n");
826+
$printed_nogitdir = 1;
827+
}
828+
return 0;
831829
}
832830

833-
$cmd = "git log --since=${email_git_since} -- ${file}";
831+
return 1;
832+
}
833+
834+
sub git_find_signers {
835+
my ($cmd) = @_;
836+
837+
my $output;
838+
my @lines = ();
839+
my $commits;
840+
841+
return (0, @lines) if (!has_git());
834842

835843
$output = `${cmd}`;
836844
$output =~ s/^\s*//gm;
837845

838846
@lines = split("\n", $output);
847+
$commits = grep(/^commit [0-9a-f]{40,40}/, @lines); # of commits
839848

840849
@lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
841850
if (!$email_git_penguin_chiefs) {
@@ -844,10 +853,28 @@ sub recent_git_signoffs {
844853
# cut -f2- -d":"
845854
s/.*:\s*(.+)\s*/$1/ for (@lines);
846855

847-
$total_sign_offs = @lines;
856+
## Reformat email addresses (with names) to avoid badly written signatures
857+
848858
foreach my $line (@lines) {
849859
my ($name, $address) = parse_email($line);
850-
$line = format_email($name, $address);
860+
$line = format_email($name, $address, 1);
861+
}
862+
863+
return ($commits, @lines);
864+
}
865+
866+
sub git_assign_signers {
867+
my ($role, $divisor, @lines) = @_;
868+
869+
my %hash;
870+
my $count = 0;
871+
872+
return if (!has_git());
873+
return if (@lines <= 0);
874+
875+
if ($divisor <= 0) {
876+
warn("Bad divisor in git_assign_signers: $divisor\n");
877+
$divisor = 1;
851878
}
852879

853880
if ($email_remove_duplicates) {
@@ -862,27 +889,43 @@ sub recent_git_signoffs {
862889
# sort -rn
863890
foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
864891
my $sign_offs = $hash{$line};
865-
my $role;
892+
my $percent = $sign_offs * 100 / $divisor;
866893

894+
$percent = 100 if ($percent > 100);
867895
$count++;
868896
last if ($sign_offs < $email_git_min_signatures ||
869897
$count > $email_git_max_maintainers ||
870-
$sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
898+
$percent < $email_git_min_percent);
871899
push_email_address($line, '');
872-
$role = "git-signer";
873900
if ($output_rolestats) {
874-
my $percent = sprintf("%.0f", $sign_offs * 100 / $total_sign_offs);
875-
$role = "$role:$sign_offs/$total_sign_offs=$percent%";
901+
my $fmt_percent = sprintf("%.0f", $percent);
902+
add_role($line, "$role:$sign_offs/$divisor=$fmt_percent%");
903+
} else {
904+
add_role($line, $role);
876905
}
877-
add_role($line, $role);
878906
}
879907
}
880908

909+
sub git_file_signoffs {
910+
my ($file) = @_;
911+
912+
my @signers = ();
913+
my $total_signers;
914+
915+
return if (!has_git());
916+
917+
($total_signers, @signers) =
918+
git_find_signers("git log --since=$email_git_since -- $file");
919+
git_assign_signers("git_signer", $total_signers, @signers);
920+
}
921+
881922
sub save_commits {
882923
my ($cmd, @commits) = @_;
883924
my $output;
884925
my @lines = ();
885926

927+
return (@lines) if (!has_git());
928+
886929
$output = `${cmd}`;
887930

888931
@lines = split("\n", $output);
@@ -897,13 +940,10 @@ sub save_commits {
897940
sub git_assign_blame {
898941
my ($file) = @_;
899942

900-
my @lines = ();
901-
my @commits = ();
902943
my $cmd;
903-
my $output;
904-
my %hash;
905-
my $total_sign_offs;
906-
my $count;
944+
my @commits = ();
945+
my @signers = ();
946+
my $total_commits;
907947

908948
if (@range) {
909949
foreach my $file_range_diff (@range) {
@@ -922,65 +962,35 @@ sub git_assign_blame {
922962
}
923963
}
924964

925-
$total_sign_offs = 0;
926965
@commits = uniq(@commits);
927-
foreach my $commit (@commits) {
928-
$cmd = "git log -1 ${commit}";
929-
930-
$output = `${cmd}`;
931-
$output =~ s/^\s*//gm;
932-
@lines = split("\n", $output);
933-
934-
@lines = grep(/^[-_ a-z]+by:.*\@.*$/i, @lines);
935-
if (!$email_git_penguin_chiefs) {
936-
@lines = grep(!/${penguin_chiefs}/i, @lines);
937-
}
938-
939-
# cut -f2- -d":"
940-
s/.*:\s*(.+)\s*/$1/ for (@lines);
941-
942-
$total_sign_offs += @lines;
966+
$total_commits = @commits;
943967

944-
if ($email_remove_duplicates) {
945-
@lines = mailmap(@lines);
946-
}
968+
foreach my $commit (@commits) {
969+
my $commit_count;
970+
my @commit_signers = ();
947971

948-
$hash{$_}++ for @lines;
972+
($commit_count, @commit_signers) =
973+
git_find_signers("git log -1 $commit");
974+
@signers = (@signers, @commit_signers);
949975
}
950976

951-
$count = 0;
952-
foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
953-
my $sign_offs = $hash{$line};
954-
my $role;
955-
956-
$count++;
957-
last if ($sign_offs < $email_git_min_signatures ||
958-
$count > $email_git_max_maintainers ||
959-
$sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
960-
push_email_address($line, '');
961-
if ($from_filename) {
962-
$role = "commits";
963-
} else {
964-
$role = "modified commits";
965-
}
966-
if ($output_rolestats) {
967-
my $percent = sprintf("%.0f", $sign_offs * 100 / $total_sign_offs);
968-
$role = "$role:$sign_offs/$total_sign_offs=$percent%";
969-
}
970-
add_role($line, $role);
977+
if ($from_filename) {
978+
git_assign_signers("commits", $total_commits, @signers);
979+
} else {
980+
git_assign_signers("modified commits", $total_commits, @signers);
971981
}
972982
}
973983

974984
sub uniq {
975-
my @parms = @_;
985+
my (@parms) = @_;
976986

977987
my %saw;
978988
@parms = grep(!$saw{$_}++, @parms);
979989
return @parms;
980990
}
981991

982992
sub sort_and_uniq {
983-
my @parms = @_;
993+
my (@parms) = @_;
984994

985995
my %saw;
986996
@parms = sort @parms;
@@ -1008,7 +1018,7 @@ sub merge_email {
10081018
}
10091019

10101020
sub output {
1011-
my @parms = @_;
1021+
my (@parms) = @_;
10121022

10131023
if ($output_multiline) {
10141024
foreach my $line (@parms) {

0 commit comments

Comments
 (0)