Received: from eggs.gnu.org ([2001:4830:134:3::10]:38603) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gSPNY-0003FF-Hl for qemu-devel@nongnu.org; Thu, 29 Nov 2018 11:41:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gSPNQ-0000xr-QN for qemu-devel@nongnu.org; Thu, 29 Nov 2018 11:41:28 -0500 Received: from mx1.redhat.com ([209.132.183.28]:26839) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gSPNO-0000vY-QS for qemu-devel@nongnu.org; Thu, 29 Nov 2018 11:41:20 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D86133082B10 for ; Thu, 29 Nov 2018 16:41:11 +0000 (UTC) From: Markus Armbruster References: <20181129090120.28828-1-pbonzini@redhat.com> <20181129090120.28828-5-pbonzini@redhat.com> <2eed95c3-4598-6b4c-674c-4eef2dc598bc@redhat.com> Date: Thu, 29 Nov 2018 17:41:07 +0100 In-Reply-To: <2eed95c3-4598-6b4c-674c-4eef2dc598bc@redhat.com> ("Philippe =?utf-8?Q?Mathieu-Daud=C3=A9=22's?= message of "Thu, 29 Nov 2018 15:09:34 +0100") Message-ID: <87tvjz6dlo.fsf@dusky.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH for-4.0 4/4] checkpatch: colorize output to terminal List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Philippe =?utf-8?Q?Mathieu-Daud=C3=A9?= Cc: Paolo Bonzini , qemu-devel@nongnu.org Philippe Mathieu-Daud=C3=A9 writes: > On 29/11/18 10:01, Paolo Bonzini wrote: >> Add optional colors to make seeing message types a bit easier. >> The default is to show them on a tty. The chosen colors should resemble >> gcc's. >>=20 >> Inspired by Linux commit 57230297116faf5b0a995916d5dd5fedab54cba3 >> ("checkpatch: colorize output to terminal"). And also commit 737c0767758b "checkpatch: change format of --color argument to --color[=3DWHEN]". Funny: ERROR: Missing Signed-off-by: line(s) total: 1 errors, 0 warnings, 114 lines checked >> --- >> scripts/checkpatch.pl | 51 +++++++++++++++++++++++++++++++++++++------ >> 1 file changed, 44 insertions(+), 7 deletions(-) >>=20 >> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl >> index f5284d910c..14be11719c 100755 >> --- a/scripts/checkpatch.pl >> +++ b/scripts/checkpatch.pl >> @@ -7,6 +7,7 @@ >>=20=20 >> use strict; >> use warnings; >> +use Term::ANSIColor qw(:constants); >>=20=20 >> my $P =3D $0; >> $P =3D~ s@.*/@@g; >> @@ -26,6 +27,7 @@ my $tst_only; >> my $emacs =3D 0; >> my $terse =3D 0; >> my $file =3D undef; >> +my $color =3D "auto"; >> my $no_warnings =3D 0; >> my $summary =3D 1; >> my $mailback =3D 0; >> @@ -64,6 +66,8 @@ Options: >> is all off) >> --test-only=3DWORD report only warnings/errors containing W= ORD >> literally >> + --color[=3DWHEN] Use colors 'always', 'never', or only wh= en output >> + is a terminal ('auto'). Default is 'auto'. >> -h, --help, --version display this help and exit >>=20=20 >> When FILE is - read standard input. >> @@ -72,6 +76,14 @@ EOM >> exit($exitcode); >> } >>=20=20 >> +# Perl's Getopt::Long allows options to take optional arguments after a= space. >> +# Prevent --color by itself from consuming other arguments >> +foreach (@ARGV) { >> + if ($_ eq "--color" || $_ eq "-color") { >> + $_ =3D "--color=3D$color"; >> + } >> +} >> + >> GetOptions( >> 'q|quiet+' =3D> \$quiet, >> 'tree!' =3D> \$tree, >> @@ -89,6 +101,8 @@ GetOptions( >>=20=20 >> 'debug=3Ds' =3D> \%debug, >> 'test-only=3Ds' =3D> \$tst_only, >> + 'color=3Ds' =3D> \$color, >> + 'no-color' =3D> sub { $color =3D 'never'; }, >> 'h|help' =3D> \$help, >> 'version' =3D> \$help >> ) or help(1); Note for next hunk: Linux has 'color=3Ds' =3D> \$color, 'no-color' =3D> \$color, #keep old behaviors of -nocolor 'nocolor' =3D> \$color, #keep old behaviors of -nocolor >> @@ -144,6 +158,18 @@ if (!$chk_patch && !$chk_branch && !$file) { >> die "One of --file, --branch, --patch is required\n"; >> } >>=20=20 >> +if ($color =3D~ /^[01]$/) { >> + $color =3D !$color; > > Without this if: > Reviewed-by: Philippe Mathieu-Daud=C3=A9 Well, there's value in keeping our checkpatch.pl reasonably close to the original. But I agree with Philippe, if we deviate in the argument of GetOptions, it makes sense to deviate here, too. >> +} elsif ($color =3D~ /^always$/i) { >> + $color =3D 1; >> +} elsif ($color =3D~ /^never$/i) { >> + $color =3D 0; >> +} elsif ($color =3D~ /^auto$/i) { >> + $color =3D (-t STDOUT); >> +} else { >> + die "Invalid color mode: $color\n"; >> +} >> + >> my $dbg_values =3D 0; >> my $dbg_possible =3D 0; >> my $dbg_type =3D 0; >> @@ -372,7 +398,9 @@ if ($chk_branch) { >> close($FILE); >> $vname =3D substr($hash, 0, 12) . ' (' . $git_commits{$hash} . ')'; >> if ($num_patches > 1 && $quiet =3D=3D 0) { >> - print "$i/$num_patches Checking commit $vname\n"; >> + my $prefix =3D "$i/$num_patches"; >> + $prefix =3D BLUE . BOLD . $prefix . RESET if $color; >> + print "$prefix Checking commit $vname\n"; >> $vname =3D "Patch $i/$num_patches"; >> } else { >> $vname =3D "Commit " . $vname; Linux doesn't have this hunk, because it doesn't have all of your (Linux-inspired) PATCH 3. Okay, but it makes me wonder whether the missing parts would make sense for Linux's checkpatch.pl, too. >> @@ -1182,14 +1210,23 @@ sub possible { >> my $prefix =3D ''; >>=20=20 >> sub report { >> - if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) { >> + my ($level, $msg) =3D @_; >> + if (defined $tst_only && $msg !~ /\Q$tst_only\E/) { >> return 0; >> } >> - my $line =3D $prefix . $_[0]; >>=20=20 >> - $line =3D (split('\n', $line))[0] . "\n" if ($terse); >> + my $output =3D ''; >> + $output .=3D BOLD if $color; >> + $output .=3D $prefix; >> + $output .=3D RED if $color && $level eq 'ERROR'; >> + $output .=3D MAGENTA if $color && $level eq 'WARNING'; >> + $output .=3D $level . ':'; >> + $output .=3D RESET if $color; >> + $output .=3D ' ' . $msg . "\n"; More idiomatic than Linux's nested if. Worth the deviation? Hmm, the colors differ, too: Your patch Linux ERROR RED RED WARNING MAGENTA YELLOW other GREEN In addition, you use BOLD. Worth the deviation? Note that I'm not asking for a debate here, I merely want you to consider the tradeoff. "Yes" would be a sufficient answer as far as I'm concerned :) >> + >> + $output =3D (split('\n', $output))[0] . "\n" if ($terse); >>=20=20 >> - push(our @report, $line); >> + push(our @report, $output); >>=20=20 >> return 1; >> } >> @@ -1197,13 +1234,13 @@ sub report_dump { >> our @report; >> } >> sub ERROR { >> - if (report("ERROR: $_[0]\n")) { >> + if (report("ERROR", $_[0])) { >> our $clean =3D 0; >> our $cnt_error++; >> } >> } >> sub WARN { >> - if (report("WARNING: $_[0]\n")) { >> + if (report("WARNING", $_[0])) { >> our $clean =3D 0; >> our $cnt_warn++; >> } >>=20