Skip to content

Commit

Permalink
justfile, pre-commit-hook: add black-hook recipe
Browse files Browse the repository at this point in the history
Install a pre-commit hook that launch black on files in the staging area
This approach ensures that all your commits contain Python code formatted according to Black’s rules.

Issue #69
  • Loading branch information
iacopy committed Sep 28, 2024
1 parent 22619b8 commit 4c2c2a2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ install-hooks:
# install pre-push hook
echo "just test" > .git/hooks/pre-push&&chmod +x .git/hooks/pre-push

black-hook:
# ensures that all your commits contain Python code formatted according to Black’s rules.
cp pre-commit-black .git/hooks/pre-commit&&chmod +x .git/hooks/pre-commit

# bootstrap your virtualenv (deprecated)
setenv VIRTUALENV:
@echo Create virtualenv and use it to install requirements
Expand Down
10 changes: 10 additions & 0 deletions pre-commit-black
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

# Run Black on all Python files
files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.py$')
if [ -n "$files" ]; then
echo "Running Black on Python files..."
black $files
# Add the formatted files back to the staging area
git add $files
fi

0 comments on commit 4c2c2a2

Please sign in to comment.