2 Use Cases of Python Pre-commit Hooks to Tidy Up Your Git Repositories | by Eldad Uzman | Apr, 2022

picture by Kelly Sikkema on unsplash

Effectively organized repo results in a well-organized code and a greater developer expertise.

On this article, I’ll present you 2 use-cases of pre-commit to keep up a greater repository.

Pre-commit is a python primarily based software that lets you ‘hook’ into your git repository and set off a easy code at any time when adjustments are dedicated.

It’s written and maintained by the superior python developer and YouTuber Anthony Sottile.

With pre-commit, builders can make the most of gits distributed structure and make brief and easy code checks earlier than submitting to code evaluate.

This protects plenty of again and ahead inter-team communication that’s redundant and counter-productive and permits the code reviewer to concentrate on precise code implementation fairly than on floor degree issues.

Through the use of git commit and code evaluate earlier than working cicd builds, we will save time to ship and a few of the prices concerned.

To start out with git commit you solely want 3 steps:

  1. Set up pre-commit from pip:
pip set up pre-commit

2. Create the pre-commit configuration file in your repository (about that, shortly)

3. Set up the commit configuration into your git repo:

pre-commit set up

And from that second on, the hook shall be executed in line with your configuration file each time whenever you commit new code.

Let’s take the instance of Jupyter notebooks.

A Jupyter pocket book comprises each the supply code and its output supplies in a JSON format.

pip set up pandas plotly jupyter

In case you commit the pocket book as is, you’ll commit it with its outputs and typically it may take an excessive amount of area and contaminate the repository.

So let’s create a hook to scrub the repository on commit.

1. Let’s provoke our git repo:

git init

2. Now let’s add a pre-commit configuration file:

3. Set up the hook:

pre-commit set up>>>
pre-commit put in at .githookspre-commit

4. Run git standing

git standing>>>
On department important
No commits butUntracked information:
(use "git add <file>..." to incorporate in what shall be dedicated)
.gitignore
.pre-commit-config.yaml
notebooks/

I’ve positioned the pocket book underneath the notebooks listing

5. Observe all of the information

git add --all>>>
warning: LF shall be changed by CRLF in .gitignore.
The file could have its authentic line endings in your working listing
warning: LF shall be changed by CRLF in notebooks/pocket book.ipynb.
The file could have its authentic line endings in your working listing

6. Commit the adjustments

git commit -m "good commit">>>
jupyter-nb-clear-output..................................................Failed
- hook id: jupyter-nb-clear-output
- exit code: 1
C:workspace-vscodepre-commit-demovenvScriptspython.EXE: No module named nbconvert

Our pre-commit failed as a result of it couldn’t discover nbconvert module.
We have to set up it:

pip set up nbconvert

And commit once more:

git commit -m "good commit">>>
jupyter-nb-clear-output..................................................Failed
- hook id: jupyter-nb-clear-output
- information had been modified by this hook
[NbConvertApp] Changing pocket book notebooks/pocket book.ipynb to pocket book
[NbConvertApp] Writing 878 bytes to notebooksnotebook.ipynb

Now pre-commit notifies us that the file has been modified.
It cleaned the output off of the pocket book.
Now we have to add the adjustments:

git add --all

And commit:

git commit -m "good commit">>>
jupyter-nb-clear-output..................................................Handed
[main(root-commit) 8560ba3] good commit
4 information modified, 379 insertions(+)
create mode 100644 .gitignore
create mode 100644 .pre-commit-config.yaml
create mode 100644 notebooks/pocket book.ipynb
create mode 100644 necessities.txt

This may be divided into 2 domains:

Linting

Let’s create a brand new dir named scripts and add a file named mainapp.py

On this file, we are going to write some ineffective code 🙂

I’ve already written about the advantages of linting here, now it’s time so as to add linting into our workflow.

  1. Run git standing:
git standing>>>
On department important
Untracked information:
(use "git add <file>..." to incorporate in what shall be dedicated)
scripts

2. Add a brand new hook to pre-commit config file:

We’ve added a brand new hook with pylint id to our pre-commit config.

3. Set up the brand new hook configuration:

pre-commit set up>>>
pre-commit put in at .githookspre-commit

4. Observe adjustments:

git add --all

5. Commit

git commit -m "commit #2">>>
jupyter-nb-clear-output..............................(no information to examine)Skipped
pylint...................................................................Failed
- hook id: pylint
- exit code: 16
PYLINTHOME is now 'C:UsersuserAppDataLocalpylintpylintCache' however obsolescent 'C:Usersuser.pylint.d' is discovered; you may safely take away the latter
************* Module somescript
scriptssomescript.py:1:0: C0114: Lacking module docstring (missing-module-docstring)
-----------------------------------
Your code has been rated at 5.00/10

The commit failed resulting from lacking module docstring error from pylint.
Now lets add a module docstring.

Add the adjustments

git add --all

And commit:

git commit -m "commit #2"
jupyter-nb-clear-output..............................(no information to examine)Skipped
pylint...................................................................Handed
[master b2d3105] commit #2
2 information modified, 8 insertions(+), 2 deletions(-)

Commit handed 🙂

Discover — the hook for Jupyter notebooks skipped as a result of there have been no adjustments made to the pocket book.
This makes pre-commit extraordinarily environment friendly.

Formatting

Identical to linting, we will set off auto formatting packages like black

We added the operate operate to our authentic script, however foo is ugly formatted.

  1. add the hook configuration:

2. Set up the brand new hook

pre-commit set up>>>
pre-commit put in at .githookspre-commit

3. Observe the adjustments:

git add --all

4. Commit

git commit -m "add operate">>>
jupyter-nb-clear-output..............................(no information to examine)Skipped
pylint...................................................................Handed
black....................................................................Failed
- hook id: black
- exit code: 1
Executable `black` not discovered

Our commit failed as a result of black was not discovered.
Lets set up it:

pip set up black

And commit once more:

git commit -m "add operate">>>
jupyter-nb-clear-output..............................(no information to examine)Skipped
pylint...................................................................Handed
black....................................................................Failed
- hook id: black
- information had been modified by this hook
reformatted scriptssomescript.pyAll performed! u2728 U0001f370 u2728
1 file reformatted.

File has been formatted!

Significantly better!

Now lets add the adjustments:

git add --all

And commit:

git commit -m "add operate"
jupyter-nb-clear-output..............................(no information to examine)Skipped
pylint...................................................................Handed
black....................................................................Handed
[master 4bd3217] add operate
2 information modified, 12 insertions(+)

Pre-commit might help you shortly establish issues in your native clone of the code with out going all the way in which to a ci/cd construct or code evaluate.

It lets you successfully implement coding requirements and conventions throughout the group and have a better-organized codebase.

More Posts