This step is entirely optional but is recommended.
Pre-commit
Pre-commit is a tool that runs git hooks on your code before you commit it. It's a great way to ensure your code is formatted correctly and passes some basic checks before you commit it.
It will run black, isort and flake8 on my code and check for missing migrations and also that requirements.txt is up to date before I commit it.
Install pre-commit
Add pre-commit to the development dependencies in Pipfile:
pipenv install --dev pre-commit
If you already have pre-commit installed globally, you can skip this step.
Configure pre-commit
Create a .pre-commit-config.yaml file in the root of the project and add the following content:
Add the git hook entry points
Create a checks directory in the root of the project and add the following files:
mkdir checks
touch checks/dependencies.sh
touch checks/migrations.sh
touch checks/static.sh
chmod +x checks/*.sh
Update the content of each file as follows:
File checks/dependencies.sh:
#!/bin/bash
# Get the contents of requirements.txt
requirements=$(cat requirements.txt)
# Get the output from pipenv requirements
pipenv_requirements=$(pipenv requirements)
# Compare the two outputs
if [ "$requirements" == "$pipenv_requirements" ]; then
echo "Requirements are correct"
else
echo "Requirements are different, please run pipenv requirements > requirements.txt and stage the changes"
exit 1
fi
File checks/migrations.sh:
File checks/static.sh:
#!/bin/bash
# This script generates production-ready files for the scripts and styles
# It runs npm run build
# If files are changed then fails
# Exit on error
set -e
# Run npm run build
npm run build
Use Pipenv to install some development dependencies
pipenv install --dev black==23.7.0 pre-commit==3.3.3 flake8==6.1.0 isort==5.12.0 tomli==2.0.1
I've pinned the versions of the development dependencies to the same versions as the pre-commit hooks.
Install the pre-commit hooks
pipenv run pre-commit install
You can use the pre-commit install command to initialize pre-commit. You can also run pre-commit uninstall to remove the hooks.
Run the pre-commit hooks
pipenv run pre-commit run --all-files
If this is the first time running pre-commit you'll probably see some file changes have been made. You can commit these changes and then run the pre-commit hooks again to make sure everything is ok.
Commit the changes
git add .
git commit -m "add your commit message here"
Having pre-commit installed means that every time you commit code, the pre-commit hooks will run and check your code.
If there are any errors, the commit will fail and you'll need to fix the errors before you can commit again.
Make sure you have stopped the npm server before you commit, the one you started with npm start.