Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125#!/bin/bashset +xset +vset -euo pipefail
fail() { printf 'Mirror: %s\n' "$*" >&2; exit 1; }[[ $# == 0 || ( $# == 1 && $1 == --dry-run ) ]] || { printf 'Usage: %s [--dry-run]\n' "$0" >&2; exit 2; }root=$(cd "$(dirname "$0")/.." && pwd)# shellcheck source=Scripts/product.envsource "$root/Scripts/product.env"repo_pattern='^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$'[[ $PRODUCT_GITHUB_REPO =~ $repo_pattern && $PRODUCT_TANGLED_REPO =~ $repo_pattern ]] || fail 'Invalid product repository.'[[ $PRODUCT_KNOT_HOST =~ ^[A-Za-z0-9][A-Za-z0-9.-]*$ ]] || fail 'Invalid Knot host.'[[ $PRODUCT_DEFAULT_BRANCH =~ ^[A-Za-z0-9_.-]+$ && $PRODUCT_RELEASE_WORKFLOW =~ ^[A-Za-z0-9_.-]+\.yml$ ]] || fail 'Invalid default branch or release workflow.'source_url="https://$PRODUCT_KNOT_HOST/$PRODUCT_TANGLED_REPO"destination_url="https://github.com/$PRODUCT_GITHUB_REPO.git"main_ref="refs/heads/$PRODUCT_DEFAULT_BRANCH"if [[ ${1:-} == --dry-run ]]; then printf 'Dry run: pull %s and eligible v* tags from public %s.\n' "$main_ref" "$source_url" printf 'Fast-forward %s; create tags only, never force or delete any ref.\n' "$destination_url" printf 'New tag pushes trigger the trusted default-branch release dispatcher.\n' printf 'No network calls, credential access, or pushes.\n' exit 0fi[[ ${GITHUB_ACTIONS:-} == true && ${GITHUB_REPOSITORY:-} == "$PRODUCT_GITHUB_REPO" && ${GITHUB_REF:-} == "$main_ref" ]] || fail 'Run the mirror from the GitHub default branch.'[[ ${GITHUB_EVENT_NAME:-} == schedule || ${GITHUB_EVENT_NAME:-} == workflow_dispatch ]] || fail 'Use a scheduled or dispatched mirror run.'[[ -n ${MIRROR_TOKEN:-} && $MIRROR_TOKEN != *[$'\r\n']* ]] || fail 'Set a single-line MIRROR_TOKEN in the mirror environment.'mirror_token=$MIRROR_TOKENexport -n mirror_tokenunset MIRROR_TOKEN RELEASE_DISPATCH_TOKEN GH_TOKEN GITHUB_TOKEN GH_HOST GH_ENTERPRISE_TOKEN GH_DEBUG GH_CONFIG_DIR BASH_ENV ENVfor variable in ${!GIT_TRACE@}; do unset "$variable"; doneunset GIT_CURL_VERBOSE GIT_CONFIG GIT_CONFIG_PARAMETERS GIT_CONFIG_COUNT GIT_SSL_NO_VERIFYunset GIT_DIR GIT_WORK_TREE GIT_COMMON_DIR GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIESunset GIT_SSH GIT_SSH_VARIANT GIT_PROXY_COMMAND GIT_EXEC_PATH GIT_ASKPASS SSH_ASKPASSunset HTTPS_PROXY HTTP_PROXY ALL_PROXY NO_PROXY https_proxy http_proxy all_proxy no_proxyunset SSL_CERT_FILE SSL_CERT_DIR CURL_CA_BUNDLEexport GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_SYSTEM=/dev/null GIT_CONFIG_GLOBAL=/dev/null GIT_TERMINAL_PROMPT=0umask 077work=$(mktemp -d "${TMPDIR:-/tmp}/mirror.XXXXXX")trap 'rm -rf "$work"' EXITtrap 'exit 130' INTtrap 'exit 143' TERMmkdir "$work/gh"export GH_CONFIG_DIR="$work/gh" GH_HOST=github.com GH_PROMPT_DISABLED=1
cat > "$work/askpass" <<'HELPER'#!/bin/bashset +xset +vset -euo pipefailcase "${1% }" in "Username for 'https://github.com':") printf 'x-access-token\n' ;; "Password for 'https://x-access-token@github.com':") printf '%s\n' "$MIRROR_TOKEN" ;; *) exit 1 ;;esacHELPERchmod 700 "$work/askpass"safe_git() { git --git-dir="$work/repository" -c core.hooksPath=/dev/null -c http.followRedirects=false \ -c http.extraHeader= -c credential.helper= "$@"}github_git() { MIRROR_TOKEN=$mirror_token GIT_ASKPASS="$work/askpass" safe_git "$@"; }normalize() { local sha ref extra : > "$2" while read -r sha ref extra; do # Ignore unrelated refs before parsing: they have no say in mirroring main. [[ $ref == "$main_ref" || $ref == refs/tags/v* ]] || continue [[ -z $extra && $sha =~ ^[[:xdigit:]]{40}$ ]] || fail 'Invalid remote ref response.' safe_git check-ref-format "$ref" || fail 'Invalid remote ref name.' printf '%s\t%s\n' "$sha" "$ref" >> "$2" done < "$1" LC_ALL=C sort -k2,2 "$2" -o "$2" [[ $(cut -f2 "$2" | uniq -d | wc -l | tr -d ' ') == 0 ]] || fail 'Duplicate remote ref.'}lookup() { local sha ref while read -r sha ref; do if [[ $ref == "$2" ]]; then printf '%s' "$sha"; return; fi done < "$1"}safe_git init --bare --template= "$work/repository" >/dev/null# No source checkout or hooks ever run with the token.safe_git fetch --no-tags "$source_url" "$main_ref:refs/source/main" 'refs/tags/v*:refs/source/tags/v*'source_main=$(safe_git rev-parse 'refs/source/main^{commit}')[[ $source_main =~ ^[[:xdigit:]]{40}$ ]] || fail 'Missing source main.'github_git ls-remote --refs "$destination_url" "$main_ref" 'refs/tags/v*' > "$work/destination.raw"normalize "$work/destination.raw" "$work/destination.refs"destination_main=$(lookup "$work/destination.refs" "$main_ref")if [[ -n $destination_main ]]; then github_git fetch --no-tags "$destination_url" "$main_ref:refs/destination/main" [[ $(safe_git rev-parse 'refs/destination/main^{commit}') == "$destination_main" ]] || fail 'GitHub main changed during the snapshot; retry.' safe_git merge-base --is-ancestor "$destination_main" "$source_main" || fail 'GitHub main has diverged from tangled main. Review it manually; nothing will be forced or resolved automatically.'fisafe_git for-each-ref '--format=%(objectname)%09%(refname)' refs/source/tags/ > "$work/tags"updates=("$source_main:$main_ref")new_tags=()while read -r sha source_ref; do tag=${source_ref#refs/source/tags/} # Trees, blobs, and tags off main are unrelated input, not mirror failures. if ! commit=$(safe_git rev-parse --verify "$source_ref^{commit}" 2>/dev/null); then continue; fi if ! safe_git merge-base --is-ancestor "$commit" "$source_main"; then continue; fi ref="refs/tags/$tag" existing=$(lookup "$work/destination.refs" "$ref") if [[ -n $existing ]]; then [[ $existing == "$sha" ]] || fail "Release tag conflict: $ref differs on GitHub and tangled; never moving it." continue fi # GitHub does not emit push events when more than three tags are pushed at once. # Leave further eligible tags for the next scheduled mirror run. if (( ${#new_tags[@]} >= 3 )); then continue; fi updates+=("$sha:$ref") new_tags+=("$tag")done < "$work/tags"# Ordinary atomic pushes enforce fast-forward main and create-only tags, even in a race.github_git push --atomic "$destination_url" "${updates[@]}" || fail 'GitHub rejected the fast-forward or new tags. Inspect remote refs and retry.'github_git ls-remote --refs "$destination_url" "$main_ref" 'refs/tags/v*' > "$work/after.raw"normalize "$work/after.raw" "$work/after.refs"[[ $(lookup "$work/after.refs" "$main_ref") == "$source_main" ]] || fail 'GitHub main readback differs. Inspect tag-triggered release runs before approving signing.'for tag in "${new_tags[@]}"; do expected=$(safe_git rev-parse "refs/source/tags/$tag") [[ $(lookup "$work/after.refs" "refs/tags/$tag") == "$expected" ]] || fail "Tag readback differs for $tag. Inspect tag-triggered release runs before approving signing."doneprintf 'Mirrored %s and %s new release tags; verified refs. No refs were deleted.\n' "$PRODUCT_DEFAULT_BRANCH" "${#new_tags[@]}"