ci(lintcommit): use nvim -l

This commit is contained in:
Lewis Russell 2023-04-22 14:10:35 +01:00 committed by GitHub
parent d89eb71040
commit 732cb9e1e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 31 deletions

View File

@ -16,4 +16,4 @@ jobs:
- uses: rhysd/action-setup-vim@v1
with:
neovim: true
- run: nvim --clean -es +"lua require('scripts.lintcommit').main({trace=false})"
- run: nvim --clean -l scripts/lintcommit.lua main --notrace

View File

@ -259,7 +259,7 @@ add_glob_target(
TOUCH_STRATEGY SINGLE)
add_custom_target(lintcommit
COMMAND ${PROJECT_BINARY_DIR}/bin/nvim -u NONE -es -c [[lua require('scripts.lintcommit').main({trace=false})]]
COMMAND ${PROJECT_BINARY_DIR}/bin/nvim -u NONE -l scripts/lintcommit.lua main --notrace
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
VERBATIM)
add_dependencies(lintcommit nvim)

View File

@ -1,41 +1,30 @@
-- Usage:
-- # verbose
-- nvim -es +"lua require('scripts.lintcommit').main()"
-- nvim -l scripts/lintcommit.lua main
--
-- # silent
-- nvim -es +"lua require('scripts.lintcommit').main({trace=false})"
-- nvim -l scripts/lintcommit.lua main --notrace
--
-- # self-test
-- nvim -es +"lua require('scripts.lintcommit')._test()"
-- nvim -l scripts/lintcommit.lua _test
--- @type table<string,fun(opt: LintcommitOptions)>
local M = {}
local _trace = false
-- Print message
local function p(s)
vim.cmd('set verbose=1')
vim.api.nvim_echo({{s, ''}}, false, {})
vim.cmd('set verbose=0')
end
local function die()
p('')
vim.cmd("cquit 1")
end
-- Executes and returns the output of `cmd`, or nil on failure.
--
-- Prints `cmd` if `trace` is enabled.
local function run(cmd, or_die)
if _trace then
p('run: '..vim.inspect(cmd))
print('run: '..vim.inspect(cmd))
end
local rv = vim.trim(vim.fn.system(cmd)) or ''
if vim.v.shell_error ~= 0 then
if or_die then
p(rv)
die()
print(rv)
os.exit(1)
end
return nil
end
@ -150,6 +139,7 @@ local function validate_commit(commit_message)
return nil
end
--- @param opt? LintcommitOptions
function M.main(opt)
_trace = not opt or not not opt.trace
@ -160,17 +150,18 @@ function M.main(opt)
ancestor = run({'git', 'merge-base', 'upstream/master', branch})
end
local commits_str = run({'git', 'rev-list', ancestor..'..'..branch}, true)
assert(commits_str)
local commits = {}
local commits = {} --- @type string[]
for substring in commits_str:gmatch("%S+") do
table.insert(commits, substring)
table.insert(commits, substring)
end
local failed = 0
for _, commit_id in ipairs(commits) do
local msg = run({'git', 'show', '-s', '--format=%s' , commit_id})
if vim.v.shell_error ~= 0 then
p('Invalid commit-id: '..commit_id..'"')
print('Invalid commit-id: '..commit_id..'"')
else
local invalid_msg = validate_commit(msg)
if invalid_msg then
@ -178,10 +169,10 @@ function M.main(opt)
-- Some breathing room
if failed == 1 then
p('\n')
print('\n')
end
p(string.format([[
print(string.format([[
Invalid commit message: "%s"
Commit: %s
%s
@ -194,13 +185,13 @@ Invalid commit message: "%s"
end
if failed > 0 then
p([[
print([[
See also:
https://github.com/neovim/neovim/blob/master/CONTRIBUTING.md#commit-messages
]])
die() -- Exit with error.
os.exit(1)
else
p('')
print('')
end
end
@ -261,14 +252,35 @@ function M._test()
local is_valid = (nil == validate_commit(message))
if is_valid ~= expected then
failed = failed + 1
p(string.format('[ FAIL ]: expected=%s, got=%s\n input: "%s"', expected, is_valid, message))
print(string.format('[ FAIL ]: expected=%s, got=%s\n input: "%s"', expected, is_valid, message))
end
end
if failed > 0 then
die() -- Exit with error.
os.exit(1)
end
end
return M
--- @class LintcommitOptions
--- @field trace? boolean
local opt = {}
for _, a in ipairs(arg) do
if vim.startswith(a, '--') then
local nm, val = a:sub(3), true
if vim.startswith(a, '--no') then
nm, val = a:sub(5), false
end
if nm == 'trace' then
opt.trace = val
end
end
end
for _, a in ipairs(arg) do
if M[a] then
M[a](opt)
end
end