lefthook
lefthook copied to clipboard
How prevent commit with empty diff during pre-commit stage?
My config
pre-commit:
parallel: true
commands:
eslint:
glob: "*.{js,jsx}"
run: npx eslint {staged_files} --fix && git add {staged_files}`
In that case where eslint fixes style rules and diff stand empty I want to prevent commit.
How can I rich such behavior?
Same question here. Here's my lefthook file:
pre-commit:
parallel: true
commands:
lint:
glob: "*.{js,ts}"
run: eslint --fix {staged_files} && git add {staged_files}
Now if I make a change to my JS files that ESLint catches and automatically fixes, I should have no commit. However, lefthook does end up creating the commit even though nothing in the file will be different.
You can use these:
pre-commit:
git_staged:
# This check helps to avoid a situation where fixes were applied and thus Lefthook raises no fault, but those
# changes weren't committed, and so wouldn't be pushed to the remote.
fail_text: Staged changes exist in your Git working copy. Please commit or stash them before pushing.
files: git diff --name-only --staged
run: git diff --exit-code --name-status --staged
git_untracked:
# This check helps to avoid a situation where Lefthook linters succeed because of an untracked configuration
# file, and then fail when the repository is cloned without this configuration file.
fail_text: Untracked changes exist in your Git working copy. Please remove or index them before pushing.
files: git ls-files --exclude-standard --others
run: exit 9
git_unstaged:
# This check helps to avoid a situation where fixes were applied and thus Lefthook raises no fault, but those
# changes weren't committed, and so wouldn't be pushed to the remote.
fail_text: Unstaged changes exist in your Git working copy. Please commit or stash them before pushing.
files: git diff --name-only
run: git diff --exit-code --name-status -- {files}
@zarabotaet Does that resolve the issue? If so, please close.