code-sync

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Code Sync

代码同步

Batch sync all git repos under
~/code
— push (end-of-day) or pull (start-of-day).
批量同步
~/code
目录下的所有Git仓库——下班时推送(push)或上班时拉取(pull)。

Prerequisites

前置条件

ToolTypeRequiredInstall
gitcliYes
brew install git
or git-scm.com
git-workflowskillNoIncluded in
npx skills add niracler/skill
(used for dirty repo commits)
Do NOT proactively verify these tools on skill load. If a command fails due to a missing tool, directly guide the user through installation and configuration step by step.
工具类型是否必需安装方式
git命令行工具
brew install git
git-scm.com
git-workflow技能包含在
npx skills add niracler/skill
中(用于脏仓库提交)
请勿在技能加载时主动验证这些工具。如果因缺少工具导致命令失败,请直接逐步引导用户完成安装和配置。

Mode Selection

模式选择

User saysMode
「下班同步」or "push"Push
「上班更新」or "pull"Pull
「同步代码」「code-sync」Ask user
用户指令模式
「下班同步」或 "push"推送(Push)
「上班更新」或 "pull"拉取(Pull)
「同步代码」「code-sync」询问用户

Workflow (shared by both modes)

工作流程(两种模式通用)

  1. Scan → 2. Categorize → 3. Batch action (auto, no confirmation) → 4. Handle exceptions (interactive) → 5. Summary
If all repos are up-to-date, report that and stop.
  1. 扫描 → 2. 分类 → 3. 批量操作(自动执行,无需确认) → 4. 异常处理(交互式) → 5. 汇总
如果所有仓库均为最新状态,则直接报告并终止流程。

Scan

扫描

bash
bash scripts/scan.sh           # Push: local data only
bash scripts/scan.sh --fetch   # Pull: fetch remote first (10s timeout/repo)
Output: JSON array with fields
path
,
name
,
branch
,
remote
,
remote_url
,
dirty_count
,
has_upstream
,
ahead
,
behind
, and
fetch_error
(only on
--fetch
failure).
bash
bash scripts/scan.sh           # 推送模式:仅扫描本地数据
bash scripts/scan.sh --fetch   # 拉取模式:先拉取远程数据(每个仓库超时10秒)
输出:JSON数组,包含字段
path
name
branch
remote
remote_url
dirty_count
has_upstream
ahead
behind
,以及
fetch_error
(仅在
--fetch
失败时出现)。

Categorize

分类

Push mode:
CategoryConditionAction
up-to-date
dirty_count == 0 && ahead == 0
Report
needs-push
dirty_count == 0 && ahead > 0
Auto
git push
dirty
dirty_count > 0
Interactive
no-upstream
has_upstream == false
Ask user
Pull mode:
CategoryConditionAction
up-to-date
dirty_count == 0 && behind == 0
Report
needs-pull
dirty_count == 0 && behind > 0
Auto
git pull --ff-only
dirty+behind
dirty_count > 0 && behind > 0
Interactive
fetch-error
fetch_error == true
Report, skip
推送模式:
分类条件操作
已同步
dirty_count == 0 && ahead == 0
报告状态
需要推送
dirty_count == 0 && ahead > 0
自动执行
git push
脏仓库
dirty_count > 0
交互式处理
无上游分支
has_upstream == false
询问用户
拉取模式:
分类条件操作
已同步
dirty_count == 0 && behind == 0
报告状态
需要拉取
dirty_count == 0 && behind > 0
自动执行
git pull --ff-only
脏仓库+落后远程
dirty_count > 0 && behind > 0
交互式处理
拉取失败
fetch_error == true
报告并跳过

Exception Handling

异常处理

SituationSteps
Dirty repo (push)
git diff --stat
+
git status
→ describe to user → ask: commit, stash, or skip. If commit, use git-workflow skill to commit and push.
No upstream (push)Report → ask: set upstream and push (
git push -u origin <branch>
), or skip
ff-only fails (pull)
git log --oneline HEAD..@{u}
+
@{u}..HEAD
→ explain divergence → suggest: rebase, merge, or skip
Dirty + behind (pull)Report both issues → ask: stash and pull (stash, pull --ff-only, pop), or skip
场景处理步骤
脏仓库(推送模式)执行
git diff --stat
+
git status
→ 向用户描述状态 → 询问:提交暂存跳过。若选择提交,使用git-workflow技能完成提交和推送。
无上游分支(推送模式)报告状态 → 询问:设置上游并推送
git push -u origin <branch>
),或跳过
ff-only执行失败(拉取模式)执行
git log --oneline HEAD..@{u}
+
@{u}..HEAD
→ 解释分支分歧 → 建议:变基合并跳过
脏仓库+落后远程(拉取模式)报告两个问题 → 询问:暂存并拉取(暂存、拉取--ff-only、恢复暂存),或跳过

Summary

汇总

Group repos by outcome after all operations complete:
text
undefined
所有操作完成后,按结果分组汇总仓库状态:
text
undefined

{Push|Pull} Summary

{推送|拉取}汇总

{Pushed|Updated} (N):
  • repo-name (branch, N commits) Already up-to-date (N):
  • repo-a, repo-b, ... Resolved (N):
  • repo-c: action taken Skipped (N):
  • repo-d: reason
undefined
{已推送|已更新} (N个):
  • 仓库名称 (分支, N次提交) 已同步 (N个):
  • 仓库a, 仓库b, ... 已解决 (N个):
  • 仓库c: 执行的操作 已跳过 (N个):
  • 仓库d: 跳过原因
undefined

Common Issues

常见问题

IssueFix
scan.sh
finds 0 repos
Check
~/code/*/
has git repos
fetch_error
Check network, SSH keys
ff-only failsRebase or merge manually
Push rejectedPull first, then push
问题解决方法
scan.sh
未找到任何仓库
检查
~/code/*/
目录下是否存在Git仓库
fetch_error
出现
检查网络连接、SSH密钥
ff-only执行失败手动执行变基或合并操作
推送被拒绝先拉取远程代码,再重新推送