# pi-herdr-worktree

Pi extension: Git worktrees via [Herdr](https://github.com/ogulcancelik/herdr)'s native `herdr worktree` API, with optional project-level post-create/pre-remove hooks and file linking.

Unlike generic worktree extensions, this one does not reimplement `git worktree` logic. Herdr already manages worktree-backed panes/workspaces natively (`herdr worktree list/create/open/remove`); this extension just wraps that as pi tools and layers project hooks on top.

## Install

```bash
pi install npm:@jalco/pi-herdr-worktree
```

Requires Herdr `worktree` support (check with `herdr worktree --help`) and pi running inside a Herdr pane.

## Tools

| Tool | Wraps |
|---|---|
| `herdr_worktree_list` | `herdr worktree list` |
| `herdr_worktree_create` | `herdr worktree create`, then runs `postCreate` hooks and `linkPaths` symlinks |
| `herdr_worktree_open` | `herdr worktree open` |
| `herdr_worktree_remove` | `herdr worktree remove`, resolving `path` to a workspace ID via `list` if `workspace` isn't given; runs `preRemove` hooks first |

Also registers `/worktree`, which lists worktrees for the current repo.

## Project hooks

Create `.pi/worktree.json` in your repo root:

```json
{
  "postCreate": ["npm install", "npx prisma db push"],
  "preRemove": ["dropdb --if-exists myapp_$(basename $PWD) 2>/dev/null || true"],
  "linkPaths": [".env.local"]
}
```

- `postCreate` runs (via `sh -c`, cwd = new worktree) after Herdr creates the worktree.
- `preRemove` runs (cwd = worktree path) before Herdr removes it. Failures do not block removal.
- `linkPaths` are relative paths symlinked from the source checkout into the new worktree before `postCreate` runs — useful for `.env.local` and similar files that shouldn't live in version control but are needed per-worktree.

## Why not Herdr's CLI directly?

You still can (`herdr worktree create --json`). This extension exists so the *model* can call worktree operations as tools during a session, and so project-level hooks (dependency install, DB provisioning, env-file linking) run automatically without you scripting them by hand each time.

## License

MIT
