55from __future__ import annotations
66
77import os
8+ import re
89import shutil
910import subprocess
11+ import unicodedata
1012from contextlib import contextmanager
1113from pathlib import Path
1214from tempfile import TemporaryDirectory
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+
2028def 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