Git Hooks
Server-side Git Hooks
Feature available in Enterprise and On-premise versions
Server-side Git hooks (not to be confused with system or file hooks) run custom logic on the GitFlic server. You can use them to trigger tasks related to Git, such as:
- Enforcing certain conditions when creating commits
- Performing tasks based on repository state
Server-side Git hooks use the pre-receive
, post-receive
, and update
hooks on the server side.
Description of Server Hook Types
pre-receive
The first script triggered when processing push notifications from the client is pre-receive
. It receives a list of references passed through standard input. If it exits with a non-zero status, none of the references are accepted. You can use this hook to ensure, for example, that none of the updated commits use fast-forward
, or to control access to all references and files affected by the push.
update
The update
script is similar to the pre-receive
script, except it runs once for each updated branch. If the client tries to push to multiple branches, pre-receive
runs only once, while update
runs once per updated branch. update
receives three arguments: the reference (branch) name, the SHA-1 the reference pointed to before the push, and the SHA-1 the user is trying to push. If the update
script exits with a non-zero status, only that reference is rejected; other references may still be updated.
post-receive
The post-receive
hook runs after the entire process is completed and can be used to update other services or notify users. It receives the same data as the pre-receive
hook. Examples include emailing a list, notifying a continuous integration server, or updating an issue tracker—you can even analyze commit messages to decide whether to open, modify, or close any issues. This script cannot stop the push process, but the client will not disconnect until it finishes, so be careful if you attempt something that may take a long time.
Example Script for Git Hooks
Check the path to the required project on the server in the Admin Panel. To do this, go to the projects section and open the project editing page. On the "General" tab, you will find the path to the project in the repository storage.
In the project directory, for example, /var/tmp/gitflic/repo/c20242c3-aaaa-aaaa-aaaa-49afff94c56c.git
, there is a hooks
folder. This folder contains executable script files for your hooks. The script files must be named according to their hook type.
If you want to create a
pre-receive
hook, the executable file name should bepre-receive
Example of a pre-receive
hook using a bash script:
#!/bin/bash
# check each branch being pushed
echo "pre-receive HOOK"
while read old_sha new_sha refname
do
if git diff "$old_sha" "$new_sha" | grep -qE '^\+(<<<<<<<|>>>>>>>)'; then
echo "Saw a conflict marker in $(basename "$refname")."
git diff "$old_sha" "$new_sha" | grep -nE '^\+(<<<<<<<|>>>>>>>)'
exit 1
fi
if git diff "$old_sha" "$new_sha" | grep -qE '^\+.*\s+$'; then
echo "Saw whitespaces at EOL."
git diff "$old_sha" "$new_sha" | grep -nE '^\+.*\s+$'
exit 1
fi
done
exit 0
You can use any scripting language instead of Bash, but you must specify the path to the interpreter in the first line of your script. Example using Python to create a pre-receive
hook:
#!/usr/bin/python3
import re
import sys
print(sys.argv[0])
print(sys.argv[1],sys.argv[2])
if sys.argv[0] != sys.argv[1]: sys.exit(0)
Automated translation!
This page was translated using automatic translation tools. The text may contain inaccuracies.