lintcommit.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 has exactly one whitespace after colon, followed by
  82. -- a lowercase letter and then any number of letters.
  83. if not string.match(after_colon, '^ %l%a*') then
  84. return [[There should be one whitespace after the colon and the first letter should lowercase.]]
  85. end
  86. return nil
  87. end
  88. function M.main(opt)
  89. _trace = not opt or not not opt.trace
  90. local branch = run({'git', 'rev-parse', '--abbrev-ref', 'HEAD'}, true)
  91. -- TODO(justinmk): check $GITHUB_REF
  92. local ancestor = run({'git', 'merge-base', 'origin/master', branch})
  93. if not ancestor then
  94. ancestor = run({'git', 'merge-base', 'upstream/master', branch})
  95. end
  96. local commits_str = run({'git', 'rev-list', ancestor..'..'..branch}, true)
  97. local commits = {}
  98. for substring in commits_str:gmatch("%S+") do
  99. table.insert(commits, substring)
  100. end
  101. local failed = 0
  102. for _, commit_id in ipairs(commits) do
  103. local msg = run({'git', 'show', '-s', '--format=%s' , commit_id})
  104. if vim.v.shell_error ~= 0 then
  105. p('Invalid commit-id: '..commit_id..'"')
  106. else
  107. local invalid_msg = validate_commit(msg)
  108. if invalid_msg then
  109. failed = failed + 1
  110. p(string.format([[
  111. Invalid commit message: "%s"
  112. Commit: %s
  113. %s
  114. See also:
  115. https://github.com/neovim/neovim/blob/master/CONTRIBUTING.md#commit-messages
  116. https://www.conventionalcommits.org/en/v1.0.0/
  117. ]],
  118. msg,
  119. commit_id,
  120. invalid_msg))
  121. end
  122. end
  123. end
  124. if failed > 0 then
  125. die() -- Exit with error.
  126. else
  127. p('')
  128. end
  129. end
  130. function M._test()
  131. -- message:expected_result
  132. local test_cases = {
  133. ['ci: normal message'] = true,
  134. ['build: normal message'] = true,
  135. ['docs: normal message'] = true,
  136. ['feat: normal message'] = true,
  137. ['fix: normal message'] = true,
  138. ['perf: normal message'] = true,
  139. ['refactor: normal message'] = true,
  140. ['revert: normal message'] = true,
  141. ['test: normal message'] = true,
  142. ['chore: normal message'] = true,
  143. ['ci(window): message with scope'] = true,
  144. ['ci!: message with breaking change'] = true,
  145. ['ci(tui)!: message with scope and breaking change'] = true,
  146. ['vim-patch:8.2.3374: Pyret files are not recognized (#15642)'] = true,
  147. ['vim-patch:8.1.1195,8.2.{3417,3419}'] = true,
  148. [':no type before colon 1'] = false,
  149. [' :no type before colon 2'] = false,
  150. [' :no type before colon 3'] = false,
  151. ['ci(empty description):'] = false,
  152. ['ci(whitespace as description): '] = false,
  153. ['docs(multiple whitespaces as description): '] = false,
  154. ['ci no colon after type'] = false,
  155. ['test: extra space after colon'] = false,
  156. ['ci: tab after colon'] = false,
  157. ['ci:no space after colon'] = false,
  158. ['ci :extra space before colon'] = false,
  159. ['refactor(): empty scope'] = false,
  160. ['ci( ): whitespace as scope'] = false,
  161. ['chore: period at end of sentence.'] = false,
  162. ['ci: Starting sentence capitalized'] = false,
  163. ['unknown: using unknown type'] = false,
  164. ['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,
  165. }
  166. local failed = 0
  167. for message, expected in pairs(test_cases) do
  168. local is_valid = (nil == validate_commit(message))
  169. if is_valid ~= expected then
  170. failed = failed + 1
  171. p(string.format('[ FAIL ]: expected=%s, got=%s\n input: "%s"', expected, is_valid, message))
  172. end
  173. end
  174. if failed > 0 then
  175. die() -- Exit with error.
  176. end
  177. end
  178. return M