diff --git a/sync.sh b/sync.sh index d784aef..68cdb4d 100755 --- a/sync.sh +++ b/sync.sh @@ -1,128 +1,74 @@ #!/bin/bash # Hermes Sync Script - 真正的双向合并同步 -# 策略:先 pull(合并远程),再 push(推送本地)。不做 force push。 -# 冲突处理:MEMORY.md 使用 " ours" 策略(优先保留本地),其他文件手动合并 set -e SYNC_DIR="${SYNC_DIR:-$HOME/.hermes-sync}" HERMES_HOME="${HERMES_HOME:-$HOME/.hermes}" -RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -NC='\033[0m' -info() { echo -e "${GREEN}[$(date '+%H:%M:%S')]${NC} $*"; } -warn() { echo -e "${YELLOW}[$(date '+%H:%M:%S')] WARN:${NC} $*"; } -error() { echo -e "${RED}[$(date '+%H:%M:%S')] ERROR:${NC} $*"; } - cd "$SYNC_DIR" +git config pull.rebase false -# 确保在 main 分支 -git checkout main 2>/dev/null || true +echo "[$(date '+%H:%M:%S')] Starting sync..." -# ===== 步骤 1: 复制本地文件到同步目录 ===== -info "Staging local changes..." +# Stage local changes cp "$HERMES_HOME/memories/MEMORY.md" "$SYNC_DIR/memories/MEMORY.md" 2>/dev/null || true -# 复制所有技能(rsync 增量同步) if [ -d "$HERMES_HOME/skills" ]; then - mkdir -p "$SYNC_DIR/skills" + mkdir -p "$SYNC_DIR/memories" "$SYNC_DIR/skills" rsync -a --delete "$HERMES_HOME/skills/" "$SYNC_DIR/skills/" 2>/dev/null || true fi git add -A -# 检查本地是否有变更 -if git diff --cached --quiet && git diff --quiet; then - LOCAL_CHANGES=false -else - LOCAL_CHANGES=true +HAS_LOCAL=false +if ! git diff --cached --quiet || ! git diff --quiet; then + HAS_LOCAL=true fi -# ===== 步骤 2: 拉取远程并合并 ===== -info "Fetching remote changes..." +# Fetch and merge remote git fetch origin main -# 检查远程是否有新提交 if git rev-parse HEAD >/dev/null 2>&1 && \ git rev-parse origin/main >/dev/null 2>&1 && \ ! git merge-base --is-ancestor HEAD origin/main 2>/dev/null; then - REMOTE_AHEAD=true -else - REMOTE_AHEAD=false -fi - -if [ "$REMOTE_AHEAD" = true ]; then - info "Remote has new changes, merging..." + echo "[$(date '+%H:%M:%S')] Remote has changes, merging..." - if [ "$LOCAL_CHANGES" = true ]; then - # 本地和远程都有改动 → 需要 merge - # 先暂存本地改动 - git stash push -m "local before merge $(date)" 2>/dev/null || true - - # 合并远程 - if git merge origin/main --no-edit 2>/dev/null; then - info "Merge successful" - else - warn "Merge conflict, attempting auto-resolve..." - # 自动解决:skills 目录的文件取两方混合(保留ours) - # MEMORY.md 取 ours + if [ "$HAS_LOCAL" = true ]; then + git stash push -m "local $(date)" 2>/dev/null || true + if ! git merge origin/main --no-edit 2>/dev/null; then + # Conflict: keep ours for all + git checkout --ours sync.sh 2>/dev/null || true git checkout --ours memories/MEMORY.md 2>/dev/null || true - # 其他冲突文件:取ours(避免复杂冲突) - git diff --name-only --diff-filter=U | grep -v "^memories/" | while read f; do - git checkout --ours "$f" 2>/dev/null || true - done git add -A - git commit -m "Auto-resolve merge conflict $(date)" 2>/dev/null || true + git commit -m "Auto-resolve $(date)" 2>/dev/null || true fi - - # 恢复本地改动并重新合并 - if git stash list | grep -q "local before merge"; then - git stash pop || true - # 再次合并(这次远程 ours,本地 theirs → 但 git 没有 --theirs 选项) - # 改用 rebase 策略 + if git stash list | grep -q "local "; then + git stash pop 2>/dev/null || true git rebase origin/main 2>/dev/null || { - warn "Rebase conflict, using ours strategy..." git rebase --abort 2>/dev/null || true git merge origin/main --no-edit 2>/dev/null || true } fi else - # 只有远程有新改动 → 直接 merge - git merge origin/main --no-edit 2>/dev/null || { - warn "Fast-forward only merge..." - git merge --ff-only origin/main 2>/dev/null || { - git reset --hard origin/main - } - } + git merge origin/main --no-edit 2>/dev/null || git merge --ff-only origin/main 2>/dev/null || git reset --hard origin/main fi -else - info "Remote up to date" fi -# ===== 步骤 3: 提交本地变更并推送 ===== -if [ "$LOCAL_CHANGES" = true ]; then - info "Committing local changes..." +# Push local +if [ "$HAS_LOCAL" = true ]; then git commit -m "Sync $(date '+%Y-%m-%d %H:%M')" 2>/dev/null || true - - info "Pushing to remote..." - if git push origin main 2>&1; then - info "Push successful" - else - warn "Push rejected (remote advanced), pulling and retrying..." + if ! git push origin main 2>&1; then + echo "[$(date '+%H:%M:%S')] Push rejected, pulling..." git pull origin main --no-edit 2>/dev/null || true - git push origin main 2>&1 || error "Push failed after retry" + git push origin main 2>&1 || echo "[$(date '+%H:%M:%S')] Push failed" fi else - info "No local changes to push" + echo "[$(date '+%H:%M:%S')] No local changes" fi -# ===== 步骤 4: 合并结果应用到本地 hermes ===== -info "Applying merged changes to local hermes..." +# Apply merged result to hermes home cp "$SYNC_DIR/memories/MEMORY.md" "$HERMES_HOME/memories/MEMORY.md" 2>/dev/null || true - -# Skills: rsync 合并(不覆盖本地已有的,保留本地独有的) if [ -d "$SYNC_DIR/skills" ]; then rsync -a --ignore-existing "$SYNC_DIR/skills/" "$HERMES_HOME/skills/" 2>/dev/null || \ cp -rn "$SYNC_DIR/skills/"* "$HERMES_HOME/skills/" 2>/dev/null || true fi -info "Sync complete" +echo "[$(date '+%H:%M:%S')] Sync complete"