commit-msg hook for message validation in Git - Time & Space Complexity
We want to understand how the time to check a commit message grows as the message gets longer.
Specifically, how does the validation process scale with message size?
Analyze the time complexity of the following commit-msg hook script.
#!/bin/sh
commit_msg_file=$1
# Read the commit message
commit_msg=$(cat "$commit_msg_file")
# Check if message matches pattern
if ! echo "$commit_msg" | grep -qE '^[a-z]+: .{10,}'; then
echo "Error: Commit message must start with type and have at least 10 characters."
exit 1
fi
This script reads the commit message and checks if it starts with a word and a colon, followed by at least 10 characters.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The grep command scans the entire commit message text.
- How many times: It processes each character once to match the pattern.
The time to check grows roughly in direct proportion to the length of the commit message.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 characters | About 10 checks |
| 100 characters | About 100 checks |
| 1000 characters | About 1000 checks |
Pattern observation: The work grows linearly as the message gets longer.
Time Complexity: O(n)
This means the time to validate grows directly with the length of the commit message.
[X] Wrong: "The validation time is always constant because the pattern is simple."
[OK] Correct: Even a simple pattern check must look at each character, so longer messages take more time.
Understanding how simple checks scale helps you write efficient hooks and scripts that keep your workflow smooth.
"What if the commit-msg hook also checked multiple patterns one after another? How would the time complexity change?"