pre-push hook in Git - Time & Space Complexity
We want to understand how the time taken by a git pre-push hook changes as the amount of work it does grows.
Specifically, how does the hook's execution time grow when it checks many files or runs many commands before pushing?
Analyze the time complexity of the following pre-push hook script.
#!/bin/sh
# pre-push hook example
while read local_ref local_sha remote_ref remote_sha ; do
git diff --name-only $local_sha $remote_sha | while read file ; do
echo "Checking $file"
# simulate check command
done
done
This hook reads refs being pushed, then lists changed files and checks each one before allowing the push.
- Primary operation: Loop over changed files to check each one.
- How many times: Once per changed file in the push.
As the number of changed files increases, the hook runs more checks, so time grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 changed files | 10 checks |
| 100 changed files | 100 checks |
| 1000 changed files | 1000 checks |
Pattern observation: The time grows linearly with the number of changed files.
Time Complexity: O(n)
This means the hook's running time increases directly with the number of files it checks before pushing.
[X] Wrong: "The hook always runs in constant time no matter how many files change."
[OK] Correct: The hook checks each changed file one by one, so more files mean more work and longer time.
Understanding how hooks scale helps you write efficient checks that keep your code safe without slowing down your workflow too much.
"What if the hook ran a single command that checked all files at once instead of one by one? How would the time complexity change?"
Practice
pre-push hook in Git?Solution
Step 1: Understand the role of pre-push hooks
Pre-push hooks run scripts before pushing code to a remote repository to catch errors early.Step 2: Identify the correct purpose
They help ensure code quality by running tests or checks before the push happens.Final Answer:
To run checks or tests before code is pushed to a remote repository -> Option CQuick Check:
Pre-push hook = run checks before push [OK]
- Confusing pre-push with post-push hooks
- Thinking it merges branches automatically
- Assuming it deletes branches
pre-push hook script executable on Linux or macOS?Solution
Step 1: Identify how to make scripts executable
On Linux/macOS,chmod +xsets execute permission on a file.Step 2: Apply to pre-push hook
Runningchmod +x .git/hooks/pre-pushmakes the hook script runnable by Git.Final Answer:
chmod +x .git/hooks/pre-push -> Option AQuick Check:
Make hook executable = chmod +x [OK]
- Using git commands that don't exist for hooks
- Setting overly permissive permissions like 777
- Forgetting to make the script executable
pre-push hook script snippet, what will happen if the tests fail?
#!/bin/sh npm test || exit 1
Solution
Step 1: Understand the script logic
The script runsnpm test. If tests fail,npm testreturns non-zero, triggeringexit 1.Step 2: Effect of exit 1 in pre-push hook
Exiting with a non-zero code stops the push process, blocking the push.Final Answer:
The push will be blocked and stopped -> Option DQuick Check:
Exit 1 in pre-push = block push [OK]
- Assuming push continues even if tests fail
- Thinking tests are skipped automatically
- Confusing exit codes with success
pre-push hook script but it does not run when you push. What is the most likely cause?Solution
Step 1: Check script execution requirements
Git requires hook scripts to have execute permission to run them.Step 2: Identify common cause of hook not running
If the script lacks execute permission, Git silently skips running it.Final Answer:
The script is missing execute permissions -> Option BQuick Check:
Missing execute permission = hook won't run [OK]
- Blaming remote repository for local hook issues
- Thinking commit status affects hook execution
- Assuming branch name affects hook running
pre-push hook to run tests only on files changed in the last commit before pushing. Which approach is best?Solution
Step 1: Identify how to detect changed files
The commandgit diff --name-only HEAD~1 HEADlists files changed in the last commit.Step 2: Use this command in pre-push hook
Using this inside the hook lets you run tests only on changed files, saving time and resources.Final Answer:
Use git diff --name-only HEAD~1 HEAD inside the hook to get changed files and run tests accordingly -> Option AQuick Check:
Detect changed files with git diff for selective tests [OK]
- Running tests on all files unnecessarily
- Manually updating file lists each time
- Running tests after push instead of before
Ready to prove it?
Pass the test and this pattern moves unknown
