|
| 1 | +# opencode-github-sync |
| 2 | + |
| 3 | +[English](./README.md) |
| 4 | + |
| 5 | +通过私有 GitHub 仓库跨设备同步 [OpenCode](https://opencode.ai) 配置和会话数据。 |
| 6 | + |
| 7 | +将四个本地目录合并到一个 git 仓库: |
| 8 | + |
| 9 | +| 本地目录 | 仓库路径 | 内容 | |
| 10 | +|---|---|---| |
| 11 | +| `~/.config/opencode/` | 仓库根目录 | 配置、技能、命令、MCP 设置 | |
| 12 | +| `~/.local/share/opencode/` | `_data/` | 认证、同步状态、SQLite 数据库 | |
| 13 | +| `~/.local/state/opencode/` | `_state/` | 使用频率、KV 存储、模型缓存 | |
| 14 | +| `~/.agents/skills/` | `_agents/` | Skills CLI 全局安装 + lock 文件 | |
| 15 | + |
| 16 | +## 功能特点 |
| 17 | + |
| 18 | +- **配置同步** — 跨设备推送/拉取配置文件、技能和状态 |
| 19 | +- **会话同步** — 推送/拉取 SQLite 数据库、存储和工具输出(在 OpenCode 外运行以避免数据库损坏) |
| 20 | +- **自动暂存** — 拉取时自动暂存本地变更 |
| 21 | +- **强制模式** — `--force` 在冲突时覆盖 |
| 22 | +- **Git LFS** — 大型二进制文件(SQLite 数据库)通过 Git LFS 追踪 |
| 23 | +- **Migration 自动修复** — 拉取较旧数据库时自动恢复迁移记录 |
| 24 | +- **跨平台** — macOS 和 Windows (PowerShell 7) |
| 25 | + |
| 26 | +## 前置条件 |
| 27 | + |
| 28 | +- **Node.js** (v18+) |
| 29 | +- **Git** + [Git LFS](https://git-lfs.github.com/) |
| 30 | +- **一个私有 GitHub 仓库** 用于存储同步数据 |
| 31 | +- **SQLite3 CLI**(会话同步需要 — 数据库备份和完整性检查) |
| 32 | + |
| 33 | +## 安装 |
| 34 | + |
| 35 | +### 1. 克隆此仓库 |
| 36 | + |
| 37 | +```bash |
| 38 | +git clone https://github.com/doomsday616/opencode-github-sync.git |
| 39 | +``` |
| 40 | + |
| 41 | +### 2. 复制脚本到 OpenCode 配置目录 |
| 42 | + |
| 43 | +```bash |
| 44 | +# macOS / Linux |
| 45 | +cp opencode-github-sync/scripts/*.js ~/.config/opencode/scripts/ |
| 46 | + |
| 47 | +# Windows (PowerShell) |
| 48 | +Copy-Item opencode-github-sync\scripts\*.js "$HOME\.config\opencode\scripts\" |
| 49 | +``` |
| 50 | +
|
| 51 | +### 3. 安装 wrapper 命令 |
| 52 | +
|
| 53 | +**macOS / Linux:** |
| 54 | +```bash |
| 55 | +cp opencode-github-sync/wrappers/mac/* ~/.local/bin/ |
| 56 | +chmod +x ~/.local/bin/opencode-{pull,push,pull-force,push-force} |
| 57 | +``` |
| 58 | +
|
| 59 | +**Windows:** |
| 60 | +```powershell |
| 61 | +Copy-Item opencode-github-sync\wrappers\windows\*.cmd "$HOME\.local\bin\" |
| 62 | +# 确保 ~/.local/bin 在 PATH 中 |
| 63 | +``` |
| 64 | + |
| 65 | +### 4. 设置远端 URL |
| 66 | + |
| 67 | +将 `SYNC_REMOTE_URL` 环境变量设为你的私有 GitHub 仓库: |
| 68 | + |
| 69 | +**macOS / Linux**(添加到 `~/.zshrc` 或 `~/.bashrc`): |
| 70 | +```bash |
| 71 | +export SYNC_REMOTE_URL="https://github.com/YOUR_USER/YOUR_REPO.git" |
| 72 | +``` |
| 73 | + |
| 74 | +**Windows**(PowerShell profile 或系统环境变量): |
| 75 | +```powershell |
| 76 | +[System.Environment]::SetEnvironmentVariable("SYNC_REMOTE_URL", "https://github.com/YOUR_USER/YOUR_REPO.git", "User") |
| 77 | +``` |
| 78 | + |
| 79 | +## 使用方法 |
| 80 | + |
| 81 | +### 交互模式 |
| 82 | + |
| 83 | +```bash |
| 84 | +opencode-pull # 拉取 — 选择配置/会话/全部 |
| 85 | +opencode-push # 推送 — 选择配置/会话/全部 |
| 86 | +``` |
| 87 | + |
| 88 | +### 强制模式 |
| 89 | + |
| 90 | +```bash |
| 91 | +opencode-pull-force # 强制拉取(丢弃本地变更) |
| 92 | +opencode-push-force # 强制推送(冲突时覆盖远端) |
| 93 | +``` |
| 94 | + |
| 95 | +### 直接调用脚本 |
| 96 | + |
| 97 | +```bash |
| 98 | +node ~/.config/opencode/scripts/opencode-sync-core.js pull |
| 99 | +node ~/.config/opencode/scripts/opencode-sync-core.js push --force |
| 100 | +node ~/.config/opencode/scripts/opencode-sync-core.js status |
| 101 | +``` |
| 102 | + |
| 103 | +## 注意事项 |
| 104 | + |
| 105 | +- **会话同步必须在 OpenCode 外运行** — 脚本会终止运行中的 OpenCode 进程以安全复制 SQLite 数据库 |
| 106 | +- **配置同步可以在 OpenCode 内运行** — 但之后需要重启会话使变更生效 |
| 107 | +- Git 仓库会在首次 push/pull 时自动初始化 |
| 108 | +- `.gitignore` 和 `.gitattributes` 会在同步仓库中自动生成 |
| 109 | + |
| 110 | +## 环境变量 |
| 111 | + |
| 112 | +| 变量 | 必需 | 说明 | |
| 113 | +|---|---|---| |
| 114 | +| `SYNC_REMOTE_URL` | **是** | GitHub 仓库 URL(例如 `https://github.com/user/repo.git`) | |
| 115 | +| `SYNC_CONFIG_ROOT` | 否 | 覆盖配置目录(默认 `~/.config/opencode`) | |
| 116 | +| `SYNC_DATA_ROOT` | 否 | 覆盖数据目录(默认 `~/.local/share/opencode`) | |
| 117 | +| `SYNC_STATE_ROOT` | 否 | 覆盖状态目录(默认 `~/.local/state/opencode`) | |
| 118 | +| `SYNC_AGENTS_ROOT` | 否 | 覆盖 agents/skills 目录(默认 `~/.agents/skills`) | |
| 119 | +| `SYNC_SKIP_LFS` | 否 | 设为 `1` 跳过 Git LFS 设置 | |
| 120 | + |
| 121 | +## 许可证 |
| 122 | + |
| 123 | +[MIT](./LICENSE) |
0 commit comments