aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
authorJohn Brooks <john@fastquake.com>2017-07-10 18:52:24 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2017-07-10 19:32:35 -0400
commit737c0767758bbd65cf95c44ccc09bca970e2ef8e (patch)
tree0354f58b628c3d954be0b519e85980fda40078e3 /scripts/checkpatch.pl
parent8d81ae05d0176da1c54aeaed697fa34be5c5575e (diff)
checkpatch: change format of --color argument to --color[=WHEN]
The boolean --color argument did not offer the ability to force colourized output even if stdout is not a terminal. Change the format of the argument to the familiar --color[=WHEN] construct as seen in common Linux utilities such as git, ls and dmesg, which allows the user to specify whether to colourize output "always", "never", or "auto" when the output is a terminal. The default is "auto". The old command-line uses of --color and --no-color are unchanged. Link: http://lkml.kernel.org/r/efe43bdbad400f39ba691ae663044462493b0773.1496799721.git.joe@perches.com Signed-off-by: John Brooks <john@fastquake.com> Signed-off-by: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl35
1 files changed, 29 insertions, 6 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index ab12e3040abb..63409dbd0de5 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -57,7 +57,7 @@ my $codespell = 0;
57my $codespellfile = "/usr/share/codespell/dictionary.txt"; 57my $codespellfile = "/usr/share/codespell/dictionary.txt";
58my $conststructsfile = "$D/const_structs.checkpatch"; 58my $conststructsfile = "$D/const_structs.checkpatch";
59my $typedefsfile = ""; 59my $typedefsfile = "";
60my $color = 1; 60my $color = "auto";
61my $allow_c99_comments = 1; 61my $allow_c99_comments = 1;
62 62
63sub help { 63sub help {
@@ -116,7 +116,8 @@ Options:
116 (default:/usr/share/codespell/dictionary.txt) 116 (default:/usr/share/codespell/dictionary.txt)
117 --codespellfile Use this codespell dictionary 117 --codespellfile Use this codespell dictionary
118 --typedefsfile Read additional types from this file 118 --typedefsfile Read additional types from this file
119 --color Use colors when output is STDOUT (default: on) 119 --color[=WHEN] Use colors 'always', 'never', or only when output
120 is a terminal ('auto'). Default is 'auto'.
120 -h, --help, --version display this help and exit 121 -h, --help, --version display this help and exit
121 122
122When FILE is - read standard input. 123When FILE is - read standard input.
@@ -182,6 +183,14 @@ if (-f $conf) {
182 unshift(@ARGV, @conf_args) if @conf_args; 183 unshift(@ARGV, @conf_args) if @conf_args;
183} 184}
184 185
186# Perl's Getopt::Long allows options to take optional arguments after a space.
187# Prevent --color by itself from consuming other arguments
188foreach (@ARGV) {
189 if ($_ eq "--color" || $_ eq "-color") {
190 $_ = "--color=$color";
191 }
192}
193
185GetOptions( 194GetOptions(
186 'q|quiet+' => \$quiet, 195 'q|quiet+' => \$quiet,
187 'tree!' => \$tree, 196 'tree!' => \$tree,
@@ -212,7 +221,9 @@ GetOptions(
212 'codespell!' => \$codespell, 221 'codespell!' => \$codespell,
213 'codespellfile=s' => \$codespellfile, 222 'codespellfile=s' => \$codespellfile,
214 'typedefsfile=s' => \$typedefsfile, 223 'typedefsfile=s' => \$typedefsfile,
215 'color!' => \$color, 224 'color=s' => \$color,
225 'no-color' => \$color, #keep old behaviors of -nocolor
226 'nocolor' => \$color, #keep old behaviors of -nocolor
216 'h|help' => \$help, 227 'h|help' => \$help,
217 'version' => \$help 228 'version' => \$help
218) or help(1); 229) or help(1);
@@ -238,6 +249,18 @@ if ($#ARGV < 0) {
238 push(@ARGV, '-'); 249 push(@ARGV, '-');
239} 250}
240 251
252if ($color =~ /^[01]$/) {
253 $color = !$color;
254} elsif ($color =~ /^always$/i) {
255 $color = 1;
256} elsif ($color =~ /^never$/i) {
257 $color = 0;
258} elsif ($color =~ /^auto$/i) {
259 $color = (-t STDOUT);
260} else {
261 die "Invalid color mode: $color\n";
262}
263
241sub hash_save_array_words { 264sub hash_save_array_words {
242 my ($hashRef, $arrayRef) = @_; 265 my ($hashRef, $arrayRef) = @_;
243 266
@@ -1883,7 +1906,7 @@ sub report {
1883 return 0; 1906 return 0;
1884 } 1907 }
1885 my $output = ''; 1908 my $output = '';
1886 if (-t STDOUT && $color) { 1909 if ($color) {
1887 if ($level eq 'ERROR') { 1910 if ($level eq 'ERROR') {
1888 $output .= RED; 1911 $output .= RED;
1889 } elsif ($level eq 'WARNING') { 1912 } elsif ($level eq 'WARNING') {
@@ -1894,10 +1917,10 @@ sub report {
1894 } 1917 }
1895 $output .= $prefix . $level . ':'; 1918 $output .= $prefix . $level . ':';
1896 if ($show_types) { 1919 if ($show_types) {
1897 $output .= BLUE if (-t STDOUT && $color); 1920 $output .= BLUE if ($color);
1898 $output .= "$type:"; 1921 $output .= "$type:";
1899 } 1922 }
1900 $output .= RESET if (-t STDOUT && $color); 1923 $output .= RESET if ($color);
1901 $output .= ' ' . $msg . "\n"; 1924 $output .= ' ' . $msg . "\n";
1902 1925
1903 if ($showfile) { 1926 if ($showfile) {