Creating custom hook scripts in Git - Performance & Efficiency
When we create custom hook scripts in Git, we want to know how the time to run these scripts changes as the project grows.
We ask: How does running a hook script scale when the repository size or number of files increases?
Analyze the time complexity of this simple pre-commit hook script.
#!/bin/sh
# pre-commit hook
# Check all staged files for TODO comments
files=$(git diff --cached --name-only)
for file in $files; do
grep -q "TODO" "$file" && echo "TODO found in $file" && exit 1
done
exit 0
This script checks each staged file for the word "TODO" and stops the commit if any are found.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each staged file and running a search inside it.
- How many times: Once for each staged file, so the number of files affects the count.
As the number of staged files grows, the script checks more files one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files | 10 file checks |
| 100 files | 100 file checks |
| 1000 files | 1000 file checks |
Pattern observation: The work grows directly with the number of files checked.
Time Complexity: O(n)
This means the time to run the hook grows linearly with the number of staged files.
[X] Wrong: "The hook runs instantly no matter how many files are staged."
[OK] Correct: Each file adds work because the script checks them one by one, so more files mean more time.
Understanding how hook scripts scale helps you write efficient checks that keep your workflow smooth as projects grow.
"What if the hook script also searched inside each file line by line? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand Git hooks
Git hooks are scripts that run automatically at certain points in Git's workflow, like before a commit or after a push.Step 2: Identify the purpose of custom hooks
Custom hooks let you automate tasks or enforce rules during these Git events without manual intervention.Final Answer:
To run scripts automatically at specific Git events -> Option DQuick Check:
Git hooks = automatic scripts at Git events [OK]
- Thinking hooks manually change commit messages
- Confusing hooks with branch deletion
- Assuming hooks create repositories
Solution
Step 1: Understand file permissions for hooks
Git hooks must be executable files to run automatically.Step 2: Use the correct command to make a file executable
The commandchmod +xadds execute permission without changing other permissions.Final Answer:
chmod +x .git/hooks/pre-commit -> Option AQuick Check:
Make hook executable = chmod +x [OK]
- Using git commands that don't exist for hooks
- Setting overly permissive permissions like 777
- Trying to enable hooks via git config
.git/hooks/pre-commit and made executable:
#!/bin/sh if git diff --cached --quiet; then echo "No changes staged for commit." exit 1 fiWhat happens when you try to commit without staging any changes?
Solution
Step 1: Analyze the script logic
The script checks if there are no staged changes usinggit diff --cached --quiet. If true, it prints a message and exits with code 1.Step 2: Understand Git hook exit codes
Exiting with code 1 in a pre-commit hook stops the commit process and shows the message.Final Answer:
The commit is blocked with message 'No changes staged for commit.' -> Option AQuick Check:
Exit 1 in pre-commit = block commit [OK]
- Assuming commit proceeds despite exit 1
- Confusing staged vs unstaged changes
- Expecting syntax errors from correct shell script
#!/bin/sh echo "Commit done!"Which is the most likely reason?
Solution
Step 1: Check common hook issues
Hooks must be executable files inside.git/hooks/with correct names.Step 2: Identify the most common cause
Often, scripts don't run because they lack execute permission, even if named correctly.Final Answer:
The script is not executable -> Option BQuick Check:
Hooks must be executable to run [OK]
- Placing scripts outside .git/hooks directory
- Assuming echo is not allowed in hooks
- Using wrong hook filenames
.git/hooks/commit-msg will achieve this?Solution
Step 1: Understand commit-msg hook argument
The commit message file path is passed as the first argument ($1) to the script.Step 2: Check script logic for enforcing 'JIRA'
#!/bin/sh if ! grep -q 'JIRA' "$1"; then echo "Commit message must include 'JIRA'." exit 1 fi uses grep -q to check if 'JIRA' is missing, then prints a message and exits with 1 to block the commit.Step 3: Evaluate other options
#!/bin/sh grep 'JIRA' "$1" || echo "Commit message missing JIRA" only echoes but does not block commit. #!/bin/sh if grep -q 'JIRA' "$1"; then exit 1 fi blocks if 'JIRA' is present (wrong logic). #!/bin/sh echo "Checking commit message for JIRA" just echoes without checks.Final Answer:
#!/bin/sh if ! grep -q 'JIRA' "$1"; then echo "Commit message must include 'JIRA'." exit 1 fi -> Option CQuick Check:
Exit 1 if 'JIRA' missing blocks commit [OK]
- Not exiting with non-zero to block commit
- Checking for presence instead of absence
- Only printing message without blocking
Ready to prove it?
Pass the test and this pattern moves unknown
