Recently I started to use Black code formatter for my projects and sometimes it can be hard to track did I formatted all my changed files or not. Let's assume that my project contains 20 different scripts (.py files) and I made changes on last 5 of them what I do normally before commit.
First option
black /project_directory
Second option
black script1.py
black script2.py
and etc.
Is there a way to automate this process before git commit or not ?
Thanks a lot in advance
Yes, you can use a pre-commit hook. If you're using Linux or macOS, or Windows with Git Bash, put the following in a file named .git/hooks/pre-commit:
#!/bin/bash
# Paths are relative to the root of the git repository
black .
black script1.py
black script2.py
Use chmod +x .git/hooks/pre-commit to make this runnable. Then, every time you do a commit, the hook will run first and format all your code. You can read more about hooks on the Git Hooks documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With