Skip to content

Commit

Permalink
Change the default logic for inferring git machete definition file pa…
Browse files Browse the repository at this point in the history
…th to what is was before (#551)
  • Loading branch information
amalota authored Jun 17, 2022
1 parent e1eb329 commit 9c5d5f1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/source/cli_help/file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
3 changes: 2 additions & 1 deletion git_machete/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions git_machete/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <b>worktree</b>, 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 <b>submodule</b>, this file is located in the git folder of the submodule itself under `.git/modules/.../machete`.
""",
"fork-point": """
Expand Down
9 changes: 6 additions & 3 deletions git_machete/git_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
17 changes: 13 additions & 4 deletions tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit 9c5d5f1

Please sign in to comment.