Skip to content

Commit 2b7724a

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 0977535 commit 2b7724a

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
@@ -87,8 +87,7 @@ def _switch_to_branch_worktree(
8787
if current_worktree_root != main_worktree_path:
8888
print(f"Changing directory to main worktree at {bold(main_worktree_path)}")
8989
os.chdir(main_worktree_path)
90-
# Flush cache after directory change so get_root_dir() returns the correct path
91-
self._git.flush_caches()
90+
# checkout() below will flush all caches, including __root_dir
9291
self._git.checkout(branch)
9392
# Update cache after checkout
9493
self._update_worktrees_cache_after_checkout(branch)
@@ -99,8 +98,8 @@ def _switch_to_branch_worktree(
9998
print(f"Branch {bold(branch)} is checked out in worktree at {bold(worktree_path)}")
10099
print("Changing directory to that worktree")
101100
os.chdir(worktree_path)
102-
# Flush cache after directory change so get_root_dir() returns the correct path
103-
self._git.flush_caches()
101+
# Flush root dir cache after directory change so get_root_dir() returns the correct path
102+
self._git.flush_root_dir_cache()
104103

105104
def traverse(
106105
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)