Commit 6bf79bdc0cf2
Changed files (3)
dots
config
lazyworktree
dots/config/lazyworktree/config.yaml
@@ -55,9 +55,8 @@ layout_sizes:
custom_commands:
universal:
e:
- command: pi
- description: Pi tab
- new_tab: true
+ command: ~/.config/lazyworktree/herdr-pi
+ description: Open Pi in Herdr
show_help: true
_build:
command: make build
dots/config/lazyworktree/herdr-pi
@@ -0,0 +1,51 @@
+#!/usr/bin/env bash
+# Open a Pi tab in the Herdr workspace belonging to the selected Lazyworktree
+# worktree. Lazyworktree passes WORKTREE_PATH to custom commands.
+set -euo pipefail
+
+: "${WORKTREE_PATH:?WORKTREE_PATH must be set by lazyworktree}"
+
+herdr_bin="${HERDR_BIN:-herdr}"
+worktree_path="$(realpath --no-symlinks --canonicalize-missing -- "$WORKTREE_PATH")"
+worktree_key="v1:$(printf %s "$worktree_path" | sha256sum | cut -c1-32)"
+
+workspaces="$($herdr_bin workspace list)"
+workspace_id="$(jq -er --arg key "$worktree_key" '
+ [.result.workspaces[] | select(.tokens.lazyworktree_id == $key)]
+ | if length == 0 then empty
+ elif length == 1 then .[0].workspace_id
+ else error("multiple Herdr workspaces are tagged for this worktree")
+ end
+' <<<"$workspaces" 2>/dev/null || true)"
+
+if [[ -z "$workspace_id" ]]; then
+ created="$($herdr_bin workspace create \
+ --cwd "$worktree_path" \
+ --label "$(basename -- "$worktree_path")" \
+ --no-focus)"
+ workspace_id="$(jq -er '.result.workspace.workspace_id' <<<"$created")"
+ "$herdr_bin" workspace report-metadata "$workspace_id" \
+ --source lazyworktree \
+ --token "lazyworktree_id=$worktree_key"
+fi
+
+tabs="$($herdr_bin tab list --workspace "$workspace_id")"
+pi_tab_id="$(jq -er '
+ [.result.tabs[] | select(.label == "pi")]
+ | if length == 0 then empty
+ elif length == 1 then .[0].tab_id
+ else error("multiple Pi tabs exist in the Herdr workspace")
+ end
+' <<<"$tabs" 2>/dev/null || true)"
+
+if [[ -n "$pi_tab_id" ]]; then
+ exec "$herdr_bin" tab focus "$pi_tab_id"
+fi
+
+created_tab="$($herdr_bin tab create \
+ --workspace "$workspace_id" \
+ --cwd "$worktree_path" \
+ --label pi \
+ --focus)"
+pane_id="$(jq -er '.result.root_pane.pane_id' <<<"$created_tab")"
+exec "$herdr_bin" pane run "$pane_id" pi
dots/Makefile
@@ -214,7 +214,7 @@ git-template : ~/.config/git/template
copilot-hooks : ~/.config/copilot-hooks
opencode-plugin : ~/.config/opencode/plugin
lazygit : ~/.config/lazygit/config.yml
-lazyworktree : ~/.config/lazyworktree/config.yaml
+lazyworktree : ~/.config/lazyworktree/config.yaml ~/.config/lazyworktree/herdr-pi
lazypr : ~/.config/lazypr/config.toml
raffi : ~/.config/raffi/raffi.yaml ~/.config/raffi/scripts