“Cook” it and “Pre Hook” it – Cook your application code and use the capabilities of pre-commit hook “linters” to do a baseline check before you commit your code and push it to the repository. So, I assume my readers would have the following three questions
- What is a hook?
- What is Pre-commit?
- What is the problem statement that pre-commit resolves?
- What are Pre-commit hooks?
- What is a linter?
What is a Hook?
In programming, a hook is an application, a set of customized program code that helps to perform other functionality. This simplifies interoperability between two applications or platforms. In technical term, it is a callback for an event or set of events triggered by certain actions, which might be clicking a button to running certain specific scripts.
What is Pre-commit?
Pre-commit is a framework for managing and maintaining multi-language pre-commit hooks.
What is the problem statement that pre-commit resolves?
Git hook scripts are very useful for identifying simple issues before submission to code review. We run these hooks on every commit to automatically point out issues in the code such as missing semicolons, trailing whitespace, and debug statements. By pointing out these issues out before code reviewing, allows a code reviewer to focus more on the architecture of the application rather than petty issues.
As we keep on creating more libraries and projects it becomes complicated to share pre-commit hooks across projects. It is recommended that we should always use the best industry standard linters.
What are Pre-commit hook?
The pre-commit hook platform solves the hook issues. It is a multi-language package manager for pre-commit hooks. We specify a list of hooks we want and pre-commit manages the installation and execution of any hook written in any language before every commit. Pre-commit hook is specifically designed to not require root access. Pre-commit hook automatically handles downloading and building from packages without root.
What is a linter?
A “linter” or “lint” is a tool or certain program/programs that analyze the source code to flag programming errors, bugs, style based errors and suspicious constructs. The term is originated from a UNIX utility that examined C language source code.
Installing pre-commit can be done in two ways:
pip install pre-commit
Using Home-Brew for MacOS
brew install pre-commit
For system level installation
curl| sudo python - pre-commit
In a python based project
In a python based project, you can add the dependency to the
Add 'pre-commit' to theRun pip install -r
Adding pre-commit plugins to your project
Once you have pre-commit installed, adding pre-commit plugins to your project is done with the
.pre-commit-config.yaml configuration file.
Add a file called
.pre-commit-config.yaml to the root of your project. The pre-commit config file describes what repositories and hooks are installed.
A typical .would look something like this:
repos: - repo: git://sha: v0.20.2 hooks: - id: yapf - repo: sha: v1.2.0 hooks: - id: trailing-whitespace - id: check-merge-conflict
In the above example, I have included plugins for python linter which is “yapf“, “trailing-whitespace” to remove trailing whitespace and “check-merge-conflict” to check for merge conflicts in the file.
You would also have to add a “.” file which would have the style standards defined within. The “. ” file would look something like this:
[style] based_on_style = pep8 COLUMN_LIMIT = 120 DEDENT_CLOSING_BRACKETS = true
Updating hooks automatically
You can update your hooks to the latest version automatically by running
pre-commit autoupdate. This will bring the hooks to the latest sha on the master branch.
You have to run
pre-commit install to install pre-commit into your git hooks. pre-commit will now run on every commit. Every time you clone a project using pre-commit running
pre-commit install --install-hooks should always be the first thing you do. Check the screenshot for more reference.
If you want to manually run all pre-commit hooks on a repository, run
pre-commit run --all-files. To run individual hooks use
pre-commit run .
The first time pre-commit runs on a file it will automatically download, install, and run the hook. Note that running a hook for the first time may be slow. For example: If the machine does not have node installed, pre-commit will download and build a copy of node.
To get started with the pre-commit checks just commit your code to see whether your codebase follows industry standards or not. Check the screenshot for more reference.
Updating hooks automatically
You can update your hooks to the latest version automatically by running.
pre-commit autoupdate This will bring the hooks to the latest sha on the master branch. Check the screenshot for more reference.
Please post your feedback. Your feedback is valuable in terms of improving my blog contents and technical aspects as well. Thank you ? Have a nice day !!
Soumyajit is 5+ years experienced Software professional with his prime focus on automation technologies based on quality development and takes interest in the CI/CD processes. He provides help in developing the QA process in an organization with his skills in automation for the web platform. His focus is on improving the delivery process for an ongoing project and connects the dot to help out with a successful deployment. He has experience in working on analytics, e-commerce, and the ad-tech domain.
Besides being a professional he takes an immense interest in learning new skills and technologies. He is a research guide author/writer at Dzone and Web Code Geeks. He also maintains a blog platform of his own where he likes to keep up his technology junks.