build(lint): commit linter #15725

Example test failure:

    $ nvim -es +"lua require('scripts.lintcommit')._test()"
    [ FAIL ]: expected=true, got=false
        input: ":no type before colon 1"
    [ FAIL ]: expected=true, got=false
        input: "ci: tab after colon"

Example main({trace=true}) output:

    $ nvim -es +"lua require('scripts.lintcommit').main()"
    run: { "git", "branch", "--show-current" }
    run: { "git", "merge-base", "origin/master", "master" }
    run: { "git", "merge-base", "upstream/master", "master" }
    run: { "git", "rev-list", "89db07556dbdce97c0c150ed7e47d80e1ddacad3..master" }
    run: { "git", "show", "-s", "--format=%s", "d2e6d2f5fc93b6da3c6153229135ba2f0b24f8cc" }
    Invalid commit message: "buildlint): commit linter #15620"
        Commit: d2e6d2f5fc93b6da3c6153229135ba2f0b24f8cc
        Invalid commit type "buildlint)". Allowed types are:
        { "build", "ci", "docs", "feat", "fix", "perf", "refactor", "revert", "test", "chore", "vim-patch" }
        See also:
            https://github.com/neovim/neovim/blob/master/CONTRIBUTING.md#commit-messages
            https://www.conventionalcommits.org/en/v1.0.0/
This commit is contained in:
Justin M. Keyes
2021-09-19 12:57:57 -07:00
committed by GitHub
parent 89db07556d
commit 6565adcbff

View File

