dev: fix and refactor commit lint rules

- Fix rules that weren't working due to faulty logic.
- Revise linter messages for clarity and concision.
- Use conventional commit nomenclature: summary -> subject
- case-fold-search = nil, to ensure case sensitive searches
- Exclude bump type from length checks
This commit is contained in:
Henrik Lissner
2021-08-01 15:40:45 -04:00
parent 11a0c0cbdb
commit 5df31a4895

View File

@ -32,11 +32,11 @@
;;; Git hooks ;;; Git hooks
(defvar doom-cli-commit-rules (defvar doom-cli-commit-rules
(list (cons "^[^\n]\\{10,\\}\n" (list (cons "^[^\n]\\{10,\\}$"
"Commit summary is too short (<10) and should be more descriptive") "Subject is too short (<10) and should be more descriptive")
(cons "^\\(revert!?:[^\n]\\{,72\\}\\|[^\n]\\{,80\\}\\)\n" (cons "^\\(\\(revert\\|bump\\)!?: \\|[^\n]\\{,80\\}\\)$"
"Commit summary too long; <=72 is ideal, 80 is max") "Subject too long; <=72 is ideal, 80 is max")
(cons (concat (cons (concat
"^\\(" "^\\("
@ -54,23 +54,24 @@
(cons (lambda () (cons (lambda ()
(looking-at "^\\(bump\\|revert\\|release\\|merge\\|module\\)!?([^)]+):")) (looking-at "^\\(bump\\|revert\\|release\\|merge\\|module\\)!?([^)]+):"))
"This commit type's scope goes after the colon, not before") "This type's scope goes after the colon, not before")
(cons (lambda () (cons (lambda ()
(when (looking-at "[^ :!(]+!?(\\([^)]+\\)): ") (when (looking-at "[^ :!(]+!?(\\([^)]+\\)): ")
(not (not
(string-match (cl-loop with scopes =
(string-join (cl-loop for path
(cons (concat in (cdr (doom-module-load-path (list doom-modules-dir)))
"^" (regexp-opt for (_category . module)
(cl-loop for path = (doom-module-from-path path)
in (cdr (doom-module-load-path (list doom-modules-dir))) collect (symbol-name module))
for (_category . module) with scopes-re =
= (doom-module-from-path path) (string-join (cons (concat "^" (regexp-opt scopes) "$")
collect (symbol-name module))) "$") '("^&" "^cli$"))
'("^&" "^cli$")) "\\|")
"\\|") for scope in (split-string (match-string 1) ",")
(match-string 1))))) if (string-match scopes-re scope)
return t))))
"Invalid scope") "Invalid scope")
(cons (lambda () (cons (lambda ()
@ -102,7 +103,7 @@
nil t)) nil t))
"Use present tense/imperative voice for footer references, without a colon") "Use present tense/imperative voice for footer references, without a colon")
;; TODO Check that bump/revert SUMMARY list: 1) valid modules and 2) ;; TODO Check that bump/revert SUBJECT list: 1) valid modules and 2)
;; modules whose files are actually being touched. ;; modules whose files are actually being touched.
;; TODO Ensure your diff corraborates your SCOPE ;; TODO Ensure your diff corraborates your SCOPE
@ -136,45 +137,51 @@
;;; ;;;
(defun doom-cli--ci-lint-commits (from &optional to) (defun doom-cli--ci-lint-commits (from &optional to)
(with-temp-buffer (let ((errors? 0)
(save-excursion commits
case-fold-search)
(with-temp-buffer
(insert (insert
(cdr (doom-call-process (cdr (doom-call-process
"git" "log" "git" "log"
(format "%s...%s" from (or to "HEAD")))))) (format "%s...%s" from (or to "HEAD")))))
(while (re-search-forward "^commit \\([a-z0-9]\\{40\\}\\)" nil t) (while (re-search-backward "^commit \\([a-z0-9]\\{40\\}\\)" nil t)
(let ((commit (match-string 1)) (push (cons (match-string 1)
errors) (replace-regexp-in-string
(forward-line 4) "^ " ""
(save-restriction (save-excursion
(save-match-data (buffer-substring-no-properties
(narrow-to-region (search-forward "\n\n")
(point) (save-excursion (if (re-search-forward "\ncommit \\([a-z0-9]\\{40\\}\\)" nil t)
(if (re-search-forward "^commit \\([a-z0-9]\\{40\\}\\)" nil t) (match-beginning 0)
(match-beginning 0) (point-max))))))
(point-max)))) commits)))
(indent-rigidly (point-min) (point-max) -4)) (dolist (commit commits)
(save-excursion (let (errors)
(print! (start "Commit %s") commit) (with-temp-buffer
(dolist (rule doom-cli-commit-rules) (save-excursion (insert (cdr commit)))
(cl-destructuring-bind (pred . msg) rule (dolist (rule doom-cli-commit-rules)
(goto-char (point-min)) (save-excursion
(save-match-data (save-match-data
(when (if (functionp pred) (cl-destructuring-bind (pred . msg) rule
(funcall pred) (and (cond ((functionp pred)
(if (stringp pred) (funcall pred))
(not (re-search-forward pred nil t)) ((stringp pred)
(error "Invalid predicate: %S" pred))) (not (re-search-forward pred nil t)))
(push msg (alist-get commit errors nil nil #'equal))))))) ((error "Invalid predicate: %S" pred)))
(when errors (push msg errors))))))
(dolist (error (reverse errors)) (if (not errors)
(print! (error "Commit %s") (car error)) (print! (success "Commit %s") (car commit))
(print-group! (cl-incf errors?)
(dolist (e (reverse (cdr error))) (print! (error "Commit %s") (car commit))
(print! (info e))))) (print-group!
(terpri) (print! "%S" (cdr commit))
(print! "%d commit(s) failed the linter" (length errors)) (dolist (e (reverse errors))
(terpri) (print! (error "%s" e))))))))
(print! "See https://doomemacs.org/project.org#commit-message-formatting for details") (when (> errors? 0)
(throw 'exit 1))))) (terpri)
t)) (print! "%d commit(s) failed the linter" errors?)
(terpri)
(print! "See https://doomemacs.org/project.org#commit-message-formatting for details")
(throw 'exit 1)))
t)