Set up pre-commit
This step is entirely optional but is recommended.
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.
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.
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:
#!/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
#!/bin/bash # Check if MySQL is already running # so we can stop it after the checks if it was started here. if docker-compose top | grep mysqld 2> /dev/null; then echo "MySQL is running." MYSQL_RUNNING=true else echo "MySQL is not running." MYSQL_RUNNING=false fi if [ "$MYSQL_RUNNING" = false ]; then docker-compose up -d db echo "MySQL has been started." fi if pipenv run python manage.py makemigrations --check --dry-run; then # There are no changes to migrations if [ "$MYSQL_RUNNING" = false ]; then docker-compose stop db echo "MySQL has been stopped." fi exit 0 else # There are changes to migrations echo "Migrations are out of date. Please run 'pipenv run python manage.py makemigrations' and commit the changes." # If MySQL was not originally running, stop it before exiting if [ "$MYSQL_RUNNING" = false ]; then docker-compose stop db echo "MySQL has been stopped." fi exit 1 fi
#!/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.