Skip to content

Commit 36e601c

Browse files
committed
Improve naming and caching
1 parent 962b2b0 commit 36e601c

File tree

5 files changed

+86
-97
lines changed

5 files changed

+86
-97
lines changed

git_machete/client/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def __init__(self, git: GitContext) -> None:
112112
def __get_git_machete_branch_layout_file_path(self) -> str:
113113
use_top_level_machete_file = self._git.get_boolean_config_attr(key=git_config_keys.WORKTREE_USE_TOP_LEVEL_MACHETE_FILE,
114114
default_value=True)
115-
machete_file_directory = self._git.get_main_git_dir() if use_top_level_machete_file else self._git.get_worktree_git_dir()
115+
machete_file_directory = self._git.get_main_worktree_git_dir() if use_top_level_machete_file else self._git.get_current_worktree_git_dir()
116116
return os.path.join(machete_file_directory, 'machete')
117117

118118
def __init_state(self) -> None:
@@ -606,7 +606,7 @@ def print_line_prefix(branch_: LocalBranchShortName, suffix: str) -> None:
606606
if hook_executable:
607607
debug(f"running machete-status-branch hook ({hook_path}) for branch {branch}")
608608
hook_env = dict(os.environ, ASCII_ONLY=str(utils.ascii_only).lower())
609-
status_code, stdout, stderr = self.__popen_hook(hook_path, branch, cwd=self._git.get_root_dir(), env=hook_env)
609+
status_code, stdout, stderr = self.__popen_hook(hook_path, branch, cwd=self._git.get_current_worktree_root_dir(), env=hook_env)
610610

