pre-receive
- Git course
- Expert
- Lesson 6/32
- 4 min
Learn the server-side pre-receive hook.
What Is the pre-receive Hook?
#!/bin/bash
echo "Checking push"Push Checks and Input Data
while read old new ref
do
echo "$ref"
donePractice Reject Pushes to the Main Branch
Write a `pre-receive` script. Read input lines into the `old`, `new`, and `ref` variables. Reject the push if the reference is `refs/heads/main`. Otherwise, print that the check is complete.
Starter code
# Girdi satırlarını oku
# Referansı denetleSample solution
#!/bin/bash
while read old new ref
do
if [[ "$ref" == "refs/heads/main" ]]; then
echo "Ana dala push yasak" >&2
exit 1
fi
done
echo "Push kontrol edildi"Expected output
Push kontrol edildi
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 When does the pre-receive hook run?
- When a repository is cloned
- When commit history is displayed
- When a local file is saved
- Before the push is accepted by the server Correct answer
Explanation: `pre-receive` runs to determine whether an incoming push request should be accepted.
2 Read pre-receive Input
while read old new ref
do
echo "$_____"
done____
while read old new ref
do
echo "$_____"
doneref
Explanation: The `ref` variable contains the name of the reference being updated.
3 Match the pre-receive Concepts
| Term | Meaning |
|---|---|
pre-receive |
Runs before a push is accepted |
Standart girdi |
Carries update information |
Sunucu tarafı |
Runs on the remote repository |
Sıfır dışı çıkış kodu |
Rejects the push operation |