Skip to content

Commit 6e3d672

Browse files
committed
Improve naming and caching
1 parent 962b2b0 commit 6e3d672

File tree

5 files changed

+91
-98
lines changed

5 files changed

+91
-98
lines changed

git_machete/client/base.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ 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+
if use_top_level_machete_file:
116+
machete_file_directory = self._git.get_main_worktree_git_dir()
117+
else:
118+
machete_file_directory = self._git.get_current_worktree_git_dir()
116119
return os.path.join(machete_file_directory, 'machete')
117120

118121
def __init_state(self) -> None:
@@ -606,7 +609,8 @@ def print_line_prefix(branch_: LocalBranchShortName, suffix: str) -> None:
606609
if hook_executable:
607610
debug(f"running machete-status-branch hook ({hook_path}) for branch {branch}")
608611
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)
612+
status_code, stdout, stderr = self.__popen_hook(
613+
hook_path, branch, cwd=self._git.get_current_worktree_root_dir(), env=hook_env)
610614

611615
if status_code == 0:
612616
if not stdout.isspace():
@@ -683,7 +687,7 @@ def rebase(
683687
hook_path = self._git.get_hook_path("machete-pre-rebase")
684688
if self._git.check_hook_executable(hook_path):
685689
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())
690+
exit_code = self.__run_hook(hook_path, onto, from_exclusive, branch, cwd=self._git.get_current_worktree_root_dir())
687691
if exit_code == 0:
688692
self._git.rebase(
689693
onto, from_exclusive, branch,
@@ -1032,7 +1036,7 @@ def _run_post_slide_out_hook(
10321036
debug(f"running machete-post-slide-out hook ({hook_path})")
10331037
new_downstreams_strings: List[str] = [str(db) for db in new_downstreams]
10341038
exit_code = self.__run_hook(hook_path, new_upstream, slid_out_branch, *new_downstreams_strings,
1035-
cwd=self._git.get_root_dir())
1039+
cwd=self._git.get_current_worktree_root_dir())
10361040
if exit_code != 0:
10371041
raise MacheteException(f"The machete-post-slide-out hook exited with {exit_code}, aborting.")
10381042

git_machete/client/traverse.py

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import itertools
2-
import os
32
from enum import auto
43
from typing import Dict, List, Optional, Type, Union
54

@@ -55,48 +54,42 @@ def _update_worktrees_cache_after_checkout(self, checked_out_branch: LocalBranch
5554
Only the current worktree's entry needs to be updated - linked worktrees
5655
don't change when we checkout in a different worktree.
5756
"""
58-
current_worktree_path = self._git.get_root_dir()
57+
current_worktree_root_dir = self._git.get_current_worktree_root_dir()
5958

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]
59+
for branch, path in self.__worktree_root_dir_for_branch.items():
60+
if path == current_worktree_root_dir: # pragma: no branch
61+
del self.__worktree_root_dir_for_branch[branch]
6562
break
6663

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

7066
def _switch_to_branch_worktree(
7167
self,
72-
branch: LocalBranchShortName) -> None:
68+
target_branch: LocalBranchShortName) -> None:
7369
"""
7470
Switch to the worktree where the branch is checked out, or to main worktree if branch is not checked out.
7571
This may involve changing the current working directory.
7672
Updates the worktrees cache after checkout.
7773
"""
78-
worktree_path = self.__worktrees_cache.get(branch)
79-
current_worktree_root = self._git.get_root_dir()
74+
target_worktree_root_dir = self.__worktree_root_dir_for_branch.get(target_branch)
75+
current_worktree_root_dir = self._git.get_current_worktree_root_dir()
8076

81-
if worktree_path is None:
77+
if target_worktree_root_dir is None:
8278
# Branch is not checked out anywhere, need to checkout in main worktree
8379
# 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)
80+
main_worktree_root_dir = self._git.get_main_worktree_root_dir()
81+
if current_worktree_root_dir != main_worktree_root_dir:
82+
print(f"Changing directory to main worktree at {bold(main_worktree_root_dir)}")
83+
self._git.chdir(main_worktree_root_dir)
84+
self._git.checkout(target_branch)
9085
# Update cache after checkout
91-
self._update_worktrees_cache_after_checkout(branch)
86+
self._update_worktrees_cache_after_checkout(target_branch)
9287
else:
9388
# Branch is checked out in a worktree
9489
# 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()
90+
if current_worktree_root_dir != target_worktree_root_dir:
91+
print(f"Changing directory to {bold(target_worktree_root_dir)} worktree where {bold(target_branch)} is checked out")
92+
self._git.chdir(target_worktree_root_dir)
10093

10194
def traverse(
10295
self,
@@ -140,10 +133,10 @@ def traverse(
140133

141134
# Store the initial directory for later restoration
142135
initial_branch = nearest_remaining_branch = self._git.get_current_branch()
143-
initial_worktree_root = self._git.get_root_dir()
136+
initial_worktree_root = self._git.get_current_worktree_root_dir()
144137

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

148141
try:
149142
if opt_start_from == TraverseStartFrom.ROOT:
@@ -502,7 +495,7 @@ def traverse(
502495
finally:
503496
# Warn if the initial directory doesn't correspond to the final checked out branch's worktree
504497
final_branch = self._git.get_current_branch()
505-
final_worktree_path = self.__worktrees_cache.get(final_branch)
498+
final_worktree_path = self.__worktree_root_dir_for_branch.get(final_branch)
506499
if final_worktree_path and initial_worktree_root != final_worktree_path:
507500
# Final branch is checked out in a worktree different from where we started
508501
warn(

git_machete/client/with_code_hosting.py

Lines changed: 6 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,12 @@ 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)
318+
for path in spec.pr_description_paths]
318319
existing = find_or_none(os.path.isfile, code_hosting_description_paths)
319320
if existing:
320321
description = slurp_file(existing)
@@ -345,7 +346,7 @@ def create_pull_request(
345346
self.code_hosting_client.set_description_of_pull_request(pr.number, new_description)
346347
print(fmt(ok_str))
347348

348-
milestone_path: str = self._git.get_main_git_subpath('info', 'milestone')
349+
milestone_path: str = self._git.get_main_worktree_git_subpath('info', 'milestone')
349350
if os.path.isfile(milestone_path):
350351
milestone = slurp_file(milestone_path).strip()
351352
else:
@@ -360,7 +361,7 @@ def create_pull_request(
360361
self.code_hosting_client.add_assignees_to_pull_request(pr.number, [current_user])
361362
print(fmt(ok_str))
362363

363-
reviewers_path = self._git.get_main_git_subpath('info', 'reviewers')
364+
reviewers_path = self._git.get_main_worktree_git_subpath('info', 'reviewers')
364365
if os.path.isfile(reviewers_path):
365366
reviewers = utils.get_non_empty_lines(slurp_file(reviewers_path))
366367
else:

0 commit comments

Comments
 (0)