Skip to content

Commit 6b0a1f0

Browse files
committed
refactor: Improve check output
1 parent 68906cb commit 6b0a1f0

2 files changed

Lines changed: 43 additions & 20 deletions

File tree

src/griffe/diff.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44

55
import contextlib
66
import enum
7-
from typing import TYPE_CHECKING, Any, Iterable, Iterator
7+
from pathlib import Path
8+
from typing import Any, Iterable, Iterator
89

910
from colorama import Fore, Style
1011

1112
from griffe.dataclasses import Alias, Attribute, Class, Function, Object, ParameterKind
1213
from griffe.exceptions import AliasResolutionError
14+
from griffe.git import WORKTREE_PREFIX
1315
from griffe.logger import get_logger
1416

15-
if TYPE_CHECKING:
16-
from pathlib import Path
17-
1817
POSITIONAL = frozenset((ParameterKind.positional_only, ParameterKind.positional_or_keyword))
1918
KEYWORD = frozenset((ParameterKind.keyword_only, ParameterKind.positional_or_keyword))
2019
POSITIONAL_KEYWORD_ONLY = frozenset((ParameterKind.positional_only, ParameterKind.keyword_only))
@@ -106,6 +105,27 @@ def _filepath(self) -> Path:
106105
return self.obj.parent.filepath # type: ignore[union-attr,return-value]
107106
return self.obj.filepath # type: ignore[return-value]
108107

108+
@property
109+
def _relative_filepath(self) -> Path:
110+
if self.obj.is_alias:
111+
return self.obj.parent.relative_filepath # type: ignore[union-attr]
112+
return self.obj.relative_filepath
113+
114+
@property
115+
def _relative_package_filepath(self) -> Path:
116+
if self.obj.is_alias:
117+
return self.obj.parent.relative_package_filepath # type: ignore[union-attr]
118+
return self.obj.relative_package_filepath
119+
120+
@property
121+
def _location(self) -> Path:
122+
if self._relative_filepath.is_absolute():
123+
parts = self._relative_filepath.parts
124+
for index, part in enumerate(parts):
125+
if part.startswith(WORKTREE_PREFIX):
126+
return Path(*parts[index + 3 :])
127+
return self._relative_filepath
128+
109129
@property
110130
def _canonical_path(self) -> str:
111131
if self.obj.is_alias:
@@ -120,7 +140,7 @@ def _module_path(self) -> str:
120140

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

125145
@property
126146
def _lineno(self) -> int:
@@ -129,7 +149,7 @@ def _lineno(self) -> int:
129149
return self.obj.lineno or 0
130150

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

134154
def _format_title(self) -> str:
135155
return self._relative_path

src/griffe/git.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@
2828
from griffe.extensions import Extensions
2929

3030

31-
def _assert_git_repo(repo: str) -> None:
31+
WORKTREE_PREFIX = "griffe-worktree-"
32+
33+
34+
def _assert_git_repo(repo: str | Path) -> None:
3235
if not shutil.which("git"):
3336
raise RuntimeError("Could not find git executable. Please install git.")
3437

3538
try:
3639
subprocess.run(
37-
["git", "-C", repo, "rev-parse", "--is-inside-work-tree"],
40+
["git", "-C", str(repo), "rev-parse", "--is-inside-work-tree"],
3841
check=True,
3942
stdout=subprocess.DEVNULL,
4043
stderr=subprocess.DEVNULL,
4144
)
4245
except subprocess.CalledProcessError as err:
43-
raise OSError(f"Not a git repository: {repo!r}") from err
46+
raise OSError(f"Not a git repository: {repo}") from err
4447

4548

4649
def _get_latest_tag(path: str | Path) -> str:
@@ -88,24 +91,24 @@ def tmp_worktree(repo: str | Path = ".", ref: str = "HEAD") -> Iterator[Path]:
8891
OSError: If `repo` is not a valid `.git` repository
8992
RuntimeError: If the `git` executable is unavailable, or if it cannot create a worktree
9093
"""
91-
repo = str(repo)
9294
_assert_git_repo(repo)
93-
with TemporaryDirectory(prefix="griffe-worktree-") as td:
94-
uid = f"griffe_{ref}"
95-
target = os.path.join(td, uid)
96-
retval = subprocess.run(
97-
["git", "-C", repo, "worktree", "add", "-b", uid, target, ref],
95+
repo_name = Path(repo).resolve().name
96+
with TemporaryDirectory(prefix=f"{WORKTREE_PREFIX}{repo_name}-{ref}-") as tmp_dir:
97+
branch = f"griffe_{ref}"
98+
location = os.path.join(tmp_dir, branch)
99+
process = subprocess.run(
100+
["git", "-C", repo, "worktree", "add", "-b", branch, location, ref],
98101
capture_output=True,
99102
)
100-
if retval.returncode:
101-
raise RuntimeError(f"Could not create git worktree: {retval.stderr.decode()}")
103+
if process.returncode:
104+
raise RuntimeError(f"Could not create git worktree: {process.stderr.decode()}")
102105

103106
try:
104-
yield Path(target)
107+
yield Path(location)
105108
finally:
106-
subprocess.run(["git", "-C", repo, "worktree", "remove", uid], stdout=subprocess.DEVNULL)
109+
subprocess.run(["git", "-C", repo, "worktree", "remove", branch], stdout=subprocess.DEVNULL)
107110
subprocess.run(["git", "-C", repo, "worktree", "prune"], stdout=subprocess.DEVNULL)
108-
subprocess.run(["git", "-C", repo, "branch", "-D", uid], stdout=subprocess.DEVNULL)
111+
subprocess.run(["git", "-C", repo, "branch", "-D", branch], stdout=subprocess.DEVNULL)
109112

110113

111114
def load_git(

0 commit comments

Comments
 (0)