Skip to content

aotodev/pre-commit-hook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

C/C++ Pre-commit Hook

A simple yet effective pre-commit hook for C and C++ projects that automatically formats and cleans up your code before each commit.

Features

This pre-commit hook performs three main tasks on staged files:

  1. Remove trailing whitespaces - Cleans up trailing spaces and tabs at the end of lines
  2. Convert tabs to spaces - Replaces all tabs with 4 spaces for consistent indentation
  3. Format C/C++ code - Runs clang-format on all C/C++ source files

File Types Processed

  • General files (whitespace/tab cleanup): .sh, .s, .S, .asm, .ld, .txt, .cmake, .clang-format
  • C/C++ source files (formatting): .h, .c, .hpp, .cpp, .cppm

Requirements

  • clang-format must be installed and available in your PATH
  • A .clang-format configuration file must be present in your git repository root
  • Bash shell - The hook is written in bash and uses bash-specific features

Compatibility

  • Linux with bash
  • macOS with bash or zsh (the script runs in bash via the #!/bin/bash shebang)
  • WSL/Windows with bash

The hook uses bash-specific features like associative arrays and process substitution, so it requires bash to be available on your system.

Installation

  1. Copy the pre-commit file to your git hooks directory:
cp pre-commit .git/hooks/pre-commit
  1. Make sure the hook is executable:
chmod +x .git/hooks/pre-commit
  1. Ensure you have a .clang-format file in your repository root with your preferred formatting rules.

Usage

Once installed, the hook runs automatically on every git commit. It will:

  • Process only staged files (files added with git add)
  • Show a clear status for each operation with colored output:
    • Passed - No changes needed
    • Failed - Files were modified by the hook
    • Skipped - No relevant files found

If any files are modified by the hook, the commit will be rejected with exit code 1, allowing you to review the changes before committing again.

Example Output

Running pre-commit hook
✅ whitespaces...............................................................Passed
✅ tabs......................................................................Passed
❌ clang-format..............................................................Failed
- Files were modified by this hook

How It Works

The hook operates in the git repository root and:

  1. Checks for staged files using git diff --diff-filter=d --cached --name-only
  2. Separates files into general files and C/C++ source files
  3. Applies whitespace/tab cleanup to general files
  4. Runs clang-format with --dry-run first to check if formatting is needed
  5. If changes are required, applies them and reports the modifications
  6. Exits with code 1 if any files were modified, preventing the commit

About

A simple yet effective pre-commit hook for C and C++ projects

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages