lintcommit.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. -- Usage:
  2. -- # verbose
  3. -- nvim -l scripts/lintcommit.lua main --trace
  4. --
  5. -- # silent
  6. -- nvim -l scripts/lintcommit.lua main
  7. --
  8. -- # self-test
  9. -- nvim -l scripts/lintcommit.lua _test
  10. --- @type table<string,fun(opt: LintcommitOptions)>
  11. local M = {}
  12. local _trace = false
  13. -- Print message
  14. local function p(s)
  15. vim.cmd('set verbose=1')
  16. vim.api.nvim_echo({ { s, '' } }, false, {})
  17. vim.cmd('set verbose=0')
  18. end
  19. -- Executes and returns the output of `cmd`, or nil on failure.
  20. --
  21. -- Prints `cmd` if `trace` is enabled.
  22. local function run(cmd, or_die)
  23. if _trace then
  24. p('run: ' .. vim.inspect(cmd))
  25. end
  26. local rv = vim.trim(vim.fn.system(cmd)) or ''
  27. if vim.v.shell_error ~= 0 then
  28. if or_die then
  29. p(rv)
  30. os.exit(1)
  31. end
  32. return nil
  33. end
  34. return rv
  35. end
  36. -- Returns nil if the given commit message is valid, or returns a string
  37. -- message explaining why it is invalid.
  38. local function validate_commit(commit_message)
  39. local commit_split = vim.split(commit_message, ':', { plain = true })
  40. -- Return nil if the type is vim-patch since most of the normal rules don't
  41. -- apply.
  42. if commit_split[1] == 'vim-patch' then
  43. return nil
  44. end
  45. -- Check that message isn't too long.
  46. if commit_message:len() > 80 then
  47. return [[Commit message is too long, a maximum of 80 characters is allowed.]]
  48. end
  49. local before_colon = commit_split[1]
  50. local after_idx = 2
  51. if before_colon:match('^[^%(]*%([^%)]*$') then
  52. -- Need to find the end of commit scope when commit scope contains colons.
  53. while after_idx <= vim.tbl_count(commit_split) do
  54. after_idx = after_idx + 1
  55. if commit_split[after_idx - 1]:find(')') then
  56. break
  57. end
  58. end
  59. end
  60. if after_idx > vim.tbl_count(commit_split) then
  61. return [[Commit message does not include colons.]]
  62. end
  63. local after_colon_split = {}
  64. while after_idx <= vim.tbl_count(commit_split) do
  65. table.insert(after_colon_split, commit_split[after_idx])
  66. after_idx = after_idx + 1
  67. end
  68. local after_colon = table.concat(after_colon_split, ':')
  69. -- Check if commit introduces a breaking change.
  70. if vim.endswith(before_colon, '!') then
  71. before_colon = before_colon:sub(1, -2)
  72. end
  73. -- Check if type is correct
  74. local type = vim.split(before_colon, '(', { plain = true })[1]
  75. local allowed_types =
  76. { 'build', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'test', 'vim-patch' }
  77. if not vim.tbl_contains(allowed_types, type) then
  78. return string.format(
  79. [[Invalid commit type "%s". Allowed types are:
  80. %s.
  81. If none of these seem appropriate then use "fix"]],
  82. type,
  83. vim.inspect(allowed_types)
  84. )
  85. end
  86. -- Check if scope is appropriate
  87. if before_colon:match('%(') then
  88. local scope = vim.trim(commit_message:match('%((.-)%)'))
  89. if scope == '' then
  90. return [[Scope can't be empty]]
  91. end
  92. if vim.startswith(scope, 'nvim_') then
  93. return [[Scope should be "api" instead of "nvim_..."]]
  94. end
  95. local alternative_scope = {
  96. ['filetype.vim'] = 'filetype',
  97. ['filetype.lua'] = 'filetype',
  98. ['tree-sitter'] = 'treesitter',
  99. ['ts'] = 'treesitter',
  100. ['hl'] = 'highlight',
  101. }
  102. if alternative_scope[scope] then
  103. return ('Scope should be "%s" instead of "%s"'):format(alternative_scope[scope], scope)
  104. end
  105. end
  106. -- Check that description doesn't end with a period
  107. if vim.endswith(after_colon, '.') then
  108. return [[Description ends with a period (".").]]
  109. end
  110. -- Check that description starts with a whitespace.
  111. if after_colon:sub(1, 1) ~= ' ' then
  112. return [[There should be a whitespace after the colon.]]
  113. end
  114. -- Check that description doesn't start with multiple whitespaces.
  115. if after_colon:sub(1, 2) == ' ' then
  116. return [[There should only be one whitespace after the colon.]]
  117. end
  118. -- Allow lowercase or ALL_UPPER but not Titlecase.
  119. if after_colon:match('^ *%u%l') then
  120. return [[Description first word should not be Capitalized.]]
  121. end
  122. -- Check that description isn't just whitespaces
  123. if vim.trim(after_colon) == '' then
  124. return [[Description shouldn't be empty.]]
  125. end
  126. return nil
  127. end
  128. --- @param opt? LintcommitOptions
  129. function M.main(opt)
  130. _trace = not opt or not not opt.trace
  131. local branch = run({ 'git', 'rev-parse', '--abbrev-ref', 'HEAD' }, true)
  132. -- TODO(justinmk): check $GITHUB_REF
  133. local ancestor = run({ 'git', 'merge-base', 'origin/master', branch })
  134. if not ancestor then
  135. ancestor = run({ 'git', 'merge-base', 'upstream/master', branch })
  136. end
  137. local commits_str = run({ 'git', 'rev-list', ancestor .. '..' .. branch }, true)
  138. assert(commits_str)
  139. local commits = {} --- @type string[]
  140. for substring in commits_str:gmatch('%S+') do
  141. table.insert(commits, substring)
  142. end
  143. local failed = 0
  144. for _, commit_id in ipairs(commits) do
  145. local msg = run({ 'git', 'show', '-s', '--format=%s', commit_id })
  146. if vim.v.shell_error ~= 0 then
  147. p('Invalid commit-id: ' .. commit_id .. '"')
  148. else
  149. local invalid_msg = validate_commit(msg)
  150. if invalid_msg then
  151. failed = failed + 1
  152. -- Some breathing room
  153. if failed == 1 then
  154. p('\n')
  155. end
  156. p(string.format(
  157. [[
  158. Invalid commit message: "%s"
  159. Commit: %s
  160. %s
  161. ]],
  162. msg,
  163. commit_id,
  164. invalid_msg
  165. ))
  166. end
  167. end
  168. end
  169. if failed > 0 then
  170. p([[
  171. See also:
  172. https://github.com/neovim/neovim/blob/master/CONTRIBUTING.md#commit-messages
  173. ]])
  174. os.exit(1)
  175. else
  176. p('')
  177. end
  178. end
  179. function M._test()
  180. -- message:expected_result
  181. local test_cases = {
  182. ['ci: normal message'] = true,
  183. ['build: normal message'] = true,
  184. ['docs: normal message'] = true,
  185. ['feat: normal message'] = true,
  186. ['fix: normal message'] = true,
  187. ['perf: normal message'] = true,
  188. ['refactor: normal message'] = true,
  189. ['revert: normal message'] = true,
  190. ['test: normal message'] = true,
  191. ['ci(window): message with scope'] = true,
  192. ['ci!: message with breaking change'] = true,
  193. ['ci(tui)!: message with scope and breaking change'] = true,
  194. ['vim-patch:8.2.3374: Pyret files are not recognized (#15642)'] = true,
  195. ['vim-patch:8.1.1195,8.2.{3417,3419}'] = true,
  196. ['revert: "ci: use continue-on-error instead of "|| true""'] = true,
  197. ['fixup'] = false,
  198. ['fixup: commit message'] = false,
  199. ['fixup! commit message'] = false,
  200. [':no type before colon 1'] = false,
  201. [' :no type before colon 2'] = false,
  202. [' :no type before colon 3'] = false,
  203. ['ci(empty description):'] = false,
  204. ['ci(only whitespace as description): '] = false,
  205. ['docs(multiple whitespaces as description): '] = false,
  206. ['revert(multiple whitespaces and then characters as description): description'] = false,
  207. ['ci no colon after type'] = false,
  208. ['test: extra space after colon'] = false,
  209. ['ci: tab after colon'] = false,
  210. ['ci:no space after colon'] = false,
  211. ['ci :extra space before colon'] = false,
  212. ['refactor(): empty scope'] = false,
  213. ['ci( ): whitespace as scope'] = false,
  214. ['ci: period at end of sentence.'] = false,
  215. ['ci: period: at end of sentence.'] = false,
  216. ['ci: Capitalized first word'] = false,
  217. ['ci: UPPER_CASE First Word'] = true,
  218. ['unknown: using unknown type'] = false,
  219. ['feat: foo:bar'] = true,
  220. ['feat: :foo:bar'] = true,
  221. ['feat: :Foo:Bar'] = true,
  222. ['feat(something): foo:bar'] = true,
  223. ['feat(something): :foo:bar'] = true,
  224. ['feat(something): :Foo:Bar'] = true,
  225. ['feat(:grep): read from pipe'] = true,
  226. ['feat(:grep/:make): read from pipe'] = true,
  227. ['feat(:grep): foo:bar'] = true,
  228. ['feat(:grep/:make): foo:bar'] = true,
  229. ['feat(:grep)'] = false,
  230. ['feat(:grep/:make)'] = false,
  231. ['feat(:grep'] = false,
  232. ['feat(:grep/:make'] = false,
  233. ["ci: you're saying this commit message just goes on and on and on and on and on and on for way too long?"] = false,
  234. }
  235. local failed = 0
  236. for message, expected in pairs(test_cases) do
  237. local is_valid = (nil == validate_commit(message))
  238. if is_valid ~= expected then
  239. failed = failed + 1
  240. p(
  241. string.format('[ FAIL ]: expected=%s, got=%s\n input: "%s"', expected, is_valid, message)
  242. )
  243. end
  244. end
  245. if failed > 0 then
  246. os.exit(1)
  247. end
  248. end
  249. --- @class LintcommitOptions
  250. --- @field trace? boolean
  251. local opt = {}
  252. for _, a in ipairs(arg) do
  253. if vim.startswith(a, '--') then
  254. local nm, val = a:sub(3), true
  255. if vim.startswith(a, '--no') then
  256. nm, val = a:sub(5), false
  257. end
  258. if nm == 'trace' then
  259. opt.trace = val
  260. end
  261. end
  262. end
  263. for _, a in ipairs(arg) do
  264. if M[a] then
  265. M[a](opt)
  266. end
  267. end