Skip to content

Commit 0821e67

Browse files
committed
fix: Normalize paths of temporary Git worktrees
Issue-324: #324
1 parent de6c243 commit 0821e67

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/_griffe/git.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
from __future__ import annotations
66

77
import os
8+
import re
89
import shutil
910
import subprocess
11+
import unicodedata
1012
from contextlib import contextmanager
1113
from pathlib import Path
1214
from tempfile import TemporaryDirectory
@@ -17,6 +19,12 @@
1719
_WORKTREE_PREFIX = "griffe-worktree-"
1820

1921

22+
def _normalize(value: str) -> str:
23+
value = unicodedata.normalize("NFKC", value)
24+
value = re.sub(r"[^\w]+", "-", value)
25+
return re.sub(r"[-\s]+", "-", value).strip("-")
26+
27+
2028
def assert_git_repo(path: str | Path) -> None:
2129
"""Assert that a directory is a Git repository.
2230
@@ -104,11 +112,11 @@ def tmp_worktree(repo: str | Path = ".", ref: str = "HEAD") -> Iterator[Path]:
104112
"""
105113
assert_git_repo(repo)
106114
repo_name = Path(repo).resolve().name
107-
with TemporaryDirectory(prefix=f"{_WORKTREE_PREFIX}{repo_name}-{ref}-") as tmp_dir:
108-
branch = f"griffe_{ref}"
109-
location = os.path.join(tmp_dir, branch) # noqa: PTH118
115+
normref = _normalize(ref)
116+
with TemporaryDirectory(prefix=f"{_WORKTREE_PREFIX}{repo_name}-{normref}-") as tmp_dir:
117+
location = os.path.join(tmp_dir, normref) # noqa: PTH118
110118
process = subprocess.run(
111-
["git", "-C", repo, "worktree", "add", "-b", branch, location, ref],
119+
["git", "-C", repo, "worktree", "add", "-b", normref, location, ref],
112120
capture_output=True,
113121
check=False,
114122
)
@@ -118,6 +126,6 @@ def tmp_worktree(repo: str | Path = ".", ref: str = "HEAD") -> Iterator[Path]:
118126
try:
119127
yield Path(location)
120128
finally:
121-
subprocess.run(["git", "-C", repo, "worktree", "remove", branch], stdout=subprocess.DEVNULL, check=False)
129+
subprocess.run(["git", "-C", repo, "worktree", "remove", normref], stdout=subprocess.DEVNULL, check=False)
122130
subprocess.run(["git", "-C", repo, "worktree", "prune"], stdout=subprocess.DEVNULL, check=False)
123-
subprocess.run(["git", "-C", repo, "branch", "-D", branch], stdout=subprocess.DEVNULL, check=False)
131+
subprocess.run(["git", "-C", repo, "branch", "-D", normref], stdout=subprocess.DEVNULL, check=False)

0 commit comments

Comments
 (0)