I have husky
and lint-staged
setup in a repository I am a main contributor to, and these guards consist of ensuring correct commitlint
messages, proper branch naming patterns, and checking for linting errors on all commits.
This all works great when creating/committing/pushing branches from the command line, but when using any IDE's integrated Git UI, the checks are completely ignored, and anything can make it to the repo (incorrect commit messages, incorrect branch names, linting errors, etc.). Not sure why, nor do I know how to avoid this.
Here are some of the relevant pieces of code that is within this web of guards (obviously with irrelevant code omitted for clarity):
package.json
:
"scripts": {
"lint": "eslint app/"
},
"devDependencies": {
"@commitlint/cli": "^16.2.3",
"@commitlint/config-conventional": "^16.2.1",
"enforce-branch-name": "^1.0.1"
},
...
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-push": "enforce-branch-name '^(branch-name-regex-rules)$'"
}
},
"lint-staged": {
"*.js": "npm run lint"
}
And then I have my .husky
folder at the project root, with my commit-msg
, pre-commit
, and pre-push
hook scripts:
commit-msg
:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx --no -- commitlint --edit $1
pre-commit
:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
pre-push
:
#!/bin/sh
LC_ALL=C
local_branch="$(git rev-parse --abbrev-ref HEAD)"
valid_branch_regex="^(branch-name-regex-rules)$"
message="Error message for incorrect branch naming"
if [[ ! $local_branch =~ $valid_branch_regex ]]
then
echo "$message"
exit 1
fi
exit 0
I think that should cover all the relevant code pieces that I have. Like I said, when using the command line to make commits and push branches to the remote, these rules come into play and work, informing developers of incorrect patterns.
But any developer that uses like, VSCode's integrated Git feature to commit and/or push (or even just stage the files), none of the guards work, and it's as if someone just added --no-verify
to a command line Git declaration to force the commit/push despite failing hooks/guards.
Not sure what I need to do to get husky
and lint-staged
to work appropriately for any method of staging files, making commits, branch naming, etc. etc.
Does anyone know what I'm doing wrong, or can point me somewhere that might explain what I'm doing wrong? Thanks in advance!