diff --git a/src/_griffe/git.py b/src/_griffe/git.py index 4b5b45fc..cc2817c8 100644 --- a/src/_griffe/git.py +++ b/src/_griffe/git.py @@ -5,8 +5,10 @@ from __future__ import annotations import os +import re import shutil import subprocess +import unicodedata from contextlib import contextmanager from pathlib import Path from tempfile import TemporaryDirectory @@ -17,6 +19,12 @@ _WORKTREE_PREFIX = "griffe-worktree-" +def _normalize(value: str) -> str: + value = unicodedata.normalize("NFKC", value) + value = re.sub(r"[^\w]+", "-", value) + return re.sub(r"[-\s]+", "-", value).strip("-") + + def assert_git_repo(path: str | Path) -> None: """Assert that a directory is a Git repository. @@ -105,7 +113,7 @@ def tmp_worktree(repo: str | Path = ".", ref: str = "HEAD") -> Iterator[Path]: assert_git_repo(repo) repo_name = Path(repo).resolve().name with TemporaryDirectory(prefix=f"{_WORKTREE_PREFIX}{repo_name}-{ref}-") as tmp_dir: - branch = f"griffe_{ref}" + branch = _normalize(ref) location = os.path.join(tmp_dir, branch) # noqa: PTH118 process = subprocess.run( ["git", "-C", repo, "worktree", "add", "-b", branch, location, ref],