From fc7a85ad145990052e9a22c58b5f409d36504a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Tue, 1 Oct 2024 15:28:00 +0200 Subject: [PATCH] fix: Normalize paths of temporary Git worktrees Issue-324: https://github.com/mkdocstrings/griffe/issues/324 --- src/_griffe/git.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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],