Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
PawelLipski marked this conversation as resolved.
Show resolved Hide resolved
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
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), this simply resolves to ``machete`` in .git folder,
PawelLipski marked this conversation as resolved.
Show resolved Hide resolved
* 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!),
PawelLipski marked this conversation as resolved.
Show resolved Hide resolved
* 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/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
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 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!).
PawelLipski marked this conversation as resolved.
Show resolved Hide resolved
""",
"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