#!/bin/bash # ClaudeTools Bidirectional Sync Script # Ensures proper pull BEFORE push on all machines set -e # Exit on error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Detect machine name if [ -n "$COMPUTERNAME" ]; then MACHINE="$COMPUTERNAME" else MACHINE=$(hostname) fi # Timestamp TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") echo -e "${GREEN}[OK]${NC} Starting ClaudeTools sync from $MACHINE at $TIMESTAMP" # Navigate to ClaudeTools directory if [ -d "$HOME/ClaudeTools" ]; then cd "$HOME/ClaudeTools" elif [ -d "/d/ClaudeTools" ]; then cd "/d/ClaudeTools" elif [ -d "D:/ClaudeTools" ]; then cd "D:/ClaudeTools" else echo -e "${RED}[ERROR]${NC} ClaudeTools directory not found" exit 1 fi echo -e "${GREEN}[OK]${NC} Working directory: $(pwd)" # Phase 1: Check and commit local changes echo "" echo "=== Phase 1: Local Changes ===" if ! git diff-index --quiet HEAD -- 2>/dev/null; then echo -e "${YELLOW}[INFO]${NC} Local changes detected" # Show status git status --short # Stage all changes echo -e "${GREEN}[OK]${NC} Staging all changes..." git add -A # Commit with timestamp COMMIT_MSG="sync: Auto-sync from $MACHINE at $TIMESTAMP Synced files: - Session logs updated - Latest context and credentials - Command/directive updates Machine: $MACHINE Timestamp: $TIMESTAMP Co-Authored-By: Claude Sonnet 4.5 " git commit -m "$COMMIT_MSG" echo -e "${GREEN}[OK]${NC} Changes committed" else echo -e "${GREEN}[OK]${NC} No local changes to commit" fi # Phase 2: Sync with remote (CRITICAL: Pull BEFORE Push) echo "" echo "=== Phase 2: Remote Sync (Pull + Push) ===" # Fetch to see what's available echo -e "${GREEN}[OK]${NC} Fetching from remote..." git fetch origin # Check if remote has updates LOCAL=$(git rev-parse main) REMOTE=$(git rev-parse origin/main) if [ "$LOCAL" != "$REMOTE" ]; then echo -e "${YELLOW}[INFO]${NC} Remote has updates, pulling..." # Pull with rebase if git pull origin main --rebase; then echo -e "${GREEN}[OK]${NC} Successfully pulled remote changes" git log --oneline "$LOCAL..origin/main" else echo -e "${RED}[ERROR]${NC} Pull failed - may have conflicts" echo -e "${YELLOW}[INFO]${NC} Resolve conflicts and run sync again" exit 1 fi else echo -e "${GREEN}[OK]${NC} Already up to date with remote" fi # Push local changes echo "" echo -e "${GREEN}[OK]${NC} Pushing local changes to remote..." if git push origin main; then echo -e "${GREEN}[OK]${NC} Successfully pushed to remote" else echo -e "${RED}[ERROR]${NC} Push failed" exit 1 fi # Phase 3: Report final status echo "" echo "=== Sync Complete ===" echo -e "${GREEN}[OK]${NC} Local branch: $(git rev-parse --abbrev-ref HEAD)" echo -e "${GREEN}[OK]${NC} Current commit: $(git log -1 --oneline)" echo -e "${GREEN}[OK]${NC} Remote status: $(git status -sb | head -1)" echo "" echo -e "${GREEN}[SUCCESS]${NC} All machines in sync. Ready to continue work."