Skip to content

commit-msg hook

diffract has a commit-msg hook for pre-commit and prek. The hook rejects a commit message if the commit type tells less than the change to the public API.

Install the hook

  1. Add the hook to .pre-commit-config.yaml:

    repos:
      - repo: https://github.com/andrewasheridan/diffract
        rev: v<VERSION>
        hooks:
          - id: diffract-validate
    
  2. Install the commit-msg hook type. Run one of these commands:

    pre-commit install --hook-type commit-msg
    prek install --hook-type commit-msg
    

Write the source directory in diffract.toml or in pyproject.toml, not in the hook arguments. Read Configuration.

The rules

Change that diffract finds Types that pass
A public name was removed or had a breaking change (feat!) Any type with a ! or a BREAKING CHANGE: footer
A public name was added or had a change that adds to it (feat) feat, or any type with a ! or a BREAKING CHANGE: footer
No public name changed (code, test or other) Any Conventional Commit type

The hook also obeys these rules:

  • A message with no type, or with a type that is not a Conventional Commit type, fails.
  • A revert: message always passes, because a revert can undo any change.
  • The hook does not check the messages that git writes: Merge …, Revert "…", fixup! …, amend! … and squash! ….
  • The hook ignores comment lines and the blank lines at the start of the message, as git does. A comment line starts with #, or with the value of core.commentChar.

diffract compares only the public API. If no public name changed, diffract cannot tell a fix from a docs change. Thus the hook accepts each type.

If the type is feat and no public name changed, the hook writes a warning to stderr. The commit continues, because a change in behavior can be a feature also when no public name changes.

What the hook compares

The hook compares HEAD with the git staging area, not HEAD~1 with HEAD. Thus it finds the change in the commit that you make now, not the change in the last commit. git runs the commit-msg hook before it makes the new commit. Thus HEAD is still the last commit, and the staging area has the content of the new commit.

Output

If the commit type tells less than the API change, the hook writes the change, the written type and the possible types to stderr. Then it exits with code 1. The written type keeps the scope of your message. For example, fix(parser): … for a change that adds a public name gives:

diffract: commit type mismatch
  written:  fix(parser)
  possible: feat, or any type with a '!' or a 'BREAKING CHANGE:' footer

Run the check without the hook

The hook runs this command. You can run it yourself:

diffract --validate-msg-file .git/COMMIT_EDITMSG