lintcommit.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. local commit_split = vim.split(commit_message, ":")
  43. -- Return nil if the type is vim-patch since most of the normal rules don't
  44. -- apply.
  45. if commit_split[1] == "vim-patch" then
  46. return nil
  47. end
  48. -- Check that message isn't too long.
  49. if commit_message:len() > 80 then
  50. return [[Commit message is too long, a maximum of 80 characters is allowed.]]
  51. end
  52. if vim.tbl_count(commit_split) < 2 then
  53. return [[Commit message does not include colons.]]
  54. end
  55. local before_colon = commit_split[1]
  56. local after_colon = commit_split[2]
  57. -- Check if commit introduces a breaking change.
  58. if vim.endswith(before_colon, "!") then
  59. before_colon = before_colon:sub(1, -2)
  60. end
  61. -- Check if type is correct
  62. local type = vim.split(before_colon, "%(")[1]
  63. local allowed_types = {'build', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'test', 'chore', 'vim-patch'}
  64. if not vim.tbl_contains(allowed_types, type) then
  65. return string.format(
  66. 'Invalid commit type "%s". Allowed types are:\n %s',
  67. type,
  68. vim.inspect(allowed_types))
  69. end
  70. -- Check if scope is empty
  71. if before_colon:match("%(") then
  72. local scope = vim.trim(before_colon:match("%((.*)%)"))
  73. if scope == '' then
  74. return [[Scope can't be empty.]]
  75. end
  76. end
  77. -- Check that description doesn't end with a period
  78. if vim.endswith(after_colon, ".") then
  79. return [[Description ends with a period (".").]]
  80. end
  81. -- Check that description starts with a whitespace.
  82. if after_colon:sub(1,1) ~= " " then
  83. return [[There should be a whitespace after the colon.]]
  84. end
  85. -- Check that description doesn't start with multiple whitespaces.
  86. if after_colon:sub(1,2) == " " then
  87. return [[There should only be one whitespace after the colon.]]
  88. end
  89. -- Check that first character after space isn't uppercase.
  90. if string.match(after_colon:sub(2,2), '%u') then
  91. return [[First character should not be uppercase.]]
  92. end
  93. -- Check that description isn't just whitespaces
  94. if vim.trim(after_colon) == "" then
  95. return [[Description shouldn't be empty.]]
  96. end
  97. return nil
  98. end
  99. function M.main(opt)
  100. _trace = not opt or not not opt.trace
  101. local branch = run({'git', 'rev-parse', '--abbrev-ref', 'HEAD'}, true)
  102. -- TODO(justinmk): check $GITHUB_REF
  103. local ancestor = run({'git', 'merge-base', 'origin/master', branch})
  104. if not ancestor then
  105. ancestor = run({'git', 'merge-base', 'upstream/master', branch})
  106. end
  107. local commits_str = run({'git', 'rev-list', ancestor..'..'..branch}, true)
  108. local commits = {}
  109. for substring in commits_str:gmatch("%S+") do
  110. table.insert(commits, substring)
  111. end
  112. local failed = 0
  113. for _, commit_id in ipairs(commits) do
  114. local msg = run({'git', 'show', '-s', '--format=%s' , commit_id})
  115. if vim.v.shell_error ~= 0 then
  116. p('Invalid commit-id: '..commit_id..'"')
  117. else
  118. local invalid_msg = validate_commit(msg)
  119. if invalid_msg then
  120. failed = failed + 1
  121. p(string.format([[
  122. Invalid commit message: "%s"
  123. Commit: %s
  124. %s
  125. See also:
  126. https://github.com/neovim/neovim/blob/master/CONTRIBUTING.md#commit-messages
  127. https://www.conventionalcommits.org/en/v1.0.0/
  128. ]],
  129. msg,
  130. commit_id,
  131. invalid_msg))
  132. end
  133. end
  134. end
  135. if failed > 0 then
  136. die() -- Exit with error.
  137. else
  138. p('')
  139. end
  140. end
  141. function M._test()
  142. -- message:expected_result
  143. local test_cases = {
  144. ['ci: normal message'] = true,
  145. ['build: normal message'] = true,
  146. ['docs: normal message'] = true,
  147. ['feat: normal message'] = true,
  148. ['fix: normal message'] = true,
  149. ['perf: normal message'] = true,
  150. ['refactor: normal message'] = true,
  151. ['revert: normal message'] = true,
  152. ['test: normal message'] = true,
  153. ['chore: normal message'] = true,
  154. ['ci(window): message with scope'] = true,
  155. ['ci!: message with breaking change'] = true,
  156. ['ci(tui)!: message with scope and breaking change'] = true,
  157. ['vim-patch:8.2.3374: Pyret files are not recognized (#15642)'] = true,
  158. ['vim-patch:8.1.1195,8.2.{3417,3419}'] = true,
  159. ['revert: "ci: use continue-on-error instead of "|| true""'] = true,
  160. [':no type before colon 1'] = false,
  161. [' :no type before colon 2'] = false,
  162. [' :no type before colon 3'] = false,
  163. ['ci(empty description):'] = false,
  164. ['ci(only whitespace as description): '] = false,
  165. ['docs(multiple whitespaces as description): '] = false,
  166. ['revert(multiple whitespaces and then characters as description): description'] = false,
  167. ['ci no colon after type'] = false,
  168. ['test: extra space after colon'] = false,
  169. ['ci: tab after colon'] = false,
  170. ['ci:no space after colon'] = false,
  171. ['ci :extra space before colon'] = false,
  172. ['refactor(): empty scope'] = false,
  173. ['ci( ): whitespace as scope'] = false,
  174. ['chore: period at end of sentence.'] = false,
  175. ['ci: Starting sentence capitalized'] = false,
  176. ['unknown: using unknown type'] = false,
  177. ['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,
  178. }
  179. local failed = 0
  180. for message, expected in pairs(test_cases) do
  181. local is_valid = (nil == validate_commit(message))
  182. if is_valid ~= expected then
  183. failed = failed + 1
  184. p(string.format('[ FAIL ]: expected=%s, got=%s\n input: "%s"', expected, is_valid, message))
  185. end
  186. end
  187. if failed > 0 then
  188. die() -- Exit with error.
  189. end
  190. end
  191. return M