@@ -69,9 +69,7 @@ def _update_worktrees_cache_after_checkout(self, checked_out_branch: LocalBranch
6969
7070 def _switch_to_branch_worktree (
7171 self ,
72- branch : LocalBranchShortName ,
73- * ,
74- main_worktree_path : str ) -> None :
72+ branch : LocalBranchShortName ) -> None :
7573 """
7674 Switch to the worktree where the branch is checked out, or to main worktree if branch is not checked out.
7775 This may involve changing the current working directory.
@@ -83,6 +81,7 @@ def _switch_to_branch_worktree(
8381 if worktree_path is None :
8482 # Branch is not checked out anywhere, need to checkout in main worktree
8583 # Only cd if we're currently in a different worktree (linked worktree)
84+ main_worktree_path = self ._git .get_main_worktree_path ()
8685 if current_worktree_root != main_worktree_path :
8786 print (f"Changing directory to main worktree at { bold (main_worktree_path )} " )
8887 os .chdir (main_worktree_path )
@@ -139,8 +138,7 @@ def traverse(
139138 self ._init_code_hosting_client ()
140139 current_user = self .code_hosting_client .get_current_user_login ()
141140
142- # Store the main worktree path and initial directory for later restoration
143- main_worktree_path = self ._git .get_main_worktree_path ()
141+ # Store the initial directory for later restoration
144142 initial_branch = nearest_remaining_branch = self ._git .get_current_branch ()
145143 initial_worktree_root = self ._git .get_root_dir ()
146144
@@ -151,14 +149,14 @@ def traverse(
151149 dest = self .root_branch_for (self ._git .get_current_branch (), if_unmanaged = PickRoot .FIRST )
152150 self ._print_new_line (False )
153151 print (f"Checking out the root branch ({ bold (dest )} )" )
154- self ._switch_to_branch_worktree (dest , main_worktree_path = main_worktree_path )
152+ self ._switch_to_branch_worktree (dest )
155153 current_branch = dest
156154 elif opt_start_from == TraverseStartFrom .FIRST_ROOT :
157155 # Note that we already ensured that there is at least one managed branch.
158156 dest = self .managed_branches [0 ]
159157 self ._print_new_line (False )
160158 print (f"Checking out the first root branch ({ bold (dest )} )" )
161- self ._switch_to_branch_worktree (dest , main_worktree_path = main_worktree_path )
159+ self ._switch_to_branch_worktree (dest )
162160 current_branch = dest
163161 elif opt_start_from == TraverseStartFrom .HERE :
164162 current_branch = self ._git .get_current_branch ()
@@ -168,7 +166,7 @@ def traverse(
168166 self .expect_in_managed_branches (dest )
169167 self ._print_new_line (False )
170168 print (f"Checking out branch { bold (dest )} " )
171- self ._switch_to_branch_worktree (dest , main_worktree_path = main_worktree_path )
169+ self ._switch_to_branch_worktree (dest )
172170 current_branch = dest
173171 else :
174172 raise UnexpectedMacheteException (f"Unexpected value for opt_start_from: { opt_start_from } " )
@@ -245,7 +243,7 @@ def traverse(
245243 if branch != current_branch and needs_any_action :
246244 self ._print_new_line (False )
247245 print (f"Checking out { bold (branch )} " )
248- self ._switch_to_branch_worktree (branch , main_worktree_path = main_worktree_path )
246+ self ._switch_to_branch_worktree (branch )
249247 current_branch = branch
250248 self ._print_new_line (False )
251249 self .status (
@@ -471,9 +469,9 @@ def traverse(
471469 if opt_return_to == TraverseReturnTo .HERE :
472470 # Return to initial branch
473471 # No point switching back to initial directory as cwd won't propagate back to the calling shell anyway
474- self ._switch_to_branch_worktree (initial_branch , main_worktree_path = main_worktree_path )
472+ self ._switch_to_branch_worktree (initial_branch )
475473 elif opt_return_to == TraverseReturnTo .NEAREST_REMAINING :
476- self ._switch_to_branch_worktree (nearest_remaining_branch , main_worktree_path = main_worktree_path )
474+ self ._switch_to_branch_worktree (nearest_remaining_branch )
477475 # For NEAREST_REMAINING, we stay in the worktree where the branch is checked out
478476 # otherwise opt_return_to == TraverseReturnTo.STAY, so no action is needed
479477
0 commit comments