conventional-changelog / commitlint

📓 Lint commit messages
https://commitlint.js.org
MIT License
16.78k stars 904 forks source link

How to lint commit message when squashing locally #1940

Closed wenfangdu closed 2 years ago

wenfangdu commented 4 years ago

For example: image Lint this message image I noticed rewording when rebase -i will run the "commit-msg" hook, but squashing doesn't.

git hooks

{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "commit-msg": "commitlint --env HUSKY_GIT_PARAMS"
    }
  },
  "lint-staged": {
    "*.{js,jsx,vue}": "vue-cli-service lint",
    "*.{ts,flow,json,css,scss,less,html,gql,graphql,md,yml,yaml}": "prettier --write"
  }
}

commitlint.config.js

module.exports = {
  extends: ['@commitlint/config-conventional'],
}

dependencies

{
  "@commitlint/cli": "^8.3.5",
  "@commitlint/config-conventional": "^8.3.4",
  "husky": "^4.2.5",
  "lint-staged": "^10.2.6"
}
escapedcat commented 4 years ago

Hey, not really sure what could solve this but did you try to find another hook that might be triggered? Maybe prepare_commit_msg could work? I didn't try it.

wenfangdu commented 4 years ago

@escapedcat I gave it a go.

  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "commit-msg": "commitlint --env HUSKY_GIT_PARAMS",
      "prepare-commit-msg": "commitlint --env HUSKY_GIT_PARAMS"
    }
  },
[Error: ENOENT: no such file or directory, open 'repo\.git\COMMIT_EDITMSG message'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'repo\\.git\\COMMIT_EDITMSG message'
}
husky > prepare-commit-msg hook failed (cannot be bypassed with --no-verify due to Git specs)

Seems "prepare-commit-msg": "commitlint --env HUSKY_GIT_PARAMS" is not valid.

escapedcat commented 4 years ago

https://git-scm.com/docs/githooks#_prepare_commit_msg:

It takes one to three parameters. The first is the name of the file that contains the commit log message. The second is the source of the commit message, and can be: [...] squash (if a .git/SQUASH_MSG file exists);

Looks like it is looking for COMMIT_EDITMSG but in this case should look for SQUASH_MSG.
Maybe it can be checked in the code where this behaviour can be changed or modified.

escapedcat commented 2 years ago

Closing this. Happy for a PR though.