Implementing Git Hooks Using Pre-Commit Framework

Arvind Patel
3 min readOct 31, 2023

--

Introduction :-

What is a Git Hook? A Git Hook is a script that executes before or after a certain Git operation, such as a commit to a repository. With a Git Hook, you can enforce specific checks and actions to make sure certain conditions are satisfied before committing changes to a repository.

Why Git Hooks Are Essential :-

Git hooks are a valuable tool in the software development process, providing a way to automate actions and enforce processes that help maintain code quality, enhance collaboration, and improve overall development workflows. Here are several reasons why Git hooks are essential:

  1. Code Quality Assurance
  2. Preventing Bad Commits
  3. Enforcing Commit Message Conventions
  4. Automating Repetitive Tasks
  5. Continuous Integration (CI) Integration
  6. Reducing Human Error

Setting Up Pre-Commit Framework

Pre-Commit is well-liked framework for handling and maintaining Git hooks. You can specify different actions and checks that should be carried out before each commit. Let’s look at how to configure Pre-Commit in iOS project.

Prerequisites

Before you get started, make sure you have the following prerequisites:

  1. Git installed on your machine.
  2. Xcode installed for iOS development.
  3. Homebrew installed for macOS package management.

Installation

First, you need to install the Pre-Commit framework using Homebrew

brew install pre-commit

Once Pre-Commit is installed, navigate to your iOS project’s root directory in the terminal.

Configuration: Setting Up Git Hooks

To configure Git hooks for your iOS project, follow these steps:

  1. Create a Configuration File: Begin by creating a .pre-commit-config.yaml file in your project's root directory. You can use the touch .pre-commit-config.yaml command to create this file.
  2. Define Hooks: Inside the .pre-commit-config.yaml file, you can define the Git hooks you want to use for your iOS project. Each hook configuration should be structured as follows:
repos:
- repo: local
hooks:
- id: swiftformat
name: Swift Format
description: Enforces formatting guidelines for Swift files before commiting.
language: system
entry: swiftformat --swiftversion 5
stages:
- pre-commit

- id: swiftlint
name: Swift Linter
description: Running a linter before commit.
language: system
always_run: true
entry: swiftlint lint --lenient --config .swiftlint.yml
stages:
- pre-commit

The meaning of each portion of this setup is as follows :-

  • repos: This section defines the repositories where your Git hooks are sourced from. In this example, we use a local repository (repo: local), but you can also reference remote repositories.
  • hooks: Under the hooks section, you specify the individual Git hooks you want to configure. In this case, we configure the swiftformat hook.
  • id: This is a unique identifier for the hook. It's used to reference the hook configuration.
  • name: A descriptive name for the hook, which helps identify its purpose.
  • language: Specifies the execution environment or language required for the hook. Here, we use system to indicate it's a system-level command.
  • entry: The actual command or script that the hook runs. In this example, it invokes the swiftformat tool with a specific Swift version.
  • stages: Defines the stages at which the hook runs. For a pre-commit hook, it should be set to pre-commit.

3. Installing Hooks

To install the hooks defined in your configuration file, run the following command from your project’s root directory:

chapre-commit install --hook-type commit-msg --hook-type pre-push --hook-type pre-commit

This command sets up the Git hooks in your local repository.

The command “pre-commit install — hook-type commit-msg — hook-type pre-push — hook-type pre-commit” installs three types of hooks in your project: commit-msg, pre-commit, and pre-push.

4. Summary

Git hooks, and specifically the Pre-Commit framework, are powerful tools that help automate tasks, maintain code quality, and streamline development processes. By following the outlined steps, you can effectively set up and configure Git hooks in your iOS project, ensuring that your code adheres to quality standards and best practices, all while reducing the potential for human error. Git hooks have become an essential component in modern software development workflows, providing a foundation for collaboration and code quality assurance.

It is important to note that this yaml code is just an example and it should be modified to suit your specific use case.

If you like this post, please share or comment on the post. Any sort of interaction is highly appreciated. Thank you !!!

--

--

Arvind Patel
Arvind Patel

Written by Arvind Patel

Arvind is an iOS app developer passionate about open source and boosting developer productivity. Follow him on Instagram www.instagram.com/arvind_iosdev