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
  • Loading branch information
PawelLipski committed Nov 27, 2021
1 parent dacbd2b commit 2481213
Show file tree
Hide file tree
Showing 9 changed files with 39 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
2 changes: 1 addition & 1 deletion docs/source/cli_help/discover.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ discover
git machete discover [-C|--checked-out-since=<date>] [-l|--list-commits] [-r|--roots=<branch1>,<branch2>,...] [-y|--yes]
Discovers and displays tree of branch dependencies using a heuristic based on reflogs and asks whether to overwrite the existing definition :ref:`file` with the new discovered tree.
If confirmed with a ``y[es]`` or ``e[dit]`` reply, backs up the current definition file (if it exists) as ``$GIT_DIR/machete~`` and saves the new tree under the usual ``$GIT_DIR/machete`` path.
If confirmed with a ``y[es]`` or ``e[dit]`` reply, backs up the current definition file (if it exists) as ``machete~`` and saves the new tree in the usual ``machete`` file.
If the reply was ``e[dit]``, additionally an editor is opened (as in :ref:`git machete edit<edit>`) after saving the new definition file.

**Options:**
Expand Down
8 changes: 8 additions & 0 deletions docs/source/cli_help/file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ 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 the 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), this simply resolves to `machete` in .git folder,
* if `git machete` is executed from a **worktree**, this resolves to `machete` in the .git folder of the **top-level project** (not the worktree's .git folder!),
* if `git machete` is executed from a **submodule**, this resolves to `machete` in the .git folder of the **submodule** itself (not the top-level project's .git folder!).
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/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '3.6.0'
__version__ = '3.6.1'
11 changes: 8 additions & 3 deletions git_machete/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
<b>Usage: git machete discover [-C|--checked-out-since=<date>] [-l|--list-commits] [-r|--roots=<branch1>,<branch2>,...] [-y|--yes]</b>
Discovers and displays tree of branch dependencies using a heuristic based on reflogs and asks whether to overwrite the existing definition file with the new discovered tree.
If confirmed with a `y[es]` or `e[dit]` reply, backs up the current definition file (if it exists) as `$GIT_DIR/machete~` and saves the new tree under the usual `$GIT_DIR/machete` path.
If confirmed with a `y[es]` or `e[dit]` reply, backs up the current definition file (if it exists) as `machete~` and saves the new tree in the usual `machete` file.
If the reply was `e[dit]`, additionally an editor is opened (as in `git machete edit`) after saving the new definition file.
Options:
Expand Down 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 the 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), this simply resolves to `machete` in .git folder,
* if `git machete` is executed from a <b>worktree</b>, this resolves to `machete` in the .git folder of the <b>top-level project</b> (not the worktree's .git folder!),
* if `git machete` is executed from a <b>submodule</b>, this resolves to `machete` in the .git folder of the <b>submodule</b> itself (not the top-level project's .git folder!).
""",
"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 @@ -255,7 +256,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 2481213

Please sign in to comment.