git-workspace-hook-tmux
v0.2.1
Published
Manage detached tmux sessions with git workspace command hooks
Readme
git-workspace-hook-tmux
Detached tmux sessions for git-workspace-bare. Add a checkout and its session is ready; rename it and keep your running programs; remove it and close its session.
Installation
Requires tmux on PATH, Bare, and a version of git-workspace-bare supporting post hooks.
Install the published package from npm:
npm install -g bare git-workspace-hook-tmuxVerify that git-workspace discovers the installed hooks:
git workspace hook listThe package installs three executables on PATH:
git-workspace-hook-add-post-tmux
git-workspace-hook-remove-post-tmux
git-workspace-hook-rename-post-tmuxgit-workspace discovers them automatically. There is no workspace configuration or
registration step. Uninstall the package to disable the integration, or use
git workspace <command> --no-hooks to skip hooks for one invocation.
Behavior
- Add: create a detached session starting in each existing, registered checkout. An implicit root and its worktree each get a session. Repeating add restores a missing session, even when the checkout itself was unchanged.
- Rename: follow each checkout that actually moved, preserving its session, windows, panes, and running programs. Update the session name and checkout metadata. If its session is missing, create one in the new directory.
- Remove: kill the owned session when its checkout is gone, including when a later step failed. This terminates every pane and program in that session. If removal failed and the checkout remains, keep the session.
Hooks never attach to a session or switch your client. Attach when you want:
tmux attach-session -t '=nicolasfont/git-workspace-hook-tmux'Sessions are named after the checkout, such as owner/repo or owner/repo@topic/one.
Dots, colons, and whitespace become underscores to avoid tmux name restrictions.
If a name is already occupied, append the stable checkout ID (and a counter if
necessary).
On add or rename, an existing session with no workspace metadata is adopted when both its readable name and tmux's session starting directory match the checkout. Rename also checks the previous name and directory, so an older session can be adopted while its checkout moves. Adoption adds the manifest checkout's stable ID and workspace metadata, preserving all windows, panes, and running programs. Later renames and removals manage it just like a session created by the hook.
An existing session already owned by the checkout takes precedence. Sessions with partial metadata or belonging to another workspace are not adopted. If multiple unmarked sessions match the before/after snapshots, none is adopted. A name match alone is insufficient: unrelated sessions keep their collision protection. Remove never adopts an unmarked session just to kill it.
Ownership uses the workspace root and stable checkout ID, rather than the session name. Shared manifest IDs in a second local workspace do not claim the first workspace's sessions. Unregistered worktrees with no stable ID are ignored. Partial command results are handled using the actual before/after snapshots. Repeated delivery, including resumed pull operations, is safe.
The integration uses tmux's normal server selection, including an inherited TMUX
environment variable, and your normal tmux configuration. Keep add/remove/rename on
the same server to manage the same sessions. A missing server starts on add/rename
and is a successful no-op on removal. Other tmux errors make the hook fail;
git-workspace warns and preserves the original command's result.
Session metadata
These are local session options, available as tmux formats:
| Option | Value |
| -------------------------- | ------------------------------ |
| @git-workspace-hook-tmux | 1, the ownership marker |
| @git-workspace-root | Absolute workspace root |
| @git-workspace-id | Stable checkout ID |
| @git-workspace-name | Current checkout name |
| @git-workspace-path | Current absolute checkout path |
Renaming updates the path metadata without sending commands into your running
shells or changing tmux's original session_path. To open new windows in the
checkout's current directory after a rename, you can use this optional tmux binding:
bind-key c new-window -c '#{?@git-workspace-path,#{@git-workspace-path},#{pane_current_path}}'This falls back to the current pane directory in other sessions. The package does not edit your tmux configuration.
As with git-workspace hooks generally, delivery is not transactional with tmux. An interrupted creation can leave an unmarked session; it remains untouched on retry. Re-running add can restore missing sessions. Installing the package does not scan existing checkouts until their commands run.
Development
Install dependencies and link this checkout to try local changes:
npm install
npm linkRun the checks:
npm test
npm run lint
npx prettier . --checkTests require tmux and run against separate temporary sockets with /dev/null as
their configuration. They do not use your live server. Tests cover stdin/argv,
detached creation, root/worktree renames with running panes, removals, name
collisions, ownership, partial failures, and repeated delivery.
License
Apache-2.0
