From nobody Tue Jul 2 17:32:43 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1545234643088227.9698296519257; Wed, 19 Dec 2018 07:50:43 -0800 (PST) Received: from localhost ([::1]:60767 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gZdlE-0004Ip-85 for importer@patchew.org; Wed, 19 Dec 2018 10:27:48 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55460) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gZddF-0005iR-Tl for qemu-devel@nongnu.org; Wed, 19 Dec 2018 10:19:35 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gZddE-0003x2-Ap for qemu-devel@nongnu.org; Wed, 19 Dec 2018 10:19:33 -0500 Received: from mx1.redhat.com ([209.132.183.28]:44030) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gZddD-0003vv-TW for qemu-devel@nongnu.org; Wed, 19 Dec 2018 10:19:32 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EFB0C15552; Wed, 19 Dec 2018 15:19:30 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-76.ams2.redhat.com [10.36.112.76]) by smtp.corp.redhat.com (Postfix) with ESMTP id EA0F417197; Wed, 19 Dec 2018 15:19:29 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 19 Dec 2018 16:18:48 +0100 Message-Id: <20181219151917.3874-7-pbonzini@redhat.com> In-Reply-To: <20181219151917.3874-1-pbonzini@redhat.com> References: <20181219151917.3874-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Wed, 19 Dec 2018 15:19:31 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 06/35] checkpatch: colorize output to terminal X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Add optional colors to make seeing message types a bit easier. The default is to show them on a tty. Inspired by Linux commits 57230297116fa ("checkpatch: colorize output to terminal") and 737c0767758b ("checkpatch: change format of --color argument to --color[=3DWHEN]"). Signed-off-by: Paolo Bonzini Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- scripts/checkpatch.pl | 49 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index b8e78d51c2..3d1ba9e8ae 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -7,6 +7,7 @@ =20 use strict; use warnings; +use Term::ANSIColor qw(:constants); =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 WORD literally + --color[=3DWHEN] Use colors 'always', 'never', or only when = output + is a terminal ('auto'). Default is 'auto'. -h, --help, --version display this help and exit =20 When FILE is - read standard input. @@ -72,6 +76,14 @@ EOM exit($exitcode); } =20 +# Perl's Getopt::Long allows options to take optional arguments after a sp= ace. +# 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 '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); @@ -144,6 +158,16 @@ if (!$chk_patch && !$chk_branch && !$file) { die "One of --file, --branch, --patch is required\n"; } =20 +if ($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; @@ -371,7 +395,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; @@ -1181,14 +1207,23 @@ sub possible { my $prefix =3D ''; =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 - $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"; + + $output =3D (split('\n', $output))[0] . "\n" if ($terse); =20 - push(our @report, $line); + push(our @report, $output); =20 return 1; } @@ -1196,13 +1231,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 2.20.1