Skip to content

Commit f166296

Browse files
committed
Optimize cache invalidation after directory changes
- Add flush_root_dir_cache() method to selectively clear only __root_dir cache - Remove redundant flush_caches() before checkout() since checkout flushes anyway - Use flush_root_dir_cache() instead of flush_caches() when switching to linked worktrees (no checkout follows)
1 parent fb6237a commit f166296

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

git_machete/client/traverse.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ def _switch_to_branch_worktree(
8686
if current_worktree_root != main_worktree_path:
8787
print(f"Changing directory to main worktree at {bold(main_worktree_path)}")
8888
os.chdir(main_worktree_path)
89-
# Flush cache after directory change so get_root_dir() returns the correct path
90-
self._git.flush_caches()
89+
# checkout() below will flush all caches, including __root_dir
9190
self._git.checkout(branch)
9291
# Update cache after checkout
9392
self._update_worktrees_cache_after_checkout(branch)
@@ -98,8 +97,8 @@ def _switch_to_branch_worktree(
9897
print(f"Branch {bold(branch)} is checked out in worktree at {bold(worktree_path)}")
9998
print("Changing directory to that worktree")
10099
os.chdir(worktree_path)
101-
# Flush cache after directory change so get_root_dir() returns the correct path
102-
self._git.flush_caches()
100+
# Flush root dir cache after directory change so get_root_dir() returns the correct path
101+
self._git.flush_root_dir_cache()
103102

104103
def traverse(
105104
self,

git_machete/git_operations.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ def flush_caches(self) -> None:
256256
self.__root_dir = None
257257
self.__short_commit_hash_by_revision_cached = {}
258258

259+
def flush_root_dir_cache(self) -> None:
260+
"""Flush only the cached root directory. Useful after changing directories with os.chdir()."""
261+
self.__root_dir = None
262+
259263
def _run_git(self, git_cmd: str, *args: str, flush_caches: bool, allow_non_zero: bool = False) -> int:
260264
exit_code = utils.run_cmd(*GIT_EXEC, git_cmd, *args)
261265
if flush_caches:

0 commit comments

Comments
 (0)