#!/bin/bash
# Git post-receive hook for bcachefs-tools.git
#
# Updates the desired commit for the CI orchestrator and wakes it up.
# Install: cp to /var/www/git/bcachefs-tools.git/hooks/post-receive

STATE_DIR="/home/aptbcachefsorg/package-ci"
PID_FILE="$STATE_DIR/orchestrator.pid"

queue_build() {
    local commit=$1

    echo "$commit" > "$STATE_DIR/desired"
    echo "CI: queued build for ${commit:0:12}"

    if [ -f "$PID_FILE" ]; then
        kill -USR1 "$(cat "$PID_FILE")" 2>/dev/null || true
    fi
}

while read oldrev newrev refname; do
    case "$refname" in
        refs/heads/master)
            queue_build "$newrev"
            ;;
        refs/tags/v*)
            # Release tags may be pushed without (or after) a branch
            # update - e.g. tagged on the stable branch. The orchestrator
            # publishes to the release suite for any exact-tagged commit
            # (git describe --exact-match), it just needs to be told to
            # build it. Resolve annotated tag objects to the commit:
            commit=$(git rev-parse "$newrev^{commit}" 2>/dev/null) || continue
            queue_build "$commit"
            ;;
    esac
done
