#!/usr/bin/env bash set -euo pipefail upstream_url="${SYNC_UPSTREAM_URL:-https://github.com/syncthing/syncthing.git}" remote="${SYNC_REMOTE:-origin}" upstream_branch="${SYNC_UPSTREAM_BRANCH:-upstream}" main_branch="${SYNC_MAIN_BRANCH:-main}" tmp="" die() { printf 'error: %s\n' "$*" >&2 exit 1 } cleanup() { [[ -z "$tmp" ]] || rm -rf "$tmp" } main() { [[ -z "$(git status --porcelain)" ]] || die "working tree has uncommitted changes" local current_main local current_upstream local new_upstream current_main="$(git rev-parse "refs/heads/$main_branch")" current_upstream="$(git rev-parse "refs/remotes/$remote/$upstream_branch")" git fetch "$upstream_url" "refs/heads/main:refs/remotes/official/main" new_upstream="$(git rev-parse refs/remotes/official/main)" if [[ "$new_upstream" == "$current_upstream" ]]; then printf 'Upstream is already current at %s.\n' "$new_upstream" return fi tmp="$(mktemp -d)" trap cleanup EXIT mkdir -p "$tmp/.gitea/workflows" "$tmp/.github/workflows" "$tmp/patches" "$tmp/scripts/tests" cp .gitea/workflows/custom-release.yml "$tmp/.gitea/workflows/" cp .github/workflows/custom-release.yml "$tmp/.github/workflows/" cp patches/*.patch patches/README.md "$tmp/patches/" cp scripts/update-custom-release.sh scripts/sync-upstream.sh "$tmp/scripts/" cp scripts/tests/test-custom-release-macos-runner.bats "$tmp/scripts/tests/" git branch -f "$upstream_branch" "$new_upstream" git push --force-with-lease="$upstream_branch:$current_upstream" "$remote" "$upstream_branch" git checkout -B "$main_branch" "$new_upstream" cp -R "$tmp/.gitea" "$tmp/.github" "$tmp/patches" "$tmp/scripts" . git apply patches/sync-stignore.patch patches/webui-build-marker.patch git add -A git commit -m "sync upstream, apply stignore patch" git push --force-with-lease="$main_branch:$current_main" "$remote" "$main_branch" } main "$@"