]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
checkpatch: allow passing config directory
authorPetr Vorel <pvorel@suse.cz>
Tue, 21 Apr 2026 21:14:06 +0000 (23:14 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 29 May 2026 04:24:39 +0000 (21:24 -0700)
checkpatch.pl searches for .checkpatch.conf in $CWD, $HOME and
$CWD/.scripts.  Allow passing a single directory via CHECKPATCH_CONFIG_DIR
environment variable (empty value is ignored).  This allows to directly
use project configuration file for projects which vendored checkpatch.pl
(e.g.  LTP or u-boot).

Although it'd be more convenient for user to have --conf-dir option
(instead of using environment variable), code would get ugly because
options from the configuration file needs to be read before processing
command line options with Getopt::Long.

While at it, document directories and environment variable in -h help
and HTML doc.

Link: https://lore.kernel.org/20260421211408.383972-1-pvorel@suse.cz
Signed-off-by: Petr Vorel <pvorel@suse.cz>
Reviewed-by: Simon Glass <sjg@chromium.org>
Acked-by: Joe Perches <joe@perches.com>
Cc: Dwaipayan Ray <dwaipayanray1@gmail.com>
Cc: Lukas Bulwahn <lukas.bulwahn@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Documentation/dev-tools/checkpatch.rst
scripts/checkpatch.pl

index dccede68698ca436472df2f9b906aed2186bf0c8..010dfcd615afcbabc18e6add3abeee2f004e67d5 100644 (file)
@@ -210,6 +210,13 @@ Available options:
 
    Display the help text.
 
+Configuration file
+==================
+
+Default configuration options can be stored in ``.checkpatch.conf``, search
+path: ``.:$HOME:.scripts`` or in a directory specified by ``$CHECKPATCH_CONFIG_DIR``
+environment variable (falling back to the default search path).
+
 Message Levels
 ==============
 
index 0492d6afc9a1fc6ca3dfa4e7e8e5764584c4f16f..8b44b3a6a4dd3a4a1becc7decc49feec8f97413a 100755 (executable)
@@ -57,6 +57,9 @@ my %ignore_type = ();
 my @ignore = ();
 my $help = 0;
 my $configuration_file = ".checkpatch.conf";
+my $def_configuration_dirs_help = '.:$HOME:.scripts';
+(my $def_configuration_dirs = $def_configuration_dirs_help) =~ s/\$(\w+)/$ENV{$1}/g;
+my $env_config_dir = 'CHECKPATCH_CONFIG_DIR';
 my $max_line_length = 100;
 my $ignore_perl_version = 0;
 my $minimum_perl_version = 5.10.0;
@@ -146,6 +149,11 @@ Options:
   -h, --help, --version      display this help and exit
 
 When FILE is - read standard input.
+
+CONFIGURATION FILE
+Default configuration options can be stored in $configuration_file,
+search path: '$def_configuration_dirs_help' or in a directory specified by
+\$$env_config_dir environment variable (fallback to the default search path).
 EOM
 
        exit($exitcode);
@@ -237,7 +245,7 @@ sub list_types {
        exit($exitcode);
 }
 
-my $conf = which_conf($configuration_file);
+my $conf = which_conf($configuration_file, $env_config_dir, $def_configuration_dirs);
 if (-f $conf) {
        my @conf_args;
        open(my $conffile, '<', "$conf")
@@ -1531,9 +1539,15 @@ sub which {
 }
 
 sub which_conf {
-       my ($conf) = @_;
+       my ($conf, $env_key, $paths) = @_;
+       my $env_dir = $ENV{$env_key};
+
+       if (defined($env_dir) && $env_dir ne "") {
+               return "$env_dir/$conf" if (-e "$env_dir/$conf");
+               warn "$P: Can't find a readable $conf in '$env_dir', falling back to default search paths\n";
+       }
 
-       foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
+       foreach my $path (split(/:/, $paths)) {
                if (-e "$path/$conf") {
                        return "$path/$conf";
                }