mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-27 11:25:08 +00:00
feat(services): add services_repo_force and compact refresh output
Force-reset repo clones on the automatic TTL refresh when enabled. Report refresh changes as one +/~/-/!TAG line per repo.
This commit is contained in:
@@ -128,6 +128,7 @@ class Config:
|
||||
self.decrypt_labs_api_key: str = kwargs.get("decrypt_labs_api_key") or ""
|
||||
self.ipinfo_api_key: str = kwargs.get("ipinfo_api_key") or ""
|
||||
self.update_checks: bool = kwargs.get("update_checks", True)
|
||||
self.services_repo_force: bool = bool(kwargs.get("services_repo_force", False))
|
||||
self.update_check_interval: int = kwargs.get("update_check_interval", 24)
|
||||
# mask local base dirs (install root/venv/home) in logged paths; False shows full paths
|
||||
self.redact_paths: bool = kwargs.get("redact_paths", True)
|
||||
|
||||
@@ -80,10 +80,11 @@ def repos_base() -> Path:
|
||||
def resolve_service_repo(spec: str, *, ttl: int = DEFAULT_TTL, force: bool = False) -> Optional[Path]:
|
||||
"""Find the local clone dir of a repo spec.
|
||||
|
||||
First use clones it. ``force=True`` (manual ``refresh-services``) hard-resets the clone to
|
||||
upstream, discarding local changes. Otherwise a TTL-stale clone is fast-forwarded, but a dirty
|
||||
clone raises ``DirtyServiceRepo`` so the automated dl/search path can warn and exit instead of
|
||||
clobbering in-progress edits.
|
||||
First use clones it. Only a TTL-stale clone touches the network. With ``force=True``
|
||||
(``services_repo_force``) the refresh hard-resets the clone to upstream and discards local
|
||||
changes. Without it the refresh fast-forwards, and a dirty clone raises ``DirtyServiceRepo``
|
||||
so the automated dl/search path can warn and exit instead of clobbering in-progress edits.
|
||||
Manual ``refresh-services`` uses ``refresh_repo`` and ignores the TTL.
|
||||
"""
|
||||
url, branch, dest_name = parse_spec(spec)
|
||||
dest = repos_base() / dest_name
|
||||
@@ -91,10 +92,8 @@ def resolve_service_repo(spec: str, *, ttl: int = DEFAULT_TTL, force: bool = Fal
|
||||
|
||||
if not dest.exists():
|
||||
return clone(url, branch, dest, stamp)
|
||||
if force:
|
||||
force_sync(dest, stamp)
|
||||
elif is_stale(stamp, ttl):
|
||||
pull(dest, stamp)
|
||||
if is_stale(stamp, ttl):
|
||||
force_sync(dest, stamp) if force else pull(dest, stamp)
|
||||
return dest
|
||||
|
||||
|
||||
@@ -135,9 +134,9 @@ def pull(dest: Path, stamp: Path) -> None:
|
||||
def refresh_repo(spec: str) -> tuple[Optional[Path], list[str]]:
|
||||
"""Force-sync a repo to upstream and return (clone dir, git-style per-service change lines).
|
||||
|
||||
Used by the manual ``refresh-services`` command. Change lines look like ``+ TAG (added)`` /
|
||||
``~ TAG (modified)`` / ``- TAG (removed)`` plus a shortstat. An empty list means the clone is
|
||||
already up to date.
|
||||
Used by the manual ``refresh-services`` command. Each line is one service tag with a status
|
||||
prefix: ``+TAG`` added, ``~TAG`` modified, ``-TAG`` removed, ``!TAG`` local changes discarded.
|
||||
An empty list means the clone is already up to date.
|
||||
"""
|
||||
url, branch, dest_name = parse_spec(spec)
|
||||
dest = repos_base() / dest_name
|
||||
@@ -149,7 +148,7 @@ def refresh_repo(spec: str) -> tuple[Optional[Path], list[str]]:
|
||||
old = head(dest)
|
||||
discarded = local_dirty_services(dest)
|
||||
force_sync(dest, stamp)
|
||||
lines = [f"! {tag} (local changes discarded)" for tag in discarded]
|
||||
lines = [f"!{tag}" for tag in discarded] # ! = local changes discarded
|
||||
lines += changed_services(dest, old, head(dest))
|
||||
return dest, lines
|
||||
|
||||
@@ -168,7 +167,7 @@ def local_dirty_services(dest: Path) -> list[str]:
|
||||
|
||||
|
||||
def changed_services(dest: Path, old: Optional[str], new: Optional[str]) -> list[str]:
|
||||
"""Summarise a HEAD old->new diff grouped by top-level service dir, plus a line shortstat."""
|
||||
"""Summarise a HEAD old->new diff as one ``+TAG``/``~TAG``/``-TAG`` token per top-level service directory."""
|
||||
if not old or not new or old == new:
|
||||
return []
|
||||
git = str(binaries.Git)
|
||||
@@ -185,15 +184,12 @@ def changed_services(dest: Path, old: Optional[str], new: Optional[str]) -> list
|
||||
for top in sorted(by_dir):
|
||||
codes = by_dir[top]
|
||||
kind = "added" if codes == {"A"} else "removed" if codes == {"D"} else "modified"
|
||||
lines.append(f"{sym[kind]} {top} ({kind})")
|
||||
short = subprocess.run([git, "-C", str(dest), "diff", "--shortstat", f"{old}..{new}"], capture_output=True) # nosec B603
|
||||
if short.returncode == 0 and short.stdout.strip():
|
||||
lines.append(short.stdout.decode(errors="ignore").strip())
|
||||
lines.append(f"{sym[kind]}{top}")
|
||||
return lines
|
||||
|
||||
|
||||
def force_sync(dest: Path, stamp: Path) -> None:
|
||||
"""Hard-reset the clone to its upstream, discarding local changes (manual refresh only)."""
|
||||
"""Hard-reset the clone to its upstream, discarding local changes."""
|
||||
if not binaries.Git:
|
||||
write_stamp(stamp)
|
||||
return
|
||||
|
||||
@@ -25,7 +25,7 @@ DIRTY_REPOS: list[str] = []
|
||||
for _entry in raw:
|
||||
if isinstance(_entry, str) and is_repo_spec(_entry):
|
||||
try:
|
||||
_resolved = resolve_service_repo(_entry)
|
||||
_resolved = resolve_service_repo(_entry, force=config.services_repo_force)
|
||||
except DirtyServiceRepo as e:
|
||||
# local edits in the clone - record it and let check_load_errors() exit cleanly
|
||||
DIRTY_REPOS.append(redact_path(str(e.path)))
|
||||
|
||||
Reference in New Issue
Block a user