📌 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:
✅ Valid example:
feat: add resume analysis service
🔹 Step 4: Create a Hook Setup Script
Add a setup script at:
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
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
File Content
def test_hook():
print("This should block the commit")
test_hook()
Expected Result
Error message displayed
📸 Screenshot required: Terminal output
📄 Test File 3: Clean File (Successful Commit Test)
File Name
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:
📸 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
📌 Objective
Set up Git Hooks to automatically:
This task requires no database or backend logic changes.
Contribution Rules ✅
🔧 Step-by-Step Implementation Guide
🔹 Step 1: Create Hooks Directory (If Needed)
.git/hooks/
🔹 Step 2: Create a pre-commit Hook
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:
🔹 Step 3: Create a commit-msg Hook
commit-msg
:
Allowed types:
🚫 Invalid example:
✅ Valid example:
🔹 Step 4: Create a Hook Setup Script
Add a setup script at:
The script should:
🔹 Step 5: Add Documentation
Create a file:
docs/git-hooks.md
Document:
🔹 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:
🔁 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
File Content
Expected Result
Terminal should show:
📸 Screenshot required: Terminal showing blocked commit
📄 Test File 2: Python Print Detection (Optional)
File Name
File Content
Expected Result
Error message displayed
📸 Screenshot required: Terminal output
📄 Test File 3: Clean File (Successful Commit Test)
File Name
File Content
Expected Result
##🧪 Commit Message Tests (commit-msg Hook)
❌ Invalid Commit Message
Expected:
📸 Screenshot required: Commit blocked
✅ Valid Commit Message
Expected:
📸 Screenshot required: Successful commit
📌 Files to Commit
You must commit only these three files inside your folder:
❌ What NOT to Commit
📸 Proof of Work (MANDATORY)
Add 3 screenshots to your Pull Request:
PRs without screenshots will be rejected.
✅ Acceptance Criteria