#!/bin/sh
#
# Copyright (c) 2026 The NetBSD Foundation, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

#
# post-update <ref1> <ref2> ...
#
#       This git hook is run by git-receive-pack(1) after applying
#       updates from a client running `git push'.  It receives as
#       arguments all the refs that were updated by the push.
#
#	By the time this hook runs, the updates have been committed to
#	the repository; on failure of this hook, the push is _not_
#	rolled back.
#
#	See the githooks(5) man page for details.
#
# WHAT THIS HOOK IMPLEMENTATION DOES
#
#	If the repository is configured as a one-way git->anoncvs
#	mirror, this hook uses cvsmirror(1) to commit the changes to
#	the CVS mirror so they can be viewed with read-only anoncvs
#	access.  If this is interrupted, cvsmirror(1) will remember the
#	last push that did make it through, so this will pick up where
#	it left off next time.
#

DEBUG=$(git config --type bool --default false netbsd.debug)
if $DEBUG; then
	echo BEGIN $$ post-update "$@"
	trap 'echo END $$ post-update' EXIT HUP INT TERM
fi

set -Ceu
if $DEBUG; then
	set -x
fi

CVSMIRRORROOT=$(git config netbsd.cvsmirrorroot) || exit 0
module=$(git config netbsd.cvsmodule) || exit 0

export CVSMIRRORROOT

status=0
for ref; do
	case $ref in
	refs/heads/*)
		ref=${ref##refs/heads/}
		;;
	*)	printf >&2 'ignoring non-head ref %s\n' "$ref"
		continue
		;;
	esac
	old_latest=$(${CVSMIRROR:-cvsmirror} latest "$module" "$ref") || {
		printf >&2 'ignoring unmirrored ref %s\n' "$ref"
		continue
	}
	new_latest=$(git rev-list -n1 "$ref")
	git rev-list "$old_latest".."$ref" \
	| ${CVSMIRROR:-cvsmirror} commit "$module" "$ref" "$new_latest" \
	    sh -c '
		set -Ceu
		d=$1
		shift
		while read commit; do
			git --git-dir "$d" cvsexportcommit -acpv "$commit"
		done
	    ' -- "$(pwd)" || {
		status=$?
		printf >&2 'failed to update module %s branch %s\n' \
		    "$module" "$ref"
	}
done
exit $status
