SuperAPI AI Directory

close-github-worktree-done

General & other

View repository

Updated 10-16 08:35

About this skill

Close Worktree and Complete Requirement (GitHub Version)

Fixed Parameters

  • Repository owner = TencentBlueKing,repo = bk-ci
  • Remote = origin
  • GitHub MCP server = user-github

Hard Constraints

  1. Must be executed on master: before execution git branch --show-current; ifnot master,abortand explain that this skill is only used on the master branch, please switch to the master branch first.

Workflow

Step 0: Determine the target branch

The user may specify the branch name to close in the instruction (e.g. bug-12994)。

  • Branch name specified: directly use that branch name as the target branch and proceed to Step 1.
  • Branch name not specified: execute git worktree list(or --porcelain) to list all worktrees,exclude the main working tree(--porcelain the first entry under worktree path corresponds to the one identical to the main repository root), display the remaining entries'branch names(and paths, for identification) to the user, use AskQuestion to let the user select the branch to close. After the user selects, use the selected branch name as the target branch and proceed to Step 1. If there is no attached worktree, explain the situation and end.

In the following steps, "target branch" refers to the branch name determined in this step.

Step 1: Parse the Issue number

Fromtarget branch nameparse the GitHub Issue number, with the convention {type}-{keywords}-{issue号} or {type}-{issue号}(e.g.:bug-12994、feat-tenant-3-11524、issue-12123). Use - to split,the last segmentis the issue number (pure digits).

  • Last segment is not pure digits / cannot be parsed: ask the user whether toonly remove the worktree and skip the GitHub Issue operation; after the user confirms, continue (skip Step 3).
  • Parsable: record the issue number for use in Step 3.

Step 2: Parse the target worktree path and confirm whether it can be removed

Order explanation: must first confirm that a removable worktree exists locally and that the dirty data policy has been agreed upon, then operate on the GitHub Issue (Step 3), to avoid the Issue already being closed when there is "no worktree / user aborts".

  1. Main repository root MAIN_ROOT:git rev-parse --show-toplevel(currently already on master, should be the main working tree).
  2. Target worktree root TARGET_ROOT: based on git worktree list(or --porcelain) find the line path where the "target branch" is checked out. If not found (the branch has no local worktree), explain the situation and abort.

If TARGET_ROOT and MAIN_ROOT are the same:must notcontinue removal (the main working tree should not be closed by this skill); abort and explain.

Under TARGET_ROOT execute git status --porcelain(can use git -C "<TARGET_ROOT>" status --porcelain)。

  • has uncommitted changes: explain the risk,requires explicit user consentbefore using at removal time git worktree remove --force;otherwise, please commit or stash first.
  • No changes: no need to remove --force(unless Git still requires it, then follow the prompt).

Merge status check: the local master must first be made consistent with the remote, then determine whether the target branch has been merged; all under MAIN_ROOT(currently checked out master) execute:

git fetch origin master
git merge origin/master
  • fetch / merge If any step fails (including local master having uncommitted changes that prevent fast-forward/merge): explain the reason to the user andabortthis process.
  • After successful synchronization, execute:
git branch --merged master

If the outputdoes not containthe target branch name, it means the branch has not yet been merged into master. At this point, you must use AskQuestion to explicitly ask the user:

Branch <目标分支名> has not been merged into master; after deletion, its local commits will no longer be accessible via this branch. Are you sure you want to continue deleting?

  • User choosesCancel: abort the entire process.
  • User choosesConfirm: continue executing Step 3.

This stepdoes not delete the remote branch; does not execute git push --delete and other operations.

Step 3: Close the GitHub Issue (optional and confirm)

(If Step 1 already selectedSkip GitHub, skip this step.)

Note: If bk-ci's PR merge includes fixes #N / closes #Nin the description, the Issue is usuallyautomatically closedat merge time. Therefore, this step does not force closing by default; you must first confirm the status, then decide.

  1. Read the Issue's current status:
CallMcpTool:
  server: user-github
  toolName: issue_read
  arguments:
    method: get
    owner: TencentBlueKing
    repo: bk-ci
    issue_number: <issue号>
  1. If state is already closed: skip closing and proceed directly to Step 4.
  2. If it is still open: use AskQuestion to ask the user whether to close this Issue now.
    • User confirms closing:
CallMcpTool:
  server: user-github
  toolName: issue_write
  arguments:
    method: update
    owner: TencentBlueKing
    repo: bk-ci
    issue_number: <issue号>
    state: closed
    state_reason: completed
  • User chooses not to close: keep the Issue open and continue to Step 4.

If MCP fails, explain to the user and let the user decide whether to still execute Step 4 to remove the worktree.This step does not delete the remote branch.

Step 4: Remove the local worktree

Should be operated in master / MAIN_ROOT to avoid failure caused by executing inside the directory to be deleted.

Method A (recommended, path already specified): Under MAIN_ROOT , execute:

git worktree remove [--force] "<TARGET_ROOT>"

Method B (resolve by branch name, consistent with the path rule at creation time): Under MAIN_ROOT , execute the script:

Windows(PowerShell):

powershell -File .cursor/skills/github-worktree/scripts/git-worktree.ps1 -Action Remove -Branch "<目标分支名>"

macOS / Linux:

bash .cursor/skills/github-worktree/scripts/git-worktree.sh remove -b "<目标分支名>"

If necessary, add -Force / -f(only after confirming with the user).

Step 5: Delete the local branch

After the worktree is successfully removed, delete the corresponding local branch:

git branch -d "<目标分支名>"
  • If Git indicates that the branch is not fully merged (-d refuses),explicit user consent is requiredbefore using -D to force deletion; otherwise skip this step and prompt the user to handle it manually.
  • This stepdoes not delete the remote branch; does not execute git push --delete and other operations.

Notes

  • After removing the worktree, if Cursor still has TARGET_ROOT directory open, that workspace path will become invalid, which is expected.
  • If GitHub and any removal step fail, report truthfully; executed steps are not automatically rolled back.