-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make python pre-commit hook fail if it would reformat #192
base: master
Are you sure you want to change the base?
Conversation
Currently, when I run `git commit` with a staged file that would be reformatted by the pre-commit hook, the hook will reformat the file but the the commit succeed with the unforamtted staged files. I'd like to make the pre-commit hook fail if it would reformat the file, so that I can fix the formatting and then commit.
@@ -16,6 +16,6 @@ fi | |||
EXCLUDE_RE='.venv|venv|build|granulate_utils/generated' | |||
|
|||
python3 -m isort --settings-path .isort.cfg $check_arg --skip granulate_utils/generated --skip venv --skip .venv --skip build . | |||
python3 -m black --line-length 120 $check_arg --exclude $EXCLUDE_RE . | |||
python3 -m black --check --diff --color --line-length 120 $check_arg --exclude $EXCLUDE_RE . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to do some if ci
here so when running locally it does fix things.
In gProfiler it's done this way:
black_extra_args=""
isort_extra_args=""
if [[ "$1" = "--ci" ]]; then
black_extra_args="--check"
isort_extra_args="--check-only"
fi
You can fix it in gProfiler as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also want it to --check
in the precommit hook.
You imagine some argument to lint.sh
that does either --check
or --fix
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add as many flags as you want. The default should be autofix so this happens when running locally. In the CI we can add additional flags so the lint.sh
run in the CI does something different (e.g doesn't reformat).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently it edits in place or only gives diff?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It edits in-place, but returns 0 so you actually succeed in the hook and get the commit editmsg before the reformatted stuff is staged
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okie
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My above statement refers to the "current" state in master.
My change causes it to no longer reformat, just display diff and exit 1, yeah?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. Default behavior is to format. In ci pass a flag and only show diff and exit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the point I'm arguing at is that also in development I don't want the default behavior.
What good is a pre-commit hook if it reformats but then let's you commit the non-formatted code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dude what do you suggest we're nitpicking about this small thing for the entire day lol. If black doesn't fix for u in the script, then u need to manually run black with the same flags which is not convenient. The script needs to fix it for you. I understand a problem exists now but your suggestion worsens the UX
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
Currently, when I run
git commit
with a staged file that would be reformatted by the pre-commit hook, the hook will reformat the file but the the commit succeed with the unforamtted staged files.I'd like to make the pre-commit hook fail if it would reformat the file, so that I can fix the formatting and then commit.