diff --git a/.claude/commands/sync.md b/.claude/commands/sync.md index d7752a0..b28b31a 100644 --- a/.claude/commands/sync.md +++ b/.claude/commands/sync.md @@ -4,6 +4,40 @@ Synchronize ClaudeTools configuration, session data, and context bidirectionally --- +## IMPORTANT: Use Automated Sync Script + +**CRITICAL:** When user invokes `/sync`, execute the automated sync script instead of manual steps. + +**Windows:** +```bash +bash .claude/scripts/sync.sh +``` +OR +```cmd +.claude\scripts\sync.bat +``` + +**Mac/Linux:** +```bash +bash .claude/scripts/sync.sh +``` + +**Why use the script:** +- Ensures PULL happens BEFORE PUSH (prevents missing remote changes) +- Consistent behavior across all machines +- Proper error handling and conflict detection +- Automated timestamping and machine identification +- No steps can be accidentally skipped + +**The script automatically:** +1. Checks for local changes +2. Commits local changes (if any) +3. **Fetches and pulls remote changes FIRST** +4. Pushes local changes +5. Reports sync status + +--- + ## What Gets Synced **FROM Local TO Gitea (PUSH):** diff --git a/.claude/scripts/sync.bat b/.claude/scripts/sync.bat new file mode 100644 index 0000000..b0bc886 --- /dev/null +++ b/.claude/scripts/sync.bat @@ -0,0 +1,5 @@ +@echo off +REM ClaudeTools Sync - Windows Wrapper +REM Calls the bash sync script via Git Bash + +bash "%~dp0sync.sh" diff --git a/.claude/scripts/sync.sh b/.claude/scripts/sync.sh new file mode 100644 index 0000000..cca09df --- /dev/null +++ b/.claude/scripts/sync.sh @@ -0,0 +1,118 @@ +#!/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."