@@ -2018,7 +2018,7 @@ def test_traverse_cd_from_linked_to_main_worktree(self) -> None:
20182018 check_out ("root" )
20192019 new_branch ("branch-2" )
20202020 commit ()
2021- push ()
2021+ # Don't push branch-2 so it will be pushed during traverse
20222022
20232023 body = """
20242024 root
@@ -2035,17 +2035,62 @@ def test_traverse_cd_from_linked_to_main_worktree(self) -> None:
20352035 os .chdir (branch_1_worktree )
20362036
20372037 # Now run traverse --start-from=first-root
2038- # This should checkout root, which is NOT in any worktree
2039- # We're currently in branch-1 linked worktree
2040- # So it should cd to main worktree (lines 88-89)
2041- output = launch_command ("traverse" , "-y" , "--return-to=here" , "--start-from=first-root" )
2038+ # This will:
2039+ # 1. Checkout root (not in any worktree) in main worktree - triggers cache update (lines 63-66)
2040+ # 2. Checkout branch-1 (already in linked worktree where we are)
2041+ # 3. Checkout branch-2 (was in main worktree, now root is there) - triggers cache update again
2042+ # The cache updates should cover lines 63-66 where we delete the old branch entry
2043+ output = launch_command ("traverse" , "-y" , "--start-from=first-root" )
20422044
2043- # Verify lines 88-89 were executed
2045+ # Verify lines 88-89 were executed (cd to main worktree for root)
20442046 assert "Changing directory to main worktree at" in output , \
20452047 f"Expected 'Changing directory to main worktree at' in output, but got:\n { output } "
20462048
2047- # Verify we returned to branch-1 worktree (line 475)
2048- current_dir = os .path .realpath (os .getcwd ())
2049- branch_1_worktree_real = os .path .realpath (branch_1_worktree )
2050- assert current_dir == branch_1_worktree_real , \
2051- f"Expected to be in { branch_1_worktree_real } , but in { current_dir } "
2049+ # Verify branch-2 was pushed (confirms traverse completed successfully)
2050+ assert "Pushing untracked branch branch-2" in output
2051+
2052+ def test_traverse_updates_worktree_cache_on_checkout (self ) -> None :
2053+ """Test that the worktree cache is properly updated when checking out in the same worktree."""
2054+ if get_git_version () < (2 , 5 ):
2055+ # git worktree command was introduced in git 2.5
2056+ return
2057+
2058+ (local_path , _ ) = create_repo_with_remote ()
2059+ new_branch ("root" )
2060+ commit ()
2061+ push ()
2062+ new_branch ("branch-1" )
2063+ commit ()
2064+ push ()
2065+ new_branch ("branch-2" )
2066+ commit ()
2067+ # Don't push branch-2 so it will be pushed during traverse
2068+
2069+ body : str = \
2070+ """
2071+ root
2072+ branch-1
2073+ branch-2
2074+ """
2075+ rewrite_branch_layout_file (body )
2076+
2077+ # Setup: Main worktree on root, create a linked worktree for branch-1
2078+ check_out ("root" )
2079+ add_worktree ("branch-1" )
2080+
2081+ # Run traverse from root in main worktree
2082+ # This will:
2083+ # 1. Initial cache: {root: main_worktree_path, branch-1: linked_worktree_path}
2084+ # 2. Visit root (already checked out in main worktree) - no operation
2085+ # 3. Visit branch-1 (in linked worktree) - cd to linked worktree, no checkout
2086+ # 4. Visit branch-2 (not checked out anywhere) - cd to main worktree, checkout branch-2
2087+ # - When checking out branch-2, _update_worktrees_cache_after_checkout is called
2088+ # - The cache has {root: main_worktree_path, branch-1: linked_worktree_path}
2089+ # - current_worktree_path = main_worktree_path
2090+ # - Loop finds root with main_worktree_path, deletes it (covers line 64-65)
2091+ # - Adds branch-2: main_worktree_path
2092+ output = launch_command ("traverse" , "-y" )
2093+
2094+ # Verify branch-2 was checked out and pushed
2095+ assert "Checking out branch-2" in output
2096+ assert "Pushing untracked branch branch-2" in output
0 commit comments