Skip to content

Preserve cwd in native worktree workspaces - #14

Draft
asalisbury wants to merge 1 commit into
devashish2203:mainfrom
asalisbury:fix/preserve-native-workspace-cwd
Draft

Preserve cwd in native worktree workspaces#14
asalisbury wants to merge 1 commit into
devashish2203:mainfrom
asalisbury:fix/preserve-native-workspace-cwd

Conversation

@asalisbury

Copy link
Copy Markdown

Summary

  • preserve the source worktree's relative directory when opening a new native Herdr worktree workspace
  • fall back to the target worktree root when that relative directory does not exist
  • retain the current pane state when the target workspace is already open
  • report failures when Herdr cannot set the new root pane's cwd

Why

Workspace mode calls wt switch --no-cd and then opens the checkout root through herdr 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

  • Bash syntax checks for plugin and test scripts
  • tests/config_test.sh
  • tests/helpers_test.sh, including preserved/fallback paths, already-open workspaces, missing pane IDs, and pane-run failures
  • end-to-end: created a real worktree from a nested directory, confirmed the new Herdr root pane settled in the same relative directory, and confirmed the configured post-switch hook completed successfully

Assisted-By: devx/235f14fc-7887-4bab-adfe-12194c27d705
Comment thread picker.sh
# 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 \

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't changing $PWD to pwd -P cause issues if anyone has symlinks in the path?

Comment thread helpers.sh
# 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() {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify this by using git rev-parse --show-prefix to get the relative path in the source repo?

Comment thread picker.sh
--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' \

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread picker.sh

exec "$herdr" worktree open --workspace "$root_ws" \
--path "$wtpath" --label "$name" --focus --json
target_cwd=$(worktrunk_preserved_cwd "$wtpath" "$source_root" "$source_cwd")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming https://github.com/devashish2203/herdr-worktrunk/pull/14/changes#r3672942202 is used. This function call should not be needed.

@devashish2203

Copy link
Copy Markdown
Owner

Thanks for the PR @asalisbury. The intention looks good, have left a few comments that need addressing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants