Skip to content

Add wt CLI: git worktree management utility#1

Merged
stabai merged 9 commits intomainfrom
claude/implement-wt-cli-k8QCx
Mar 2, 2026
Merged

Add wt CLI: git worktree management utility#1
stabai merged 9 commits intomainfrom
claude/implement-wt-cli-k8QCx

Conversation

@stabai
Copy link
Copy Markdown
Owner

@stabai stabai commented Mar 2, 2026

Summary

This PR introduces wt, a comprehensive CLI utility for managing git worktrees. It provides a user-friendly interface for creating, switching between, and cleaning up git worktrees with intelligent branch management.

Key Changes

  • Core git operations (src/git.ts): Comprehensive git wrapper functions for worktree and branch management, including:

    • Worktree listing, creation, and removal
    • Branch existence checks and metadata queries
    • Dirty state and unpushed commit detection
    • Branch classification and filtering utilities
  • CLI commands:

    • add: Create new worktrees with new branches
    • checkout/switch: Create worktrees for existing branches
    • cd: Navigate to worktree directories with auto-creation
    • ls: List all worktrees with optional status badges
    • rm: Remove worktrees and optionally clean up branches
    • purge: Clean up stale worktree metadata and branches with gone upstreams
    • completion: Generate shell completion scripts for zsh, bash, and fish
  • Configuration system (src/config.ts): JSON-based configuration file (~/.wtrc) supporting:

    • Default branch selection
    • Custom worktree base directory
    • Repository owner for branch classification
    • Agent prefix patterns for filtering
  • Shell integration:

    • Dynamic shell completions for all commands and branches
    • Build script with installation instructions
    • Wrapper function guidance for cd functionality

Notable Implementation Details

  • Intelligent branch classification into groups (yours, your-agents, active, remote) based on ownership and worktree status
  • Safety checks for dirty worktrees and unpushed commits with user prompts
  • Auto-detection of current worktree when removing without explicit branch argument
  • Graceful handling of edge cases (main worktree protection, gone upstreams, detached heads)
  • Structured worktree metadata parsing from git worktree list --porcelain
  • Output to stdout for shell integration (enables cd functionality via wrapper)

https://claude.ai/code/session_017UQ2nQgzW7MiNPmsfoAqPu

claude added 9 commits March 2, 2026 01:34
TypeScript/Bun rewrite with citty for subcommands and @clack/prompts for
interactive UI. Commands: add, checkout/switch, cd, ls, rm, purge, completion.

Uses a shell wrapper protocol where stdout carries cd targets and all user
output goes to stderr, enabling the parent shell to change directories.

Includes shell completions for zsh, bash, and fish with branch grouping
(user branches, agent branches, active worktrees, remote-only).

Configuration via ~/.wtrc (JSON) for default_branch, base_dir, owner,
and agent_prefixes.

https://claude.ai/code/session_017UQ2nQgzW7MiNPmsfoAqPu
Tests cover:
- classifyBranch pure logic (10 cases)
- config loading/parsing with various .wtrc contents (7 cases)
- git helper integration tests against a real temp repo (18 cases)
- CLI entrypoint smoke tests for subcommands and completions (8 cases)

3 CLI --help tests currently fail due to citty writing help output
to a file descriptor that Bun.spawn doesn't capture in child processes.

https://claude.ai/code/session_017UQ2nQgzW7MiNPmsfoAqPu
- docs/: user guide with pages for commands, configuration, shell
  integration, and completions (ready for GitHub Pages)
- README.md: rewritten with install, usage, development, and
  contributing sections linking to docs
- CLAUDE.md: project context for AI-assisted development covering
  architecture, conventions (cd protocol, bun shell escaping), and
  testing notes
- .claude/settings.json: pre-approved permissions for test/build
- tests/cli.test.ts: fix 3 failing --help tests by testing command
  registration via direct import (bun test can't capture stdout from
  child bun processes that call process.exit)

All 44 tests pass.

https://claude.ai/code/session_017UQ2nQgzW7MiNPmsfoAqPu
- Delete build.sh, add scripts/install.ts (same logic, TypeScript)
- Add "install-cli" script to package.json
- Update README, docs, CLAUDE.md to reference bun run install-cli
- Add contributing guideline: no bash scripts, all scripts in
  scripts/ as TypeScript

https://claude.ai/code/session_017UQ2nQgzW7MiNPmsfoAqPu
Replace three large inline shell script strings with a single
SUBCOMMANDS data structure and three generator functions. Adding a
subcommand or flag now means editing one array entry instead of
three shell-specific templates.

https://claude.ai/code/session_017UQ2nQgzW7MiNPmsfoAqPu
- src/command.ts: defineWtCommand() wraps citty's defineCommand,
  requiring completionType on every positional arg. TypeScript enforces
  that new commands conform. getWtArgs() reads the metadata back.
- src/subcommands.ts: central registry of name → command mappings.
  Aliases (switch → checkout) declared here. Both index.ts and
  completion.ts import this.
- src/commands/*.ts: all commands converted from defineCommand to
  defineWtCommand with inline completionType annotations.
- src/commands/completion.ts: generates zsh/bash/fish scripts by
  introspecting the subCommands registry and _wtArgs metadata.
  No more hand-maintained shell string templates.
- src/index.ts: simplified to import subCommands from registry.
- tests/cli.test.ts: added test verifying _wtArgs completion metadata.

45 tests pass.

https://claude.ai/code/session_017UQ2nQgzW7MiNPmsfoAqPu
- .github/workflows/ci.yml: runs tests and build verification on
  push to main/dev and on all PRs
- .github/TODO-release.md: tracks plan for one-line install with
  prebuilt binaries (cross-compiled via bun build --compile)
- README.md: add "keep tests and docs current" contributing guideline,
  note about upcoming prebuilt binaries in install section
- CLAUDE.md: add "tests and docs" convention

https://claude.ai/code/session_017UQ2nQgzW7MiNPmsfoAqPu
- Set up Biome for linting and formatting with auto-fix applied to all
  source files (import sorting, node: protocol, formatting)
- Add lint and check scripts to package.json
- Add lint job to CI workflow
- Configure Dependabot for npm and GitHub Actions dependencies
- Update CLAUDE.md with linting docs, issue quality expectations,
  git-host-agnostic policy, and "question existing patterns" guideline
- Soften GitHub-specific language in docs/commands.md

https://claude.ai/code/session_017UQ2nQgzW7MiNPmsfoAqPu
Define a WtCommand interface that extends citty's CommandDef with our
_wtArgs metadata and non-optional meta. This eliminates all `as any`
casts in command.ts and completion.ts, types WtPositionalArg.default
as string instead of any, and makes getWtArgs accept WtCommand instead
of any.

https://claude.ai/code/session_017UQ2nQgzW7MiNPmsfoAqPu
@stabai stabai merged commit 81b8763 into main Mar 2, 2026
3 checks passed
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.

2 participants