I have prepared the following pre-commit
hook:
#!/bin/sh
# Run flake8 and mypy
flake8 ./src
FLAKE8_EXIT_CODE=$?
mypy ./src
MYPY_EXIT_CODE=$?
# If either flake8 or mypy return an error, skip black and the commit process
if [ $FLAKE8_EXIT_CODE -ne 0 ] || [ $MYPY_EXIT_CODE -ne 0 ]; then
echo "flake8 or mypy checks failed. Skipping black and commit."
exit 1
fi
black .
git add -u
if ! git diff-index --quiet HEAD; then
git commit --no-verify -m "Same message all the times!"
fi
Note that black .
reformat the files and therefore I have a second git add -u
. To avoid infinite recursions, I added a last check that git commit with --no-verify
, but then the commit message is the same for every commit and I like to avoid it. Instead, I want to be prompted for a commit message when I enter the last if
branch.
I tried to use read
command inside the last if
branch but with no success.
I would also like to avoid external tools as pre-commit, but I would rather a native solution, if possible.
How to fix it?
I have prepared the following pre-commit
hook:
#!/bin/sh
# Run flake8 and mypy
flake8 ./src
FLAKE8_EXIT_CODE=$?
mypy ./src
MYPY_EXIT_CODE=$?
# If either flake8 or mypy return an error, skip black and the commit process
if [ $FLAKE8_EXIT_CODE -ne 0 ] || [ $MYPY_EXIT_CODE -ne 0 ]; then
echo "flake8 or mypy checks failed. Skipping black and commit."
exit 1
fi
black .
git add -u
if ! git diff-index --quiet HEAD; then
git commit --no-verify -m "Same message all the times!"
fi
Note that black .
reformat the files and therefore I have a second git add -u
. To avoid infinite recursions, I added a last check that git commit with --no-verify
, but then the commit message is the same for every commit and I like to avoid it. Instead, I want to be prompted for a commit message when I enter the last if
branch.
I tried to use read
command inside the last if
branch but with no success.
I would also like to avoid external tools as pre-commit, but I would rather a native solution, if possible.
How to fix it?
Share Improve this question edited Nov 20, 2024 at 8:09 Guildenstern 3,9272 gold badges28 silver badges54 bronze badges asked Nov 19, 2024 at 15:25 Barzi2001Barzi2001 1,7961 gold badge21 silver badges32 bronze badges 5 |1 Answer
Reset to default 2I'd suggest to do the same for black as for pyflakes and mypy: run it as a checker, and refuse the commit if the checker fails, but don't actually reformat the code. So you'll always have to run black manually, but it won't reformat files without you seeing and knowing it.
black --check .
can do that (you can send the output of that check to /dev/null
to reduce commit-hook noise if you want to). It (also) returns a non-zero value when the check fails, indicating black
would reformat the code.
But for what you specifically want, perhaps
git commit --no-verify --amend --no-edit
does that: it keeps the last commit message by amending the current commit (and doesn't open an editor for adjusting the commit message).
black .
butflake8 ./src ; mypy ./src
though? – 9769953 Commented Nov 19, 2024 at 15:37flake8
andmypy
can only read files and tell you what is wrong, otherwise if they could fix the code I would have run them as I am doing withblack
straight away. – Barzi2001 Commented Nov 19, 2024 at 15:41./src
I want max compliance, whereas outside./src
I have scratch scripts that I don't mind if they are notflake8
ormypy
compliant, yet I want them to be formatted according to` black`. – Barzi2001 Commented Nov 19, 2024 at 15:52