lintcommit.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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, ":")
  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. if vim.tbl_count(commit_split) < 2 then
  58. return [[Commit message does not include colons.]]
  59. end
  60. local before_colon = commit_split[1]
  61. local after_colon = commit_split[#commit_split]
  62. -- Check if commit introduces a breaking change.
  63. if vim.endswith(before_colon, "!") then
  64. before_colon = before_colon:sub(1, -2)
  65. end
  66. -- Check if type is correct
  67. local type = vim.split(before_colon, "%(")[1]
  68. local allowed_types = {'build', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'test', 'vim-patch'}
  69. if not vim.tbl_contains(allowed_types, type) then
  70. return string.format(
  71. [[Invalid commit type "%s". Allowed types are:
  72. %s.
  73. If none of these seem appropriate then use "fix"]],
  74. type,
  75. vim.inspect(allowed_types))
  76. end
  77. -- Check if scope is appropriate
  78. if before_colon:match("%(") then
  79. local scope = vim.trim(commit_message:match("%((.-)%)"))
  80. if scope == '' then
  81. return [[Scope can't be empty]]
  82. end
  83. if vim.startswith(scope, "nvim_") then
  84. return [[Scope should be "api" instead of "nvim_..."]]
  85. end
  86. local alternative_scope = {
  87. ['filetype.vim'] = 'filetype',
  88. ['filetype.lua'] = 'filetype',
  89. ['tree-sitter'] = 'treesitter',
  90. ['ts'] = 'treesitter',
  91. ['hl'] = 'highlight',
  92. }
  93. if alternative_scope[scope] then
  94. return ('Scope should be "%s" instead of "%s"'):format(alternative_scope[scope], scope)
  95. end
  96. end
  97. -- Check that description doesn't end with a period
  98. if vim.endswith(after_colon, ".") then
  99. return [[Description ends with a period (".").]]
  100. end
  101. -- Check that description starts with a whitespace.
  102. if after_colon:sub(1,1) ~= " " then
  103. return [[There should be a whitespace after the colon.]]
  104. end
  105. -- Check that description doesn't start with multiple whitespaces.
  106. if after_colon:sub(1,2) == " " then
  107. return [[There should only be one whitespace after the colon.]]
  108. end
  109. -- Allow lowercase or ALL_UPPER but not Titlecase.
  110. if after_colon:match('^ *%u%l') then
  111. return [[Description first word should not be Capitalized.]]
  112. end
  113. -- Check that description isn't just whitespaces
  114. if vim.trim(after_colon) == "" then
  115. return [[Description shouldn't be empty.]]
  116. end
  117. return nil
  118. end
  119. function M.main(opt)
  120. _trace = not opt or not not opt.trace
  121. local branch = run({'git', 'rev-parse', '--abbrev-ref', 'HEAD'}, true)
  122. -- TODO(justinmk): check $GITHUB_REF
  123. local ancestor = run({'git', 'merge-base', 'origin/master', branch})
  124. if not ancestor then
  125. ancestor = run({'git', 'merge-base', 'upstream/master', branch})
  126. end
  127. local commits_str = run({'git', 'rev-list', ancestor..'..'..branch}, true)
  128. local commits = {}
  129. for substring in commits_str:gmatch("%S+") do
  130. table.insert(commits, substring)
  131. end
  132. local failed = 0
  133. for _, commit_id in ipairs(commits) do
  134. local msg = run({'git', 'show', '-s', '--format=%s' , commit_id})
  135. if vim.v.shell_error ~= 0 then
  136. p('Invalid commit-id: '..commit_id..'"')
  137. else
  138. local invalid_msg = validate_commit(msg)
  139. if invalid_msg then
  140. failed = failed + 1
  141. -- Some breathing room
  142. if failed == 1 then
  143. p('\n')
  144. end
  145. p(string.format([[
  146. Invalid commit message: "%s"
  147. Commit: %s
  148. %s
  149. ]],
  150. msg,
  151. commit_id,
  152. invalid_msg))
  153. end
  154. end
  155. end
  156. if failed > 0 then
  157. p([[
  158. See also:
  159. https://github.com/neovim/neovim/blob/master/CONTRIBUTING.md#commit-messages
  160. ]])
  161. die() -- Exit with error.
  162. else
  163. p('')
  164. end
  165. end
  166. function M._test()
  167. -- message:expected_result
  168. local test_cases = {
  169. ['ci: normal message'] = true,
  170. ['build: normal message'] = true,
  171. ['docs: normal message'] = true,
  172. ['feat: normal message'] = true,
  173. ['fix: normal message'] = true,
  174. ['perf: normal message'] = true,
  175. ['refactor: normal message'] = true,
  176. ['revert: normal message'] = true,
  177. ['test: normal message'] = true,
  178. ['ci(window): message with scope'] = true,
  179. ['ci!: message with breaking change'] = true,
  180. ['ci(tui)!: message with scope and breaking change'] = true,
  181. ['vim-patch:8.2.3374: Pyret files are not recognized (#15642)'] = true,
  182. ['vim-patch:8.1.1195,8.2.{3417,3419}'] = true,
  183. ['revert: "ci: use continue-on-error instead of "|| true""'] = true,
  184. ['fixup'] = true,
  185. ['fixup: commit message'] = true,
  186. ['fixup! commit message'] = true,
  187. [':no type before colon 1'] = false,
  188. [' :no type before colon 2'] = false,
  189. [' :no type before colon 3'] = false,
  190. ['ci(empty description):'] = false,
  191. ['ci(only whitespace as description): '] = false,
  192. ['docs(multiple whitespaces as description): '] = false,
  193. ['revert(multiple whitespaces and then characters as description): description'] = false,
  194. ['ci no colon after type'] = false,
  195. ['test: extra space after colon'] = false,
  196. ['ci: tab after colon'] = false,
  197. ['ci:no space after colon'] = false,
  198. ['ci :extra space before colon'] = false,
  199. ['refactor(): empty scope'] = false,
  200. ['ci( ): whitespace as scope'] = false,
  201. ['ci: period at end of sentence.'] = false,
  202. ['ci: Capitalized first word'] = false,
  203. ['ci: UPPER_CASE First Word'] = true,
  204. ['unknown: using unknown type'] = false,
  205. ['feat(:grep): read from pipe'] = true,
  206. ['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,
  207. }
  208. local failed = 0
  209. for message, expected in pairs(test_cases) do
  210. local is_valid = (nil == validate_commit(message))
  211. if is_valid ~= expected then
  212. failed = failed + 1
  213. p(string.format('[ FAIL ]: expected=%s, got=%s\n input: "%s"', expected, is_valid, message))
  214. end
  215. end
  216. if failed > 0 then
  217. die() -- Exit with error.
  218. end
  219. end
  220. return M