aboutsummaryrefslogtreecommitdiff
path: root/scripts/lintcommit.lua
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2023-04-22 14:10:35 +0100
committerGitHub <noreply@github.com>2023-04-22 15:10:35 +0200
commit732cb9e1e0a90f40d9189bffe009bbb7f8456eeb (patch)
tree4c8166a91583c25b2ee41fed02df4a2dd203844e /scripts/lintcommit.lua
parentd89eb71040a1426a7aded5426b5ce5f50f8ce223 (diff)
downloadrneovim-732cb9e1e0a90f40d9189bffe009bbb7f8456eeb.tar.gz
rneovim-732cb9e1e0a90f40d9189bffe009bbb7f8456eeb.tar.bz2
rneovim-732cb9e1e0a90f40d9189bffe009bbb7f8456eeb.zip
ci(lintcommit): use nvim -l
Diffstat (limited to 'scripts/lintcommit.lua')
-rw-r--r--scripts/lintcommit.lua70
1 files changed, 41 insertions, 29 deletions
diff --git a/scripts/lintcommit.lua b/scripts/lintcommit.lua
index f9a4631b7e..7193821afe 100644
--- a/scripts/lintcommit.lua
+++ b/scripts/lintcommit.lua
@@ -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