83 lines
No EOL
4.1 KiB
Bash
Executable file
83 lines
No EOL
4.1 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# sync.sh — Copy telegram-approval-join submodule into internal/, then apply patches.
|
|
# Usage: ./scripts/sync.sh
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
|
|
|
|
SUBMODULE_DIR="$ROOT_DIR/telegram-approval-join"
|
|
PATCH_FILE="$ROOT_DIR/patches/0001-nuzzles.patch"
|
|
|
|
# ── 1. Ensure submodule is initialised ────────────────────────────────────────
|
|
echo "→ Updating submodule..."
|
|
|
|
# Capture the submodule commit before update
|
|
OLD_COMMIT=$(git -C "$SUBMODULE_DIR" rev-parse HEAD 2>/dev/null || echo "")
|
|
|
|
# Update the submodule
|
|
git -C "$ROOT_DIR" submodule update --init --recursive --remote
|
|
|
|
# Capture the submodule commit after update
|
|
NEW_COMMIT=$(git -C "$SUBMODULE_DIR" rev-parse HEAD 2>/dev/null || echo "")
|
|
|
|
# Check if there were any changes
|
|
# if [[ "$OLD_COMMIT" == "$NEW_COMMIT" ]] && [[ -n "$OLD_COMMIT" ]]; then
|
|
# echo " Submodule is already up to date. Nothing to do."
|
|
# exit 0
|
|
# fi
|
|
|
|
# ── 2. Wipe and re-copy the submodule source ──────────────────────────────────
|
|
echo "→ Copying telegram-approval-join source to root..."
|
|
# Clean up directories that will be replaced
|
|
rm -rf "$ROOT_DIR/cmd" "$ROOT_DIR/internal" "$ROOT_DIR/Dockerfile" "$ROOT_DIR/go.mod" "$ROOT_DIR/go.sum" "$ROOT_DIR/config.yaml.example"
|
|
|
|
# Copy source files, excluding .git and keeping patches/ and scripts/
|
|
rsync -a --stats --exclude='.git' --exclude='internal/telegram-approval-join' \
|
|
"$SUBMODULE_DIR/" "$ROOT_DIR/" \
|
|
--exclude='patches' --exclude='scripts' --exclude='README.md'
|
|
|
|
# Remove scripts/ from .gitignore so we can track our sync script
|
|
sed -i '/^scripts\/$/d' "$ROOT_DIR/.gitignore"
|
|
|
|
# ── 3. Rewrite the Go module path inside the copied source ────────────────────
|
|
# Change the module from telegram-approval-join to telegram-join-approval-nuzzles
|
|
echo "→ Rewriting module path in go.mod ..."
|
|
sed -i "s|^module git\.zio\.sh/astra/telegram-approval-join|module git.zio.sh/astra/telegram-join-approval-nuzzles|" "$ROOT_DIR/go.mod"
|
|
|
|
# Fix all import references in the copied source
|
|
echo "→ Rewriting import paths in .go files ..."
|
|
find "$ROOT_DIR" -name '*.go' -not -path "*/telegram-approval-join/*" | xargs sed -i 's|git\.zio\.sh/astra/telegram-approval-join|git.zio.sh/astra/telegram-join-approval-nuzzles|g'
|
|
|
|
# ── 4. Apply string patch ────────────────────────────────────────────
|
|
if [[ -f "$PATCH_FILE" ]]; then
|
|
echo "→ Applying patch ..."
|
|
# Check if patch applies cleanly first (dry run)
|
|
if patch --dry-run -p1 -d "$ROOT_DIR" < "$PATCH_FILE" &>/dev/null; then
|
|
patch -p1 --no-backup-if-mismatch -d "$ROOT_DIR" < "$PATCH_FILE"
|
|
echo " Patch applied successfully."
|
|
else
|
|
echo ""
|
|
echo "⚠️ Patch did not apply cleanly — telegram-approval-join may have changed."
|
|
echo " Run the following to see conflicts:"
|
|
echo " patch --dry-run -p1 -d $ROOT_DIR < $PATCH_FILE"
|
|
echo ""
|
|
echo " Update patches/0001-nuzzles.patch to match the new source, then re-run sync."
|
|
exit 1
|
|
fi
|
|
else
|
|
echo " No patch file found at patches/0001-nuzzles.patch — skipping."
|
|
fi
|
|
|
|
# ── 5. Verify it builds ───────────────────────────────────────────────────────
|
|
echo "→ Verifying build ..."
|
|
go build ./... 2>&1 && echo " Build OK." || { echo "❌ Build failed."; exit 1; }
|
|
|
|
# ── 6. Commit changes ─────────────────────────────────────────────────────────
|
|
echo "→ Committing changes..."
|
|
git -C "$ROOT_DIR" add -A
|
|
git -C "$ROOT_DIR" commit -m "Update telegram-approval-join submodule and apply patches" || true
|
|
|
|
echo ""
|
|
echo "✅ Sync complete. Root directory is up to date with telegram-approval-join (patched)." |