Worktree setup repository
Find a file
David Kong a18e6fe64a Milestone 2.1: Implement git_worktree_create tool (#4) (#17)
## Summary
Implements the `git_worktree_create` tool with full validation and error handling per issue #4 acceptance criteria.

## Changes
- New `pi-worktree-create.ts` module with pure, testable functions (no pi SDK deps)
- `validateCreateWorktreeInput()` — validates path and branch name parameters
- `validatePathDoesNotExist()` — refuses if target path exists (injectable for testing)
- `isCurrentBranch()` — prevents creating worktree on current branch
- `buildWorktreeAddCommand()` — builds correct git command with `-b` flag support
- Integrated all helpers into `createCreateWorktreeTool()` in main extension
- 17 new tests covering all validation paths and edge cases (29 total, all passing)

## Testing
- [x] All 29 tests pass (`bun test .pi/extensions/*.test.ts`)
- [x] Full test suite run with no regressions
- [ ] Manual verification (requires pi runtime)

## Checklist
- [x] Self-reviewed the diff
- [x] Code follows project conventions
- [x] Descriptive naming (no single-character variables)
- [x] Documentation updated (JSDoc on all new functions)

Co-authored-by: David Kong <davkon@gmail.com>
Reviewed-on: #17
2026-07-24 20:50:45 +00:00
.pi/extensions Milestone 2.1: Implement git_worktree_create tool (#4) (#17) 2026-07-24 20:50:45 +00:00
main@cd51baa2c1 Move docs into main/ subdirectory 2026-07-24 12:27:47 +10:00
.gitignore Fix code review issues: structured error returns, remove unused count, add JSDoc (#14) 2026-07-24 08:43:34 +00:00
AGENTS.md Milestone 1.2: Register all four worktree tools (#15) 2026-07-24 12:31:29 +00:00
bunfig.toml Milestone 1.3: Implement git_worktree_list tool (#3) (#16) 2026-07-24 13:11:08 +00:00
DESIGN.md Milestone 1.2: Register all four worktree tools (#15) 2026-07-24 12:31:29 +00:00
DOMAIN.md Milestone 1.2: Register all four worktree tools (#15) 2026-07-24 12:31:29 +00:00
package.json Milestone 1.3: Implement git_worktree_list tool (#3) (#16) 2026-07-24 13:11:08 +00:00
README.md Milestone 1.2: Register all four worktree tools (#15) 2026-07-24 12:31:29 +00:00

pi-worktree

A pi extension for managing git worktrees through intuitive tool commands and an interactive picker.

Tools

git_worktree_create

Create a linked worktree for a branch in the current repo.

{
  path: string,           // Path for the new worktree
  branch?: string,        // Branch name (new or existing). Defaults to 'main'.
  create_branch?: boolean // Create a new branch? Defaults to true if branch doesn't exist.
}

Example: git_worktree_create({ path: "./feature-x", branch: "feature-x" })

git_worktree_list

List all linked worktrees in the current repo.

// No parameters required

Returns structured data with branch, path, and isCurrent for each worktree.

Example output (raw git worktree list format, parsed into structured data):

/home/user/repo          abc1234 [main]
/home/user/repo/feature-x def5678 [feature-x]

git_worktree_switch

Switch pi's working directory to a linked worktree.

{
  target: string   // Path or branch name of the target worktree
}

Example: git_worktree_switch({ target: "./feature-x" })

After switching, you'll be offered the option to create a new session for that worktree context.

git_worktree_remove

Remove a linked worktree.

{
  path: string   // Path of the worktree to remove
}

Example: git_worktree_remove({ path: "./feature-x" })

Command

/worktree

Interactive picker to switch between linked worktrees.

Lists all worktrees with a branch-first display and lets you select one to switch to. Automatically updates pi's branch display after switching.

Behavior

  • Fail fast: Refuses dangerous operations (existing paths, current branch, uncommitted changes) with clear messages and suggested fixes.
  • Stateless tools: Each call works independently based on current git state.
  • Session integration: Branch display updates automatically; you're offered a new session after switching worktrees.
  • Back-switching: When returning to the main repo, pi restores the previous session.

Requirements

  • pi >= 0.5.0 (with ExtensionAPI and registerCommand support)
  • Git 2.x with worktree support (all modern versions)
  • No external npm dependencies — uses only pi's extension API and TypeBox schemas.