Skip to main content

Guide to Using Pre-Commit Hooks

a quick guide to using pre-commit hooks to make contributing to Parsons (and other projects) easier

Published onAug 10, 2023
Guide to Using Pre-Commit Hooks

Git pre-commit hooks are a simple and helpful way to ensure your code is formatted appropriately before committing your code and making pull requests.

Normally in Parsons, we have tests that run once a pull request is made to check if code is formatted appropriately. If it isn’t, changes must be made before the pull request can be merged. Using pre-commit hooks ensures that these changes are made up-front to save time in the code review process.

You must install pre-commit hooks for them to work. Open a terminal and navigate to the parsons repository. Make sure you have activated the python virtual environment that you use for parsons development. Install the pre-commit package by running pip install pre-commit, and then tell your parsons repository to use pre-commit by running pre-commit install.

Now whenever you attempt to make a git commit, your code will have the flake8 and black linters run. If any formatting issues arise, the commit will fail and git will have an error message describing all formatting issues. Issues with black formatting will be automatically fixed. You will need to manually review and correct any issues with flake8 formatting. Once changes are made, you will need to stage the new changes (ie add them with git add). At this point you should be able to successfully commit your changes.

Comments
1
?
Santiago Lorenzo Barcelona University:

Thank you for the help with the code, very useful. Florist Spain