lintcommit.lua 7.9 KB

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