diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 49644a6e5..cd9d4b0a0 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -3,7 +3,7 @@ ## New in git-machete 3.11.0 - added: `git machete help config` help topic and sphinx documentation page for config keys and environment variables -- added: boolean git config key `machete.worktree.useTopLevelMacheteFile` that enables per-worktree machete definition file (as opposed to a single central `.git/machete` file for all worktrees) +- added: boolean git config key `machete.worktree.useTopLevelMacheteFile` for switching the machete file location for worktrees: a single central `.git/machete` for all worktrees (as up to 3.10) or a per-worktree `.git/worktrees/.../machete` - added: when GitHub token is invalid/expired, provide information which token provider has been used ## New in git-machete 3.10.1 diff --git a/docs/source/cli_help/file.rst b/docs/source/cli_help/file.rst index f34a151d4..76b369d9f 100644 --- a/docs/source/cli_help/file.rst +++ b/docs/source/cli_help/file.rst @@ -15,6 +15,6 @@ Three cases are possible: * if ``git machete`` is executed from a regular working directory (not a worktree or submodule), the file is located under ``.git/machete``, * if ``git machete`` is executed from a **worktree**, the file path depends on the ``machete.worktree.useTopLevelMacheteFile`` config key value: - * if ``machete.worktree.useTopLevelMacheteFile`` is true, the file is located under ``.git/machete`` - * if ``machete.worktree.useTopLevelMacheteFile`` is false (default), the file is located under ``.git/worktrees/.../machete``, + * if ``machete.worktree.useTopLevelMacheteFile`` is true (default), the file is located under ``.git/machete`` + * if ``machete.worktree.useTopLevelMacheteFile`` is false, the file is located under ``.git/worktrees/.../machete``, * if ``git machete`` is executed from a **submodule**, this file is located in the git folder of the submodule itself under ``.git/modules/.../machete``. diff --git a/git_machete/client.py b/git_machete/client.py index 1ade6ba0c..2708312aa 100644 --- a/git_machete/client.py +++ b/git_machete/client.py @@ -934,7 +934,8 @@ def fp_hash(branch_: LocalBranchShortName) -> Optional[FullCommitHash]: hook_path = self.__git.get_hook_path("machete-status-branch") hook_executable = self.__git.check_hook_executable(hook_path) - maybe_space_before_branch_name = ' ' if self.__git.get_boolean_config_attr('machete.status.extraSpaceBeforeBranchName') else '' + maybe_space_before_branch_name = ' ' if self.__git.get_boolean_config_attr(key='machete.status.extraSpaceBeforeBranchName', + default_value=False) else '' def print_line_prefix(branch_: LocalBranchShortName, suffix: str) -> None: out.write(" " + maybe_space_before_branch_name) diff --git a/git_machete/docs.py b/git_machete/docs.py index a7a13dbf6..85d9781f6 100644 --- a/git_machete/docs.py +++ b/git_machete/docs.py @@ -313,8 +313,8 @@ Three cases are possible: * if `git machete` is executed from a regular working directory (not a worktree or submodule), the file is located under `.git/machete`, * if `git machete` is executed from a worktree, the file path depends on the `machete.worktree.useTopLevelMacheteFile` config key value: - * if `machete.worktree.useTopLevelMacheteFile` is true, the file is located under `.git/machete` - * if `machete.worktree.useTopLevelMacheteFile` is false (default), the file is located under `.git/worktrees/.../machete`, + * if `machete.worktree.useTopLevelMacheteFile` is true (default), the file is located under `.git/machete` + * if `machete.worktree.useTopLevelMacheteFile` is false, the file is located under `.git/worktrees/.../machete`, * if `git machete` is executed from a submodule, this file is located in the git folder of the submodule itself under `.git/modules/.../machete`. """, "fork-point": """ diff --git a/git_machete/git_operations.py b/git_machete/git_operations.py index 3b58b7041..d4904cc68 100644 --- a/git_machete/git_operations.py +++ b/git_machete/git_operations.py @@ -296,7 +296,8 @@ def get_main_git_subpath(self, *fragments: str) -> str: return os.path.join(self.__get_main_git_dir(), *fragments) def get_git_machete_definition_file_path(self) -> str: - use_top_level_machete_file = self.get_boolean_config_attr(key='machete.worktree.useTopLevelMacheteFile') + use_top_level_machete_file = self.get_boolean_config_attr(key='machete.worktree.useTopLevelMacheteFile', + default_value=True) return os.path.join(self.__get_main_git_dir() if use_top_level_machete_file else self.__get_worktree_git_dir(), 'machete') def get_git_timespec_parsed_to_unix_timestamp(self, date: str) -> int: @@ -318,9 +319,11 @@ def get_config_attr_or_none(self, key: str) -> Optional[str]: self.__ensure_config_loaded() return self.__config_cached.get(key.lower()) - def get_boolean_config_attr(self, key: str) -> bool: + def get_boolean_config_attr(self, key: str, default_value: bool) -> bool: self.__ensure_config_loaded() - return self.__config_cached.get(key.lower()) == 'true' + if self.__config_cached.get(key.lower()) is not None: + return self.__config_cached.get(key.lower()) == 'true' + return default_value def set_config_attr(self, key: str, value: str) -> None: self._run_git("config", "--", key, value) diff --git a/tests/test_file.py b/tests/test_file.py index 355a4f316..3bb3cbdd8 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -41,12 +41,21 @@ def test_file(self, mocker: Any) -> None: definition_file_path_relative_to_git_dir = '/'.join(definition_file_path[-2:]).rstrip('\n') assert definition_file_path_relative_to_git_dir == '.git/machete' - # check git machete definition file path when inside a worktree if GitContext().get_git_version() >= (2, 5): # `git worktree` command was introduced in git version 2.5 + # check git machete definition file path when inside a worktree using the default `True` value + # for the `machete.worktree.useTopLevelMacheteFile` key + self.repo_sandbox.execute("git worktree add -f -b snickers_feature snickers_worktree develop") + os.chdir('snickers_worktree') + definition_file_full_path = launch_command("file") + definition_file_path = Path(definition_file_full_path).parts + definition_file_path_relative_to_git_dir = '/'.join(definition_file_path[-2:]).rstrip('\n') + assert definition_file_path_relative_to_git_dir == '.git/machete' + + # check git machete definition file path when inside a worktree but with the `machete.worktree.useTopLevelMacheteFile` key set to `False` self.repo_sandbox.add_git_config_key('machete.worktree.useTopLevelMacheteFile', 'false') - self.repo_sandbox.execute("git worktree add -f -b new_feature test_worktree develop") - os.chdir('test_worktree') + self.repo_sandbox.execute("git worktree add -f -b mars_feature mars_worktree develop") + os.chdir('mars_worktree') definition_file_full_path = launch_command("file") definition_file_path = Path(definition_file_full_path).parts definition_file_path_relative_to_git_dir = '/'.join(definition_file_path[-4:]).rstrip('\n') - assert definition_file_path_relative_to_git_dir == '.git/worktrees/test_worktree/machete' + assert definition_file_path_relative_to_git_dir == '.git/worktrees/mars_worktree/machete'