CodeBlanks Logo Icon CodeBlanks

Summary - Custom hooks

Combine the custom hook flow to set up server-side policy.

Summary: Server-Side Hook Policies

Custom hooks let you add automatic rules to Git operations. `pre-receive` checks all reference updates in an incoming push, while `update` checks each reference separately. Both hooks run on the server and can reject operations that violate a policy.
#!/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

terminal
# Referans adını oku
# Politika koşulunu ekle

Sample solution

terminal
#!/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

Question
if [[ "$refname" == "refs/heads/main" ]]; then
  echo "Güncelleme reddedildi"
  _____ 1
fi____
Answer
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

All lessons in Expert