|
| 1 | +# git-auto-sync |
| 2 | + |
| 3 | +定时巡检多个本地 Git 仓库,对有变动的仓库**智能挑选要提交的文件 → AI 生成提交信息 → 提交并同步到远程**,并把结果推送到你选择的通知通道。 |
| 4 | + |
| 5 | +由参考脚本(单仓库 bash)演进而来,核心差异:多仓库、配置驱动、平台原生定时、可插拔的 AI provider 与通知通道。 |
| 6 | + |
| 7 | +## 工作流程 |
| 8 | + |
| 9 | +每个仓库依次执行: |
| 10 | + |
| 11 | +1. 无变动 → 静默跳过 |
| 12 | +2. **智能暂存**:把变更交给 AI,挑出该提交的文件,排除机密 / 构建产物 / 超大文件;被排除项可自动写入 `.gitignore`(AI 不可用时降级为 `git add -A`) |
| 13 | +3. AI 生成 Conventional Commits 中文提交信息(失败回退规则法) |
| 14 | +4. `git commit` |
| 15 | +5. `git pull --rebase` → `git push`(冲突则 abort 保持干净、标记失败、继续下一个仓库) |
| 16 | +6. 按策略汇总并通知 |
| 17 | + |
| 18 | +## 安装 |
| 19 | + |
| 20 | +### 一行安装(推荐) |
| 21 | + |
| 22 | +macOS / Linux: |
| 23 | + |
| 24 | +```bash |
| 25 | +curl -fsSL https://raw.githubusercontent.com/OctopusGarage/git-auto-sync/main/install.sh | bash |
| 26 | +``` |
| 27 | + |
| 28 | +Windows PowerShell: |
| 29 | + |
| 30 | +```powershell |
| 31 | +powershell -ExecutionPolicy ByPass -c "irm https://raw.githubusercontent.com/OctopusGarage/git-auto-sync/main/install.ps1 | iex" |
| 32 | +``` |
| 33 | + |
| 34 | +会自动:装 [uv](https://docs.astral.sh/uv/)(若缺失)→ 下载**最新 release** 到 `~/.git-auto-sync` → `uv sync` → 在 `~/.local/bin` 放全局 `git-auto-sync` 启动器(macOS/Linux 为 shell 脚本,Windows 为 `.cmd`) → 首次安装运行引导向导 `init`。重复执行即更新已有安装,已有配置时会跳过向导避免覆盖。可用 `GIT_AUTO_SYNC_VERSION=v0.1.0` 固定版本、`GIT_AUTO_SYNC_DIR` 改安装目录。 |
| 35 | + |
| 36 | +> 通过管道安装(无 tty)时会以 `--yes` 跑 `init`:写好配置但**不**安装定时任务——本工具会自动提交并 push,无人值守地启用它需要你显式执行 `git-auto-sync install`。 |
| 37 | +
|
| 38 | +### 手动安装 |
| 39 | + |
| 40 | +```bash |
| 41 | +cd git-auto-sync |
| 42 | +uv sync # 安装依赖 |
| 43 | +uv run git-auto-sync init # 引导向导(见下) |
| 44 | +# 或安装为命令行工具: |
| 45 | +uv tool install . |
| 46 | +``` |
| 47 | + |
| 48 | +### 引导向导(`init`) |
| 49 | + |
| 50 | +```bash |
| 51 | +git-auto-sync init # 交互式:扫描目录挑选仓库、选 AI provider、配置通知、可选安装定时任务 |
| 52 | +git-auto-sync init --yes # 非交互,全用默认值,不安装定时任务 |
| 53 | +``` |
| 54 | + |
| 55 | +向导会扫描你指定的父目录(默认 `~/programming`)下**两层内**的 git 仓库供勾选,也可手动补充路径;写好 `~/.git-auto-sync/config.toml` 后即时校验。 |
| 56 | + |
| 57 | +### 更新 |
| 58 | + |
| 59 | +```bash |
| 60 | +git-auto-sync update # 下载最新 release、uv sync(定时任务无需重装,启动器路径稳定) |
| 61 | +git-auto-sync update --check # 只看有没有新版本 |
| 62 | +``` |
| 63 | + |
| 64 | +## 配置 |
| 65 | + |
| 66 | +默认读取 `~/.git-auto-sync/config.toml`(可用 `--config` 指定)。所有运行时数据(配置、日志、调度产物)都在 `~/.git-auto-sync/` 下。 |
| 67 | + |
| 68 | +```bash |
| 69 | +mkdir -p ~/.git-auto-sync |
| 70 | +cp config.example.toml ~/.git-auto-sync/config.toml |
| 71 | +``` |
| 72 | + |
| 73 | +完整字段见 [`config.example.toml`](config.example.toml)。要点: |
| 74 | + |
| 75 | +- `[defaults]` 提供全局默认,每个 `[[repos]]` 可逐字段覆盖。 |
| 76 | +- 任意字符串值支持 `env:VARNAME`,运行时从环境变量解析,避免把密钥写进文件。 |
| 77 | +- `ai_provider`:`claude-cli`(默认,复用本地 `claude` CLI)/ `anthropic-api`(读 `ANTHROPIC_API_KEY`)/ `rules`(纯规则)。任何 provider 调用失败都会自动回退到规则法,工具不会因为 AI 不可用而中断。 |
| 78 | +- `notify_on`:`change_or_fail`(默认,有变动或失败才通知)/ `fail_only` / `always`。 |
| 79 | + |
| 80 | +## 命令 |
| 81 | + |
| 82 | +```bash |
| 83 | +git-auto-sync init # 引导向导,生成配置 |
| 84 | +git-auto-sync sync # 同步全部仓库 |
| 85 | +git-auto-sync sync --repo foo # 只同步路径匹配 foo 的仓库 |
| 86 | +git-auto-sync sync --dry-run # 预演:打印将要提交的内容,不落地 |
| 87 | +git-auto-sync status # 列出各仓库的分支与 clean/dirty |
| 88 | +git-auto-sync config check # 校验配置文件 |
| 89 | +git-auto-sync install --interval 30m # 安装平台原生定时任务 |
| 90 | +git-auto-sync uninstall # 卸载定时任务 |
| 91 | +git-auto-sync update # 自更新到最新 release |
| 92 | +``` |
| 93 | + |
| 94 | +退出码:`0` 成功,`1` 有仓库同步失败,`2` `--repo` 未匹配到任何仓库。 |
| 95 | + |
| 96 | +## 定时运行 |
| 97 | + |
| 98 | +`install` 按当前平台生成并挂载原生调度,不自带常驻进程: |
| 99 | + |
| 100 | +- **macOS** → launchd(`~/Library/LaunchAgents/com.octopusgarage.git-auto-sync.plist`) |
| 101 | +- **Linux** → systemd user timer(`~/.config/systemd/user/git-auto-sync.timer`) |
| 102 | +- **Windows** → Task Scheduler(`OctopusGarage.git-auto-sync`) |
| 103 | + |
| 104 | +间隔形如 `30m` / `1h` / `90s`。卸载用 `git-auto-sync uninstall`。 |
| 105 | + |
| 106 | +## 通知通道 |
| 107 | + |
| 108 | +- `log`:追加时间戳块到日志文件,并打印到 stdout(审计用) |
| 109 | +- `telegram`:bot token + chat_id |
| 110 | +- `lark`:飞书自定义机器人 webhook |
| 111 | + |
| 112 | +一次同步结束后,按 `notify_on` 策略向所有启用的通道推送一条汇总;单个通道发送失败不影响其他通道与主流程。 |
| 113 | + |
| 114 | +## 安全边界 |
| 115 | + |
| 116 | +无人值守自动提交的最大风险是误提交机密或垃圾。本工具的防线: |
| 117 | + |
| 118 | +- AI 暂存分析排除 `.env`、私钥、证书、`node_modules`/`dist`/`build`/`__pycache__`、超大二进制、日志等; |
| 119 | +- 被排除的文件**不提交**,并(在 `ai_gitignore_autowrite=true` 时)自动写入 `.gitignore`; |
| 120 | +- 拿不准时可先 `--dry-run` 预演。 |
| 121 | + |
| 122 | +## 开发 |
| 123 | + |
| 124 | +```bash |
| 125 | +uv run pytest # 运行测试 |
| 126 | +uv run ruff check . # 代码检查 |
| 127 | +``` |
| 128 | + |
| 129 | +## License |
| 130 | + |
| 131 | +MIT |
0 commit comments