Skip to content

Commit

Permalink
fix: Remove pygit2 dependency
Browse files Browse the repository at this point in the history
On a few platforms (ask @peckto) the pygit2 dependency cannot be
installed with poetry, since the toolchain tries to build from source
and fails. Since we only use this library to detect the git root, let's
implement this with calling the Git binary instead. When git is not available,
then Git repos are not used for config discovery.
  • Loading branch information
rumpelsepp committed Aug 26, 2024
1 parent 9fa5749 commit d2070e9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 93 deletions.
119 changes: 39 additions & 80 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ tabulate = ">=0.9"
construct = "^2.10"
msgspec = ">=0.11,<0.19"
pydantic = "^2.0"
pygit2 = "^1.10"
platformdirs = ">=2.6,<5.0"
exitcode = "^0.1.0"
psutil = ">=5.9.4,<7.0.0"
Expand Down Expand Up @@ -83,7 +82,6 @@ plugins = [
[[tool.mypy.overrides]]
module = [
"argcomplete",
"pygit2",
]
ignore_missing_imports = true

Expand Down
21 changes: 12 additions & 9 deletions src/gallia/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
# SPDX-License-Identifier: Apache-2.0

import os
import shutil
import subprocess
import tomllib
from pathlib import Path
from typing import Any

from platformdirs import user_config_path
from pygit2 import discover_repository


class Config(dict[str, Any]):
Expand All @@ -28,14 +29,16 @@ def get_value(self, key: str, default: Any | None = None) -> Any | None:


def get_git_root() -> Path | None:
res = discover_repository(Path.cwd())
match res:
case str() as p:
return Path(p).parent
case None:
return None

raise ValueError(f"unexpected return from pygit2 {res}")
git_path = shutil.which("git")
if git_path is None:
return None
try:
p = subprocess.run(
[git_path, "rev-parse", "--show-toplevel"], capture_output=True, check=True
)
except subprocess.CalledProcessError:
return None
return Path(p.stdout.decode().strip())


def get_config_dirs() -> list[Path]:
Expand Down
10 changes: 8 additions & 2 deletions tests/pytest/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
# SPDX-License-Identifier: Apache-2.0

import os
import shutil
import subprocess
from pathlib import Path

import platformdirs
import pytest
from pygit2 import init_repository

from gallia.config import get_config_dirs, load_config_file


def init_repository(path: Path) -> None:
subprocess.run(["git", "init", path], check=True)


@pytest.mark.skipif(shutil.which("git") is None, reason="git binary is not available")
def test_config_discovery_git(tmp_path: Path) -> None:
testrepo = tmp_path.joinpath("testrepo")
testrepo.mkdir()
init_repository(str(testrepo))
init_repository(testrepo)
os.chdir(testrepo)

config_file = testrepo.joinpath("gallia.toml")
Expand Down

0 comments on commit d2070e9

Please sign in to comment.