Commit message linting within Magit? reddit / emacs ---------------------------------------------------------------------- azfrak · 2022-09-29 02:32 Hi all, just wondering if anyone has worked on any solutions to integrate some git commit message linting tools within magit. Can setup tools like [Commitlint](https://github.com/conventional- changelog/commitlint) to add hooks that will reject a commit message if it is invalid, but if you format a commit message incorrectly it simply closes the commit buffer and returns to git status, meaning you have to retype out your commit message, which is obviously less than ideal. ​ Has anyone looked at something like this before or have any suggestions about how to address this? Another potential solution is to possibly use a custom commit command that will open a shell in emacs and run something like [cz- cli](https://github.com/commitizen/cz-cli) to create commit messages that adhere to things like the conventional commits format. My search has yielded little results, so just wanted to ask others for anything I might be missing or some hints before I maybe start looking at implementing stuff myself :) ---------------------------------------------------------------------- aisamu · 2022-09-29 02:59 A tiny improvement, but still: https://github.com/akirak/conventional- commit.el ---------------------------------------------------------------------- 7890yuiop · 2022-09-29 06:29 > meaning you have to retype out your commit message, which is obviously less than ideal. `M-p` ? ---------------------------------------------------------------------- azfrak · 2022-09-29 12:42 thank you so much for this! didnt know this existed. This gets me about 99% of what I want so I am happy with this :) ---------------------------------------------------------------------- CJ6_ · 2022-09-29 19:01 Wow, had no idea this existed. Thanks for sharing ---------------------------------------------------------------------- azfrak · 2022-09-29 23:58 This looks really interesting, thanks for sharing this. I think I might include this as well. Having a look at the issues and development of the package, it looks like it wants to move towards combining with commitlint to provide something like commitizen, which is really what I was after. This plus not having to retype my commit messages if linting fails will be mostly what I need I think. Ideally would probably have something like flycheck/flymake for linting the message in realtime. maybe something I will work on to try and learn a bit of lisp when time allows it. ---------------------------------------------------------------------- tedreed · 2022-10-04 21:46 Magit has settings to enforce commit conventions: https://magit.vc/manual/magit/Commit-Message-Conventions.html#Commit- Message-Conventions At some point I intended to write some to validate conventional commit standards, but never got around to it. It probably wouldn't be that hard. ---------------------------------------------------------------------- tedreed · 2022-10-04 21:52 Oh, unfortunately it's just a list of symbols from a hardcoded set, and not a freeform set of functions. Could probably use advise, I guess. Edit: Oh, but I guess git-commit-finish-query-functions *is* freeform; so you could put it there. ---------------------------------------------------------------------- tedreed · 2022-10-04 22:35 Went ahead and made something simple that at least validates the format of the first line: ``` (setq treed/conventional-commit-types '("feat" "fix")) (defun treed/magit-query-conventional-commit (&optional force) (or force (save-excursion (goto-char (point-min)) (and (if (re-search-forward (rx-to-string `(seq (or ,@treed/conventional- commit-types) (zero-or-one "(" anything ")") (zero-or-one "!") ": ")) nil t) t (y-or-n-p "Unable to find commit type/scope at beginning of first line. Commit anyway? ")) (if (re-search-forward (rx-to-string '(seq (one-or-more (not whitespace)) eol)) nil t) t (y-or-n-p "Unable to find commit description after type/scope. Commit anyway? ")))))) ``` ---------------------------------------------------------------------- azfrak · 2022-10-05 00:45 Thank you so much for this. I searched the magit docs a bit before and missed this. I think formatting the first line is the most important. I might extend upon this to add some other checks, like using imperative mood and present tense etc. ---------------------------------------------------------------------- tedreed · 2022-10-06 07:08 Yeah, no problem. It was a fun little puzzle to work out. :)