dlx-claude/scripts/sync-memory.sh

183 lines
5.6 KiB
Bash
Executable File

#!/bin/bash
# sync-memory.sh - Sync Claude memory files to dlx-claude repository
#
# This script copies Claude's in-memory project knowledge to the version-controlled
# dlx-claude repository for backup and team sharing.
#
# Usage:
# ./scripts/sync-memory.sh # Interactive sync with git commit
# ./scripts/sync-memory.sh --auto # Auto-commit with timestamp
# ./scripts/sync-memory.sh --dry-run # Preview changes only
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(dirname "$SCRIPT_DIR")"
CLAUDE_HOME="$HOME/.claude"
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Parse arguments
DRY_RUN=false
AUTO_COMMIT=false
for arg in "$@"; do
case $arg in
--dry-run)
DRY_RUN=true
shift
;;
--auto)
AUTO_COMMIT=true
shift
;;
*)
echo "Unknown option: $arg"
echo "Usage: $0 [--dry-run] [--auto]"
exit 1
;;
esac
done
echo -e "${BLUE}╔════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ Claude Memory Sync to dlx-claude Repository ║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════════════════════════════╝${NC}"
echo
# Define sync mappings: source -> destination
declare -A SYNC_MAP=(
["$CLAUDE_HOME/projects/-source-dlx-src-dlx-ansible/memory/MEMORY.md"]="$REPO_ROOT/memory/dlx-ansible/MEMORY.md"
["$CLAUDE_HOME/projects/-source-hiveops-src-hiveops-mgmt/memory/MEMORY.md"]="$REPO_ROOT/memory/hiveops-mgmt/MEMORY.md"
["$CLAUDE_HOME/projects/-source-hiveops-src-hiveops-incident/memory/MEMORY.md"]="$REPO_ROOT/memory/hiveops-incident/MEMORY.md"
["$CLAUDE_HOME/projects/-source-hiveops-src-hiveops-browser/memory/MEMORY.md"]="$REPO_ROOT/memory/hiveops-browser/MEMORY.md"
)
# Check if running in dry-run mode
if [ "$DRY_RUN" = true ]; then
echo -e "${YELLOW}[DRY RUN MODE]${NC} No files will be modified."
echo
fi
# Sync files
SYNCED_COUNT=0
SKIPPED_COUNT=0
CHANGED_FILES=()
for source in "${!SYNC_MAP[@]}"; do
dest="${SYNC_MAP[$source]}"
if [ ! -f "$source" ]; then
echo -e "${YELLOW}${NC} Skipped (source not found): $(basename "$source")"
SKIPPED_COUNT=$((SKIPPED_COUNT + 1))
continue
fi
# Create destination directory if needed
dest_dir="$(dirname "$dest")"
if [ "$DRY_RUN" = false ]; then
mkdir -p "$dest_dir"
fi
# Check if files differ
if [ -f "$dest" ] && cmp -s "$source" "$dest"; then
echo -e "${GREEN}${NC} No changes: $(basename "$source")"
else
if [ "$DRY_RUN" = false ]; then
cp "$source" "$dest"
echo -e "${GREEN}${NC} Synced: $(basename "$source")"
CHANGED_FILES+=("$dest")
else
echo -e "${BLUE}~${NC} Would sync: $(basename "$source")"
fi
SYNCED_COUNT=$((SYNCED_COUNT + 1))
fi
done
echo
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "Files synced: $SYNCED_COUNT"
echo -e "Files skipped: $SKIPPED_COUNT"
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo
# Exit if dry-run or no changes
if [ "$DRY_RUN" = true ]; then
echo -e "${YELLOW}Dry run complete. No files were modified.${NC}"
exit 0
fi
if [ $SYNCED_COUNT -eq 0 ]; then
echo -e "${GREEN}All memory files are up to date. Nothing to commit.${NC}"
exit 0
fi
# Git operations
cd "$REPO_ROOT"
# Check if we're in a git repository
if [ ! -d ".git" ]; then
echo -e "${RED}Error: Not a git repository: $REPO_ROOT${NC}"
exit 1
fi
# Stage changed files
echo -e "${BLUE}Staging changed files...${NC}"
for file in "${CHANGED_FILES[@]}"; do
git add "$file"
echo " + $(git diff --cached --stat "$file" | head -1)"
done
# Show git status
echo
echo -e "${BLUE}Git status:${NC}"
git status --short
# Commit changes
if [ "$AUTO_COMMIT" = true ]; then
# Auto-commit with timestamp
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
git commit -m "Auto-sync Claude memory files - $TIMESTAMP
Updated memory files:
$(for file in "${CHANGED_FILES[@]}"; do echo " - $(basename "$file")"; done)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>"
echo
echo -e "${GREEN}✓ Changes committed automatically${NC}"
echo
echo -e "${YELLOW}Push to remote:${NC}"
echo " git push origin main"
else
# Interactive commit
echo
echo -e "${YELLOW}Ready to commit changes.${NC}"
echo
read -p "Commit message (or Enter for default): " COMMIT_MSG
if [ -z "$COMMIT_MSG" ]; then
COMMIT_MSG="Sync Claude memory files
Updated memory files:
$(for file in "${CHANGED_FILES[@]}"; do echo " - $(basename "$file")"; done)"
fi
git commit -m "$COMMIT_MSG
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>"
echo
echo -e "${GREEN}✓ Changes committed${NC}"
echo
echo -e "${YELLOW}Push to remote:${NC}"
echo " git push origin main"
fi
echo
echo -e "${GREEN}Memory sync complete!${NC}"