Skip to content

Commit 39a63f1

Browse files
committed
checkpatch: favor version of the script supplied by projects
Linux kernel is not the only project that uses checkpatch.pl for patch validation, other projects (for example qemu) use it too, and their version is different from the one used by the kernel. Also checkpatch.pl changes from kernel version to kernel version when working with multiple versions, so requiring a single version covering everything is not practical. Additionally running checkpatch.pl on projects that do not use it and do not use kernel coding style is counter-productive. To solve this issue make the linter locate and favor the version of checkpatch.pl belonging to the project that file being linted belongs to. Also, if checkpatch.pl is not accessible use "true" as the command so that there are no errors/notifications and no diagnostic.
1 parent a0718ad commit 39a63f1

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

lua/lint/linters/checkpatch.lua

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
-- The scripts/ directory of the Linux kernel tree needs to be in your PATH or
2-
-- the full path to the checkpatch.pl script can be overriden from user config:
1+
-- This favors the version of `checkpatch.pl` supplied by the project
2+
-- to which the file being linted belongs, falling back to a version
3+
-- accessible via `$PATH`. If a user wishes to use a different version
4+
-- of the script they can override "cmd" in their config:
35
-- require("lint").linters.checkpatch.cmd = '…/checkpatch.pl'
46

7+
local tool_name = 'checkpatch.pl'
8+
59
-- path/to/file:line: severity: message
610
local pattern = '([^:]+):(%d+): (%a+): (.+)'
711
local groups = { 'file', 'lnum', 'severity', 'message' }
@@ -11,8 +15,39 @@ local severity_map = {
1115
['CHECK'] = vim.diagnostic.severity.INFO,
1216
}
1317

18+
local function locate_checkpatch()
19+
local current_file = vim.api.nvim_buf_get_name(0)
20+
local dir = current_file ~= '' and vim.fs.dirname(current_file) or vim.fn.getcwd()
21+
22+
-- FIXME: switch to using vim.fs.root() once minimal version is bumped to 0.10.
23+
local git_dir = vim.fs.find('.git', {
24+
path = dir,
25+
upward = true,
26+
type = 'directory',
27+
limit = 1,
28+
})[1]
29+
30+
if git_dir then
31+
local project_root = vim.fs.dirname(git_dir)
32+
local tool_path = vim.fs.joinpath(project_root, 'scripts', tool_name)
33+
if vim.fn.executable(tool_path) == 1 then
34+
return tool_path
35+
end
36+
end
37+
38+
if vim.fn.executable(tool_name) == 1 then
39+
return tool_name
40+
end
41+
42+
-- If checkpatch executable is not found return "true" which is a valid command
43+
-- that will not produce any diagnostic but return success error code. This will
44+
-- allow enabling checkpatch linter globally and only return diagnostic if
45+
-- a project is actually using it.
46+
return "true"
47+
end
48+
1449
return {
15-
cmd = 'checkpatch.pl',
50+
cmd = locate_checkpatch,
1651
stdin = false,
1752
args = {
1853
'--strict',

0 commit comments

Comments
 (0)