#!/usr/bin/env bash## Sync the two swisskyrepo mirrors (PayloadsAllTheThings, InternalAllTheThings)# into the site, prove the result builds, and optionally publish it.## .github/workflows/sync-mirrors.yml runs this same script with --commit, so a# sync you watch here is the sync the scheduled run performs. The workflow# pushes from a second job that holds the write token and runs none of this;# --push below is for publishing from your own machine.#set -euo pipefail
cd"$(dirname "${BASH_SOURCE[0]}")/.."PATT_PATHS=(src/content/payloads vendor/PayloadsAllTheThings payloads-manifest.json)IATT_PATHS=(src/content/internal internal-manifest.json)usage(){ cat <<'EOF'Sync the PayloadsAllTheThings and InternalAllTheThings mirrors into the site. scripts/sync-mirrors.sh sync + validate, leave it for you to read scripts/sync-mirrors.sh --commit ... and commit, one commit per upstream scripts/sync-mirrors.sh --push ... and push to main, which deploys scripts/sync-mirrors.sh --no-checks skip the build and tests (a quick look only)Nothing is committed unless you ask for it, so the bare form is safe to runwhenever you want to see what upstream has done since the last sync.EOF}commit=falsepush=falsechecks=truewhile[$# -gt 0]; docase"$1"in --commit)commit=true ;;
--push)commit=true; push=true ;;
--no-checks)checks=false ;;
-h|--help) usage; exit0 ;;
*)echo"unknown option: $1" >&2; usage >&2; exit2 ;;
esacshiftdonefor tool in git python3 node npm; docommand -v "$tool" >/dev/null ||{echo"missing required tool: $tool" >&2; exit1; }donework=$(mktemp -d)trap'rm -rf "$work"' EXIT
say(){printf'\n\033[1m==> %s\033[0m\n'"$1"; }# Report to the workflow when running under Actions, so it knows whether there# is anything to deploy. A no-op outside CI.emit_changed(){if[ -n "${GITHUB_OUTPUT:-}"]; thenecho"changed=$1" >> "$GITHUB_OUTPUT"fi}say 'Cloning upstreams at HEAD'git clone --quiet --depth 1 https://github.com/swisskyrepo/PayloadsAllTheThings "$work/patt"git clone --quiet --depth 1 https://github.com/swisskyrepo/InternalAllTheThings "$work/iatt"# 12-char short shas match the pin convention already in the mirrors; a full# 40-char sha would rewrite sourceUrl in every file.PATT_SHA=$(git -C "$work/patt" rev-parse --short=12 HEAD)IATT_SHA=$(git -C "$work/iatt" rev-parse --short=12 HEAD)echo"PayloadsAllTheThings @ $PATT_SHA"echo"InternalAllTheThings @ $IATT_SHA"say 'Regenerating the mirrors'python3 scripts/sync-payloads.py "$work/patt""$PATT_SHA"python3 scripts/sync-internal.py "$work/iatt""$IATT_SHA"# Count before building. The build writes dist/ and public/pagefind/, both# gitignored, but naming the paths keeps this honest if that ever changes.patt_files=$(git status --porcelain -- "${PATT_PATHS[@]}" | wc -l | tr -d ' ')iatt_files=$(git status --porcelain -- "${IATT_PATHS[@]}" | wc -l | tr -d ' ')say 'What changed'if["$patt_files" -eq 0]&&["$iatt_files" -eq 0]; thenecho'Nothing. Both mirrors are already current.' emit_changed falseexit0fiecho"PayloadsAllTheThings: $patt_files file(s)"echo"InternalAllTheThings: $iatt_files file(s)"printf'\nRead it with: git diff -- %s %s\n'"${PATT_PATHS[*]}""${IATT_PATHS[*]}"if["$checks"=true]; then say 'Validating'# The site has to build and the suite has to pass before any of this is# allowed to land. With no review gate on the scheduled sync, these are the# only thing standing between upstream and the live vault.[ -d node_modules ]|| npm ci
npm run build
node --test
elseprintf'\nSkipping the build and tests. Do not publish a sync you have not validated.\n'fiif["$commit"=false]; then say 'Done'echo'Left in the working tree, uncommitted. Re-run with --commit or --push to publish.' emit_changed trueexit0fisay 'Committing'# Fall back to the Actions identity only when the environment has none of its own.git config user.name >/dev/null 2>&1|| git config user.name 'github-actions[bot]'git config user.email >/dev/null 2>&1|| git config user.email '41898282+github-actions[bot]@users.noreply.github.com'committed=false# Commit one tree, by pathspec, so an index that already had something staged# in it cannot smuggle unrelated changes into a sync commit.commit_tree(){localmsg="$1"; shift git add -A -- "$@"if git diff --cached --quiet -- "$@"; thenecho"nothing to commit for '$msg' (already at this state)"return0fi git commit -q -m "$msg" -- "$@"echo"$msg"committed=true}if["$patt_files" -gt 0]; then commit_tree "sync: PayloadsAllTheThings @ ${PATT_SHA}""${PATT_PATHS[@]}"fiif["$iatt_files" -gt 0]; then commit_tree "sync: InternalAllTheThings @ ${IATT_SHA}""${IATT_PATHS[@]}"fiif["$committed"=false]; then say 'Done'echo'Nothing was committed, so there is nothing to publish.' emit_changed falseexit0fiif["$push"=true]; then say 'Pushing to main'# Retry once through a rebase in case main moved while the build was running. git push origin HEAD:main ||{ git fetch origin main
git rebase origin/main
git push origin HEAD:main
}fiemit_changed truesay 'Done'