@@ -1,7 +1,16 @@
-- Usage: -- Usage:
-- nvim -es +'luafile scripts/lintcommit.lua' -- # verbose
-- nvim -es +"lua require('scripts.lintcommit').main()"
--
-- # silent
-- nvim -es +"lua require('scripts.lintcommit').main({trace=false})"
--
-- # self-test
-- nvim -es +"lua require('scripts.lintcommit')._test()"
local trace = true local M = {}
local _trace = false
-- Print message -- Print message
local function p(s) local function p(s)
@@ -19,7 +28,7 @@ end
-- --
-- Prints `cmd` if `trace` is enabled. -- Prints `cmd` if `trace` is enabled.
local function run(cmd, or_die) local function run(cmd, or_die)
if trace then if _trace then
p('run: '..vim.inspect(cmd)) p('run: '..vim.inspect(cmd))
end end
local rv = vim.trim(vim.fn.system(cmd)) or '' local rv = vim.trim(vim.fn.system(cmd)) or ''
@@ -33,25 +42,25 @@ local function run(cmd, or_die)
return rv return rv
end end
local function commit_message_is_ok(commit_message) -- Returns nil if the given commit message is valid, or returns a string
-- message explaining why it is invalid.
local function validate_commit(commit_message)
local commit_split = vim.split(commit_message, ":") local commit_split = vim.split(commit_message, ":")
-- Return true if the type is vim-patch since most of the normal rules don't -- Return true if the type is vim-patch since most of the normal rules don't
-- apply. -- apply.
if commit_split[1] == "vim-patch" then if commit_split[1] == "vim-patch" then
return true return nil
end end
-- Check that message isn't too long. -- Check that message isn't too long.
if commit_message:len() > 80 then if commit_message:len() > 80 then
p([[Commit message is too long, a maximum of 80 characters is allowed.]]) return [[Commit message is too long, a maximum of 80 characters is allowed.]]
return false
end end
-- Return false if no colons are detected.
if vim.tbl_count(commit_split) < 2 then if vim.tbl_count(commit_split) < 2 then
p([[Commit message does not include colons.]]) return [[Commit message does not include colons.]]
return false
end end
local before_colon = commit_split[1] local before_colon = commit_split[1]
@@ -64,39 +73,41 @@ local function commit_message_is_ok(commit_message)
-- Check if type is correct -- Check if type is correct
local type = vim.split(before_colon, "%(")[1] local type = vim.split(before_colon, "%(")[1]
local allowed_types = {"build", "ci", "docs", "feat", "fix", "perf", "refactor", "revert", "test", "chore"} local allowed_types = {'build', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'test', 'chore', 'vim-patch'}
if not vim.tbl_contains(allowed_types, type) then if not vim.tbl_contains(allowed_types, type) then
p([[Commit type is not recognized. Allowed types are: build, ci, docs, feat, fix, perf, refactor, revert, test, chore.]]) return string.format(
return false 'Invalid commit type "%s". Allowed types are:\n %s',
type,
vim.inspect(allowed_types))
end end
-- Check if scope is empty -- Check if scope is empty
if before_colon:match("%(") then if before_colon:match("%(") then
local scope = vim.trim(before_colon:match("%((.*)%)")) local scope = vim.trim(before_colon:match("%((.*)%)"))
if scope == '' then if scope == '' then
p([[Scope can't be empty.]]) return [[Scope can't be empty.]]
return false
end end
end end
-- Check that description doesn't end with a period -- Check that description doesn't end with a period
if vim.endswith(after_colon, ".") then if vim.endswith(after_colon, ".") then
p([[Description ends with a period (\".\").]]) return [[Description ends with a period (\".\").]]
return false
end end
-- Check that description has exactly one whitespace after colon, followed by -- Check that description has exactly one whitespace after colon, followed by
-- a lowercase letter and then any number of letters. -- a lowercase letter and then any number of letters.
if not string.match(after_colon, '^ %l%a*') then if not string.match(after_colon, '^ %l%a*') then
p([[There should be one whitespace after the colon and the first letter should lowercase.]]) return [[There should be one whitespace after the colon and the first letter should lowercase.]]
return false
end end
return true return nil
end end
local function main() function M.main(opt)
_trace = not opt or not not opt.trace
local branch = run({'git', 'branch', '--show-current'}, true) local branch = run({'git', 'branch', '--show-current'}, true)
-- TODO(justinmk): check $GITHUB_REF
local ancestor = run({'git', 'merge-base', 'origin/master', branch}) local ancestor = run({'git', 'merge-base', 'origin/master', branch})
if not ancestor then if not ancestor then
ancestor = run({'git', 'merge-base', 'upstream/master', branch}) ancestor = run({'git', 'merge-base', 'upstream/master', branch})
@@ -108,76 +119,87 @@ local function main()
table.insert(commits, substring) table.insert(commits, substring)
end end
for _, commit_hash in ipairs(commits) do local failed = 0
local message = run({'git', 'show', '-s', '--format=%s' , commit_hash}) for _, commit_id in ipairs(commits) do
local msg = run({'git', 'show', '-s', '--format=%s' , commit_id})
if vim.v.shell_error ~= 0 then if vim.v.shell_error ~= 0 then
p('Invalid commit-id: '..commit_hash..'"') p('Invalid commit-id: '..commit_id..'"')
elseif not commit_message_is_ok(message) then else
p('Invalid commit format: '..message) local invalid_msg = validate_commit(msg)
die() if invalid_msg then
failed = failed + 1
p(string.format([[
Invalid commit message: "%s"
Commit: %s
%s
See also:
https://github.com/neovim/neovim/blob/master/CONTRIBUTING.md#commit-messages
https://www.conventionalcommits.org/en/v1.0.0/
]],
msg,
commit_id,
invalid_msg))
end end
end end
end
if failed > 0 then
die() -- Exit with error.
else
p('')
end
end end
local function _test() function M._test()
local good_messages = { -- message:expected_result
"ci: normal message", local test_cases = {
"build: normal message", ['ci: normal message'] = true,
"docs: normal message", ['build: normal message'] = true,
"feat: normal message", ['docs: normal message'] = true,
"fix: normal message", ['feat: normal message'] = true,
"perf: normal message", ['fix: normal message'] = true,
"refactor: normal message", ['perf: normal message'] = true,
"revert: normal message", ['refactor: normal message'] = true,
"test: normal message", ['revert: normal message'] = true,
"chore: normal message", ['test: normal message'] = true,
"ci(window): message with scope", ['chore: normal message'] = true,
"ci!: message with breaking change", ['ci(window): message with scope'] = true,
"ci(tui)!: message with scope and breaking change", ['ci!: message with breaking change'] = true,
"vim-patch:8.2.3374: Pyret files are not recognized (#15642)", ['ci(tui)!: message with scope and breaking change'] = true,
"vim-patch:8.1.1195,8.2.{3417,3419}", ['vim-patch:8.2.3374: Pyret files are not recognized (#15642)'] = true,
['vim-patch:8.1.1195,8.2.{3417,3419}'] = true,
[':no type before colon 1'] = false,
[' :no type before colon 2'] = false,
[' :no type before colon 3'] = false,
['ci(empty description):'] = false,
['ci(whitespace as description): '] = false,
['docs(multiple whitespaces as description): '] = false,
['ci no colon after type'] = false,
['test: extra space after colon'] = false,
['ci: tab after colon'] = false,
['ci:no space after colon'] = false,
['ci :extra space before colon'] = false,
['refactor(): empty scope'] = false,
['ci( ): whitespace as scope'] = false,
['chore: period at end of sentence.'] = false,
['ci: Starting sentence capitalized'] = false,
['unknown: using unknown type'] = false,
['chore: you\'re saying this commit message just goes on and on and on and on and on and on for way too long?'] = false,
} }
local bad_messages = { local failed = 0
":no type before colon 1", for message, expected in pairs(test_cases) do
" :no type before colon 2", local is_valid = (nil == validate_commit(message))
" :no type before colon 3", if is_valid ~= expected then
"ci(empty description):", failed = failed + 1
"ci(whitespace as description): ", p(string.format('[ FAIL ]: expected=%s, got=%s\n input: "%s"', expected, is_valid, message))
"docs(multiple whitespaces as description): ",
"ci no colon after type",
"test: extra space after colon",
"ci: tab after colon",
"ci:no space after colon",
"ci :extra space before colon",
"refactor(): empty scope",
"ci( ): whitespace as scope",
"chore: period at end of sentence.",
"ci: Starting sentence capitalized",
"unknown: using unknown type",
"chore: you're saying this commit message just goes on and on and on and on and on and on for way too long?",
}
p('Messages expected to pass:')
for _, message in ipairs(good_messages) do
if commit_message_is_ok(message) then
p('[ PASSED ] : '..message)
else
p('[ FAIL ] : '..message)
end end
end end
p("Messages expected to fail:") if failed > 0 then
die() -- Exit with error.
end
for _, message in ipairs(bad_messages) do
if commit_message_is_ok(message) then
p('[ PASSED ] : '..message)
else
p('[ FAIL ] : '..message)
end
end
end end
-- _test() return M
main()