Skip to content

Update CodeBuddy commands and rules for better workflow.#3213

Merged
domchen merged 1 commit intomainfrom
feature/domchen_codebuddy_rules
Jan 18, 2026
Merged

Update CodeBuddy commands and rules for better workflow.#3213
domchen merged 1 commit intomainfrom
feature/domchen_codebuddy_rules

Conversation

@domchen
Copy link
Collaborator

@domchen domchen commented Jan 18, 2026

优化 CodeBuddy 的命令和规则配置,包括:

  • 改进 CR 命令:优化本地模式和在线模式的变更获取方式
  • 改进 PR 命令:完善新建模式和追加模式的流程逻辑
  • 新增 worktree 命令:支持创建、切换和清理 git worktree,自动同步测试缓存
  • 移除 switch-main 命令:由 worktree 命令替代
  • 更新编码规范:增加设计文件目录说明
  • 更新 Git 规范:简化操作限制条件
  • 更新 .gitignore:忽略 .codebuddy/designs/ 目录

Copilot AI review requested due to automatic review settings January 18, 2026 05:31
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes CodeBuddy's command and rule configurations to improve workflow automation. It introduces a new worktree management command, refines existing CR and PR commands for better change detection, updates coding standards to centralize design documentation, and simplifies Git operation restrictions.

Changes:

  • Added new worktree command for managing Git worktrees with automatic test cache synchronization
  • Improved CR and PR commands to use consistent diff comparison against origin/main
  • Updated coding rules to centralize design files in .codebuddy/designs/ directory
  • Removed switch-main command (replaced by worktree functionality)

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
.gitignore Added .codebuddy/designs/ to ignore list for design documentation
.codebuddy/rules/Git.md Simplified operation restrictions to allow commands when staging area is empty
.codebuddy/rules/Code.md Centralized design file storage and improved workflow guidance
.codebuddy/commands/worktree.md New command for creating, switching, and managing Git worktrees
.codebuddy/commands/switch-main.md Removed in favor of worktree command
.codebuddy/commands/pr.md Enhanced logic for handling commits, staging area, and PR creation
.codebuddy/commands/cr.md Standardized diff comparison to use origin/main as base

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

## 公共步骤:获取主仓库信息

```bash
MAIN_REPO=$(git worktree list --porcelain | head -1 | sed 's/worktree //')
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sed command on line 14 may not work correctly on all platforms. The pattern assumes 'worktree ' prefix in the output, but this could be fragile. Consider using a more robust approach like: git rev-parse --path-format=absolute --git-common-dir followed by dirname to get the main repository path.

Suggested change
MAIN_REPO=$(git worktree list --porcelain | head -1 | sed 's/worktree //')
MAIN_REPO=$(dirname "$(git rev-parse --path-format=absolute --git-common-dir)")

Copilot uses AI. Check for mistakes.
### 1. 计算路径并检查是否存在

```bash
WT_PATH="$MAIN_REPO/../$REPO_NAME-{name}"
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path construction uses $MAIN_REPO/../$REPO_NAME-{name} which assumes the main repository is not at the root directory. If the main repository is at /project, this would create /../project-{name} which resolves to /project-{name}, but this could be confusing. Consider using $(dirname "$MAIN_REPO")/$REPO_NAME-{name} for clarity.

Suggested change
WT_PATH="$MAIN_REPO/../$REPO_NAME-{name}"
WT_PATH="$(dirname "$MAIN_REPO")/$REPO_NAME-{name}"

Copilot uses AI. Check for mistakes.
Comment on lines +68 to +75
cp -r "$MAIN_REPO/test/baseline/.cache" "$WT_PATH/test/baseline/"
echo "已同步 test/baseline/.cache"
fi

if [ -d "$MAIN_REPO/test/out" ]; then
mkdir -p "$WT_PATH/test"
cp -r "$MAIN_REPO/test/out" "$WT_PATH/test/"
echo "已同步 test/out"
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cp -r command at line 68 may fail if the destination directory structure doesn't exist properly, or if there are permission issues. The command should include error handling or use cp -r with -p flag to preserve attributes. Additionally, consider checking if the copy was successful before reporting it.

Suggested change
cp -r "$MAIN_REPO/test/baseline/.cache" "$WT_PATH/test/baseline/"
echo "已同步 test/baseline/.cache"
fi
if [ -d "$MAIN_REPO/test/out" ]; then
mkdir -p "$WT_PATH/test"
cp -r "$MAIN_REPO/test/out" "$WT_PATH/test/"
echo "已同步 test/out"
if cp -rp "$MAIN_REPO/test/baseline/.cache" "$WT_PATH/test/baseline/"; then
echo "已同步 test/baseline/.cache"
else
echo "同步 test/baseline/.cache 失败" >&2
fi
fi
if [ -d "$MAIN_REPO/test/out" ]; then
mkdir -p "$WT_PATH/test"
if cp -rp "$MAIN_REPO/test/out" "$WT_PATH/test/"; then
echo "已同步 test/out"
else
echo "同步 test/out 失败" >&2
fi

