|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +TARGET_REPO="${TARGET_REPO:-}" |
| 5 | +TARGET_BRANCH="${TARGET_BRANCH:-main}" |
| 6 | +SOURCE_PREFIX="${SOURCE_PREFIX:-frontend}" |
| 7 | +SYNC_TOKEN="${SYNC_TOKEN:-${GITHUB_TOKEN:-}}" |
| 8 | + |
| 9 | +usage() { |
| 10 | + cat <<'EOF' |
| 11 | +Usage: sync-frontend-mirror.sh [options] |
| 12 | +
|
| 13 | +Options: |
| 14 | + --repo <owner/name> Target GitHub repository (default: $TARGET_REPO) |
| 15 | + --branch <name> Target branch in the mirror repository (default: main) |
| 16 | + --source-prefix <path> Subtree path to mirror (default: frontend) |
| 17 | + --token <token> GitHub token with push access to target repo |
| 18 | + -h, --help Show this help |
| 19 | +EOF |
| 20 | +} |
| 21 | + |
| 22 | +while [[ $# -gt 0 ]]; do |
| 23 | + case "$1" in |
| 24 | + --repo) |
| 25 | + TARGET_REPO="${2:-}" |
| 26 | + shift 2 |
| 27 | + ;; |
| 28 | + --branch) |
| 29 | + TARGET_BRANCH="${2:-}" |
| 30 | + shift 2 |
| 31 | + ;; |
| 32 | + --source-prefix) |
| 33 | + SOURCE_PREFIX="${2:-}" |
| 34 | + shift 2 |
| 35 | + ;; |
| 36 | + --token) |
| 37 | + SYNC_TOKEN="${2:-}" |
| 38 | + shift 2 |
| 39 | + ;; |
| 40 | + -h|--help) |
| 41 | + usage |
| 42 | + exit 0 |
| 43 | + ;; |
| 44 | + *) |
| 45 | + echo "[frontend-mirror-sync] Unknown argument: $1" >&2 |
| 46 | + usage >&2 |
| 47 | + exit 1 |
| 48 | + ;; |
| 49 | + esac |
| 50 | +done |
| 51 | + |
| 52 | +if [[ -z "$TARGET_REPO" ]]; then |
| 53 | + echo "[frontend-mirror-sync] Missing target repository. Set --repo or TARGET_REPO." >&2 |
| 54 | + exit 1 |
| 55 | +fi |
| 56 | + |
| 57 | +if [[ -z "$SYNC_TOKEN" ]]; then |
| 58 | + echo "[frontend-mirror-sync] Missing token. Set --token, SYNC_TOKEN, or GITHUB_TOKEN." >&2 |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | + |
| 62 | +if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then |
| 63 | + echo "[frontend-mirror-sync] Not inside a git repository." >&2 |
| 64 | + exit 1 |
| 65 | +fi |
| 66 | + |
| 67 | +repo_root="$(git rev-parse --show-toplevel)" |
| 68 | +cd "$repo_root" |
| 69 | + |
| 70 | +if [[ ! -d "$SOURCE_PREFIX" ]]; then |
| 71 | + echo "[frontend-mirror-sync] Source prefix not found: ${SOURCE_PREFIX}" >&2 |
| 72 | + exit 1 |
| 73 | +fi |
| 74 | + |
| 75 | +if ! split_sha="$(git subtree split --prefix="$SOURCE_PREFIX" HEAD 2>/dev/null)"; then |
| 76 | + echo "[frontend-mirror-sync] Failed to compute subtree split for '${SOURCE_PREFIX}'." >&2 |
| 77 | + exit 1 |
| 78 | +fi |
| 79 | + |
| 80 | +remote_url="https://x-access-token:${SYNC_TOKEN}@github.com/${TARGET_REPO}.git" |
| 81 | +push_output="" |
| 82 | +if ! push_output="$(git push --force "$remote_url" "${split_sha}:${TARGET_BRANCH}" 2>&1)"; then |
| 83 | + sanitized_output="$push_output" |
| 84 | + if [[ -n "$SYNC_TOKEN" ]]; then |
| 85 | + sanitized_output="${sanitized_output//${SYNC_TOKEN}/***}" |
| 86 | + fi |
| 87 | + echo "[frontend-mirror-sync] Push failed for ${TARGET_REPO}:${TARGET_BRANCH}." >&2 |
| 88 | + echo "$sanitized_output" >&2 |
| 89 | + exit 1 |
| 90 | +fi |
| 91 | + |
| 92 | +echo "[frontend-mirror-sync] Synced ${SOURCE_PREFIX} (commit ${split_sha}) -> ${TARGET_REPO}:${TARGET_BRANCH}" |
0 commit comments