Update CodeBuddy commands and rules for better workflow.#3213
Conversation
There was a problem hiding this comment.
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
worktreecommand 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-maincommand (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 //') |
There was a problem hiding this comment.
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.
| MAIN_REPO=$(git worktree list --porcelain | head -1 | sed 's/worktree //') | |
| MAIN_REPO=$(dirname "$(git rev-parse --path-format=absolute --git-common-dir)") |
| ### 1. 计算路径并检查是否存在 | ||
|
|
||
| ```bash | ||
| WT_PATH="$MAIN_REPO/../$REPO_NAME-{name}" |
There was a problem hiding this comment.
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.
| WT_PATH="$MAIN_REPO/../$REPO_NAME-{name}" | |
| WT_PATH="$(dirname "$MAIN_REPO")/$REPO_NAME-{name}" |
| 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" |
There was a problem hiding this comment.
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.
| 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 |
| echo "CURRENT_BRANCH:$CURRENT_BRANCH" && \ | ||
| gh pr list --head "$CURRENT_BRANCH" --state open --json number,url && \ |
There was a problem hiding this comment.
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.
| 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." |
| - 否则:全部提交 | ||
| - 若无输出:记录为**无本地变更**,跳过第二、三步 | ||
|
|
||
| **判断方法**:第一列非空格为暂存区有内容;第二列非空格或以 `??` 开头为工作区有内容。 |
There was a problem hiding this comment.
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.
| **判断方法**:第一列非空格为暂存区有内容;第二列非空格或以 `??` 开头为工作区有内容。 | |
| **判断方法**:不要仅依赖 `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` 判断工作区及未跟踪文件是否有内容。 |
| git diff --cached # 本次暂存区变更(若第三步已获取则复用) | ||
| ``` | ||
|
|
||
| 若已有 commit 为空且暂存区无内容,提示无变更,终止流程。 |
There was a problem hiding this comment.
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.
| 若已有 commit 为空且暂存区无内容,提示无变更,终止流程。 | |
| 若已有 commit 为空且暂存区无内容,则视为“无本地变更”,按前置检查中的“无本地变更”流程处理,不进入新建模式。 |
| # 拉取最新的 main 分支 | ||
| git fetch origin main | ||
|
|
||
| # 当前分支相对 origin/main 的完整变更 | ||
| git diff origin/main | ||
|
|
There was a problem hiding this comment.
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.
| # 拉取最新的 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" |
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
优化 CodeBuddy 的命令和规则配置,包括: