118 lines
4.8 KiB
Bash
Executable File
118 lines
4.8 KiB
Bash
Executable File
#!/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 [<share-dir>]
|
|
# Default share dir: $SYNC_SHARE or ~/fifiletrans
|
|
#
|
|
# Places in the shared folder:
|
|
# <repo>-INIT-from-<host>-<timestamp>.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" <<EOF
|
|
Bootstrap instructions for: $REPO_NAME
|
|
Generated : $(date)
|
|
From host : $HOST
|
|
Bundle : $BUNDLE_FILE
|
|
|
|
On the receiving machine:
|
|
1. Make sure this folder ($(basename "$SHARE_DIR")) is accessible at the same path,
|
|
or adjust the path in the command below.
|
|
|
|
2. Run:
|
|
bash ${SHARE_DIR}/sync-init-import.sh ${SHARE_DIR} /path/to/new/repo
|
|
|
|
3. After the import completes, both machines are in sync.
|
|
Use sync-push.sh and sync-pull.sh for ongoing updates.
|
|
EOF
|
|
echo " Written : $(basename "$HOWTO")"
|
|
|
|
# ── Done ───────────────────────────────────────────────────────────────────────
|
|
echo ""
|
|
echo "Shared folder contents:"
|
|
ls -lh "$SHARE_DIR" | grep -E "(${REPO_NAME}|sync-)" | sed 's/^/ /'
|
|
echo ""
|
|
echo "On the other machine, run:"
|
|
echo " bash ${SHARE_DIR}/sync-init-import.sh ${SHARE_DIR} /path/to/new/$(basename "$REPO_ROOT")"
|