Skip to content

Commit

Permalink
Make sure that machete file for worktrees is sought for in the top-le…
Browse files Browse the repository at this point in the history
…vel git dir (#361)

* Make sure that machete file for worktrees is sought for in the top-level git dir

* Make sure that machete file for worktrees is sought for in the top-level git dir - 1st round of fixes

* Make sure that machete file for worktrees is sought for in the top-level git dir - 2nd round of fixes

* Make sure that machete file for worktrees is sought for in the top-level git dir - 3rd round of fixes
  • Loading branch information
PawelLipski authored Dec 1, 2021
1 parent 5a820e0 commit ecd8c6a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 13 deletions.
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release notes

## New in git-machete 3.6.1

- fixed: support for worktrees (reported by @kgadek)

## New in git-machete 3.6.0

- added: `t` alias for `traverse` command
Expand Down
9 changes: 7 additions & 2 deletions docs/source/cli_help/file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ file
git machete file
Outputs the absolute path of the machete definition file. Currently fixed to ``<git-directory>/machete``.
Note: this won't always be just ``<repo-root>/.git/machete`` since e.g. submodules and worktrees have their git directories in different location.
Outputs the absolute path of machete definition file.
The file is always called ``machete`` and is located in the git directory of the project.

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**, this file is located under ``.git/machete`` as well (**not** in the git folder of the worktree 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``.
2 changes: 1 addition & 1 deletion docs/source/completion.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Fish:
Please look at the section about [installation via Homebrew](https://github.com/VirtusLab/git-machete#using-homebrew-macos).
``brew install git-machete`` automatically installs fish completion files for ``git machete``.
* Linux
#. Place the completion script in ``/path/to/fish/completions/`` (typically ``~/.config/fish/completions/git-machete.fish``).
Place the completion script in ``/path/to/fish/completions/`` (typically ``~/.config/fish/completions/git-machete.fish``).

.. code-block:: shell
Expand Down
2 changes: 1 addition & 1 deletion git_machete/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ def traverse(
if not any_action_suggested and initial_branch not in self.__roots:
print(fmt("Tip: `traverse` by default starts from the current branch, "
"use flags (`--starts-from=`, `--whole` or `-w`, `-W`) to change this behavior.\n"
"Further info under `git machete traverse --help`."), file=sys.stdout)
"Further info under `git machete traverse --help`."))
if opt_return_to == "here" or (
opt_return_to == "nearest-remaining" and nearest_remaining_branch == initial_branch):
print(f"Returned to the initial branch {bold(initial_branch)}")
Expand Down
9 changes: 7 additions & 2 deletions git_machete/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@
"file": """
<b>Usage: git machete file</b>
Outputs the absolute path of the machete definition file. Currently fixed to `<git-directory>/machete`.
Note: this won't always be just `<repo-root>/.git/machete` since e.g. submodules and worktrees have their git directories in different location.
Outputs the absolute path of machete definition file.
The file is always called `machete` and is located in the git directory of the project.
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>, this file is located under `.git/machete` as well (<b>not</b> in the git folder of the worktree 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": """
<b>Usage:
Expand Down
10 changes: 9 additions & 1 deletion git_machete/git_operations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
from typing import Callable, Dict, Generator, Iterator, List, Match, Optional, Set, Tuple

import os
Expand Down Expand Up @@ -254,7 +255,14 @@ def get_root_dir(self) -> str:
def __get_git_dir(self) -> str:
if not self._git_dir:
try:
self._git_dir = self._popen_git("rev-parse", "--git-dir").strip()
git_dir: str = self._popen_git("rev-parse", "--git-dir").strip()
git_dir_parts = Path(git_dir).parts
if len(git_dir_parts) >= 3 and git_dir_parts[-3] == '.git' and git_dir_parts[-2] == 'worktrees':
self._git_dir = os.path.join(*git_dir_parts[:-2])
debug('__get_git_dir', f'git dir pointing to {git_dir} - we are in a worktree; '
f'using {self._git_dir} as the effective git dir instead')
else:
self._git_dir = git_dir
except MacheteException:
raise MacheteException("Not a git repository")
return self._git_dir
Expand Down
5 changes: 3 additions & 2 deletions git_machete/tests/functional/test_machete.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,15 @@ def setup_discover_standard_tree(self) -> None:
)

@mock.patch('git_machete.utils.run_cmd', mock_run_cmd) # to hide git outputs in tests
def test_branch_reappers_in_definition(self) -> None:
def test_branch_reappears_in_definition(self) -> None:
body: str = \
"""master
\tdevelop
\t\n
develop
"""
expected_error_msg: str = fmt('.git/machete, line 5: branch `develop` re-appears in the tree definition. Edit the definition file manually with `git machete edit`')
expected_error_msg: str = fmt('.git/machete, line 5: branch `develop` re-appears in the tree definition. '
'Edit the definition file manually with `git machete edit`')

self.repo_sandbox.new_branch("root")
self.rewrite_definition_file(body)
Expand Down
8 changes: 4 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = pep8,mypy-py{36,37,38,39},py{36,37,38,39},coverage,docs
envlist = pep8,mypy-py{36,37,38,39,310},py{36,37,38,39,310},coverage,docs
minversion = 2.3.2
skipsdist = True

Expand Down Expand Up @@ -32,7 +32,7 @@ exclude = ./.*,build,dist,*egg,venv
import-order-style = pep8

[testenv:coverage]
description = "Checking the test coverage of the code."
description = "Check the test coverage of the code"
deps = coverage
commands =
coverage erase
Expand All @@ -51,9 +51,9 @@ commands =

[testenv:mypy]
whitelist_externals = tox
commands = tox -e "mypy-py{36,37,38,39}"
commands = tox -e "mypy-py{36,37,38,39,310}"

[testenv:mypy-py{36,37,38,39}]
[testenv:mypy-py{36,37,38,39,310}]
deps = mypy
commands =
mypy --config-file mypy.ini git_machete

0 comments on commit ecd8c6a

Please sign in to comment.