set -e
GIT_REPO_URI=${1:-https://github.com/gentoo/gentoo.git}
cargo build -r -p pkgcruft-git
cargo build -r -p pkgcraft-tools
EXAMPLES_DIR=$(realpath ${BASH_SOURCE[0]%/*})
TARGET_DIR=$(realpath "${EXAMPLES_DIR}"/../../../target/release)
REPOS_DIR="${EXAMPLES_DIR}"/demo
export PATH="${TARGET_DIR}:${PATH}"
if [[ ! -d ${REPOS_DIR} ]]; then
mkdir -p "${REPOS_DIR}"
git clone --bare --depth 1 "${GIT_REPO_URI}" "${REPOS_DIR}"/remote.git
git clone "${REPOS_DIR}"/remote.git "${REPOS_DIR}"/client.git
git clone "${REPOS_DIR}"/remote.git "${REPOS_DIR}"/server.git
mkdir "${REPOS_DIR}"/remote.git/hooks
cat <<-EOF > "${REPOS_DIR}"/remote.git/hooks/pre-receive
#!/bin/sh
export PATH="${TARGET_DIR}"
pkgcruft-git push
EOF
chmod +x "${REPOS_DIR}"/remote.git/hooks/pre-receive
pk repo metadata regen "${REPOS_DIR}"/server.git
fi
cat <<-EOF
Starting a demo pkgcruft-git service:
REMOTE: ${REPOS_DIR}/remote.git
SERVER: ${REPOS_DIR}/server.git
CLIENT: ${REPOS_DIR}/client.git
Make commits in the client repo and push them to the origin remote. The git
pre-receive hook will pass them to the pkgcruft-git service for verification,
merging them on success.
EOF
pkgcruft-gitd "${REPOS_DIR}"/server.git -vv