I have a Fossil repository and a corresponding Git repository. I wanted to have the fossil repo auto-sync to the git repo.

This is very well explained on the fossil website page – How To Mirror A Fossil Repository On GitHub

I have used this before, but this requires setting up a cron to do periodic updates to the git repo. This time, I wanted to do it using the after-receive hook.

Initiate fossil git export

There are essentially three steps. First set-off the sync by having both the git repo and the fossil repo available and set it up.

fossil git export /path/to/git/repo --autopush \
  https://username:password@github.com/username/project.git

All of this is listed in the How To Mirror A Fossil Repository On GitHub

The second step is to call the following repeatedly

fossil git export -R /path/to/fossil/repository.fossil

Set up after-receive.sh shell script

For this to happen, create a after-receive.sh shell script.

#!/bin/sh
# * after-receive-hook-sync-fossil-with-git.sh
# * Hook command must pass Fossil substitutions: /full/path/to/after-receive.sh %F %R %A
# * After-receive also delivers new artifacts on standard input (one per line).
FOSSIL_EXE="$1"
REPO="$2"
AUX_FILE="$3"

# * Incremental export to the Git mirror.
if [ -n "$FOSSIL_EXE" ] && [ -n "$REPO" ]; then
  "$FOSSIL_EXE" git export -R "$REPO" -q 2>>"$(dirname "$0")/after-receive.log"
fi

This particular shell script should be made executable via

chmod +x /full/path/to/after-receive.sh

Add after-receive hook to fossil repository

Once the shell script after-receive.sh script is set up, add it as an after-receive hook to the fossil repository.

fossil hook add -R /full/path/to/repo.fossil --type after-receive --sequence 10 --command '/full/path/to/after-receive.sh %F %R %A'

Confirm that the hook has been added by running

fossil hook list -R /full/path/to/repo.fossil

Now, whenever there is a checkin, the after-receive hook fires which in turn calls the after-receive.sh script. That, in turn, does a fossil git export.

One of the key points from the Fossil Hooks page:

All scripts are expected to run relatively quickly. If a long-running process is started by a hook, it should be run in the background so that the original script can return.

For a fossil git export this should not take too much time.