@@ -55,48 +55,42 @@ def _update_worktrees_cache_after_checkout(self, checked_out_branch: LocalBranch
5555 Only the current worktree's entry needs to be updated - linked worktrees
5656 don't change when we checkout in a different worktree.
5757 """
58- current_worktree_path = self ._git .get_root_dir ()
58+ current_worktree_root_dir = self ._git .get_current_worktree_root_dir ()
5959
60- # Remove the old branch entry for this worktree (if any)
61- # We need to find which branch was previously checked out in this worktree
62- for branch , path in self .__worktrees_cache .items ():
63- if path == current_worktree_path : # pragma: no branch
64- del self .__worktrees_cache [branch ]
60+ for branch , path in self .__worktree_root_dir_for_branch .items ():
61+ if path == current_worktree_root_dir : # pragma: no branch
62+ del self .__worktree_root_dir_for_branch [branch ]
6563 break
6664
67- # Add the new branch entry for this worktree
68- self .__worktrees_cache [checked_out_branch ] = current_worktree_path
65+ self .__worktree_root_dir_for_branch [checked_out_branch ] = current_worktree_root_dir
6966
7067 def _switch_to_branch_worktree (
7168 self ,
72- branch : LocalBranchShortName ) -> None :
69+ target_branch : LocalBranchShortName ) -> None :
7370 """
7471 Switch to the worktree where the branch is checked out, or to main worktree if branch is not checked out.
7572 This may involve changing the current working directory.
7673 Updates the worktrees cache after checkout.
7774 """
78- worktree_path = self .__worktrees_cache .get (branch )
79- current_worktree_root = self ._git .get_root_dir ()
75+ target_worktree_root_dir = self .__worktree_root_dir_for_branch .get (target_branch )
76+ current_worktree_root_dir = self ._git .get_current_worktree_root_dir ()
8077
81- if worktree_path is None :
78+ if target_worktree_root_dir is None :
8279 # Branch is not checked out anywhere, need to checkout in main worktree
8380 # Only cd if we're currently in a different worktree (linked worktree)
84- main_worktree_path = self ._git .get_main_worktree_path ()
85- if current_worktree_root != main_worktree_path :
86- print (f"Changing directory to main worktree at { bold (main_worktree_path )} " )
87- os .chdir (main_worktree_path )
88- # checkout() below will flush all caches, including __root_dir
89- self ._git .checkout (branch )
81+ main_worktree_root_dir = self ._git .get_main_worktree_root_dir ()
82+ if current_worktree_root_dir != main_worktree_root_dir :
83+ print (f"Changing directory to main worktree at { bold (main_worktree_root_dir )} " )
84+ self ._git .chdir (main_worktree_root_dir )
85+ self ._git .checkout (target_branch )
9086 # Update cache after checkout
91- self ._update_worktrees_cache_after_checkout (branch )
87+ self ._update_worktrees_cache_after_checkout (target_branch )
9288 else :
9389 # Branch is checked out in a worktree
9490 # Only cd if we're in a different worktree
95- if current_worktree_root != worktree_path :
96- print (f"Changing directory to { bold (worktree_path )} worktree where { bold (branch )} is checked out" )
97- os .chdir (worktree_path )
98- # Flush root dir cache after directory change so get_root_dir() returns the correct path
99- self ._git .flush_root_dir_cache ()
91+ if current_worktree_root_dir != target_worktree_root_dir :
92+ print (f"Changing directory to { bold (target_worktree_root_dir )} worktree where { bold (target_branch )} is checked out" )
93+ self ._git .chdir (target_worktree_root_dir )
10094
10195 def traverse (
10296 self ,
@@ -140,10 +134,10 @@ def traverse(
140134
141135 # Store the initial directory for later restoration
142136 initial_branch = nearest_remaining_branch = self ._git .get_current_branch ()
143- initial_worktree_root = self ._git .get_root_dir ()
137+ initial_worktree_root = self ._git .get_current_worktree_root_dir ()
144138
145139 # Fetch worktrees once at the start to avoid repeated git worktree list calls
146- self .__worktrees_cache : Dict [LocalBranchShortName , str ] = self ._git .get_worktrees ()
140+ self .__worktree_root_dir_for_branch : Dict [LocalBranchShortName , str ] = self ._git .get_worktree_root_dirs_by_branch ()
147141
148142 try :
149143 if opt_start_from == TraverseStartFrom .ROOT :
@@ -502,7 +496,7 @@ def traverse(
502496 finally :
503497 # Warn if the initial directory doesn't correspond to the final checked out branch's worktree
504498 final_branch = self ._git .get_current_branch ()
505- final_worktree_path = self .__worktrees_cache .get (final_branch )
499+ final_worktree_path = self .__worktree_root_dir_for_branch .get (final_branch )
506500 if final_worktree_path and initial_worktree_root != final_worktree_path :
507501 # Final branch is checked out in a worktree different from where we started
508502 warn (
0 commit comments