Simple Git Pre-Commit Hooks for any Python Project

Code quality has to be top notch be it an individual project or you are working in an industry. Companies and Python itself has got some…

Simple Git Pre-Commit Hooks for any Python Project

Code quality has to be top notch be it an individual project or you are working in an industry. Companies and Python itself has got some standards settings to write a quality code.

“Make it work” is main motto we programmers follow which can be misleading from the perspective of a recruiter. Your open-sourced project on GitHub has to have certain rules and standards in order for others to understand the lines you have written.

Every Devops team and code quality control team in your group or in companies do have Git Hooks setup to check the code quality before it is pushed to the remote.

Git Hooks

Git Hooks are set of scripts that execute on different triggers. Most common triggers are commit and push. Git Hooks helps in customizing the developmental workflow.

Getting Started

Like always Python do have easy solution to this pre-commit.It is a simple python package that handles the triggers on a python project.

First things first let's install the python packagepip install pre-commit

You check the version of pre commit with --version flag. Let’s check our version of pre-commit installpre-commit --version

pre-commit version

In order for pre-commit to work we need to write some set of actions in a file .pre-commit-config.yaml Here we define different pre-commit hooks to be executed in different triggers. Let’s configure basic necessary hooks for a generic python project.

The basic structure of a pre-commit config file starts with repos and we define different necessary hooks with repo:repos:
   -  repo: <<repo directory>>
      rev: <<version>>
      hooks:
        -  id: <<id of hook>>

Here is an example of a pre-commit file

You can check for available hooks in official web directory of pre-commit.

Once you have written hooks for your project you need to install all these configurations to your .git directory. In order for so we run pre-commit install this command adds all your configurations to git hooks directory to run when an event is triggered.

Once you have installed to the hooks directory it's time to test pre-commit hook. In order to test pre-commit, we simply run pre-commit run

pre-commit run

Pre-Commit runs all the necessary quality tests and shows the results. You will be able to push your code to remote only when all the tests-cases pass.

pre-commit run results

We can see one of the hooks failed. We need to look into the file mentioned and fix the issue in order for the commit to be staged. Once you have all your files fixed according to the rules you have written following like result can be seen in your console.

pre-commit run result

Now you are ready to push your modification to remote.

Happy Coding!!
poudyalanil - Overview
I enjoy computers as much as I love my mom Block or Report May Jun Jul Aug Sep Oct Nov Dec Jan Feb Mar Apr May Mon Wed…