Summary - Custom hooks
- Git course
- Expert
- Lesson 8/32
- 5 min
Combine the custom hook flow to set up server-side policy.
Summary: Server-Side Hook Policies
#!/bin/bash
# Server-side validation script
echo "Checking policy"Practice Write a Combined Server Policy
Write an `update` hook script. Use Bash, read the reference name from the first argument, and reject updates to `refs/heads/main`. Print an approval message for other references. This example represents a branch-specific server policy.
Starter code
# Referans adını oku
# Politika koşulunu ekleSample solution
#!/bin/bash
refname="$1"
if [[ "$refname" == "refs/heads/main" ]]; then
echo "Politika: ana dal korumalı" >&2
exit 1
fi
echo "Politika: güncellemeye izin verildi"Expected output
Politika: güncellemeye izin verildi
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which hook is suitable for checking all references in a push together?
- update
- pre-receive Correct answer
- A local shell script
- git status
Explanation: `pre-receive` can inspect the update information together before the push request is accepted.
2 Complete the Hook Policy
if [[ "$refname" == "refs/heads/main" ]]; then
echo "Güncelleme reddedildi"
_____ 1
fi____
if [[ "$refname" == "refs/heads/main" ]]; then
echo "Güncelleme reddedildi"
_____ 1
fiexit
Explanation: To reject the update, the hook script must finish with a non-zero exit status. `exit 1` does this.
3 Match the Server-Side Policy Components
| Term | Meaning |
|---|---|
Hook betiği |
Performs automatic validation |
pre-receive |
Checks the push request as a whole |
update |
Checks references separately |
Politika |
Defines permitted operations |