post-merge hook in Git - Time & Space Complexity
We want to understand how the time taken by a git post-merge hook changes as the project size grows.
Specifically, how does the hook's work increase when more files or changes are involved?
Analyze the time complexity of this simple post-merge hook script.
#!/bin/sh
# post-merge hook example
changed_files=$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)
for file in $changed_files; do
echo "Processing $file"
# simulate some work per file
./process-file.sh "$file"
done
This script runs after a merge and processes each changed file one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each changed file to run a processing command.
- How many times: Once for every changed file in the merge.
The time grows with the number of changed files because the script processes each file separately.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 changed files | 10 processing runs |
| 100 changed files | 100 processing runs |
| 1000 changed files | 1000 processing runs |
Pattern observation: The work increases directly with the number of changed files.
Time Complexity: O(n)
This means the time to finish the hook grows in a straight line as more files change.
[X] Wrong: "The hook runs in constant time no matter how many files changed."
[OK] Correct: The hook processes each changed file one by one, so more files mean more work and more time.
Understanding how hooks scale helps you write efficient scripts that keep your project fast and smooth as it grows.
"What if the hook processed files in parallel instead of one by one? How would the time complexity change?"
Practice
post-merge hook in Git?Solution
Step 1: Understand the role of Git hooks
Git hooks are scripts that run at specific points in Git workflows to automate tasks.Step 2: Identify the specific timing of the post-merge hook
The post-merge hook runs right after a merge finishes, allowing automation of follow-up tasks.Final Answer:
To run commands automatically after a merge completes -> Option DQuick Check:
post-merge hook = runs after merge [OK]
- Confusing post-merge with pre-merge hooks
- Thinking it prevents merges
- Assuming it creates or deletes branches
post-merge hook script executable on a Unix-like system?Solution
Step 1: Understand file permissions for executable scripts
To run a script as a hook, it must have executable permission, usually set withchmod +x.Step 2: Identify the correct command
chmod +x .git/hooks/post-mergesets the executable bit without giving excessive permissions.Final Answer:
chmod +x .git/hooks/post-merge -> Option BQuick Check:
Make hook executable = chmod +x [OK]
- Using chmod 777 which is insecure
- Using chmod 644 which is not executable
- Trying git commands to enable hooks
post-merge hook script saved in .git/hooks/post-merge and made executable:#!/bin/sh npm install npm run build
What will happen after you run
git merge feature-branch?Solution
Step 1: Understand what the post-merge hook script does
The script runsnpm installto install dependencies, thennpm run buildto build the project.Step 2: Know when the post-merge hook runs
It runs automatically after a successful merge, so both commands will execute in order.Final Answer:
The dependencies will install and the project will build automatically -> Option AQuick Check:
post-merge runs script after merge [OK]
- Thinking hooks don't run automatically
- Assuming only first command runs
- Believing merge fails due to hook
post-merge hook script but it does not run after merging. Which of these is the most likely cause?Solution
Step 1: Check script execution permissions
If the script is not executable, Git will not run it after merge.Step 2: Understand that hooks are local and not committed
Hooks are not shared via commits, so forgetting to commit is irrelevant.Step 3: Consider merge method and script location
Rebase does not trigger post-merge, but the question states a merge was done. Also, hooks must be in.git/hooks.Final Answer:
The script file is not executable -> Option AQuick Check:
Non-executable script = hook won't run [OK]
- Thinking hooks are shared via commits
- Confusing merge with rebase triggers
- Saving script outside .git/hooks folder
requirements.txt and runs tests. Which post-merge hook script is the best choice?Solution
Step 1: Identify the correct commands for Python dependency installation and testing
pip install -r requirements.txtinstalls dependencies;pytestruns tests.Step 2: Evaluate other options for relevance
#!/bin/sh npm install npm test uses npm commands for JavaScript, not Python. #!/bin/sh pip uninstall -r requirements.txt pytest uninstalls dependencies which is wrong. #!/bin/sh git pull pytest pulls changes again, unnecessary after merge.Final Answer:
#!/bin/sh pip install -r requirements.txt pytest -> Option CQuick Check:
Python post-merge = pip install + pytest [OK]
- Using npm commands for Python projects
- Uninstalling dependencies instead of installing
- Running git pull again after merge
Ready to prove it?
Pass the test and this pattern moves unknown
