Skip to content

Commit 8e93257

Browse files
committed
Add tests for worktree cache update and remove unused initial_directory tracking
- Add test_traverse_updates_worktree_cache_on_checkout to verify cache updates when checking out in same worktree - Improve test_traverse_cd_from_linked_to_main_worktree to better cover cache update scenarios - Remove initial_directory tracking and os.chdir() back to initial directory in --return-to=here mode (as cwd changes don't propagate to calling shell anyway) Note: Coverage shows 63->69 branch not taken, which is expected - we always find and delete the old branch entry when updating the cache after checkout
1 parent f166296 commit 8e93257

File tree

2 files changed

+58
-15
lines changed

2 files changed

+58
-15
lines changed

git_machete/client/traverse.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ def traverse(
142142

143143
# Store the main worktree path and initial directory for later restoration
144144
main_worktree_path = self._git.get_main_worktree_path()
145-
initial_directory = os.getcwd()
146145
initial_branch = nearest_remaining_branch = self._git.get_current_branch()
147146

148147
# Fetch worktrees once at the start to avoid repeated git worktree list calls
@@ -470,10 +469,9 @@ def traverse(
470469
break
471470

472471
if opt_return_to == TraverseReturnTo.HERE:
473-
# Return to initial branch and initial directory
472+
# Return to initial branch
473+
# No point switching back to initial directory as cwd won't propagate back to the calling shell anyway
474474
self._switch_to_branch_worktree(initial_branch, main_worktree_path=main_worktree_path)
475-
if os.getcwd() != initial_directory and os.path.exists(initial_directory):
476-
os.chdir(initial_directory)
477475
elif opt_return_to == TraverseReturnTo.NEAREST_REMAINING:
478476
self._switch_to_branch_worktree(nearest_remaining_branch, main_worktree_path=main_worktree_path)
479477
# For NEAREST_REMAINING, we stay in the worktree where the branch is checked out

tests/test_traverse.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)