Skip to content

Commit

Permalink
refactor: Improve check output
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Jul 4, 2023
1 parent 68906cb commit 6b0a1f0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
32 changes: 26 additions & 6 deletions src/griffe/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

import contextlib
import enum
from typing import TYPE_CHECKING, Any, Iterable, Iterator
from pathlib import Path
from typing import Any, Iterable, Iterator

from colorama import Fore, Style

from griffe.dataclasses import Alias, Attribute, Class, Function, Object, ParameterKind
from griffe.exceptions import AliasResolutionError
from griffe.git import WORKTREE_PREFIX
from griffe.logger import get_logger

if TYPE_CHECKING:
from pathlib import Path

POSITIONAL = frozenset((ParameterKind.positional_only, ParameterKind.positional_or_keyword))
KEYWORD = frozenset((ParameterKind.keyword_only, ParameterKind.positional_or_keyword))
POSITIONAL_KEYWORD_ONLY = frozenset((ParameterKind.positional_only, ParameterKind.keyword_only))
Expand Down Expand Up @@ -106,6 +105,27 @@ def _filepath(self) -> Path:
return self.obj.parent.filepath # type: ignore[union-attr,return-value]
return self.obj.filepath # type: ignore[return-value]

@property
def _relative_filepath(self) -> Path:
if self.obj.is_alias:
return self.obj.parent.relative_filepath # type: ignore[union-attr]
return self.obj.relative_filepath

@property
def _relative_package_filepath(self) -> Path:
if self.obj.is_alias:
return self.obj.parent.relative_package_filepath # type: ignore[union-attr]
return self.obj.relative_package_filepath

@property
def _location(self) -> Path:
if self._relative_filepath.is_absolute():
parts = self._relative_filepath.parts
for index, part in enumerate(parts):
if part.startswith(WORKTREE_PREFIX):
return Path(*parts[index + 3 :])
return self._relative_filepath

@property
def _canonical_path(self) -> str:
if self.obj.is_alias:
Expand All @@ -120,7 +140,7 @@ def _module_path(self) -> str:

@property
def _relative_path(self) -> str:
return self._canonical_path[len(self._module_path) + 1 :]
return self._canonical_path[len(self._module_path) + 1 :] or "<module>"

@property
def _lineno(self) -> int:
Expand All @@ -129,7 +149,7 @@ def _lineno(self) -> int:
return self.obj.lineno or 0

def _format_location(self) -> str:
return f"{Style.BRIGHT}{self._filepath}{Style.RESET_ALL}:{self._lineno}"
return f"{Style.BRIGHT}{self._location}{Style.RESET_ALL}:{self._lineno}"

def _format_title(self) -> str:
return self._relative_path
Expand Down
31 changes: 17 additions & 14 deletions src/griffe/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@
from griffe.extensions import Extensions


def _assert_git_repo(repo: str) -> None:
WORKTREE_PREFIX = "griffe-worktree-"


def _assert_git_repo(repo: str | Path) -> None:
if not shutil.which("git"):
raise RuntimeError("Could not find git executable. Please install git.")

try:
subprocess.run(
["git", "-C", repo, "rev-parse", "--is-inside-work-tree"],
["git", "-C", str(repo), "rev-parse", "--is-inside-work-tree"],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except subprocess.CalledProcessError as err:
raise OSError(f"Not a git repository: {repo!r}") from err
raise OSError(f"Not a git repository: {repo}") from err


def _get_latest_tag(path: str | Path) -> str:
Expand Down Expand Up @@ -88,24 +91,24 @@ def tmp_worktree(repo: str | Path = ".", ref: str = "HEAD") -> Iterator[Path]:
OSError: If `repo` is not a valid `.git` repository
RuntimeError: If the `git` executable is unavailable, or if it cannot create a worktree
"""
repo = str(repo)
_assert_git_repo(repo)
with TemporaryDirectory(prefix="griffe-worktree-") as td:
uid = f"griffe_{ref}"
target = os.path.join(td, uid)
retval = subprocess.run(
["git", "-C", repo, "worktree", "add", "-b", uid, target, ref],
repo_name = Path(repo).resolve().name
with TemporaryDirectory(prefix=f"{WORKTREE_PREFIX}{repo_name}-{ref}-") as tmp_dir:
branch = f"griffe_{ref}"
location = os.path.join(tmp_dir, branch)
process = subprocess.run(
["git", "-C", repo, "worktree", "add", "-b", branch, location, ref],
capture_output=True,
)
if retval.returncode:
raise RuntimeError(f"Could not create git worktree: {retval.stderr.decode()}")
if process.returncode:
raise RuntimeError(f"Could not create git worktree: {process.stderr.decode()}")

try:
yield Path(target)
yield Path(location)
finally:
subprocess.run(["git", "-C", repo, "worktree", "remove", uid], stdout=subprocess.DEVNULL)
subprocess.run(["git", "-C", repo, "worktree", "remove", branch], stdout=subprocess.DEVNULL)
subprocess.run(["git", "-C", repo, "worktree", "prune"], stdout=subprocess.DEVNULL)
subprocess.run(["git", "-C", repo, "branch", "-D", uid], stdout=subprocess.DEVNULL)
subprocess.run(["git", "-C", repo, "branch", "-D", branch], stdout=subprocess.DEVNULL)


def load_git(
Expand Down

0 comments on commit 6b0a1f0

Please sign in to comment.