|
| 1 | +# Pre-Receive Hook Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `pre-receive` hook is a critical component of the Git Proxy system. It is executed before changes are accepted into a repository. This hook allows for custom logic to validate or reject incoming changes based on specific criteria, ensuring that only valid and authorized changes are pushed to the repository. |
| 6 | + |
| 7 | +## Functionality |
| 8 | + |
| 9 | +The `pre-receive` hook determines the outcome of a push based on the exit status of the hook script: |
| 10 | + |
| 11 | +- If the script exits with status `0`, the push is automatically approved. |
| 12 | +- If the script exits with status `1`, the push is automatically rejected. |
| 13 | +- If the script exits with status `2`, the push requires manual approval. |
| 14 | +- Any other exit status is treated as an error, and the push is rejected with an appropriate error message. |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +To use the `pre-receive` hook, follow these steps: |
| 19 | + |
| 20 | +- **Create a Hook Script**: |
| 21 | + Write a shell script or executable file that implements your custom validation logic. The script must accept input in the format: `<old_commit_hash> <new_commit_hash> <branch_name>`. |
| 22 | + |
| 23 | +- **Place the Script**: |
| 24 | + Save the script in the appropriate directory, such as `hooks/pre-receive.sh`. |
| 25 | + |
| 26 | +- **Make the Script Executable**: |
| 27 | + Ensure the script has executable permissions. You can do this by running the following command: |
| 28 | + |
| 29 | + ```bash |
| 30 | + chmod +x hooks/pre-receive.sh |
| 31 | + ``` |
| 32 | + |
| 33 | +> **Note**: If the `pre-receive` script does not exist, the hook will not be executed, and the push will proceed without validation. |
| 34 | +
|
| 35 | +## Example Hook Script |
| 36 | + |
| 37 | +Below is an example of a simple `pre-receive` hook script: |
| 38 | + |
| 39 | +```bash |
| 40 | +#!/bin/bash |
| 41 | + |
| 42 | +read old_commit new_commit branch_name |
| 43 | + |
| 44 | +# Example validation: Reject pushes to the main branch |
| 45 | +if [ "$branch_name" == "main" ]; then |
| 46 | + echo "Pushes to the main branch are not allowed." |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +# Approve all other pushes |
| 51 | +exit 0 |
| 52 | +``` |
0 commit comments