README

What are Git Hooks?

Git hooks are shell scripts that run automatically before or after git executes an important command like Commit or Push. You can find hooks in your .git folder. When you $ git init, that command generates a number of hooks which define how git is able to write from your local to your remote repository.

git-hooks

A description of some of the hooks above are highlighted in the table below [1]. Depending on your software requirements and your knowledge of Bash, you can harness the full potential of these hooks and also create custom hooks. For more info, read this article on creating custom pre-commit hooks Implement your own Pre-commit Hooks.

Git Hook Git command Usage
post-update.sample git push By updating all data after the push
commit-msg.sample git commit To set the message of a commit action
pre-commit.sample git commit Before committing
prepare-commit-msg.sample git commit When a commit message is set
pre-push.sample git push Before making a push
pre-receive.sample git push When we push and get the data from the remote repository
update.sample git push By updating the remote data in a push

 

Pre-Commit Hooks

Pre-commit hooks are a mechanism of the version control system git. They let you execute code right before the commit. In this wiki, we will highlight a few packages which are useful for static code analysis. Some of which are the very hooks used in the pre-commit hooks.[2]

 

Create a .pre-commit-config.yaml file within your project and use in multiple projects. This file contains the pre-commit hooks you want to run every time before you commit. It looks like this [3]

 

 

Why use Pre-commit hooks?

  • Improve the quality of commits, obviously.
  • Very useful in production environments for code styling and compliance.
  • These hooks help automate static code analysis thereby informing the developer of potential issues within the code.

 

Type of hooks

Some hooks are specific to programming languages other than Python and R. However, most of these hooks can be used in generic projects to check other non-specific programming scripts.

  • Code analysis

    • R: lintr
    • Python: pylint
  • Code quality and styling (automated)

    • R: styler, tidyverse_style
    • Python: black, flake8
  • File formatting

    • check-json
    • check-yaml
    • end-of-file-fixer
    • trailing-whitespace
  • Security

  • Miscellaneous

    • check-large-files

 

Installation in R

For full set up guide see in link Install and use pre-commit hooks in R.

 

Python

For full set up guide see in link Install and use pre-commit hooks in python.

 

Pre-commits in action

The typical results when you commit a repo with pre-commit hooks are Passed, Failed, Skipped. "Skipped" denotes there is nothing to check perhaps because the file is missing or you have forgotten to $ git add the necessary files.

Snapshots of pre-commit hooks in use (R and Python).

 

Sources

  1. What are Git Hooks and How to Start Using Them? (hostinger.co.uk)
  2. Static code analysis
  3. Pre-commit hooks you must know. Boost your productivity and code… | by Martin Thoma | Towards Data Science

Leave a Reply

Your email address will not be published. Required fields are marked *