# Worktree Support A spaces sidebar that shows every git worktree under its repository, including worktrees that other tools or agents create. Each worktree row shows how far the branch is ahead of the main branch, when it last changed, and its GitHub pull request. A right-click menu opens a worktree as a space, as a tab, or in an editor, and folds a repository's worktrees out of the way. ![Worktree Support](preview.png) ## What you see The mod fills the spaces block of the sidebar, above the agents list. It uses the theme colors, so it follows light and dark themes. From top to bottom: 1. A header row, 31 px high, with the label "Spaces" in `dim` at 11.5 px and a 26 px **+** button that runs `space.new`. 2. One row per space, 30 px high, with rounded corners of 8 px. A row shows a colored 18 px avatar with the first letter of the space name, then the name in `foreground` at 13 px. After the name, a dim tail in 11.5 px shows the repository and branch only when they differ from the name, and hides `main` and `master`. The selected space has the `selected` background. 3. Under each repository space, one row per linked worktree, 28 px high. Each worktree row has a 22 px indent, a branch icon, and a name. The tail on the right side shows these items, in this order: - A yellow dot (`ansi3`) when the worktree has uncommitted changes. - `↑N` in green (`ansi2`) for commits that the main branch does not have, and `↓N` in red (`ansi1`) for commits that the worktree does not have. - The pull request number, for GitHub repositories only. The color shows the state: green for open, `dim` for draft, purple (`ansi5`) for merged, and red for closed. An open pull request also shows its checks: `✓` green, `✗` red, or `◌` yellow while checks run. - The time since the last activity, in `dim`: `now`, `5m`, `3h`, `2d`, or `1w`. 4. A worktree that has a space shows the space name in `foreground`. A worktree with no space shows its branch name in `muted`. 5. A repository row that has worktrees shows a chevron on its right edge. When the worktrees are folded, the chevron points right and shows the count, for example `3 >`. When the person adds a second machine, the rows group under a header per machine, as in the shipped `spaces` mod. When the sidebar is narrower than 160 px, the mod draws a compact rail of numbered spaces, and worktree rows without a space do not show. ## How it behaves The mod reads git and never creates or removes a worktree by itself. A worktree made by any tool, with `git worktree add`, appears under its repository. ### Worktree rows The mod lists the linked worktrees of each repository space with `git worktree list --porcelain`. It skips bare and prunable entries. A worktree that already has a space appears once, as that space, nested under the repository. Worktree rows sort by their last activity, newest first. The activity time starts at the last commit time. It moves to "now" when the status of the worktree or the size of its diff changes between two looks. The mod stores these times in `localStorage`, so they survive a reload. The ahead and behind counts compare the worktree branch with the branch that the main checkout has checked out. The comparison uses `git rev-list --left-right --count BASE...HEAD`. ### When the mod looks again The mod polls nothing on a timer for local repositories. It watches the git folder of each local repository with `watchFolder`. When a path under `.git/worktrees/` changes, the mod lists the worktrees again. This happens when a worktree is added, removed, or receives a commit. For every repository, the mod also looks again at these times: - When an agent turn ends, or when a command finishes, in a space of that repository. - When a remote machine connects again. - Every 60 seconds, but only while the window is in front. The mod asks GitHub for pull requests at most once every 60 seconds per repository. The request runs as a separate task, so the worktree rows never wait for the network. ### Clicks | Action | Result | | --- | --- | | Click a space row | Selects the space. Drag a row to reorder spaces in one machine. | | Click a worktree row with no space | Runs `space.new` with the worktree as its folder. If a space opened there since the last look, selects that space. | | Click a pull request number | Opens the pull request in the browser. | | Click a repository chevron | Folds or unfolds the worktrees of that repository. A folded repository still shows the worktree space that is active. | | Right-click a top-level space | Opens the space menu (below). | | Right-click a worktree row | Opens the worktree menu (below). | ### The space menu The `worktree-actions` popup shows these items for a top-level space. The path is the current folder of the first terminal in the space. - **Open in VS Code** and **Open in Cursor**, for each installed editor. - **Copy path**. - **Reveal in Finder**, for local spaces only. - **New worktree** and **Open worktree**, for git spaces only. These open the shipped `worktree` dialog. - **Remove worktree**, for a space that is a linked worktree. This opens the shipped `worktree` dialog. - **Rename** and **Close space**. ### The worktree menu The popup shows these items for a worktree row: - **Open as space**, or **Go to space** when the worktree has a space. - **Open as tab in REPO**, which starts a tab in the repository space with the worktree as its folder. - **Show changes**, which adds a tab with the shipped `changes` mod in the worktree space. If the worktree has no space, the mod makes one first. - **Open in VS Code** and **Open in Cursor**, for each installed editor. - **Open pull request**, when the branch has one. - **Copy path**, and **Reveal in Finder** for local worktrees. - **Close space**, when the worktree has a space. - **Remove worktree…** when the repository sets a remove command (see below). Otherwise, **Remove worktree** with the shipped dialog, when the worktree has a space. The menu takes the arrow keys, Enter, and Esc. ### The remove command A repository can supply its own remove command: ```sh git config herdr.worktreeRemove 'wt remove {branch}' ``` The mod replaces `{path}` and `{branch}` with the values of the worktree, in single quotes. The menu asks for confirmation with the shipped `confirm` dialog. Then it runs `sh -c COMMAND` in a new tab of the repository space, from the main checkout, so the person sees the output. ### Editors At start, the mod looks for VS Code and Cursor. An editor counts as installed when its command (`code` or `cursor`) is on the PATH. On macOS, the app in `/Applications` or `~/Applications` also counts. A local folder opens with the command, or with `open -a APP` when only the app exists. A folder on another machine opens with `--remote ssh-remote+MACHINE`, which needs the command. ## Built from The build is a clone of the shipped `spaces` mod plus one new popup mod. - `mods/worktree-support/`: made with `herdr-app do mod.clone '{"name":"spaces","as":"worktree-support"}'` from the shipped `spaces` mod, version 0.1.11, then extended. The layout block that showed `spaces` shows `worktree-support` instead. - `mods/worktree-actions/`: a popup mod with `mod.json` set to `{ "place": "none" }`. `worktree-support` opens it with `openOverlay({ mod: "worktree-actions", kind: "popup", width: 230, height: 330 })` for worktrees and a height of 260 for spaces. The props of the popup: | Prop | Meaning | | --- | --- | | `kind` | `"space"` for a top-level space. Absent for a worktree. | | `path` | The worktree top folder, or the current folder of the space. | | `branch` | The worktree branch, or null when detached. | | `machine` | The machine name, or null for this computer. | | `space` | The id of the space at that path, or null. | | `repoSpace`, `repoName` | The repository space id and name, for worktrees. | | `main` | The main checkout folder, where the remove command runs. | | `remove` | The value of `herdr.worktreeRemove`, or null. | | `pr` | The pull request URL, or null. | | `editors` | `[{ label, cli, app }]`, the installed editors. | | `git`, `linked` | For spaces: in a git repository, and a linked worktree. | The `herdr` functions in use are `spaces`, `panes`, `machines`, `exec`, `hc`, `home`, `watchFolder`, `shared`, `setShared`, `openOverlay`, `closeOverlay`, `run`, `theme`, `layout`, `window`, and `props`. The `cx` methods `open_url` and `write_to_clipboard` open pull requests and copy paths. The commands in use are `space.new`, `space.select`, `space.move`, `space.rename`, `space.close`, and `tab.new`, with `folder`, `mod`, or `argv`. The programs in use are `git`, `gh`, `which`, `test`, `uname`, `open`, `code`, `cursor`, `sh`, and `herdr-app`. The shipped mods in use are `worktree`, `changes`, and `confirm`. The icons come from `mods/workspace-chrome/`. ## Watch out - **The mod does not dock by itself.** `mod.json` sets `"place": "none"`, because the mod replaces the spaces list and does not add a panel. After you copy both folders into `mods/`, edit a layout that you own. Change the `content.mod` of its spaces block from `"spaces"` to `"worktree-support"`. A shipped layout is read-only, so clone it first with `herdr-app do layout.clone '{"name":"main","as":"my-layout"}'`. - **Pull requests need GitHub.** The mod asks `gh` only when `git remote get-url origin` contains `github.com`. It uses the `gh` login of the person. For a remote machine, `gh` runs on that machine and must be installed and logged in there. - **Show changes needs a space id.** `run()` returns null, so the popup cannot learn the id of a new space. The popup writes a request to the shared key `worktree-support.request`, and `worktree-support` handles it. `worktree-support` runs `herdr-app do space.new` with `exec`, reads the space id from its output, then runs `tab.new`. A request older than 5 seconds is ignored, because a request left in the persisted `shared.json` must not run again at start. - **The popup closes before it acts.** Tasks of a closed overlay stop. Work that must continue after the click belongs in `worktree-support`. - **The `gh` call must not block the rows.** Run it in its own `cx.spawn`. When it ran inside the worktree listing, new worktrees appeared about 8 seconds late. - **Worktree rows need a fixed tail.** Give the tail `flex_none` and the name `flex_1().min_w(0).truncate()`. When the name and the tail both shrink, a long branch name draws over the counts. - **The watcher needs a filter.** Every commit and index write wakes `watchFolder` on the git folder. List worktrees again only when a changed path starts with `GITDIR/worktrees/`. - **Remote watches are not available.** `watchFolder` works on this computer only. Remote repositories update on agent turns, on reconnect, and every 60 seconds. - **The remove command runs in `sh`.** Shell aliases and zsh functions do not work in it. Scripts and programs do. - **Editor remotes assume ssh hosts.** A remote editor opens with the Herdr machine name as the ssh host. Herdr takes machine names from `~/.ssh/config`, so this is true unless the person renamed the machine. - **Paths must match.** A worktree counts as open when the `git rev-parse --show-toplevel` of a space equals the path from `git worktree list`. A path through a symlink, such as `/tmp` against `/private/tmp`, can show the worktree twice. - **The main checkout must have a space.** Worktrees show only under a repository space that is the main checkout. - **Activity is approximate.** Edits inside one turn of an agent show when the turn ends, or within 60 seconds. ## Done when - `herdr-app check ~/.config/herdr-app/mods/worktree-support` and `herdr-app check ~/.config/herdr-app/mods/worktree-actions` both pass. - The spaces block of the layout shows `worktree-support`, and `herdr-app status` shows no errors. - A space at the root of a repository shows its existing worktrees nested under it. - `git worktree add ../x -b x`, run in a terminal outside Herdr, makes a row `x` appear within 2 seconds. `git worktree remove ../x` removes the row. - A staged or unstaged change in a worktree shows the yellow dot. - Two empty commits in a worktree show `↑2` and the activity `now`. - In a repository whose origin is on GitHub, a worktree on a branch with a merged pull request shows its number in purple. - A click on a worktree row with no space makes a space there. The new space nests under the repository, and the worktree shows once. - The chevron folds the worktrees to a count, and the fold state survives a reload. - A right-click on a worktree row shows the worktree menu. A right-click on a repository row shows the space menu, with New worktree and Open worktree. - When VS Code or Cursor is installed, its "Open in" item opens the folder in that editor. - After `git config herdr.worktreeRemove 'echo {branch}'`, the worktree menu shows "Remove worktree…". The item asks first, then prints the branch name in a new tab.