Preserve cwd in native worktree workspaces - #14
Conversation
Assisted-By: devx/235f14fc-7887-4bab-adfe-12194c27d705
| # workspace from any checkout cwd via .result.source.source_workspace_id, so | ||
| # prefer that; fall back to the pane's workspace if it can't be resolved. | ||
| root_ws=$("$herdr" worktree list --cwd "$PWD" --json 2>/dev/null \ | ||
| root_ws=$("$herdr" worktree list --cwd "$source_cwd" --json 2>/dev/null \ |
There was a problem hiding this comment.
Won't changing $PWD to pwd -P cause issues if anyone has symlinks in the path?
| # SOURCE_ROOT. This matches Worktrunk's normal shell-switch behavior: preserve | ||
| # the relative subdirectory when it exists in the target, otherwise use the | ||
| # target worktree root. | ||
| worktrunk_preserved_cwd() { |
There was a problem hiding this comment.
Can we simplify this by using git rev-parse --show-prefix to get the relative path in the source repo?
| --path "$wtpath" --label "$name" --focus --json | ||
| target_cwd=$(worktrunk_preserved_cwd "$wtpath" "$source_root" "$source_cwd") | ||
| if ! worktrunk_open_workspace "$herdr" "$root_ws" "$wtpath" "$name" "$target_cwd"; then | ||
| printf '\n\033[31m%s\033[0m press any key to close' \ |
There was a problem hiding this comment.
This prints the same error irrespective of which return 1 was hit in worktrunk_open_workspace.
So technically herdr worktree open itself can fail and the plugin will log worktree opened
|
|
||
| exec "$herdr" worktree open --workspace "$root_ws" \ | ||
| --path "$wtpath" --label "$name" --focus --json | ||
| target_cwd=$(worktrunk_preserved_cwd "$wtpath" "$source_root" "$source_cwd") |
There was a problem hiding this comment.
Assuming https://github.com/devashish2203/herdr-worktrunk/pull/14/changes#r3672942202 is used. This function call should not be needed.
|
Thanks for the PR @asalisbury. The intention looks good, have left a few comments that need addressing. |
Summary
Why
Workspace mode calls
wt switch --no-cdand then opens the checkout root throughherdr worktree open. That bypasses Worktrunk's normal shell-switch behavior, which preserves the caller's relative directory when possible.This change restores that behavior after Herdr opens a newly created workspace, using the root pane returned by the supported worktree API.
Testing
tests/config_test.shtests/helpers_test.sh, including preserved/fallback paths, already-open workspaces, missing pane IDs, and pane-run failures