611611
if status_code == 0:
612612
if not stdout.isspace():
@@ -683,7 +683,7 @@ def rebase(
683683
hook_path = self._git.get_hook_path("machete-pre-rebase")
684684
if self._git.check_hook_executable(hook_path):
685685
debug(f"running machete-pre-rebase hook ({hook_path})")
686-
exit_code = self.__run_hook(hook_path, onto, from_exclusive, branch, cwd=self._git.get_root_dir())
686+
exit_code = self.__run_hook(hook_path, onto, from_exclusive, branch, cwd=self._git.get_current_worktree_root_dir())
687687
if exit_code == 0:
688688
self._git.rebase(
689689
onto, from_exclusive, branch,
@@ -1032,7 +1032,7 @@ def _run_post_slide_out_hook(
10321032
debug(f"running machete-post-slide-out hook ({hook_path})")
10331033
new_downstreams_strings: List[str] = [str(db) for db in new_downstreams]
10341034
exit_code = self.__run_hook(hook_path, new_upstream, slid_out_branch, *new_downstreams_strings,
1035-
cwd=self._git.get_root_dir())
1035+
cwd=self._git.get_current_worktree_root_dir())
10361036
if exit_code != 0:
10371037
raise MacheteException(f"The machete-post-slide-out hook exited with {exit_code}, aborting.")
10381038

git_machete/client/traverse.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,48 +55,42 @@ def _update_worktrees_cache_after_checkout(self, checked_out_branch: LocalBranch
5555
Only the current worktree's entry needs to be updated - linked worktrees
5656
don't change when we checkout in a different worktree.
5757
"""
58-
current_worktree_path = self._git.get_root_dir()
58+
current_worktree_root_dir = self._git.get_current_worktree_root_dir()
5959

60-
# Remove the old branch entry for this worktree (if any)
61-
# We need to find which branch was previously checked out in this worktree
62-
for branch, path in self.__worktrees_cache.items():
63-
if path == current_worktree_path: # pragma: no branch
64-
del self.__worktrees_cache[branch]
60+
for branch, path in self.__worktree_root_dir_for_branch.items():
61+
if path == current_worktree_root_dir: # pragma: no branch
62+
del self.__worktree_root_dir_for_branch[branch]
6563
break
6664

67-
# Add the new branch entry for this worktree
68-
self.__worktrees_cache[checked_out_branch] = current_worktree_path
65+
self.__worktree_root_dir_for_branch[checked_out_branch] = current_worktree_root_dir
6966

7067
def _switch_to_branch_worktree(
7168
self,
72-
branch: LocalBranchShortName) -> None:
69+
target_branch: LocalBranchShortName) -> None:
7370
"""
7471
Switch to the worktree where the branch is checked out, or to main worktree if branch is not checked out.
7572
This may involve changing the current working directory.
7673
Updates the worktrees cache after checkout.
7774
"""
78-
worktree_path = self.__worktrees_cache.get(branch)
79-
current_worktree_root = self._git.get_root_dir()
75+
target_worktree_root_dir = self.__worktree_root_dir_for_branch.get(target_branch)
76+
current_worktree_root_dir = self._git.get_current_worktree_root_dir()
8077

81-
if worktree_path is None:
78+
if target_worktree_root_dir is None:
8279
# Branch is not checked out anywhere, need to checkout in main worktree
8380
# Only cd if we're currently in a different worktree (linked worktree)
84-
main_worktree_path = self._git.get_main_worktree_path()
85-
if current_worktree_root != main_worktree_path:
86-
print(f"Changing directory to main worktree at {bold(main_worktree_path)}")
87-
os.chdir(main_worktree_path)
88-
# checkout() below will flush all caches, including __root_dir
89-
self._git.checkout(branch)
81+
main_worktree_root_dir = self._git.get_main_worktree_root_dir()
82+
if current_worktree_root_dir != main_worktree_root_dir:
83+
print(f"Changing directory to main worktree at {bold(main_worktree_root_dir)}")
84+
self._git.chdir(main_worktree_root_dir)
85+
self._git.checkout(target_branch)
9086
# Update cache after checkout
91-
self._update_worktrees_cache_after_checkout(branch)
87+
self._update_worktrees_cache_after_checkout(target_branch)
9288
else:
9389
# Branch is checked out in a worktree
9490
# Only cd if we're in a different worktree
95-
if current_worktree_root != worktree_path:
96-
print(f"Changing directory to {bold(worktree_path)} worktree where {bold(branch)} is checked out")
97-
os.chdir(worktree_path)
98-
# Flush root dir cache after directory change so get_root_dir() returns the correct path
99-
self._git.flush_root_dir_cache()
91+
if current_worktree_root_dir != target_worktree_root_dir:
92+
print(f"Changing directory to {bold(target_worktree_root_dir)} worktree where {bold(target_branch)} is checked out")
93+
self._git.chdir(target_worktree_root_dir)
10094

10195
def traverse(
10296
self,
@@ -140,10 +134,10 @@ def traverse(
140134

141135
# Store the initial directory for later restoration
142136
initial_branch = nearest_remaining_branch = self._git.get_current_branch()
143-
initial_worktree_root = self._git.get_root_dir()
137+
initial_worktree_root = self._git.get_current_worktree_root_dir()
144138

145139
# Fetch worktrees once at the start to avoid repeated git worktree list calls
146-
self.__worktrees_cache: Dict[LocalBranchShortName, str] = self._git.get_worktrees()
140+
self.__worktree_root_dir_for_branch: Dict[LocalBranchShortName, str] = self._git.get_worktree_root_dirs_by_branch()
147141

148142
try:
149143
if opt_start_from == TraverseStartFrom.ROOT:
@@ -502,7 +496,7 @@ def traverse(
502496
finally:
503497
# Warn if the initial directory doesn't correspond to the final checked out branch's worktree
504498
final_branch = self._git.get_current_branch()
505-
final_worktree_path = self.__worktrees_cache.get(final_branch)
499+
final_worktree_path = self.__worktree_root_dir_for_branch.get(final_branch)
506500
if final_worktree_path and initial_worktree_root != final_worktree_path:
507501
# Final branch is checked out in a worktree different from where we started
508502
warn(

git_machete/client/with_code_hosting.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def create_pull_request(
293293
fork_point = self.fork_point(head, use_overrides=True)
294294
commits: List[GitLogEntry] = self._git.get_commits_between(fork_point, head)
295295

296-
pr_title_file_path = self._git.get_main_git_subpath('info', 'title')
296+
pr_title_file_path = self._git.get_main_worktree_git_subpath('info', 'title')
297297
is_pr_title_file = os.path.isfile(pr_title_file_path)
298298
if opt_title:
299299
title = opt_title
@@ -310,11 +310,11 @@ def create_pull_request(
310310
if force_description_from_commit_message:
311311
description = self._git.get_commit_data(commits[0].hash, GitFormatPatterns.MESSAGE_BODY) if commits else ''
312312
else:
313-
machete_description_path = self._git.get_main_git_subpath('info', 'description')
313+
machete_description_path = self._git.get_main_worktree_git_subpath('info', 'description')
314314
if os.path.isfile(machete_description_path):
315315
description = slurp_file(machete_description_path)
316316
else:
317-
code_hosting_description_paths = [os.path.join(self._git.get_root_dir(), *path) for path in spec.pr_description_paths]
317+
code_hosting_description_paths = [os.path.join(self._git.get_current_worktree_root_dir(), *path) for path in spec.pr_description_paths]
318318
existing = find_or_none(os.path.isfile, code_hosting_description_paths)
319319
if existing:
320320
description = slurp_file(existing)
@@ -345,7 +345,7 @@ def create_pull_request(
345345
self.code_hosting_client.set_description_of_pull_request(pr.number, new_description)
346346
print(fmt(ok_str))
347347

348-
milestone_path: str = self._git.get_main_git_subpath('info', 'milestone')
348+
milestone_path: str = self._git.get_main_worktree_git_subpath('info', 'milestone')
349349
if os.path.isfile(milestone_path):
350350
milestone = slurp_file(milestone_path).strip()
351351
else:
@@ -360,7 +360,7 @@ def create_pull_request(
360360
self.code_hosting_client.add_assignees_to_pull_request(pr.number, [current_user])
361361
print(fmt(ok_str))
362362

363-
reviewers_path = self._git.get_main_git_subpath('info', 'reviewers')
363+
reviewers_path = self._git.get_main_worktree_git_subpath('info', 'reviewers')
364364
if os.path.isfile(reviewers_path):
365365
reviewers = utils.get_non_empty_lines(slurp_file(reviewers_path))
366366
else:

0 commit comments

Comments
 (0)