Files
tryton/ci/sync_upstream.sh
T

64 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
required=(GITEA_URL MIRROR_REPOSITORY CUSTOM_REPOSITORY TARGET_BRANCH \
INTEGRATION_BRANCH UPDATE_BRANCH GITEA_TOKEN)
for variable in "${required[@]}"; do
if [[ -z "${!variable:-}" ]]; then
echo "Missing required environment variable: ${variable}" >&2
exit 2
fi
done
auth_header="Authorization: token ${GITEA_TOKEN}"
mirror_url="${GITEA_URL}/${MIRROR_REPOSITORY}.git"
push_url="${GITEA_URL}/${CUSTOM_REPOSITORY}.git"
git remote add upstream "${mirror_url}"
git -c "http.extraHeader=${auth_header}" fetch --no-tags upstream \
"+refs/heads/${TARGET_BRANCH}:refs/remotes/upstream/${TARGET_BRANCH}"
git fetch origin "${INTEGRATION_BRANCH}"
upstream_ref="upstream/${TARGET_BRANCH}"
base_ref="origin/${INTEGRATION_BRANCH}"
if git merge-base --is-ancestor "${upstream_ref}" "${base_ref}"; then
echo "${INTEGRATION_BRANCH} already contains the current upstream commit."
exit 0
fi
if git ls-remote --exit-code --heads origin "${UPDATE_BRANCH}" >/dev/null 2>&1; then
git fetch origin "${UPDATE_BRANCH}"
git switch --create "${UPDATE_BRANCH}" --track "origin/${UPDATE_BRANCH}"
else
git switch --create "${UPDATE_BRANCH}" "${base_ref}"
fi
# Keep a reused automation branch based on the latest integration history.
# This merge is intentionally non-destructive: conflicts create an issue and
# leave every published reference untouched.
if ! git merge-base --is-ancestor "${base_ref}" HEAD; then
if ! git merge --no-edit --no-ff "${base_ref}"; then
conflicts=$(git diff --name-only --diff-filter=U | sed '/^$/d')
git merge --abort
python ci/upstream_pr.py conflict "${conflicts}"
exit 1
fi
fi
if ! git merge --no-edit --no-ff "${upstream_ref}"; then
conflicts=$(git diff --name-only --diff-filter=U | sed '/^$/d')
git merge --abort
python ci/upstream_pr.py conflict "${conflicts}"
exit 1
fi
if git diff --quiet "origin/${UPDATE_BRANCH}" HEAD 2>/dev/null; then
echo 'The existing update branch already contains this upstream revision.'
else
git -c "http.extraHeader=${auth_header}" push "${push_url}" \
"HEAD:refs/heads/${UPDATE_BRANCH}"
fi
python ci/upstream_pr.py pull-request "$(git rev-parse "${upstream_ref}")"