Skip to content

Commit f1a4812

Browse files
Merge branch 'main' into release/v0.19.1
2 parents 4afe892 + c902bc5 commit f1a4812

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

codeflash/code_utils/git_worktree_utils.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from __future__ import annotations
22

33
import configparser
4+
import shutil
5+
import stat
46
import subprocess
57
import tempfile
68
import time
79
from pathlib import Path
8-
from typing import Optional
10+
from typing import Any, Callable, Optional
911

1012
import git
1113

@@ -95,10 +97,24 @@ def create_detached_worktree(module_root: Path) -> Optional[Path]:
9597
return worktree_dir
9698

9799

100+
def _handle_remove_readonly(
101+
func: Callable[[str], None], path: str, exc_info: tuple[type[BaseException], BaseException, Any]
102+
) -> None:
103+
"""Error handler for shutil.rmtree to handle read-only files on Windows."""
104+
if isinstance(exc_info[1], PermissionError):
105+
Path(path).chmod(stat.S_IWUSR | stat.S_IRUSR | stat.S_IXUSR)
106+
func(path)
107+
else:
108+
raise exc_info[1]
109+
110+
98111
def remove_worktree(worktree_dir: Path) -> None:
112+
"""Remove a git worktree directory."""
113+
if not worktree_dir.exists():
114+
return
99115
try:
100-
repository = git.Repo(worktree_dir, search_parent_directories=True)
101-
repository.git.worktree("remove", "--force", worktree_dir)
116+
shutil.rmtree(worktree_dir, onerror=_handle_remove_readonly)
117+
logger.debug(f"Removed worktree: {worktree_dir}")
102118
except Exception:
103119
logger.exception(f"Failed to remove worktree: {worktree_dir}")
104120

0 commit comments

Comments
 (0)