Skip to content

Commit

Permalink
Ensure the full path to git is used on Windows
Browse files Browse the repository at this point in the history
# Conflicts:
#	poetry/core/vcs/git.py
#	tests/vcs/test_vcs.py
  • Loading branch information
sdispater committed Sep 17, 2021
1 parent 9a29df4 commit bd142a8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
43 changes: 41 additions & 2 deletions poetry/core/vcs/git.py
Expand Up @@ -6,6 +6,8 @@
from typing import Any
from typing import Optional

from poetry.core.utils._compat import PY36
from poetry.core.utils._compat import WINDOWS
from poetry.core.utils._compat import Path
from poetry.core.utils._compat import decode

Expand Down Expand Up @@ -154,14 +156,49 @@ def __str__(self): # type: () -> str
GitUrl = namedtuple("GitUrl", ["url", "revision"])


_executable: Optional[str] = None


def executable():
global _executable

if _executable is not None:
return _executable

if WINDOWS and PY36:
# Finding git via where.exe
where = "%WINDIR%\\System32\\where.exe"
paths = decode(
subprocess.check_output([where, "git"], shell=True, encoding="oem")
).split("\n")
for path in paths:
if not path:
continue

path = Path(path.strip())
try:
path.relative_to(Path.cwd())
except ValueError:
_executable = str(path)

break
else:
_executable = "git"

if _executable is None:
raise RuntimeError("Unable to find a valid git executable")

return _executable


class GitConfig:
def __init__(self, requires_git_presence=False): # type: (bool) -> None
self._config = {}

try:
config_list = decode(
subprocess.check_output(
["git", "config", "-l"], stderr=subprocess.STDOUT
[executable(), "config", "-l"], stderr=subprocess.STDOUT
)
)

Expand Down Expand Up @@ -310,7 +347,9 @@ def run(self, *args, **kwargs): # type: (*Any, **Any) -> str
) + args

return decode(
subprocess.check_output(["git"] + list(args), stderr=subprocess.STDOUT)
subprocess.check_output(
[executable()] + list(args), stderr=subprocess.STDOUT
)
).strip()

def _check_parameter(self, parameter): # type: (str) -> None
Expand Down
42 changes: 42 additions & 0 deletions tests/vcs/test_vcs.py
@@ -1,5 +1,9 @@
import subprocess

import pytest

from poetry.core.utils._compat import PY36
from poetry.core.utils._compat import WINDOWS
from poetry.core.utils._compat import Path
from poetry.core.vcs.git import Git
from poetry.core.vcs.git import GitError
Expand Down Expand Up @@ -276,3 +280,41 @@ def test_git_checkout_raises_error_on_invalid_repository():
def test_git_rev_parse_raises_error_on_invalid_repository():
with pytest.raises(GitError):
Git().rev_parse("-u./payload")


@pytest.mark.skipif(
not WINDOWS or not PY36,
reason="Retrieving the complete path to git is only necessary on Windows, for security reasons",
)
def test_ensure_absolute_path_to_git(mocker):
def checkout_output(cmd, *args, **kwargs):
if Path(cmd[0]).name == "where.exe":
return "\n".join(
[str(Path.cwd().joinpath("git.exe")), "C:\\Git\\cmd\\git.exe"]
)

return b""

mock = mocker.patch.object(subprocess, "check_output", side_effect=checkout_output)

Git().run("config")

assert mock.call_args_list[-1][0][0] == [
"C:\\Git\\cmd\\git.exe",
"config",
]


@pytest.mark.skipif(
not WINDOWS or not PY36,
reason="Retrieving the complete path to git is only necessary on Windows, for security reasons",
)
def test_ensure_existing_git_executable_is_found(mocker):
mock = mocker.patch.object(subprocess, "check_output", return_value=b"")

Git().run("config")

cmd = Path(mock.call_args_list[-1][0][0][0])

assert cmd.is_absolute()
assert cmd.name == "git.exe"

0 comments on commit bd142a8

Please sign in to comment.