Skip to content

The agent that grows with you

fix(profiles): route distribution .git cleanup through the shared rmtree_readonly helper

teknium1287f0ec68ca1
diff --git a/hermes_cli/profile_distribution.py b/hermes_cli/profile_distribution.py
index 9cf1a5166de..9e458af6c0b 100644
--- a/hermes_cli/profile_distribution.py
+++ b/hermes_cli/profile_distribution.py
@@ -11,7 +11,6 @@ import operator
import os
import re
import shutil
-import stat
import subprocess
import tempfile
from dataclasses import dataclass, field
@@ -22,6 +21,7 @@ from typing import Any, Dict, List, Optional, Tuple
import yaml
from hermes_cli._subprocess_compat import noninteractive_git_env
+from utils import rmtree_readonly
MANIFEST_FILENAME = "distribution.yaml"
@@ -244,28 +244,6 @@ def _git_clone(url: str, dest: Path) -> None:
raise DistributionError(f"git clone failed: {(result.stderr or '').strip()}")
-def _rmtree_make_writable(func, path, exc_info) -> None:
- """Retry a failed tree removal after clearing Windows read-only attributes."""
- exc = exc_info[1] if isinstance(exc_info, tuple) else exc_info
- if not isinstance(exc, PermissionError):
- raise exc
- for target in (path, os.path.dirname(path)):
- if target:
- try:
- os.chmod(target, os.stat(target).st_mode | stat.S_IWUSR)
- except OSError:
- pass
- func(path)
-
-
-def _remove_git_metadata(path: Path) -> None:
- """Remove cloned Git metadata completely, including read-only objects on Windows."""
- try:
- shutil.rmtree(path, onexc=_rmtree_make_writable)
- except TypeError: # Python 3.11 uses the legacy ``onerror`` callback name.
- shutil.rmtree(path, onerror=_rmtree_make_writable)
-
-
def _stage_source(source: str, workdir: Path) -> Tuple[Path, str]:
"""Resolve *source* to ``(staged_dir, provenance)``: git URLs are shallow-cloned into
*workdir* (``.git`` removed); a local directory is used in place."""
@@ -273,7 +251,9 @@ def _stage_source(source: str, workdir: Path) -> Tuple[Path, str]:
if _looks_like_git_url(src_str):
staged, provenance = workdir / "clone", src_str
_git_clone(src_str, staged)
- _remove_git_metadata(staged / ".git")
+ # Not ``ignore_errors``: a half-deleted ``.git`` (read-only objects on Windows) would
+ # otherwise be copied into the profile as distribution content (#117184).
+ rmtree_readonly(staged / ".git")
missing = (
f"No {MANIFEST_FILENAME} at the root of {src_str!r}. "
"This repository is not a Hermes profile distribution."
diff --git a/tests/test_utils_rmtree_readonly.py b/tests/test_utils_rmtree_readonly.py
index 81fc7cec551..04400be7fd1 100644
--- a/tests/test_utils_rmtree_readonly.py
+++ b/tests/test_utils_rmtree_readonly.py
@@ -2,7 +2,7 @@
Git marks loose object files read-only on Windows (``WinError 5``), and package
installs arrive as read-only trees on POSIX, so every cleanup path that deletes a
-clone needs the retry. Regression coverage for #117170, #117176 and #117179.
+clone needs the retry. Regression coverage for #117170, #117176, #117179 and #117184.
"""
from __future__ import annotations
@@ -52,34 +52,15 @@ def test_removes_read_only_file_in_writable_directory(tmp_path):
assert not root.exists()
-def _stub_rmtree(monkeypatch, exc: OSError) -> list:
- """Replace ``shutil.rmtree`` so the wrapper sees *exc* for every attempt."""
+def test_non_permission_failures_propagate(tmp_path, monkeypatch):
+ """Only ``PermissionError`` is retried — everything else keeps rmtree semantics."""
attempts: list = []
def _fake(path, **kwargs):
- attempts.append((path, kwargs))
- raise exc
+ attempts.append(path)
+ raise OSError(39, "Directory not empty", str(path))
monkeypatch.setattr(utils.shutil, "rmtree", _fake)
- return attempts
-
-
-def test_ignore_errors_swallows_a_persistent_permission_failure(tmp_path, monkeypatch):
- _stub_rmtree(monkeypatch, PermissionError(13, "Access is denied", str(tmp_path)))
-
- rmtree_readonly(tmp_path, ignore_errors=True)
-
-
-def test_permission_failure_still_raises_without_ignore_errors(tmp_path, monkeypatch):
- _stub_rmtree(monkeypatch, PermissionError(13, "Access is denied", str(tmp_path)))
-
- with pytest.raises(PermissionError):
- rmtree_readonly(tmp_path)
-
-
-def test_non_permission_failures_propagate(tmp_path, monkeypatch):
- """Only ``PermissionError`` is retried — everything else keeps rmtree semantics."""
- attempts = _stub_rmtree(monkeypatch, OSError(39, "Directory not empty", str(tmp_path)))
with pytest.raises(OSError) as excinfo:
rmtree_readonly(tmp_path)