Git hooks - The Article 🚀

What are? Where they live? What they feed on? 😖

👑 Git

Before we talk about git hooks, we need to understand what is git. Git is a free and open source distributed version control system (VCS) designed to handle everything from small to very large projects with speed and efficiency. It is very popular among developers and most open source projects use this tool. If you want to read more about git please check this awesome explanation from [Git] Getting Started - What is it?.

🚜 Hooks ⚠️⚠️⚠️

As this awesome tutorial from Atlassian explained:

Git hooks are scripts that run automatically every time a particular event occurs in a Git repository. They let you customize Git’s internal behavior and trigger customizable actions at key points in the development life cycle.

It allows custom actions to be performed at any stage of the versioning control process. Ex:

  • Prevent unwanted code to be commit in the repo.
  • Standardize commit messages and branch naming.

As the hooks documentation from git explained:

There are two groups of these hooks: client-side and server-side. Client-side hooks are triggered by operations such as committing and merging, while server-side hooks run on network operations such as receiving pushed commits. You can use these hooks for all sorts of reasons.

pre-commit hooks script

#!/usr/bin/env bash
INPUT_FILE=$1
START_LINE=`head -n1 $INPUT_FILE`
PATTERN="^(PROJ)-[[:digit:]]+: "
if ! [[ "$START_LINE" =~ $PATTERN ]]; then
  echo "Bad commit message, see example: PROJ-123: commit message"
  exit 1
fi

References