Skip to content

[FOSS] Task-04: Implement Git Hooks #142

@Suvanwita

Description

@Suvanwita

📌 Objective

Set up Git Hooks to automatically:

  • Prevent bad or unsafe commits
  • Enforce meaningful commit messages
  • Improve code quality before code reaches the repository

This task requires no database or backend logic changes.


Contribution Rules ✅

  • Work only on foss branch
  • Original content only (No AI usage, otherwise Straight PR Rejection)

🔧 Step-by-Step Implementation Guide

🔹 Step 1: Create Hooks Directory (If Needed)

  • Navigate to the project root
  • Go to the .git folder
  • Ensure the hooks directory exists:
    .git/hooks/

⚠️ Do not commit .git/hooks directly. Hooks will be installed via a script.

🔹 Step 2: Create a pre-commit Hook

  • Inside .git/hooks/, create a file named: pre-commit
  • Make the file executable:
chmod +x .git/hooks/pre-commit

Add logic to:
Scan staged files

Block commit if:
console.log() or print() statements are found

Show an error message and exit with non-zero code

✅ Example behavior:

❌ Commit blocked: Remove console.log statements before committing.

🔹 Step 3: Create a commit-msg Hook

  • Inside .git/hooks/, create:
    commit-msg
  • Make it executable:
chmod +x .git/hooks/commit-msg
  • Validate commit messages to follow this format:
    :

Allowed types:

feat   
fix   
docs    
chore   
refactor   
test   

🚫 Invalid example:

updated stuff

✅ Valid example:

feat: add resume analysis service

🔹 Step 4: Create a Hook Setup Script

Add a setup script at:

scripts/setup-hooks.sh

The script should:

  • Copy hook files into .git/hooks
  • Set execute permissions automatically
  • Contributors should enable hooks by running:
bash scripts/setup-hooks.sh

🔹 Step 5: Add Documentation

Create a file:
docs/git-hooks.md

Document:

  • What each hook does
  • How to enable hooks
  • How to bypass hooks (e.g. --no-verify)

🔹 Step 6: Test the Hooks

Try committing code with:
Invalid commit message → should be blocked
Console logs in code → should be blocked
Fix issues and re-commit successfully


📂 Where to Commit the Files (MANDATORY)

All work for this issue must be committed in the foss branch only.

Create the following directory structure exactly as shown:

tasks/
└── task-04/
    └── <your_github_username>/
        ├── hooks/
        │   ├── pre-commit
        │   └── commit-msg
        └── scripts/
            └── setup-hooks.sh

🔁 Replace <your_github_username> with your actual GitHub username.


🧪 Files to Use for Testing Git Hooks (DO NOT COMMIT)

These files help verify that both pre-commit and commit-msg hooks are working as expected.

📄 Test File 1: Console Log Detection (Pre-Commit Hook)

File Name

test-precommit.js

File Content

function testHook() {
  console.log("This should block the commit");
}

testHook();

Expected Result

Attempting to commit this file should FAIL

Terminal should show:

❌ Commit blocked: Remove console.log or print statements before committing.

📸 Screenshot required: Terminal showing blocked commit

📄 Test File 2: Python Print Detection (Optional)

File Name

test-precommit.py

File Content

def test_hook():
    print("This should block the commit")

test_hook()

Expected Result

Commit should be BLOCKED

Error message displayed

📸 Screenshot required: Terminal output

📄 Test File 3: Clean File (Successful Commit Test)

File Name

test-success.js

File Content

function add(a, b) {
  return a + b;
}

module.exports = add;

Expected Result

Commit should succeed only with valid commit message

##🧪 Commit Message Tests (commit-msg Hook)

❌ Invalid Commit Message

git commit -m "updated code"

Expected:

❌ Invalid commit message format.  
Use: <type>: <short description>

📸 Screenshot required: Commit blocked

✅ Valid Commit Message

git commit -m "feat: add test file for hook verification"

Expected:

Commit succeeds

📸 Screenshot required: Successful commit


📌 Files to Commit

You must commit only these three files inside your folder:

  • hooks/pre-commit
  • hooks/commit-msg
  • scripts/setup-hooks.sh

❌ What NOT to Commit

  • .git/ folder
  • .git/hooks/*
  • Any test files used for verification
  • Any files outside tasks/task-04/<your_github_username>/

📸 Proof of Work (MANDATORY)

Add 3 screenshots to your Pull Request:

  • Pre-commit hook blocking commit
  • Commit-msg hook blocking invalid commit message
  • Successful commit with valid message

PRs without screenshots will be rejected.


✅ Acceptance Criteria

  • Hooks run automatically on commit
  • Invalid commits are blocked with clear messages
  • Setup script installs hooks correctly
  • Documentation is clear and easy to follow
  • No external dependencies required

Metadata

Metadata

Assignees

No one assigned

    Labels

    Points: 30open-for-allAny one can work on it without getting assigned, every PR can merge

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions