Server-side Hooks in Git

beginner
6 min

Server-side Hooks in Git

Welcome to the Git Server-side Hooks tutorial! šŸŽÆ

In this lesson, we'll dive into the fascinating world of Git hooks. These are scripts that run automatically on your Git server (like GitHub, GitLab, or Bitbucket) in response to specific events. By creating and customizing your own hooks, you can automate and secure your Git workflow.

Let's get started! šŸŽ‰

Understanding Git Hooks

šŸ“ Definition: Git hooks are scripts located in the .git/hooks directory of your Git repository. They are executed automatically by the Git server in response to certain events, such as commit, push, and receive.

Why use Git Hooks?

  • Automation: Hooks allow you to automate repetitive tasks, such as linting, testing, or deploying code.
  • Security: You can enforce custom rules to secure your Git repository, for example, by requiring pre-commit reviews or enforcing strong commit messages.

Types of Git Hooks

Git hooks can be classified into two main categories:

  1. Client-side hooks: Run on your local machine before committing changes to your repository.
  2. Server-side hooks: Run on the Git server in response to specific events, like push, receive, and post-receive. We will focus on server-side hooks in this tutorial.

Installing and Creating Server-side Hooks

šŸ’” Pro Tip: By default, server-side hooks are disabled on GitHub and GitLab. To enable them, you'll need to make the scripts executable.

Enabling Server-side Hooks on GitHub

  1. Navigate to your repository on GitHub.
  2. Click on the Settings tab.
  3. Navigate to the Webhooks section and click on Add webhook.
  4. Fill in the Payload URL field with the URL of your server-side hook script.
  5. Enable the Active checkbox and click on Add webhook.

Creating a Server-side Hook

šŸ“ Note: Server-side hooks are written in shell script (.sh), but you can also use other languages like Python, Ruby, or JavaScript.

  1. Create a new file in the .git/hooks directory with a name corresponding to the event you want to hook into (e.g., post-receive).
  2. Make the script executable by running chmod +x <script-name>.
  3. Write your script, which will be executed automatically in response to the specified event.

Examples of Server-side Hooks

Example 1: Enforcing Strong Commit Messages

Let's create a script that requires commit messages to follow a specific format.

bash
#!/bin/sh # Check if the commit message is in the format: <type>: <subject> if [[ $(git rev-parse HEAD) = $(git rev-parse --short HEAD)^ ]]; then if [[ $(git log -1 --format=%s) != *:* ]]; then echo "Invalid commit message format. Please use <type>: <subject>" exit 1 fi fi exit 0

Save this script as commit-msg in your repository's .git/hooks directory, make it executable (chmod +x commit-msg), and test it out!

Example 2: Preventing Pushes with Certain Words

This script blocks pushes if a blacklisted word is found in any commit message.

bash
#!/bin/sh # List of blacklisted words BLACKLIST=("secret" "confidential" "password") # Iterate through the commit messages for commit in $(git log --format=%s --grep-file=blacklist.txt); do if [[ $commit =~ .*(secret|confidential|password).* ]]; then echo "Commit message contains a blacklisted word. Push aborted." exit 1 fi done exit 0

Save this script as pre-push in your repository's .git/hooks directory, make it executable (chmod +x pre-push), and test it out!

Quiz

Quick Quiz
Question 1 of 1

What is the purpose of Git server-side hooks?