Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
This repository contains a collection of Git hooks that can be used to enforce coding standards, run tests, and perform other tasks. This is used across repositories in our organization to ensure consistency and quality in our codebase.

Please refer to the README of each repository for specific instructions on how to use these hooks.

# Conditions
- We expect the code to be formatted before commit. Having an auto format introduces unintenional file changes
- Preserve developer intent as much as we can.
43 changes: 43 additions & 0 deletions terraform/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
#
# Authors: aslamcodes
# Created on: 08/12/2025
# Last updated on: 08/12/2025

set -euo pipefail

echo "Running Terraform pre-commit checks..."

# Check if terraform is installed
if ! command -v terraform &> /dev/null; then
echo "Error: terraform command not found. Please install Terraform."
exit 1
fi

# Get list of staged .tf files
STAGED_TF_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.tf$' || true)

if [ -z "$STAGED_TF_FILES" ]; then
echo "No Terraform files staged for commit."
exit 0
fi

echo "Checking Terraform files: $STAGED_TF_FILES"

# Format check
echo "Checking Terraform formatting..."
if ! terraform fmt -check -diff -recursive .; then
echo "Terraform files are not properly formatted."
echo "Run 'terraform fmt -recursive .' to fix formatting issues."
exit 1
fi

# Validate syntax
echo "Validating Terraform syntax..."
if ! terraform validate; then
echo "Terraform validation failed."
exit 1
fi

echo "All Terraform checks passed!"
exit 0