Sharing hooks with the team (husky) in Git - Time & Space Complexity
We want to understand how the time to set up and run shared git hooks with Husky changes as the project grows.
How does adding more hooks or team members affect the work done by Husky?
Analyze the time complexity of this Husky setup script.
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
git add .
This script runs before a commit to add all changed files automatically.
Look for repeated work inside the hook execution.
- Primary operation: Adding all changed files with
git add . - How many times: Runs once per commit, but internally git scans all changed files.
More changed files mean more work for git add ..
| Input Size (changed files) | Approx. Operations |
|---|---|
| 10 | 10 file checks and adds |
| 100 | 100 file checks and adds |
| 1000 | 1000 file checks and adds |
Pattern observation: The work grows roughly in direct proportion to the number of changed files.
Time Complexity: O(n)
This means the time to run the hook grows linearly with the number of changed files.
[X] Wrong: "Running git add . in the hook is always fast, no matter how many files changed."
[OK] Correct: The command must check and stage each changed file, so more files mean more work and longer time.
Understanding how tasks scale with input size helps you explain your choices clearly and shows you think about efficiency in real projects.
"What if the hook ran a lint check on all changed files instead of just adding them? How would the time complexity change?"
Practice
Solution
Step 1: Understand what Git hooks do
Git hooks are scripts that run at certain points in Git workflows, like before commits.Step 2: Identify Husky's role
Husky helps share these hooks easily among team members by managing them in the project.Final Answer:
To share Git hooks easily with the whole team -> Option AQuick Check:
Husky = Share Git hooks [OK]
- Thinking Husky manages branches
- Confusing Husky with backup tools
- Assuming Husky merges pull requests
Solution
Step 1: Recall Husky installation steps
First, you install Husky as a package (e.g., npm install husky), then you runhusky installto set up Git hooks.Step 2: Identify the command that sets up hooks
The commandhusky installcreates the hooks folder and configures Git to use Husky hooks.Final Answer:
husky install -> Option CQuick Check:
Setup hooks = husky install [OK]
- Using 'npm install husky' as hook setup
- Trying 'git husky add' which is invalid
- Confusing 'husky init' with 'husky install'
npm install husky npx husky install npx husky add .husky/pre-commit "npm test" git commit -m "test commit"
Solution
Step 1: Analyze the commands
First, Husky is installed. Then hooks are installed withhusky install. Next, a pre-commit hook is added to runnpm test.Step 2: Understand the effect on commit
Whengit commitruns, the pre-commit hook triggersnpm testbefore allowing the commit.Final Answer:
The pre-commit hook runs 'npm test' before the commit -> Option BQuick Check:
pre-commit runs npm test [OK]
- Assuming hooks run after commit
- Forgetting to run 'husky install'
- Confusing hook commands with commit messages
.husky folder but your teammate's hooks are not running. What is the most likely cause?Solution
Step 1: Understand sharing hooks with Husky
Even if the.huskyfolder is committed, each user must runhusky installlocally to activate hooks.Step 2: Identify the common setup mistake
If teammates don't runhusky install, hooks won't be linked to Git and won't run.Final Answer:
They forgot to runhusky installafter pulling -> Option AQuick Check:
Run husky install locally to activate hooks [OK]
- Assuming committed hooks auto-activate
- Thinking hooks don't work on Linux
- Believing Git disables hooks in shared repos
Solution
Step 1: Add the pre-push hook with Husky
Usenpx husky add .husky/pre-push "npm test"to create the hook script that runs tests before push.Step 2: Share and activate hooks
Commit the.huskyfolder so teammates get the hook, and they must runhusky installlocally to activate it.Final Answer:
Runnpx husky add .husky/pre-push "npm test", commit.husky, teammates runhusky install-> Option DQuick Check:
Add hook, commit folder, teammates install hooks [OK]
- Editing package.json instead of using husky add
- Manually adding scripts to .git/hooks (not shared)
- Using invalid git commands for hooks
Ready to prove it?
Pass the test and this pattern moves unknown
