0

My team is using Git pre-commit hooks to validate commit messages for formatting rule compliance. This means whenever I go and run git commit, then fill in COMMIT_EDITMSG and save and close the file, a script will run that will check its contents against a set of rules and put out a respective exit code which either fails the commit creation process or allows it to pass.

I tend to need multiple passes at making my Git commit messages comply with all of the rules as the rule checking is non-interactive so I only learn whether my commit message is compliant after I've saved and closed COMMIT_EDITMSG.

I also tend to write expansive commit message bodies with multiple paragraphs of context which greatly increases the likelihood of running afoul of the lint rules as well as the time required to author a commit in case I am not able to preserve the commit message draft in between git commit invocations and need to rewrite the commit message each time or craft it in a temporary file until git commit passes and I can discard the temporary file.

I am hoping to find a solution to preserving the commit message without having to use a temporary file between the multiple git commit invocations only the last of which results in the commit being created.

I have found a related questions concerning Vim here: Git: Recover failed commit's message. However, the solutions there are either to use submodules (we aren't) or to configure Vim to keep the backup file.

I am using VS Code as the configured Git editor for authoring commit messages so I am hoping to find a VS Code-compatible solution. I have set it up like so:

git config --global core.editor "code --wait"

When I go and run git commit from the VS Code integrated terminal, a new tab will open with the COMMIT_EDITMSG file and when I close that tab, Git will take notice and run the pre-commit hooks for me. I don't have to close VS Code as a whole for this even though it is not a separate instance of VS Code.

Is there a way to make Git preserve COMMIT_EDITMSG whenever the commit creation fails due to a hook returning a non-zero exit code?

Alternatively, is there a way to make VS Code preserve contents of a file at a path I saved and closed and that got deleted externally later?

There are a few VS Code settings which feel in the ballpark, like workbench.action.keepEditor, but nothing like what I need.

Is my only hope adjusting the pre-commit hook such that it stores the commit message draft in a separate file not known to Git before exiting and then recovers the message the next time around I go to run git commit? Can you even pre-fill a suggested commit message from a hook when git commit is ran?

Tomáš Hübelbauer
  • 9,179
  • 14
  • 63
  • 125
  • 1
    Since `pre-receive` hooks are not an effective control (they can be trivially bypassed with `--no-verify`), you could just remove the hook and then check your commit messages before you push or as part of CI if you want. – bk2204 Dec 29 '22 at 20:25
  • You could probably also configure vscode to run the commit message checks whenever you save the file (I know how to do that with `vim`, but not with VS Code). – larsks Dec 29 '22 at 22:04

0 Answers0