Skip to content

Commit

Permalink
fix: Normalize paths of temporary Git worktrees
Browse files Browse the repository at this point in the history
Issue-324: #324
  • Loading branch information
pawamoy committed Oct 1, 2024
1 parent de6c243 commit fc7a85a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/_griffe/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit fc7a85a

Please sign in to comment.