#!/usr/bin/env bash # sync-init-export.sh — Create a full bootstrap bundle so the other machine can # clone the repo from scratch via the shared folder. # # Usage: ./sync-init-export.sh [] # Default share dir: $SYNC_SHARE or ~/fifiletrans # # Places in the shared folder: # -INIT-from--.bundle complete git history, all branches # sync-push.sh (copy of this repo's sync scripts) # sync-pull.sh # sync-init-import.sh # # The receiving side runs sync-init-import.sh from the shared folder. # After that, both sides are identical and ready to use sync-push / sync-pull. set -euo pipefail # ── Config ──────────────────────────────────────────────────────────────────── SHARE_DIR="${1:-${SYNC_SHARE:-$HOME/fifiletrans}}" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # ── Sanity checks ───────────────────────────────────────────────────────────── if ! git rev-parse --git-dir &>/dev/null; then echo "ERROR: Not inside a git repository." >&2 exit 1 fi REPO_ROOT=$(git rev-parse --show-toplevel) REPO_NAME=$(basename "$REPO_ROOT") HOST=$(hostname -s) TIMESTAMP=$(date +%Y%m%d-%H%M%S) BUNDLE_FILE="${REPO_NAME}-INIT-from-${HOST}-${TIMESTAMP}.bundle" BUNDLE_PATH="${SHARE_DIR}/${BUNDLE_FILE}" mkdir -p "$SHARE_DIR" # ── Warn if an INIT bundle already exists for this repo ─────────────────────── EXISTING=$(find "$SHARE_DIR" -maxdepth 1 -name "${REPO_NAME}-INIT-from-*.bundle" 2>/dev/null | sort) if [ -n "$EXISTING" ]; then echo "Warning: existing INIT bundle(s) found in $SHARE_DIR:" echo "$EXISTING" | sed 's/^/ /' echo "" read -rp "Continue and create a new one? [y/N] " ans ans="${ans:-N}" [[ "$ans" =~ ^[Yy] ]] || exit 0 fi # ── Show what will be bundled ────────────────────────────────────────────────── echo "Repository : $REPO_NAME" echo "Host : $HOST" echo "Mode : FULL (all branches, complete history)" echo "" BRANCHES=() while IFS= read -r b; do BRANCHES+=("$b") done < <(git branch --format='%(refname:short)') for branch in "${BRANCHES[@]}"; do count=$(git log --oneline "$branch" | wc -l) tip=$(git log -1 --format="%h %s" "$branch") echo " $branch : $count commit(s) [tip: $tip]" done echo "" # ── Create the full bundle ──────────────────────────────────────────────────── echo "Creating bundle..." git bundle create "$BUNDLE_PATH" --all SIZE=$(du -sh "$BUNDLE_PATH" | cut -f1) echo "Bundle : $BUNDLE_PATH ($SIZE)" # ── Copy sync scripts into the shared folder ────────────────────────────────── echo "" echo "Copying sync scripts to shared folder..." SCRIPTS=("sync-push.sh" "sync-pull.sh" "sync-init-import.sh") for script in "${SCRIPTS[@]}"; do src="${SCRIPT_DIR}/${script}" if [ -f "$src" ]; then cp "$src" "${SHARE_DIR}/${script}" chmod +x "${SHARE_DIR}/${script}" echo " Copied : $script" else echo " WARNING: $script not found at $src — skipping." >&2 fi done # ── Write a HOWTO marker file ───────────────────────────────────────────────── HOWTO="${SHARE_DIR}/INIT-HOWTO-${REPO_NAME}.txt" cat > "$HOWTO" <