Copilot uses AI. Check for mistakes.
Comment on lines +16 to +17
echo "CURRENT_BRANCH:$CURRENT_BRANCH" && \
gh pr list --head "$CURRENT_BRANCH" --state open --json number,url && \
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command chain using && on lines 14-18 will fail completely if any single command fails. This is problematic because if gh pr list fails (e.g., due to network issues or missing permissions), the GitHub username won't be retrieved. Consider splitting this into separate commands with individual error handling, or at minimum ensure that a failure in gh pr list doesn't prevent getting the username.

Suggested change
echo "CURRENT_BRANCH:$CURRENT_BRANCH" && \
gh pr list --head "$CURRENT_BRANCH" --state open --json number,url && \
echo "CURRENT_BRANCH:$CURRENT_BRANCH"
gh pr list --head "$CURRENT_BRANCH" --state open --json number,url || \
echo "Warning: failed to list open PRs for branch '$CURRENT_BRANCH'; continuing without PR info."

Copilot uses AI. Check for mistakes.
- 否则:全部提交
- 若无输出:记录为**无本地变更**,跳过第二、三步

**判断方法**:第一列非空格为暂存区有内容;第二列非空格或以 `??` 开头为工作区有内容。
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for determining if the staging area or working directory has content relies on parsing git status --porcelain output by checking the first and second columns. However, the description at line 41 doesn't account for all edge cases. For example, a renamed file shows as 'R ' in the first two columns, which would be interpreted as "staging area has content, working directory empty". Consider being more explicit about which status codes map to which scenarios, or using separate git diff commands for clarity.

Suggested change
**判断方法**第一列非空格为暂存区有内容;第二列非空格或以 `??` 开头为工作区有内容
**判断方法**不要仅依赖 `git status --porcelain` 前两列是否为空格来判断(这在重命名 `R`、拷贝 `C`、冲突 `U` 等状态下会出错);实现时应显式解析 Git 状态码(覆盖 `M/A/D/R/C/U` 等所有组合),或分别使用 `git diff --cached --quiet` 判断暂存区是否有内容、使用 `git diff --quiet``git ls-files --others --exclude-standard` 判断工作区及未跟踪文件是否有内容

Copilot uses AI. Check for mistakes.
git diff --cached # 本次暂存区变更(若第三步已获取则复用)
```

若已有 commit 为空且暂存区无内容,提示无变更,终止流程。
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition at line 119 checks if "已有 commit 为空且暂存区无内容" (existing commits are empty and staging area has no content), but this check happens after the code has already handled the case where there's no local changes (line 39). This creates redundant logic. If line 39's "无本地变更" case is triggered, this code path should never be reached. Consider removing this redundant check or clarifying when this situation can occur.

Suggested change
若已有 commit 为空且暂存区无内容,提示无变更,终止流程
若已有 commit 为空且暂存区无内容,则视为“无本地变更”,按前置检查中的“无本地变更”流程处理,不进入新建模式

Copilot uses AI. Check for mistakes.
Comment on lines +105 to +110
# 拉取最新的 main 分支
git fetch origin main

# 当前分支相对 origin/main 的完整变更
git diff origin/main

Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the online mode, the code at line 83 retrieves only headRefName and author, but the original code also retrieved baseRefName. The base branch information is no longer being used, which seems intentional since the code now hardcodes comparison against origin/main (line 109). However, this assumes all PRs target the main branch. If a PR targets a different base branch, this will show incorrect diffs. Consider either retrieving and using the actual base branch, or documenting this limitation.

Suggested change
# 拉取最新的 main 分支
git fetch origin main
# 当前分支相对 origin/main 的完整变更
git diff origin/main
# 获取 PR 的基准分支名称(例如 main、develop 等)
BASE_BRANCH=$(gh pr view {pr_number} --json baseRefName -q '.baseRefName')
# 拉取最新的基准分支
git fetch origin "$BASE_BRANCH"
# 当前分支相对 origin/$BASE_BRANCH 的完整变更
git diff "origin/$BASE_BRANCH"

Copilot uses AI. Check for mistakes.
@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.12%. Comparing base (2f92aef) to head (7c45c8d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3213      +/-   ##
==========================================
+ Coverage   77.01%   77.12%   +0.11%     
==========================================
  Files         413      413              
  Lines       21910    21910              
  Branches     6291     6291              
==========================================
+ Hits        16873    16898      +25     
+ Misses       3817     3809       -8     
+ Partials     1220     1203      -17     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@domchen domchen merged commit 9bee332 into main Jan 18, 2026
15 checks passed
@domchen domchen deleted the feature/domchen_codebuddy_rules branch January 18, 2026 05:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants