Skip to content

Commit 8fbb0ef

Browse files
authored
docs: add VS Code worktree handoff (#9)
1 parent 210aaf3 commit 8fbb0ef

4 files changed

Lines changed: 90 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Added
66
- `/speckit.worktrees.specify` command for a worktree-first specify workflow: create or reuse the feature worktree before writing spec artifacts, then continue the normal Spec Kit flow from the worktree root
7+
- VS Code handoff guidance for `/speckit.worktrees.specify`: optional `--open-vscode` behavior, configurable `code -n <worktree-path>` new-window default, and explicit reuse/print modes
78

89
### Changed
910
- README now documents the recommended response to "pre-hook for specify": use an explicit worktree-first command unless Spec Kit itself can also switch the active project root for the rest of `/speckit.specify`

‎README.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ layout: "nested" # nested | sibling
113113
auto_create: true # Cursor + /worktree users: set false to avoid in-repo worktrees after every specify
114114
sibling_pattern: "{{repo}}--{{branch}}"
115115
dotworktrees_dir: ".worktrees"
116+
117+
# Optional VS Code handoff for /speckit.worktrees.specify
118+
vscode_open_after_create: false
119+
vscode_open_mode: "new-window" # new-window | reuse-window | print-command
120+
vscode_command: "code"
116121
```
117122
118123
## How worktrees stay isolated
@@ -151,6 +156,30 @@ For VS Code, the most reliable variant remains one window per active worktree:
151156
4. Commit, push, and open the PR from the worktree.
152157
5. Close the worktree window and run `/speckit.worktrees.clean` from the primary checkout when done.
153158

159+
### VS Code handoff
160+
161+
`/speckit.worktrees.specify` can hand the created worktree to VS Code via the `code` CLI:
162+
163+
```text
164+
/speckit.worktrees.specify 005-user-auth Build sign-in --open-vscode
165+
```
166+
167+
By default this uses a new window:
168+
169+
```bash
170+
code -n <worktree-path>
171+
```
172+
173+
You can make that the project default in `.specify/extensions/worktrees/worktree-config.yml`:
174+
175+
```yaml
176+
vscode_open_after_create: true
177+
vscode_open_mode: "new-window"
178+
vscode_command: "code"
179+
```
180+
181+
Use `vscode_open_mode: "reuse-window"` only when you explicitly want VS Code to replace the current window with the worktree. If the `code` CLI is unavailable, or if `vscode_open_mode: "print-command"` is set, the command reports the exact `code -n <worktree-path>` command to run manually.
182+
154183
## Hook
155184

156185
**`after_specify`** — automatically creates a worktree after a new feature is specified. Controlled by the `auto_create` config value.

‎commands/speckit.worktrees.specify.md‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ You **MUST** consider the user input before proceeding. The user input should in
1616
- A feature branch name or slug (for example `005-user-auth`)
1717
- The feature description to specify
1818
- Optional worktree flags accepted by `/speckit.worktrees.create`, such as `--layout sibling`, `--path <dir>`, or `--base-ref HEAD`
19+
- Optional IDE handoff flags: `--open-vscode`, `--no-open-vscode`, `--vscode-new-window`, `--vscode-reuse-window`, or `--vscode-print-command`
1920

2021
If the branch name is missing, ask for it before creating anything. If the feature description is missing, create the worktree only after the user confirms they want to continue specifying in that worktree.
2122

@@ -25,11 +26,32 @@ If the branch name is missing, ask for it before creating anything. If the featu
2526
2. Verify the working tree has the intended base state for the new feature
2627
3. Verify `git worktree` is available (`git worktree list` succeeds)
2728

29+
## Configuration
30+
31+
Read configuration from `.specify/extensions/worktrees/worktree-config.yml` if it exists. Defaults apply when the file is absent.
32+
33+
| Key | Default | Description |
34+
|-----|---------|-------------|
35+
| `vscode_open_after_create` | `false` | Open the returned worktree with the VS Code CLI after creation |
36+
| `vscode_open_mode` | `new-window` | `new-window`, `reuse-window`, or `print-command` |
37+
| `vscode_command` | `code` | VS Code CLI command to run |
38+
39+
User flags override configuration for the current command:
40+
41+
| Flag | Behavior |
42+
|------|----------|
43+
| `--open-vscode` | Enable VS Code handoff |
44+
| `--no-open-vscode` | Disable VS Code handoff |
45+
| `--vscode-new-window` | Run `code -n <worktree-path>` |
46+
| `--vscode-reuse-window` | Run `code -r <worktree-path>` |
47+
| `--vscode-print-command` | Print the command instead of running it |
48+
2849
## Outline
2950

3051
1. **Parse the request**:
3152
- Extract the target branch name
3253
- Extract any worktree flags
54+
- Extract any VS Code handoff flags
3355
- Preserve the remaining text as the feature description
3456

3557
2. **Create the worktree first**:
@@ -51,7 +73,29 @@ If the branch name is missing, ask for it before creating anything. If the featu
5173
- Run all file reads, writes, scripts, git commands, and follow-up Spec Kit steps from that path
5274
- If using an IDE, open the returned path as its own window before continuing
5375

54-
4. **Write the spec inside the worktree**:
76+
4. **Open VS Code when requested**:
77+
If VS Code handoff is enabled, open or print the worktree command after creation:
78+
79+
```bash
80+
if [[ "$VSCODE_OPEN_MODE" == "print-command" ]]; then
81+
echo "$VSCODE_COMMAND -n \"$WORKTREE_PATH\""
82+
elif command -v "$VSCODE_COMMAND" >/dev/null 2>&1; then
83+
if [[ "$VSCODE_OPEN_MODE" == "reuse-window" ]]; then
84+
"$VSCODE_COMMAND" -r "$WORKTREE_PATH"
85+
else
86+
"$VSCODE_COMMAND" -n "$WORKTREE_PATH"
87+
fi
88+
else
89+
echo "VS Code CLI not found. Run: code -n \"$WORKTREE_PATH\""
90+
fi
91+
```
92+
93+
Rules:
94+
- Use `new-window` unless the user or config explicitly chooses `reuse-window`
95+
- Never replace the current VS Code window unless `reuse-window` was explicit
96+
- If the CLI is unavailable or mode is `print-command`, print the exact command to run
97+
98+
5. **Write the spec inside the worktree**:
5599
Continue the normal `/speckit.specify` workflow from the worktree root using the feature description. Spec artifacts should be created under:
56100

57101
```text
@@ -60,7 +104,7 @@ If the branch name is missing, ask for it before creating anything. If the featu
60104

61105
Do not write spec artifacts to the primary checkout.
62106

63-
5. **Report**:
107+
6. **Report**:
64108
Output a concise summary:
65109

66110
```markdown
@@ -73,6 +117,7 @@ If the branch name is missing, ask for it before creating anything. If the featu
73117
| **Spec root** | /Users/me/code/my-project/.worktrees/005-user-auth/specs/005-user-auth |
74118

75119
**Next steps:**
120+
- Open VS Code: code -n /Users/me/code/my-project/.worktrees/005-user-auth
76121
- Continue `/speckit.plan` from the worktree root
77122
- Keep commits and PR work inside that worktree
78123
- Return to the primary checkout only for `/speckit.worktrees.clean`
@@ -83,5 +128,6 @@ If the branch name is missing, ask for it before creating anything. If the featu
83128
- Create or reuse the worktree before writing any spec files
84129
- Never run `git checkout` in the primary checkout as part of this workflow
85130
- Prefer `--base-ref HEAD` when the primary checkout has committed spec seed work that the new worktree must include
131+
- Open VS Code in a new window by default when VS Code handoff is enabled; reuse the current window only with an explicit user/config opt-in
86132
- Keep all generated artifacts, plans, tasks, implementation changes, commits, and PR operations in the worktree
87133
- Use `/speckit.worktrees.clean` from the primary checkout after the PR is merged or the worktree is no longer needed

‎worktree-config.yml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ sibling_pattern: "{{repo}}--{{branch}}"
1616

1717
# Directory name for nested layout (relative to repo root, auto-gitignored).
1818
dotworktrees_dir: ".worktrees"
19+
20+
# VS Code handoff for /speckit.worktrees.specify.
21+
# false — print the worktree path and let the user open it manually
22+
# true — open the worktree after creation when the VS Code CLI is available
23+
vscode_open_after_create: false
24+
25+
# VS Code open mode:
26+
# new-window — code -n <worktree-path> (recommended)
27+
# reuse-window — code -r <worktree-path> (explicit opt-in)
28+
# print-command — only print the command to run
29+
vscode_open_mode: "new-window"
30+
vscode_command: "code"

0 commit comments

Comments
 (0)