pre-commit
provides us with github hooks that point out issues in code such as missing semicolons, trailing whitespace, and debug statements.
This makes sure our code is uniform, also we don't have to overthink most of the code related things since pre-commit
handles that for us.
Since we use docker
for most of the projects & use git on our host
machine you will need to have pre-commit installed in your host
.
pip3 install pre-commit==3.4.0
If pre-commit is not found then try looking for the executeable in your $PATH. Else update your shell with:
export PATH=$PATH:/Users/UserName/Library/Python/3.9/bin/
If project doesn't have a .pre-commit-config.yaml
file. Create one with
$ pre-commit sample-config > .pre-commit-config.yaml
This should create a .pre-commit-config.yaml
file with these cconfigurations.
┌─[✗]─[newbie@parrot]─[~/tmp/naxa-backend-boilerplate]
└──╼ $cat .pre-commit-config.yaml
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
It has hooks to check.
Pre commit generallly runs hooks on each commit, but in case if you want to check older files and commits then.
$ pre-commit install-hooks
$ pre-commit run --all-files
$ pre-commit autoupdate
$ pre-commit install
Once configured you can commit your code, pre-commit
will warn and fix your codes while you commit.
┌─[newbie@parrot]─[~/tmp/naxa-backend-boilerplate]
└──╼ $git commit -m "test"
Trim Trailing Whitespace.................................................Passed
Fix End of Files.........................................................Failed
- hook id: end-of-file-fixer
- exit code: 1
- files were modified by this hook
Fixing README.md
Check Yaml...........................................(no files to check)Skipped
Check for added large files..............................................Passed