Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

michelhe
Copy link
Contributor

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.

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.
@michelhe michelhe added the enhancement New feature or request label Aug 27, 2023
@michelhe michelhe requested a review from Jongy August 27, 2023 12:51
@@ -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 .
Copy link
Contributor

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

Copy link
Contributor Author

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 ?

Copy link
Contributor

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).

Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okie

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor

@Jongy Jongy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants