Benny Powers
Benny Powers

Reputation: 5836

use git's prepare-commit-msg hook during an interactive rebase

In a lerna/yarn monorepo, we use commitizen and cz-conventional-changelog to manage releases. We use husky to lint commit messages in the commit-msg hook and run the commitizen cli in the prepare-commit-msg hook:

  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
      "pre-commit": "lint-staged",
      "prepare-commit-msg": "exec < /dev/tty && yarn commit --hook || true"
    }
  },

This works fine as long as a rebase is not required, however, while our team are still learning the ropes, we often need to rebase feature branches in order to fix commit messages.

git rebase --interactive origin/master

While running the rebase, if I choose the reword command, I will be able to edit the commit message in my editor, but the commitizen cli will not run, in other words, there's nothing to prevent us from committing bad commit messages.

While we do lint commit messages in CI, I would much rather avoid this problem altogether by enforcing use of the commitizen CLI wizard at all stages.

Question:

Can I configure git to use the prepare-commit-msg hook during rebase reword operations?

Upvotes: 4

Views: 2263

Answers (1)

phd
phd

Reputation: 94716

git rebase runs pre-rebase and post-rewrite hooks. I doubt it runs any other hooks.

I think you can use git rebase --exec to run a validation script after every rebased commit. Any error code pauses rebase process.

Upvotes: 1

Related Questions