#!/bin/bash
# Apply the s3-gateway patch series for the target versitygw tag onto
# the checked-out source tree (run ci/clone-source.sh first).
#
# Patch sets live in patches/
/. The directory for a given tag
# comes from ci/support-matrix.yaml (the `patches:` field, defaulting
# to the tag name). Patches are git-format-patch files applied with
# `git am --3way` in filename order — same model as scoutfs-notify.
#
# Environment:
# VGW_SRC_DIR source tree (default: src; set by clone-source.sh)
# VGW_TAG upstream tag being built (required)
# PATCH_DIR override the patch directory (optional)
set -euo pipefail
VGW_SRC_DIR="${VGW_SRC_DIR:-src}"
: "${VGW_TAG:?VGW_TAG must be set}"
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SRC="$(cd "${VGW_SRC_DIR}" && pwd)"
# Resolve the patch directory: explicit override, else matrix lookup,
# else the tag name.
if [[ -z "${PATCH_DIR:-}" ]]; then
PATCH_NAME=""
if command -v yq >/dev/null 2>&1; then
PATCH_NAME="$(yq -r \
".versitygw[] | select(.tag == \"${VGW_TAG}\") | .patches // \"\"" \
"${REPO_ROOT}/ci/support-matrix.yaml" 2>/dev/null || true)"
fi
[[ -n "${PATCH_NAME}" && "${PATCH_NAME}" != "null" ]] || PATCH_NAME="${VGW_TAG}"
PATCH_DIR="${REPO_ROOT}/patches/${PATCH_NAME}"
fi
echo ">>> target versitygw tag: ${VGW_TAG}"
echo ">>> patch directory: ${PATCH_DIR}"
shopt -s nullglob
patches=( "${PATCH_DIR}"/*.patch )
shopt -u nullglob
if (( ${#patches[@]} == 0 )); then
echo ">>> WARNING: no patches in ${PATCH_DIR} — building vanilla upstream"
exit 0
fi
echo ">>> applying ${#patches[@]} patch(es) with git am --3way:"
printf ' %s\n' "${patches[@]##*/}"
cd "${SRC}"
git am --whitespace=nowarn --3way "${patches[@]}"
echo ">>> applied commits:"
git log --oneline "${VGW_TAG}..HEAD"
if [[ -n "${GITHUB_ENV:-}" ]]; then
{
echo "S3GW_PATCH_DIR=${PATCH_DIR}"
echo "S3GW_PATCH_COUNT=${#patches[@]}"
} >> "${GITHUB_ENV}"
fi
echo ">>> patches applied"